decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v4; // r12
int v5; // ebx
int v6; // eax
int result; // eax
__int64 v8; // rax
double v9; // xmm0_8
__int64 v10; // r13
char *v11; // rdi
__int64 v12; // rcx
char *v13; // rsi
__int64 v14; // r12
unsigned __int64 *v15; // r13
FILE *v16; // r15
__int64 v17; // r14
int i; // ebp
int *v19; // rax
char *v20; // rax
double v21; // xmm0_8
__int64 v22; // [rsp+8h] [rbp-D0h]
int v23; // [rsp+14h] [rbp-C4h]
char *filename; // [rsp+18h] [rbp-C0h]
void *ptr; // [rsp+28h] [rbp-B0h]
char v26; // [rsp+30h] [rbp-A8h] BYREF
char *endptr; // [rsp+68h] [rbp-70h] BYREF
char v28; // [rsp+70h] [rbp-68h] BYREF

opterr = 0;
v23 = 0;
filename = 0LL;
v4 = 0LL;
v22 = 0x3FF0000000000000LL;
v5 = 1;
while ( 2 )
{
v6 = getopt(argc, (char *const *)argv, "n:d:s:o:r:ih");
if ( v6 == -1 )
{
if ( !v4 )
{
fprintf(stderr, "%s: missing scale\n", *argv);
print_usage(*argv);
return 3;
}
v8 = csv_count_fields(v4);
if ( v8 < 0 )
v9 = (double)(int)(v8 & 1 | ((unsigned __int64)v8 >> 1)) + (double)(int)(v8 & 1 | ((unsigned __int64)v8 >> 1));
else
v9 = (double)(int)v8;
v10 = (int)sqrt(v9);
ptr = calloc(v10 * v10, 8uLL);
csv_read_double_fields(ptr, v10 * v10, v4);
gsl_matrix_view_array();
v11 = &v28;
v12 = 12LL;
v13 = &v26;
while ( v12 )
{
*(_DWORD *)v11 = *(_DWORD *)v13;
v13 += 4;
v11 += 4;
--v12;
}
v14 = gsl_matrix_alloc();
v15 = (unsigned __int64 *)gsl_matrix_alloc();
if ( filename )
{
v16 = fopen(filename, "wt");
if ( !v16 )
{
v19 = __errno_location();
v20 = strerror(*v19);
fprintf(stderr, "%s: can't open %s: %s\n", *argv, filename, v20);
return 2;
}
v17 = gsl_rng_alloc();
gsl_rng_set();
if ( v5 <= 0 )
{
LABEL_32:
fclose(v16);
LABEL_33:
free(ptr);
gsl_matrix_free();
gsl_matrix_free();
gsl_rng_free();
return 0;
}
}
else
{
v16 = (FILE *)_bss_start;
v17 = gsl_rng_alloc();
gsl_rng_set();
if ( v5 <= 0 )
goto LABEL_33;
}
for ( i = 0; i < v5; ++i )
{
gsl_matrix_memcpy();
if ( v23 )
{
v21 = *(double *)&v22;
ran_invwishart(v17, v14, v15, *(double *)&v22);
}
else
{
v21 = ran_wishart(v17, v14, v15, *(double *)&v22);
}
print_matrix(v16, v15, v21);
}
if ( !filename )
goto LABEL_33;
goto LABEL_32;
}
endptr = 0LL;
switch ( v6 )
{
case '?':
fprintf(stderr, "%s: invalid option or missing argument: -%c\n", *argv, (unsigned int)optopt);
print_usage(*argv);
result = 1;
break;
case 'd':
v22 = strtod(optarg, &endptr);
continue;
case 'h':
print_usage(*argv);
result = 0;
break;
case 'i':
v23 = 1;
continue;
case 'n':
v5 = strtol(optarg, &endptr, 10);
continue;
case 'o':
filename = optarg;
continue;
case 'r':
strtol(optarg, &endptr, 0);
continue;
case 's':
v4 = optarg;
continue;
default:
continue;
}
return result;
}
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
char *v4; // r12
int v5; // ebx
int v6; // eax
int result; // eax
__int64 v8; // rax
double v9; // xmm0_8
__int64 v10; // r13
char *v11; // rdi
__int64 v12; // rcx
char *v13; // rsi
__int64 v14; // r12
unsigned __int64 *v15; // r13
FILE *v16; // r15
__int64 v17; // r14
int i; // ebp
int *v19; // rax
char *v20; // rax
double v21; // xmm0_8
__int64 v22; // [rsp+8h] [rbp-D0h]
int v23; // [rsp+14h] [rbp-C4h]
char *filename; // [rsp+18h] [rbp-C0h]
void *ptr; // [rsp+28h] [rbp-B0h]
char v26; // [rsp+30h] [rbp-A8h] BYREF
char *endptr; // [rsp+68h] [rbp-70h] BYREF
char v28; // [rsp+70h] [rbp-68h] BYREF

opterr = 0;
v23 = 0;
filename = 0LL;
v4 = 0LL;
v22 = 0x3FF0000000000000LL;
v5 = 1;
while ( 2 )
{
v6 = getopt(argc, (char *const *)argv, "n:d:s:o:r:ih");
if ( v6 == -1 )
{
if ( !v4 )
{
fprintf(stderr, "%s: missing scale\n", *argv);
func7(*argv);
return 3;
}
v8 = func9(v4);
if ( v8 < 0 )
v9 = (double)(int)(v8 & 1 | ((unsigned __int64)v8 >> 1)) + (double)(int)(v8 & 1 | ((unsigned __int64)v8 >> 1));
else
v9 = (double)(int)v8;
v10 = (int)sqrt(v9);
ptr = calloc(v10 * v10, 8uLL);
func10(ptr, v10 * v10, v4);
gsl_matrix_view_array();
v11 = &v28;
v12 = 12LL;
v13 = &v26;
while ( v12 )
{
*(_DWORD *)v11 = *(_DWORD *)v13;
v13 += 4;
v11 += 4;
--v12;
}
v14 = gsl_matrix_alloc();
v15 = (unsigned __int64 *)gsl_matrix_alloc();
if ( filename )
{
v16 = fopen(filename, "wt");
if ( !v16 )
{
v19 = __errno_location();
v20 = strerror(*v19);
fprintf(stderr, "%s: can't open %s: %s\n", *argv, filename, v20);
return 2;
}
v17 = gsl_rng_alloc();
gsl_rng_set();
if ( v5 <= 0 )
{
LABEL_32:
fclose(v16);
LABEL_33:
free(ptr);
gsl_matrix_free();
gsl_matrix_free();
gsl_rng_free();
return 0;
}
}
else
{
v16 = (FILE *)_bss_start;
v17 = gsl_rng_alloc();
gsl_rng_set();
if ( v5 <= 0 )
goto LABEL_33;
}
for ( i = 0; i < v5; ++i )
{
gsl_matrix_memcpy();
if ( v23 )
{
v21 = *(double *)&v22;
func12(v17, v14, v15, *(double *)&v22);
}
else
{
v21 = func11(v17, v14, v15, *(double *)&v22);
}
func8(v16, v15, v21);
}
if ( !filename )
goto LABEL_33;
goto LABEL_32;
}
endptr = 0LL;
switch ( v6 )
{
case '?':
fprintf(stderr, "%s: invalid option or missing argument: -%c\n", *argv, (unsigned int)optopt);
func7(*argv);
result = 1;
break;
case 'd':
v22 = strtod(optarg, &endptr);
continue;
case 'h':
func7(*argv);
result = 0;
break;
case 'i':
v23 = 1;
continue;
case 'n':
v5 = strtol(optarg, &endptr, 10);
continue;
case 'o':
filename = optarg;
continue;
case 'r':
strtol(optarg, &endptr, 0);
continue;
case 's':
v4 = optarg;
continue;
default:
continue;
}
return result;
}
}

variable_types:
argv
char *[]

variable_types: struct gsl_matrix { unsigned long size1; unsigned long size2; unsigned long tda; double *data; struct gsl_block_struct { unsigned long size; double *data; } *block; int owner; } *

user_defined_types: struct gsl_vector { unsigned long size; unsigned long stride; double *data; struct gsl_block_struct *block; int owner; }


repo: kralljr/gibbs-truncnormal-mdl

decompiled_code:
__int64 __fastcall csv_count_fields(char *a1)
{
char v1; // al
__int64 v2; // rdx

v1 = *a1;
if ( !*a1 )
return 1LL;
v2 = 1LL;
do
{
v2 += v1 == 44;
v1 = *++a1;
}
while ( *a1 );
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func9(char *a1)
{
char v1; // al
__int64 v2; // rdx

v1 = *a1;
if ( !*a1 )
return 1LL;
v2 = 1LL;
do
{
v2 += v1 == 44;
v1 = *++a1;
}
while ( *a1 );
return v2;
}

original_code:
size_t
csv_count_fields(const char *s)
{
    size_t i = 1;
    const char *c;
    for (c = s; *c != '\0'; c++) {
        if (*c == ',') {
            i++;
        }
    }
    return i;
}

canonical_original_code:
unsigned long csv_count_fields(char *s)
{
    unsigned long i = 1;
    char *c;
    for (c = s; *c != '\0'; c++) {
        if (*c == ',') {
            i++;
        }
    }
    return i;
}

variable_types:
s
char *

variable_types:
i
unsigned long

variable_types:
c
char *

return_type: unsigned long


decompiled_code:
const char *__fastcall csv_read_double_fields(__int64 a1, unsigned __int64 a2, const char *a3)
{
__int64 v4; // rbx
char *endptr; // [rsp+8h] [rbp-30h] BYREF

endptr = 0LL;
if ( !a2 )
return 0LL;
v4 = 0LL;
while ( 1 )
{
*(double *)(a1 + 8 * v4) = strtod(a3, &endptr);
if ( endptr == a3 )
break;
if ( !*endptr )
{
if ( v4 + 1 >= a2 )
a3 = 0LL;
return a3;
}
a3 = endptr + 1;
if ( a2 == ++v4 )
return 0LL;
}
return a3;
}

canonical_decompiled_code:
const char *__fastcall func10(__int64 a1, unsigned __int64 a2, const char *a3)
{
__int64 v4; // rbx
char *endptr; // [rsp+8h] [rbp-30h] BYREF

endptr = 0LL;
if ( !a2 )
return 0LL;
v4 = 0LL;
while ( 1 )
{
*(double *)(a1 + 8 * v4) = strtod(a3, &endptr);
if ( endptr == a3 )
break;
if ( !*endptr )
{
if ( v4 + 1 >= a2 )
a3 = 0LL;
return a3;
}
a3 = endptr + 1;
if ( a2 == ++v4 )
return 0LL;
}
return a3;
}

original_code:
const char *
csv_read_double_fields(double *y, size_t n, const char *s)
{
    const char *curptr = s;
    char *endptr = ((void *)0);
    size_t i;
    for (i = 0, curptr = s, endptr = ((void *)0); i < n; i++) {
        y[i] = strtod(curptr, &endptr);
        if (curptr == endptr) {
            return curptr;
        } else if (*endptr == '\0' && i + 1 < n) {
            return curptr;
        } else {
            curptr = endptr + 1;
        }
    }
    return ((void *)0);
}

canonical_original_code:
char *csv_read_double_fields(double *y, unsigned long n, char *s)
{
    char *curptr = s;
    char *endptr = ((void *)0);
    unsigned long i;
    for (i = 0, curptr = s, endptr = ((void *)0); i < n; i++) {
        y[i] = strtod(curptr, &endptr);
        if (curptr == endptr) {
            return curptr;
        } else if (*endptr == '\0' && i + 1 < n) {
            return curptr;
        } else {
            curptr = endptr + 1;
        }
    }
    return ((void *)0);
}

variable_types:
y
double *

variable_types:
n
unsigned long

variable_types:
curptr
char *

variable_types:
endptr
char *

return_type: char *


decompiled_code:
__int64 __fastcall ran_wishart(double a1, __int64 a2, __int64 a3, unsigned __int64 *a4)
{
unsigned __int64 v6; // rbx
double v7; // xmm1_8
double v8; // xmm0_8
__int64 i; // rbp
__int64 v10; // rbx

gsl_linalg_cholesky_decomp();
gsl_matrix_set_zero();
if ( *a4 )
{
v6 = 1LL;
do
{
if ( (v6 & 0x8000000000000000LL) != 0LL )
v7 = (double)(int)(v6 & 1 | (v6 >> 1)) + (double)(int)(v6 & 1 | (v6 >> 1));
else
v7 = (double)(int)v6;
v8 = (a1 - v7 + 1.0) * 0.5;
gsl_ran_gamma();
if ( v8 < 0.0 )
sqrt(v8);
gsl_matrix_set();
++v6;
}
while ( *a4 >= v6 );
if ( *a4 )
{
for ( i = 1LL; *a4 >= i + 1; ++i )
{
if ( (unsigned __int64)(i + 1) > 1 )
{
v10 = 0LL;
do
{
gsl_ran_ugaussian();
gsl_matrix_set();
++v10;
}
while ( v10 != i );
}
}
}
}
gsl_blas_dtrmm();
gsl_matrix_memcpy();
return gsl_blas_dtrmm();
}

canonical_decompiled_code:
__int64 __fastcall func11(double a1, __int64 a2, __int64 a3, unsigned __int64 *a4)
{
unsigned __int64 v6; // rbx
double v7; // xmm1_8
double v8; // xmm0_8
__int64 i; // rbp
__int64 v10; // rbx

gsl_linalg_cholesky_decomp();
gsl_matrix_set_zero();
if ( *a4 )
{
v6 = 1LL;
do
{
if ( (v6 & 0x8000000000000000LL) != 0LL )
v7 = (double)(int)(v6 & 1 | (v6 >> 1)) + (double)(int)(v6 & 1 | (v6 >> 1));
else
v7 = (double)(int)v6;
v8 = (a1 - v7 + 1.0) * 0.5;
gsl_ran_gamma();
if ( v8 < 0.0 )
sqrt(v8);
gsl_matrix_set();
++v6;
}
while ( *a4 >= v6 );
if ( *a4 )
{
for ( i = 1LL; *a4 >= i + 1; ++i )
{
if ( (unsigned __int64)(i + 1) > 1 )
{
v10 = 0LL;
do
{
gsl_ran_ugaussian();
gsl_matrix_set();
++v10;
}
while ( v10 != i );
}
}
}
}
gsl_blas_dtrmm();
gsl_matrix_memcpy();
return gsl_blas_dtrmm();
}

original_code:
void
ran_wishart(const gsl_rng *r, double nu, gsl_matrix *scale, gsl_matrix *rwish)
{
    size_t i;
    size_t j;
    gsl_linalg_cholesky_decomp(scale);
    gsl_matrix_set_zero(rwish);
    for (i = 1; i <= rwish->size1; i++) {
        double y;
        y = sqrt(gsl_ran_gamma(r, (nu - i + 1) / 2.0, 2.0));
        gsl_matrix_set(rwish, i - 1, i - 1, y);
    }
    for (i = 1; i <= rwish->size1; i++) {
        for (j = 1; j < i; j++) {
            double y;
            y = gsl_ran_ugaussian(r);
            gsl_matrix_set(rwish, i - 1, j - 1, y);
        }
    }
    gsl_blas_dtrmm(CblasLeft, CblasLower, CblasNoTrans, CblasNonUnit, 1.0,
            scale, rwish);
    gsl_matrix_memcpy(scale, rwish);
    gsl_blas_dtrmm(CblasRight, CblasLower, CblasTrans, CblasNonUnit, 1.0,
            scale, rwish);
}

canonical_original_code:
void ran_wishart(struct gsl_rng *r, double nu, struct gsl_matrix *scale, struct gsl_matrix *rwish)
{
    unsigned long i;
    unsigned long j;
    gsl_linalg_cholesky_decomp(scale);
    gsl_matrix_set_zero(rwish);
    for (i = 1; i <= rwish->size1; i++) {
        double y;
        y = sqrt(gsl_ran_gamma(r, (nu - i + 1) / 2.0, 2.0));
        gsl_matrix_set(rwish, i - 1, i - 1, y);
    }
    for (i = 1; i <= rwish->size1; i++) {
        for (j = 1; j < i; j++) {
            double y;
            y = gsl_ran_ugaussian(r);
            gsl_matrix_set(rwish, i - 1, j - 1, y);
        }
    }
    gsl_blas_dtrmm(CblasLeft, CblasLower, CblasNoTrans, CblasNonUnit, 1.0,
            scale, rwish);
    gsl_matrix_memcpy(scale, rwish);
    gsl_blas_dtrmm(CblasRight, CblasLower, CblasTrans, CblasNonUnit, 1.0,
            scale, rwish);
}

variable_types: struct gsl_rng { struct gsl_rng_type { char *name; unsigned long max; unsigned long min; unsigned long size; void (*set)(void *state, unsigned long seed); unsigned long (*get)(void *state); double (*get_double)(void *state); } *type; void *state; } *

variable_types: nu double

variable_types:
rwish
struct gsl_matrix *

variable_types:
j
unsigned long

variable_types: y double

return_type: void

user_defined_types: struct gsl_rng { struct gsl_rng_type *type; void *state; }

user_defined_types: struct gsl_matrix { unsigned long size1; unsigned long size2; unsigned long tda; double *data; struct gsl_block_struct *block; int owner; }

user_defined_types: struct gsl_block_struct { unsigned long size; double *data; }


decompiled_code:
__int64 __fastcall ran_invwishart(__int64 a1, __int64 a2, unsigned __int64 *a3, double a4)
{
gsl_linalg_cholesky_decomp();
gsl_linalg_cholesky_invert();
ran_wishart(a4, a1, a2, a3);
gsl_linalg_cholesky_decomp();
return gsl_linalg_cholesky_invert();
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2, unsigned __int64 *a3, double a4)
{
gsl_linalg_cholesky_decomp();
gsl_linalg_cholesky_invert();
func11(a4, a1, a2, a3);
gsl_linalg_cholesky_decomp();
return gsl_linalg_cholesky_invert();
}

original_code:
void
ran_invwishart(const gsl_rng *r, double nu, gsl_matrix *scale,
        gsl_matrix *riwish)
{
    gsl_linalg_cholesky_decomp(scale);
    gsl_linalg_cholesky_invert(scale);
    ran_wishart(r, nu, scale, riwish);
    gsl_linalg_cholesky_decomp(riwish);
    gsl_linalg_cholesky_invert(riwish);
}

canonical_original_code:
void ran_invwishart(struct gsl_rng *r, double nu, struct gsl_matrix *scale, struct gsl_matrix *riwish)
{
    gsl_linalg_cholesky_decomp(scale);
    gsl_linalg_cholesky_invert(scale);
    ran_wishart(r, nu, scale, riwish);
    gsl_linalg_cholesky_decomp(riwish);
    gsl_linalg_cholesky_invert(riwish);
}

variable_types:
riwish
struct gsl_matrix *

user_defined_types: struct gsl_rng_type { char *name; unsigned long max; unsigned long min; unsigned long size; void (*set)(void *state, unsigned long seed); unsigned long (*get)(void *state); double (*get_double)(void *state); }


decompiled_code:
int __fastcall print_usage(const char *a1)
{
return fprintf(
stderr,
"Usage: %s [OPTION]\n"
" -n int number of draws (default 1)\n"
" -d float degrees of freedom (default 1.0)\n"
" -s float,float,... scale\n"
" -o filename output file (default standard output)\n"
" -r int random seed (default 0)\n"
" -i draw from inverse Wishart\n"
" -h show this help\n",
a1);
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
return fprintf(
stderr,
"Usage: %s [OPTION]\n"
" -n int number of draws (default 1)\n"
" -d float degrees of freedom (default 1.0)\n"
" -s float,float,... scale\n"
" -o filename output file (default standard output)\n"
" -r int random seed (default 0)\n"
" -i draw from inverse Wishart\n"
" -h show this help\n",
a1);
}

original_code:
static void
print_usage(const char *progname)
{
    fprintf(stderr,
            "Usage: %s [OPTION] DATA MDLS OUT\n"
            "  -n int   number of iterations (default 1000)\n"
            "  -b int   number of iterations to burn-in (default 0)\n"
            "  -d int   number of random draws (default 1)\n"
            "  -r int   random seed (default 0)\n"
            "  -h       show this help\n"
            , progname);
}

canonical_original_code:
static void print_usage(char *progname)
{
    fprintf(stderr,
            "Usage: %s [OPTION] DATA MDLS OUT\n"
            "  -n int   number of iterations (default 1000)\n"
            "  -b int   number of iterations to burn-in (default 0)\n"
            "  -d int   number of random draws (default 1)\n"
            "  -r int   random seed (default 0)\n"
            "  -h       show this help\n"
            , progname);
}

variable_types:
progname
char *


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // r14
char *v5; // r15
char *v6; // r13
int v7; // er12
int v8; // eax
int result; // eax
size_t v10; // rbp
char *v11; // rdi
__int64 v12; // rcx
char *v13; // rsi
char *v14; // rdi
__int64 v15; // rcx
char *v16; // rsi
unsigned __int64 *v17; // rbp
FILE *v18; // r13
__int64 v19; // rbx
double v20; // xmm0_8
int i; // er15
double v22; // xmm0_8
double v23; // xmm0_8
int *v24; // rax
char *v25; // rax
void *ptr; // [rsp+8h] [rbp-F0h]
void *v27; // [rsp+18h] [rbp-E0h]
char v28[56]; // [rsp+20h] [rbp-D8h] BYREF
char *endptr; // [rsp+58h] [rbp-A0h] BYREF
char v30[48]; // [rsp+60h] [rbp-98h] BYREF
char v31[104]; // [rsp+90h] [rbp-68h] BYREF

opterr = 0;
v4 = 0LL;
v5 = 0LL;
v6 = 0LL;
v7 = 1;
while ( 2 )
{
v8 = getopt(argc, (char *const *)argv, "n:m:c:o:r:h");
if ( v8 == -1 )
{
if ( !v6 )
{
fprintf(stderr, "%s: missing mean\n", *argv);
print_usage(*argv);
return 3;
}
if ( !v5 )
{
fprintf(stderr, "%s: missing covariance\n", *argv);
print_usage(*argv);
return 3;
}
v10 = csv_count_fields(v6);
ptr = calloc(v10, 8uLL);
csv_read_double_fields(ptr, v10, v6);
gsl_vector_view_array();
v11 = v31;
v12 = 10LL;
v13 = v28;
while ( v12 )
{
*(_DWORD *)v11 = *(_DWORD *)v13;
v13 += 4;
v11 += 4;
--v12;
}
v27 = calloc(v10 * v10, 8uLL);
csv_read_double_fields(v27, v10 * v10, v5);
gsl_matrix_view_array();
v14 = v30;
v15 = 12LL;
v16 = v28;
while ( v15 )
{
*(_DWORD *)v14 = *(_DWORD *)v16;
v16 += 4;
v14 += 4;
--v15;
}
v17 = (unsigned __int64 *)gsl_vector_alloc();
if ( v4 )
{
v18 = fopen(v4, "wt");
if ( !v18 )
{
v24 = __errno_location();
v25 = strerror(*v24);
fprintf(stderr, "%s: can't open %s: %s\n", *argv, v4, v25);
return 2;
}
v19 = gsl_rng_alloc();
gsl_rng_set();
v20 = ran_multivariate_normal(v19, v31, v30, v17);
print_vector(v18, v17, v20);
if ( v7 <= 1 )
{
LABEL_25:
fclose(v18);
LABEL_26:
free(ptr);
free(v27);
gsl_vector_free();
gsl_rng_free();
return 0;
}
}
else
{
v18 = stdout;
v19 = gsl_rng_alloc();
gsl_rng_set();
v23 = ran_multivariate_normal(v19, v31, v30, v17);
print_vector(v18, v17, v23);
if ( v7 <= 1 )
goto LABEL_26;
}
for ( i = 1; i < v7; ++i )
{
v22 = ran_multivariate_normal_chol(v19, v31, v30, v17);
print_vector(v18, v17, v22);
}
if ( !v4 )
goto LABEL_26;
goto LABEL_25;
}
endptr = 0LL;
switch ( v8 )
{
case '?':
fprintf(stderr, "%s: invalid option or missing argument: -%c\n", *argv, (unsigned int)optopt);
print_usage(*argv);
result = 1;
break;
case 'c':
v5 = optarg;
continue;
case 'h':
print_usage(*argv);
result = 0;
break;
case 'm':
v6 = optarg;
continue;
case 'n':
v7 = strtol(optarg, &endptr, 10);
continue;
case 'o':
v4 = optarg;
continue;
case 'r':
strtol(optarg, &endptr, 0);
continue;
default:
continue;
}
return result;
}
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
const char *v4; // r14
char *v5; // r15
char *v6; // r13
int v7; // er12
int v8; // eax
int result; // eax
size_t v10; // rbp
char *v11; // rdi
__int64 v12; // rcx
char *v13; // rsi
char *v14; // rdi
__int64 v15; // rcx
char *v16; // rsi
unsigned __int64 *v17; // rbp
FILE *v18; // r13
__int64 v19; // rbx
double v20; // xmm0_8
int i; // er15
double v22; // xmm0_8
double v23; // xmm0_8
int *v24; // rax
char *v25; // rax
void *ptr; // [rsp+8h] [rbp-F0h]
void *v27; // [rsp+18h] [rbp-E0h]
char v28[56]; // [rsp+20h] [rbp-D8h] BYREF
char *endptr; // [rsp+58h] [rbp-A0h] BYREF
char v30[48]; // [rsp+60h] [rbp-98h] BYREF
char v31[104]; // [rsp+90h] [rbp-68h] BYREF

opterr = 0;
v4 = 0LL;
v5 = 0LL;
v6 = 0LL;
v7 = 1;
while ( 2 )
{
v8 = getopt(argc, (char *const *)argv, "n:m:c:o:r:h");
if ( v8 == -1 )
{
if ( !v6 )
{
fprintf(stderr, "%s: missing mean\n", *argv);
func7(*argv);
return 3;
}
if ( !v5 )
{
fprintf(stderr, "%s: missing covariance\n", *argv);
func7(*argv);
return 3;
}
v10 = func9(v6);
ptr = calloc(v10, 8uLL);
func10(ptr, v10, v6);
gsl_vector_view_array();
v11 = v31;
v12 = 10LL;
v13 = v28;
while ( v12 )
{
*(_DWORD *)v11 = *(_DWORD *)v13;
v13 += 4;
v11 += 4;
--v12;
}
v27 = calloc(v10 * v10, 8uLL);
func10(v27, v10 * v10, v5);
gsl_matrix_view_array();
v14 = v30;
v15 = 12LL;
v16 = v28;
while ( v15 )
{
*(_DWORD *)v14 = *(_DWORD *)v16;
v16 += 4;
v14 += 4;
--v15;
}
v17 = (unsigned __int64 *)gsl_vector_alloc();
if ( v4 )
{
v18 = fopen(v4, "wt");
if ( !v18 )
{
v24 = __errno_location();
v25 = strerror(*v24);
fprintf(stderr, "%s: can't open %s: %s\n", *argv, v4, v25);
return 2;
}
v19 = gsl_rng_alloc();
gsl_rng_set();
v20 = func12(v19, v31, v30, v17);
func8(v18, v17, v20);
if ( v7 <= 1 )
{
LABEL_25:
fclose(v18);
LABEL_26:
free(ptr);
free(v27);
gsl_vector_free();
gsl_rng_free();
return 0;
}
}
else
{
v18 = stdout;
v19 = gsl_rng_alloc();
gsl_rng_set();
v23 = func12(v19, v31, v30, v17);
func8(v18, v17, v23);
if ( v7 <= 1 )
goto LABEL_26;
}
for ( i = 1; i < v7; ++i )
{
v22 = func11(v19, v31, v30, v17);
func8(v18, v17, v22);
}
if ( !v4 )
goto LABEL_26;
goto LABEL_25;
}
endptr = 0LL;
switch ( v8 )
{
case '?':
fprintf(stderr, "%s: invalid option or missing argument: -%c\n", *argv, (unsigned int)optopt);
func7(*argv);
result = 1;
break;
case 'c':
v5 = optarg;
continue;
case 'h':
func7(*argv);
result = 0;
break;
case 'm':
v6 = optarg;
continue;
case 'n':
v7 = strtol(optarg, &endptr, 10);
continue;
case 'o':
v4 = optarg;
continue;
case 'r':
strtol(optarg, &endptr, 0);
continue;
default:
continue;
}
return result;
}
}

original_code:
int
main(int argc, char * const argv[])
{
    const char *data_filename;
    const char *mdls_filename;
    const char *output_filename;
    struct gibbs_problem p = {
        ((void *)0),
        ((void *)0),
        1000,
        0,
        1,
        0,
        ((void *)0), ((void *)0), ((void *)0), ((void *)0)};
    gsl_matrix *data = ((void *)0);
    gsl_matrix *mdls = ((void *)0);
    int optflag;
    int exit_val = 0;
    opterr = 0;
    while ((optflag = getopt(argc, argv, "n:b:d:r:h")) != -1) {
        char *endptr = ((void *)0);
        switch (optflag) {
            case 'n':
                p.iterations = strtol(optarg, &endptr, 10);
                break;
            case 'b':
                p.burn = strtol(optarg, &endptr, 10);
                break;
            case 'd':
                p.draws = strtol(optarg, &endptr, 10);
                break;
            case 'r':
                p.seed = strtol(optarg, &endptr, 0);
                break;
            case 'h':
                print_usage(argv[0]);
                return 0;
            case '?':
                fprintf(stderr, "%s: invalid option or missing argument: -'%c'\n",
                        argv[0], optopt);
                print_usage(argv[0]);
                return 1;
        }
    }
    if (optind == argc) {
        fprintf(stderr, "%s: missing argument DATA\n", argv[0]);
        print_usage(argv[0]);
        return 1;
    }
    data_filename = argv[optind++];
    if (optind == argc) {
        fprintf(stderr, "%s: missing argument MDLS\n", argv[0]);
        print_usage(argv[0]);
        return 1;
    }
    mdls_filename = argv[optind++];
    if (optind == argc) {
        fprintf(stderr, "%s: missing argument OUT\n", argv[0]);
        print_usage(argv[0]);
        return 1;
    }
    output_filename = argv[optind++];
    if (optind < argc) {
        fprintf(stderr, "%s: unexpected argument: %s\n", argv[0], argv[optind]);
        print_usage(argv[0]);
        return 1;
    }
    data = csv_load_file(data_filename);
    if (data == ((void *)0)) {
        fprintf(stderr, "%s: cannot read '%s': %s\n",
                argv[0], data_filename, strerror((*__errno_location ())));
        exit_val = (*__errno_location ());
        goto exit;
    }
    mdls = csv_load_file(mdls_filename);
    if (mdls == ((void *)0)) {
        fprintf(stderr, "%s: cannot read '%s': %s\n",
                argv[0], mdls_filename, strerror((*__errno_location ())));
        exit_val = (*__errno_location ());
        goto exit;
    }
    if (data->size1 != mdls->size1 || data->size2 != mdls->size2) {
        fprintf(stderr, "Non-matching `data' and `mdls' dimensions\n");
        exit_val = GSL_EBADLEN;
        goto exit;
    }
    p.data = data;
    p.mdls = mdls;
    gibbs_problem_exec(&p);
    ncdf_write_file(output_filename, &p);
    gibbs_problem_free(&p);
exit:
    if (data != ((void *)0)) {
        gsl_matrix_free(data);
    }
    if (mdls != ((void *)0)) {
        gsl_matrix_free(mdls);
    }
    return exit_val;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    char *data_filename;
    char *mdls_filename;
    char *output_filename;
    struct gibbs_problem p = {
        ((void *)0),
        ((void *)0),
        1000,
        0,
        1,
        0,
        ((void *)0), ((void *)0), ((void *)0), ((void *)0)};
    struct gsl_matrix *data = ((void *)0);
    struct gsl_matrix *mdls = ((void *)0);
    int optflag;
    int exit_val = 0;
    opterr = 0;
    while ((optflag = getopt(argc, argv, "n:b:d:r:h")) != -1) {
        char *endptr = ((void *)0);
        switch (optflag) {
            case 'n':
                p.iterations = strtol(optarg, &endptr, 10);
                break;
            case 'b':
                p.burn = strtol(optarg, &endptr, 10);
                break;
            case 'd':
                p.draws = strtol(optarg, &endptr, 10);
                break;
            case 'r':
                p.seed = strtol(optarg, &endptr, 0);
                break;
            case 'h':
                print_usage(argv[0]);
                return 0;
            case '?':
                fprintf(stderr, "%s: invalid option or missing argument: -'%c'\n",
                        argv[0], optopt);
                print_usage(argv[0]);
                return 1;
        }
    }
    if (optind == argc) {
        fprintf(stderr, "%s: missing argument DATA\n", argv[0]);
        print_usage(argv[0]);
        return 1;
    }
    data_filename = argv[optind++];
    if (optind == argc) {
        fprintf(stderr, "%s: missing argument MDLS\n", argv[0]);
        print_usage(argv[0]);
        return 1;
    }
    mdls_filename = argv[optind++];
    if (optind == argc) {
        fprintf(stderr, "%s: missing argument OUT\n", argv[0]);
        print_usage(argv[0]);
        return 1;
    }
    output_filename = argv[optind++];
    if (optind < argc) {
        fprintf(stderr, "%s: unexpected argument: %s\n", argv[0], argv[optind]);
        print_usage(argv[0]);
        return 1;
    }
    data = csv_load_file(data_filename);
    if (data == ((void *)0)) {
        fprintf(stderr, "%s: cannot read '%s': %s\n",
                argv[0], data_filename, strerror((*__errno_location ())));
        exit_val = (*__errno_location ());
        goto exit;
    }
    mdls = csv_load_file(mdls_filename);
    if (mdls == ((void *)0)) {
        fprintf(stderr, "%s: cannot read '%s': %s\n",
                argv[0], mdls_filename, strerror((*__errno_location ())));
        exit_val = (*__errno_location ());
        goto exit;
    }
    if (data->size1 != mdls->size1 || data->size2 != mdls->size2) {
        fprintf(stderr, "Non-matching `data' and `mdls' dimensions\n");
        exit_val = GSL_EBADLEN;
        goto exit;
    }
    p.data = data;
    p.mdls = mdls;
    gibbs_problem_exec(&p);
    ncdf_write_file(output_filename, &p);
    gibbs_problem_free(&p);
exit:
    if (data != ((void *)0)) {
        gsl_matrix_free(data);
    }
    if (mdls != ((void *)0)) {
        gsl_matrix_free(mdls);
    }
    return exit_val;
}

variable_types: argc int

variable_types: struct gibbs_problem { struct gsl_matrix { unsigned long size1; unsigned long size2; unsigned long tda; double *data; struct gsl_block_struct { unsigned long size; double *data; } *block; int owner; } *data; struct gsl_matrix *mdls; unsigned long iterations; unsigned long burn; unsigned long draws; long seed; struct gsl_vector { unsigned long size; unsigned long stride; double *data; struct gsl_block_struct *block; int owner; } *mthet; struct gsl_matrix *msig; struct gsl_matrix **ddata; unsigned long *dindexes; }

variable_types: optflag int

return_type: int

user_defined_types: struct gibbs_problem { struct gsl_matrix *data; struct gsl_matrix *mdls; unsigned long iterations; unsigned long burn; unsigned long draws; long seed; struct gsl_vector *mthet; struct gsl_matrix *msig; struct gsl_matrix **ddata; unsigned long *dindexes; }


decompiled_code:
__int64 __fastcall ran_multivariate_normal_chol(__int64 a1, __int64 a2, __int64 a3, unsigned __int64 *a4)
{
unsigned __int64 v5; // rbx

if ( *a4 )
{
v5 = 0LL;
do
{
gsl_ran_gaussian();
gsl_vector_set();
++v5;
}
while ( *a4 > v5 );
}
gsl_blas_dtrmv();
return gsl_vector_add();
}

canonical_decompiled_code:
__int64 __fastcall func11(__int64 a1, __int64 a2, __int64 a3, unsigned __int64 *a4)
{
unsigned __int64 v5; // rbx

if ( *a4 )
{
v5 = 0LL;
do
{
gsl_ran_gaussian();
gsl_vector_set();
++v5;
}
while ( *a4 > v5 );
}
gsl_blas_dtrmv();
return gsl_vector_add();
}

original_code:
void
ran_multivariate_normal_chol(const gsl_rng *rng, const gsl_vector *mean,
        const gsl_matrix *sigmachol, gsl_vector *z)
{
    size_t i;
    for (i = 0; i < z->size; i++) {
        double r;
        r = gsl_ran_gaussian(rng, 1.0);
        gsl_vector_set(z, i, r);
    }
    gsl_blas_dtrmv(CblasLower, CblasNoTrans, CblasNonUnit, sigmachol, z);
    gsl_vector_add(z, mean);
}

canonical_original_code:
void ran_multivariate_normal_chol(struct gsl_rng *rng, struct gsl_vector *mean, struct gsl_matrix *sigmachol, struct gsl_vector *z)
{
    unsigned long i;
    for (i = 0; i < z->size; i++) {
        double r;
        r = gsl_ran_gaussian(rng, 1.0);
        gsl_vector_set(z, i, r);
    }
    gsl_blas_dtrmv(CblasLower, CblasNoTrans, CblasNonUnit, sigmachol, z);
    gsl_vector_add(z, mean);
}

variable_types: struct gsl_vector { unsigned long size; unsigned long stride; double *data; struct gsl_block_struct { unsigned long size; double *data; } *block; int owner; } *

variable_types: struct gsl_matrix { unsigned long size1; unsigned long size2; unsigned long tda; double *data; struct gsl_block_struct *block; int owner; } *

variable_types:
z
struct gsl_vector *

variable_types: r double


decompiled_code:
__int64 __fastcall ran_multivariate_normal(__int64 a1, __int64 a2, __int64 a3, unsigned __int64 *a4)
{
gsl_linalg_cholesky_decomp();
return ran_multivariate_normal_chol(a1, a2, a3, a4);
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1, __int64 a2, __int64 a3, unsigned __int64 *a4)
{
gsl_linalg_cholesky_decomp();
return func11(a1, a2, a3, a4);
}

original_code:
void
ran_multivariate_normal(const gsl_rng *rng, const gsl_vector *mean,
        gsl_matrix *sigma, gsl_vector *z)
{
    gsl_linalg_cholesky_decomp(sigma);
    ran_multivariate_normal_chol(rng, mean, sigma, z);
}

canonical_original_code:
void ran_multivariate_normal(struct gsl_rng *rng, struct gsl_vector *mean, struct gsl_matrix *sigma, struct gsl_vector *z)
{
    gsl_linalg_cholesky_decomp(sigma);
    ran_multivariate_normal_chol(rng, mean, sigma, z);
}


decompiled_code:
int __fastcall print_usage(const char *a1)
{
return fprintf(
stderr,
"Usage: %s [OPTION]\n"
" -n int number of draws (default 1)\n"
" -m float,float,... mean\n"
" -c float,float,... covariance\n"
" -o filename output file (default standard output)\n"
" -r int random seed (default 0)\n"
" -h show this help\n",
a1);
}

canonical_decompiled_code:
int __fastcall func7(const char *a1)
{
return fprintf(
stderr,
"Usage: %s [OPTION]\n"
" -n int number of draws (default 1)\n"
" -m float,float,... mean\n"
" -c float,float,... covariance\n"
" -o filename output file (default standard output)\n"
" -r int random seed (default 0)\n"
" -h show this help\n",
a1);
}


original_code:
void
vector_remove_elem(const gsl_vector *u, size_t i, gsl_vector *v)
{
    size_t j;
    for (j = 0; j < i; j++) {
        v->data[j] = u->data[j];
    }
    for (j = i + 1; j < u->size; j++) {
        v->data[j - 1] = u->data[j];
    }
}

canonical_original_code:
void vector_remove_elem(struct gsl_vector *u, unsigned long i, struct gsl_vector *v)
{
    unsigned long j;
    for (j = 0; j < i; j++) {
        v->data[j] = u->data[j];
    }
    for (j = i + 1; j < u->size; j++) {
        v->data[j - 1] = u->data[j];
    }
}

variable_types:
i
unsigned long

variable_types:
v
struct gsl_vector *


decompiled_code:
void __fastcall matrix_row_remove_elem(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
unsigned __int64 v6[9]; // [rsp+0h] [rbp-48h] BYREF

gsl_matrix_const_row(v6, a1, a2);
vector_remove_elem(v6, a3, a4);
}

canonical_decompiled_code:
void __fastcall func1(__int64 a1, __int64 a2, __int64 a3, __int64 a4)
{
unsigned __int64 v6[9]; // [rsp+0h] [rbp-48h] BYREF

gsl_matrix_const_row(v6, a1, a2);
func0(v6, a3, a4);
}

original_code:
void
matrix_row_remove_elem(const gsl_matrix *A, size_t i, size_t j, gsl_vector *v)
{
    gsl_vector_const_view u = gsl_matrix_const_row(A, i);
    vector_remove_elem(&u.vector, j, v);
}

canonical_original_code:
void matrix_row_remove_elem(struct gsl_matrix *A, unsigned long i, unsigned long j, struct gsl_vector *v)
{
    struct _gsl_vector_const_view u = gsl_matrix_const_row(A, i);
    vector_remove_elem(&u.vector, j, v);
}

variable_types: struct gsl_matrix { unsigned long size1; unsigned long size2; unsigned long tda; double *data; struct gsl_block_struct { unsigned long size; double *data; } *block; int owner; } *

variable_types: struct gsl_vector { unsigned long size; unsigned long stride; double *data; struct gsl_block_struct *block; int owner; } *

variable_types: struct _gsl_vector_const_view { struct gsl_vector { unsigned long size; unsigned long stride; double *data; struct gsl_block_struct { unsigned long size; double *data; } *block; int owner; } vector; }

user_defined_types: struct _gsl_vector_const_view { struct gsl_vector vector; }


decompiled_code:
void __fastcall matrix_remove_rowcol(unsigned __int64 *a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 i; // rbx
__int64 v7; // rax
unsigned __int64 v8; // rbx
__int64 v9; // r12
_BYTE v10[88]; // [rsp+0h] [rbp-58h] BYREF

if ( a2 )
{
for ( i = 0LL; i != a2; ++i )
{
gsl_matrix_row(v10, a4, i);
matrix_row_remove_elem((__int64)a1, i, a3, (__int64)v10);
}
v7 = a2;
}
else
{
v7 = 0LL;
}
v8 = a2 + 1;
if ( a2 + 1 < *a1 )
{
v9 = v7 + ~a2;
do
{
gsl_matrix_row(v10, a4, v9 + v8);
matrix_row_remove_elem((__int64)a1, v8++, a3, (__int64)v10);
}
while ( *a1 > v8 );
}
}

original_code:
void
matrix_remove_rowcol(const gsl_matrix *A, size_t r1, size_t r2, gsl_matrix *B)
{
    size_t i;
    size_t j = 0;
    for (i = 0; i < r1; i++, j++) {
        gsl_vector_view v = gsl_matrix_row(B, j);
        matrix_row_remove_elem(A, i, r2, &v.vector);
    }
    for (i = r1 + 1; i < A->size1; i++, j++) {
        gsl_vector_view v = gsl_matrix_row(B, j);
        matrix_row_remove_elem(A, i, r2, &v.vector);
    }
}

canonical_original_code:
void matrix_remove_rowcol(struct gsl_matrix *A, unsigned long r1, unsigned long r2, struct gsl_matrix *B)
{
    unsigned long i;
    unsigned long j = 0;
    for (i = 0; i < r1; i++, j++) {
        struct _gsl_vector_view v = gsl_matrix_row(B, j);
        matrix_row_remove_elem(A, i, r2, &v.vector);
    }
    for (i = r1 + 1; i < A->size1; i++, j++) {
        struct _gsl_vector_view v = gsl_matrix_row(B, j);
        matrix_row_remove_elem(A, i, r2, &v.vector);
    }
}

variable_types:
B
struct gsl_matrix *

variable_types: struct _gsl_vector_view { struct gsl_vector { unsigned long size; unsigned long stride; double *data; struct gsl_block_struct { unsigned long size; double *data; } *block; int owner; } vector; }

user_defined_types: struct _gsl_vector_view { struct gsl_vector vector; }


decompiled_code:
__int64 __fastcall matrix_update_const_diag(unsigned __int64 *a1, double a2, double a3, double a4, double a5, double a6, double a7)
{
unsigned __int64 v7; // rbx
__int64 result; // rax
double v9; // xmm5_8
unsigned __int64 v10; // rbx
unsigned __int64 i; // rbx
double v12; // [rsp+0h] [rbp-28h]
double v13; // [rsp+8h] [rbp-20h]

v13 = a2;
v12 = a3;
if ( a2 == 1.0 )
{
if ( *a1 )
{
v7 = 0LL;
do
{
a2 = gsl_matrix_get(a1, v7, v7, a2, a3, a4, a5, a6, a7) + a3;
result = gsl_matrix_set(a1, v7, v7, a2);
++v7;
}
while ( *a1 > v7 );
}
}
else
{
v9 = a2;
if ( a2 == -1.0 )
{
if ( *a1 )
{
v10 = 0LL;
do
{
a3 = gsl_matrix_get(a1, v10, v10, a2, a3, a4, a5, a6, v9);
a2 = v12 - a3;
result = gsl_matrix_set(a1, v10, v10, v12 - a3);
++v10;
}
while ( *a1 > v10 );
}
}
else
{
for ( i = 0LL; *a1 > i; ++i )
{
a2 = gsl_matrix_get(a1, i, i, a2, a3, a4, a5, a6, v9) * v13 + a3;
result = gsl_matrix_set(a1, i, i, a2);
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int64 *a1, double a2, double a3, double a4, double a5, double a6, double a7)
{
unsigned __int64 v7; // rbx
__int64 result; // rax
double v9; // xmm5_8
unsigned __int64 v10; // rbx
unsigned __int64 i; // rbx
double v12; // [rsp+0h] [rbp-28h]
double v13; // [rsp+8h] [rbp-20h]

v13 = a2;
v12 = a3;
if ( a2 == 1.0 )
{
if ( *a1 )
{
v7 = 0LL;
do
{
a2 = gsl_matrix_get(a1, v7, v7, a2, a3, a4, a5, a6, a7) + a3;
result = gsl_matrix_set(a1, v7, v7, a2);
++v7;
}
while ( *a1 > v7 );
}
}
else
{
v9 = a2;
if ( a2 == -1.0 )
{
if ( *a1 )
{
v10 = 0LL;
do
{
a3 = gsl_matrix_get(a1, v10, v10, a2, a3, a4, a5, a6, v9);
a2 = v12 - a3;
result = gsl_matrix_set(a1, v10, v10, v12 - a3);
++v10;
}
while ( *a1 > v10 );
}
}
else
{
for ( i = 0LL; *a1 > i; ++i )
{
a2 = gsl_matrix_get(a1, i, i, a2, a3, a4, a5, a6, v9) * v13 + a3;
result = gsl_matrix_set(a1, i, i, a2);
}
}
}
return result;
}

original_code:
void
matrix_update_const_diag(gsl_matrix *A, double alpha, double beta)
{
    size_t i;
    if (alpha == 1.0) {
        for (i = 0; i < A->size1; i++) {
            double y = gsl_matrix_get(A, i, i);
            gsl_matrix_set(A, i, i, y + beta);
        }
    } else if (alpha == -1.0) {
        for (i = 0; i < A->size1; i++) {
            double y = gsl_matrix_get(A, i, i);
            gsl_matrix_set(A, i, i, beta - y);
        }
    } else {
        for (i = 0; i < A->size1; i++) {
            double y = gsl_matrix_get(A, i, i);
            gsl_matrix_set(A, i, i, alpha * y + beta);
        }
    }
}

canonical_original_code:
void matrix_update_const_diag(struct gsl_matrix *A, double alpha, double beta)
{
    unsigned long i;
    if (alpha == 1.0) {
        for (i = 0; i < A->size1; i++) {
            double y = gsl_matrix_get(A, i, i);
            gsl_matrix_set(A, i, i, y + beta);
        }
    } else if (alpha == -1.0) {
        for (i = 0; i < A->size1; i++) {
            double y = gsl_matrix_get(A, i, i);
            gsl_matrix_set(A, i, i, beta - y);
        }
    } else {
        for (i = 0; i < A->size1; i++) {
            double y = gsl_matrix_get(A, i, i);
            gsl_matrix_set(A, i, i, alpha * y + beta);
        }
    }
}

variable_types: alpha double

variable_types: beta double

variable_types: y double


decompiled_code:
__int64 __fastcall matrix_invert_remove_rowcol(unsigned __int64 *a1, __int64 a2, __int64 a3, __int64 a4, double a5, double a6, double a7, double a8)
{
double v10; // xmm4_8
double v11; // xmm5_8
double v12; // xmm1_8

matrix_remove_rowcol(a1, a2, a2, a3);
matrix_row_remove_elem((__int64)a1, a2, a2, a4);
v12 = gsl_matrix_get(a1, a2, a2, a5, a6, a7, a8, v10, v11);
return gsl_blas_dger(a4, a4, a3, -1.0 / v12);
}

canonical_decompiled_code:
__int64 __fastcall func4(unsigned __int64 *a1, __int64 a2, __int64 a3, __int64 a4, double a5, double a6, double a7, double a8)
{
double v10; // xmm4_8
double v11; // xmm5_8
double v12; // xmm1_8

func2(a1, a2, a2, a3);
func1((__int64)a1, a2, a2, a4);
v12 = gsl_matrix_get(a1, a2, a2, a5, a6, a7, a8, v10, v11);
return gsl_blas_dger(a4, a4, a3, -1.0 / v12);
}

original_code:
void
matrix_invert_remove_rowcol(const gsl_matrix *Ainv, size_t i, gsl_matrix *Binv,
        gsl_vector *work)
{
    matrix_remove_rowcol(Ainv, i, i, Binv);
    matrix_row_remove_elem(Ainv, i, i, work);
    gsl_blas_dger(-1.0 / gsl_matrix_get(Ainv, i, i), work, work, Binv);
}

canonical_original_code:
void matrix_invert_remove_rowcol(struct gsl_matrix *Ainv, unsigned long i, struct gsl_matrix *Binv, struct gsl_vector *work)
{
    matrix_remove_rowcol(Ainv, i, i, Binv);
    matrix_row_remove_elem(Ainv, i, i, work);
    gsl_blas_dger(-1.0 / gsl_matrix_get(Ainv, i, i), work, work, Binv);
}

variable_types:
Binv
struct gsl_matrix *


decompiled_code:
__int64 __fastcall multivariate_mean(__int64 *a1, __int64 a2)
{
__int64 v2; // rax
unsigned __int64 v3; // rbx
double v4; // xmm1_8
_BYTE v6[72]; // [rsp+0h] [rbp-48h] BYREF

gsl_vector_set_zero(a2);
v2 = *a1;
if ( *a1 )
{
v3 = 0LL;
do
{
gsl_matrix_const_row(v6, a1, v3);
gsl_vector_add(a2, v6);
++v3;
v2 = *a1;
}
while ( *a1 > v3 );
}
if ( v2 < 0 )
v4 = (double)(int)(v2 & 1 | ((unsigned __int64)v2 >> 1)) + (double)(int)(v2 & 1 | ((unsigned __int64)v2 >> 1));
else
v4 = (double)(int)v2;
return gsl_vector_scale(a2, 1.0 / v4);
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 *a1, __int64 a2)
{
__int64 v2; // rax
unsigned __int64 v3; // rbx
double v4; // xmm1_8
_BYTE v6[72]; // [rsp+0h] [rbp-48h] BYREF

gsl_vector_set_zero(a2);
v2 = *a1;
if ( *a1 )
{
v3 = 0LL;
do
{
gsl_matrix_const_row(v6, a1, v3);
gsl_vector_add(a2, v6);
++v3;
v2 = *a1;
}
while ( *a1 > v3 );
}
if ( v2 < 0 )
v4 = (double)(int)(v2 & 1 | ((unsigned __int64)v2 >> 1)) + (double)(int)(v2 & 1 | ((unsigned __int64)v2 >> 1));
else
v4 = (double)(int)v2;
return gsl_vector_scale(a2, 1.0 / v4);
}

original_code:
void
multivariate_mean(const gsl_matrix *A, gsl_vector *mu)
{
    size_t i;
    gsl_vector_set_zero(mu);
    for (i = 0; i < A->size1; i++) {
        gsl_vector_const_view row = gsl_matrix_const_row(A, i);
        gsl_vector_add(mu, &row.vector);
    }
    gsl_vector_scale(mu, 1.0 / A->size1);
}

canonical_original_code:
void multivariate_mean(struct gsl_matrix *A, struct gsl_vector *mu)
{
    unsigned long i;
    gsl_vector_set_zero(mu);
    for (i = 0; i < A->size1; i++) {
        struct _gsl_vector_const_view row = gsl_matrix_const_row(A, i);
        gsl_vector_add(mu, &row.vector);
    }
    gsl_vector_scale(mu, 1.0 / A->size1);
}


decompiled_code:
__int64 __fastcall multivariate_covariance(unsigned __int64 *a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 result; // rax
unsigned __int64 v7; // rbp
int v8; // edx
double v9; // xmm1_8
unsigned __int64 v10; // rax
_BYTE v11[88]; // [rsp+0h] [rbp-58h] BYREF

result = gsl_matrix_set_zero(a3);
if ( *a1 )
{
v7 = 0LL;
do
{
gsl_matrix_const_row(v11, a1, v7);
gsl_vector_memcpy(a4, v11);
gsl_vector_sub(a4, a2);
v10 = *a1;
if ( (*a1 & 0x8000000000000000LL) != 0LL )
{
v8 = *(_DWORD *)a1 & 1 | (v10 >> 1);
v9 = (double)v8 + (double)v8;
}
else
{
v9 = (double)(int)v10;
}
result = gsl_blas_dger(a4, a4, a3, 1.0 / v9);
++v7;
}
while ( *a1 > v7 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(unsigned __int64 *a1, __int64 a2, __int64 a3, __int64 a4)
{
__int64 result; // rax
unsigned __int64 v7; // rbp
int v8; // edx
double v9; // xmm1_8
unsigned __int64 v10; // rax
_BYTE v11[88]; // [rsp+0h] [rbp-58h] BYREF

result = gsl_matrix_set_zero(a3);
if ( *a1 )
{
v7 = 0LL;
do
{
gsl_matrix_const_row(v11, a1, v7);
gsl_vector_memcpy(a4, v11);
gsl_vector_sub(a4, a2);
v10 = *a1;
if ( (*a1 & 0x8000000000000000LL) != 0LL )
{
v8 = *(_DWORD *)a1 & 1 | (v10 >> 1);
v9 = (double)v8 + (double)v8;
}
else
{
v9 = (double)(int)v10;
}
result = gsl_blas_dger(a4, a4, a3, 1.0 / v9);
++v7;
}
while ( *a1 > v7 );
}
return result;
}

original_code:
void
multivariate_covariance(const gsl_matrix *A, const gsl_vector *mu,
        gsl_matrix *S, gsl_vector *tmp)
{
    size_t i;
    gsl_matrix_set_zero(S);
    for (i = 0; i < A->size1; i++) {
        gsl_vector_const_view row = gsl_matrix_const_row(A, i);
        gsl_vector_memcpy(tmp, &row.vector);
        gsl_vector_sub(tmp, mu);
        gsl_blas_dger(1.0 / A->size1, tmp, tmp, S);
    }
}

canonical_original_code:
void multivariate_covariance(struct gsl_matrix *A, struct gsl_vector *mu, struct gsl_matrix *S, struct gsl_vector *tmp)
{
    unsigned long i;
    gsl_matrix_set_zero(S);
    for (i = 0; i < A->size1; i++) {
        struct _gsl_vector_const_view row = gsl_matrix_const_row(A, i);
        gsl_vector_memcpy(tmp, &row.vector);
        gsl_vector_sub(tmp, mu);
        gsl_blas_dger(1.0 / A->size1, tmp, tmp, S);
    }
}

variable_types:
S
struct gsl_matrix *

variable_types:
tmp
struct gsl_vector *


decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // eax
int v6; // ebx
int v7; // eax
__int64 v8; // rsi
const char *v9; // r12
const char *v10; // r13
const char *v11; // r14
_QWORD *v12; // rbp
_QWORD *v13; // rax
_QWORD *v14; // r12
int *v15; // rbp
char *v16; // rax
int *v17; // r14
char *v18; // rax
_QWORD *v19; // [rsp+10h] [rbp-78h] BYREF
_QWORD *v20; // [rsp+18h] [rbp-70h]
__int64 v21; // [rsp+20h] [rbp-68h]
__int64 v22; // [rsp+28h] [rbp-60h]
__int64 v23; // [rsp+30h] [rbp-58h]
__int64 v24; // [rsp+38h] [rbp-50h]
__int64 v25; // [rsp+40h] [rbp-48h]
__int64 v26; // [rsp+48h] [rbp-40h]
__int64 v27; // [rsp+50h] [rbp-38h]
__int64 v28; // [rsp+58h] [rbp-30h]

v19 = 0LL;
v20 = 0LL;
v21 = 1000LL;
v22 = 0LL;
v23 = 1LL;
v24 = 0LL;
v25 = 0LL;
v26 = 0LL;
v27 = 0LL;
v28 = 0LL;
opterr = 0;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "n:b:d:r:h");
if ( v4 == -1 )
break;
if ( v4 == 63 )
{
fprintf(stderr, "%s: invalid option or missing argument: -'%c'\n", *argv, (unsigned int)optopt);
print_usage(*argv);
return 1;
}
}
v7 = optind;
if ( optind == argc )
{
fprintf(stderr, "%s: missing argument DATA\n", *argv);
print_usage(*argv);
v6 = 1;
}
else
{
++optind;
v8 = v7;
v9 = argv[v8];
if ( v7 + 1 == argc )
{
fprintf(stderr, "%s: missing argument MDLS\n", *argv);
print_usage(*argv);
v6 = 1;
}
else
{
optind = v7 + 2;
v10 = argv[v8 + 1];
if ( v7 + 2 == argc )
{
fprintf(stderr, "%s: missing argument OUT\n", *argv);
print_usage(*argv);
v6 = 1;
}
else
{
optind = v7 + 3;
if ( v7 + 3 < argc )
{
fprintf(stderr, "%s: unexpected argument: %s\n", *argv, argv[v8 + 3]);
print_usage(*argv);
v6 = 1;
}
else
{
v11 = argv[v8 + 2];
v12 = (_QWORD *)csv_load_file(v9);
if ( v12 )
{
v13 = (_QWORD *)csv_load_file(v10);
v14 = v13;
if ( v13 )
{
if ( *v12 == *v13 && v12[1] == v13[1] )
{
v19 = v12;
v20 = v13;
gibbs_problem_exec(&v19);
ncdf_write_file((__int64)v11, (__int64)&v19);
gibbs_problem_free(&v19);
v6 = 0;
}
else
{
fwrite("Non-matching `data' and `mdls' dimensions\n", 1uLL, 0x2AuLL, stderr);
v6 = 19;
}
}
else
{
v17 = _errno_location();
v18 = strerror(*v17);
fprintf(stderr, "%s: cannot read '%s': %s\n", *argv, v10, v18);
v6 = *v17;
}
gsl_matrix_free(v12);
if ( v14 )
gsl_matrix_free(v14);
}
else
{
v15 = _errno_location();
v16 = strerror(*v15);
fprintf(stderr, "%s: cannot read '%s': %s\n", *argv, v9, v16);
v6 = *v15;
}
}
}
}
}
return v6;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v4; // eax
int v6; // ebx
int v7; // eax
__int64 v8; // rsi
const char *v9; // r12
const char *v10; // r13
const char *v11; // r14
_QWORD *v12; // rbp
_QWORD *v13; // rax
_QWORD *v14; // r12
int *v15; // rbp
char *v16; // rax
int *v17; // r14
char *v18; // rax
_QWORD *v19; // [rsp+10h] [rbp-78h] BYREF
_QWORD *v20; // [rsp+18h] [rbp-70h]
__int64 v21; // [rsp+20h] [rbp-68h]
__int64 v22; // [rsp+28h] [rbp-60h]
__int64 v23; // [rsp+30h] [rbp-58h]
__int64 v24; // [rsp+38h] [rbp-50h]
__int64 v25; // [rsp+40h] [rbp-48h]
__int64 v26; // [rsp+48h] [rbp-40h]
__int64 v27; // [rsp+50h] [rbp-38h]
__int64 v28; // [rsp+58h] [rbp-30h]

v19 = 0LL;
v20 = 0LL;
v21 = 1000LL;
v22 = 0LL;
v23 = 1LL;
v24 = 0LL;
v25 = 0LL;
v26 = 0LL;
v27 = 0LL;
v28 = 0LL;
opterr = 0;
while ( 1 )
{
v4 = getopt(argc, (char *const *)argv, "n:b:d:r:h");
if ( v4 == -1 )
break;
if ( v4 == 63 )
{
fprintf(stderr, "%s: invalid option or missing argument: -'%c'\n", *argv, (unsigned int)optopt);
func0(*argv);
return 1;
}
}
v7 = optind;
if ( optind == argc )
{
fprintf(stderr, "%s: missing argument DATA\n", *argv);
func0(*argv);
v6 = 1;
}
else
{
++optind;
v8 = v7;
v9 = argv[v8];
if ( v7 + 1 == argc )
{
fprintf(stderr, "%s: missing argument MDLS\n", *argv);
func0(*argv);
v6 = 1;
}
else
{
optind = v7 + 2;
v10 = argv[v8 + 1];
if ( v7 + 2 == argc )
{
fprintf(stderr, "%s: missing argument OUT\n", *argv);
func0(*argv);
v6 = 1;
}
else
{
optind = v7 + 3;
if ( v7 + 3 < argc )
{
fprintf(stderr, "%s: unexpected argument: %s\n", *argv, argv[v8 + 3]);
func0(*argv);
v6 = 1;
}
else
{
v11 = argv[v8 + 2];
v12 = (_QWORD *)func3(v9);
if ( v12 )
{
v13 = (_QWORD *)func3(v10);
v14 = v13;
if ( v13 )
{
if ( *v12 == *v13 && v12[1] == v13[1] )
{
v19 = v12;
v20 = v13;
gibbs_problem_exec(&v19);
func4((__int64)v11, (__int64)&v19);
gibbs_problem_free(&v19);
v6 = 0;
}
else
{
fwrite("Non-matching `data' and `mdls' dimensions\n", 1uLL, 0x2AuLL, stderr);
v6 = 19;
}
}
else
{
v17 = _errno_location();
v18 = strerror(*v17);
fprintf(stderr, "%s: cannot read '%s': %s\n", *argv, v10, v18);
v6 = *v17;
}
gsl_matrix_free(v12);
if ( v14 )
gsl_matrix_free(v14);
}
else
{
v15 = _errno_location();
v16 = strerror(*v15);
fprintf(stderr, "%s: cannot read '%s': %s\n", *argv, v9, v16);
v6 = *v15;
}
}
}
}
}
return v6;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: struct gibbs_problem { struct gsl_matrix { unsigned long size1; unsigned long size2; unsigned long tda; double *data; struct gsl_block_struct { unsigned long size; double *data; } *block; int owner; } *data; struct gsl_matrix *mdls; unsigned long iterations; unsigned long burn; unsigned long draws; long seed; struct gsl_vector { unsigned long size; unsigned long stride; double *data; struct gsl_block_struct *block; int owner; } *mthet; struct gsl_matrix *msig; struct gsl_matrix **ddata; unsigned long *dindexes; }

variable_types: optflag int

variable_types:
endptr
char *


decompiled_code:
int __fastcall print_usage(const char *a1)
{
return fprintf(
stderr,
"Usage: %s [OPTION] DATA MDLS OUT\n"
" -n int number of iterations (default 1000)\n"
" -b int number of iterations to burn-in (default 0)\n"
" -d int number of random draws (default 1)\n"
" -r int random seed (default 0)\n"
" -h show this help\n",
a1);
}

canonical_decompiled_code:
int __fastcall func0(const char *a1)
{
return fprintf(
stderr,
"Usage: %s [OPTION] DATA MDLS OUT\n"
" -n int number of iterations (default 1000)\n"
" -b int number of iterations to burn-in (default 0)\n"
" -d int number of random draws (default 1)\n"
" -r int random seed (default 0)\n"
" -h show this help\n",
a1);
}

variable_types:
progname
char *


clear

repo: kremlhi/tetris

decompiled_code:
int clear()
{
return printf("\x1B[2J");
}

canonical_decompiled_code:
int func0()
{
return printf("\x1B[2J");
}

original_code:
void
clear(void)
{
 printf("\033[" "2J");
}


posxy

decompiled_code:
int __fastcall posxy(int a1, int a2)
{
int result; // eax

if ( a2 > 3 )
result = printf("\x1B[%d;%dH", (unsigned int)(a2 - 3), (unsigned int)(2 * a1 + 27));
return result;
}

canonical_decompiled_code:
int __fastcall func1(int a1, int a2)
{
int result; // eax

if ( a2 > 3 )
result = printf("\x1B[%d;%dH", (unsigned int)(a2 - 3), (unsigned int)(2 * a1 + 27));
return result;
}

original_code:
void
posxy(int x, int y)
{
 if(y < 4)
  return;
 printf("\033[" "%d;%dH", y - 3, x*2 + 27);
}

variable_types: x int

variable_types: y int


drawpixel

decompiled_code:
int __fastcall drawpixel(int a1, int a2, unsigned int a3)
{
int result; // eax

if ( a2 > 3 )
{
posxy(a1, a2);
result = printf("\x1B[7;%dm \x1B[0m", a3);
}
return result;
}

canonical_decompiled_code:
int __fastcall func2(int a1, int a2, unsigned int a3)
{
int result; // eax

if ( a2 > 3 )
{
func1(a1, a2);
result = printf("\x1B[7;%dm \x1B[0m", a3);
}
return result;
}

original_code:
void
drawpixel(int x, int y, enum colour_t colour)
{
 if(y < 4)
  return;
 posxy(x, y);
 printf("\033[" "7;%dm" "  " "\033[" "0m", colour);
}

variable_types: enum colour_t { NORMAL=0, INVERT=7, BGCOLOUR=30, RED=31, GREEN=32, YELLOW=33, BLUE=34, MAGENTA=35, CYAN=36, GREY=37 }


flush

decompiled_code:
int flush()
{
printf("\x1B[24;1H");
return fflush(stdout);
}

canonical_decompiled_code:
int func3()
{
printf("\x1B[24;1H");
return fflush(stdout);
}

original_code:
void
flush(void)
{
 printf("\033[" "24;1H");
 fflush(stdout);
}

canonical_original_code:
void flush()
{
 printf("\033[" "24;1H");
 fflush(stdout);
}


cleanexit

decompiled_code:
void __noreturn cleanexit()
{
tcsetattr(0, 0, &oldt);
exit(0);
}

canonical_decompiled_code:
void __noreturn func4()
{
tcsetattr(0, 0, &oldt);
exit(0);
}

original_code:
void
cleanexit(int sig)
{
 tcsetattr(0, 0, &oldt);
 exit(0);
}

variable_types: sig int


redraw

decompiled_code:
void redraw()
{
;
}

canonical_decompiled_code:
void func5()
{
;
}

original_code:
void
redraw(int sig)
{
}

canonical_original_code:
void redraw(int sig)
{
}


init

decompiled_code:
int init()
{
int result; // eax

if ( tcgetattr(0, &oldt) == -1 )
err(1, "tcgetattr");
*(__m128i *)&newt_1.c_iflag = _mm_load_si128((const __m128i *)&oldt);
*(__m128i *)&newt_1.c_line = _mm_load_si128((const __m128i *)&oldt.c_line);
*(__m128i *)&newt_1.c_cc[15] = _mm_load_si128((const __m128i *)&oldt.c_cc[15]);
*(_QWORD *)&newt_1.c_cc[31] = *(_QWORD *)&oldt.c_cc[31];
newt_1.c_ospeed = oldt.c_ospeed;
newt_1.c_lflag &= 0xFFFFFFF5;
result = tcsetattr(0, 0, &newt_1);
if ( result == -1 )
err(1, "tcsetattr");
return result;
}

canonical_decompiled_code:
int func6()
{
int result; // eax

if ( tcgetattr(0, &oldt) == -1 )
err(1, "tcgetattr");
*(__m128i *)&newt_1.c_iflag = _mm_load_si128((const __m128i *)&oldt);
*(__m128i *)&newt_1.c_line = _mm_load_si128((const __m128i *)&oldt.c_line);
*(__m128i *)&newt_1.c_cc[15] = _mm_load_si128((const __m128i *)&oldt.c_cc[15]);
*(_QWORD *)&newt_1.c_cc[31] = *(_QWORD *)&oldt.c_cc[31];
newt_1.c_ospeed = oldt.c_ospeed;
newt_1.c_lflag &= 0xFFFFFFF5;
result = tcsetattr(0, 0, &newt_1);
if ( result == -1 )
err(1, "tcsetattr");
return result;
}

original_code:
void
init(void)
{
 static struct termios newt;
 if(tcgetattr(0, &oldt) == -1)
  err(1, "tcgetattr");
 newt = oldt;
 newt.c_lflag &= ~(0000002 | 0000010);
 if(tcsetattr(0, 0, &newt) == -1)
  err(1, "tcsetattr");
}

canonical_original_code:
void init()
{
 static struct termios newt;
 if(tcgetattr(0, &oldt) == -1)
  err(1, "tcgetattr");
 newt = oldt;
 newt.c_lflag &= ~(0000002 | 0000010);
 if(tcsetattr(0, 0, &newt) == -1)
  err(1, "tcsetattr");
}

variable_types: struct termios { unsigned int c_iflag; unsigned int c_oflag; unsigned int c_cflag; unsigned int c_lflag; unsigned char c_line; unsigned char c_cc[32]; unsigned int c_ispeed; unsigned int c_ospeed; }


readch

decompiled_code:
__int64 readch()
{
__int64 result; // rax
char v1; // [rsp+Dh] [rbp-Bh] BYREF
char v2; // [rsp+Eh] [rbp-Ah]
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

read(0, buf, 1uLL);
result = (unsigned __int8)buf[0];
if ( buf[0] == 27 )
{
read(0, &v1, 2uLL);
result = 0LL;
if ( v1 == 91 )
{
if ( v2 == 67 )
{
result = 108LL;
}
else if ( v2 > 67 )
{
result = 0LL;
if ( v2 == 68 )
result = 106LL;
}
else
{
result = 107LL;
if ( v2 != 65 )
{
LOBYTE(result) = v2 == 66;
result = (unsigned int)(32 * result);
}
}
}
}
return result;
}

canonical_decompiled_code:
__int64 func7()
{
__int64 result; // rax
char v1; // [rsp+Dh] [rbp-Bh] BYREF
char v2; // [rsp+Eh] [rbp-Ah]
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

read(0, buf, 1uLL);
result = (unsigned __int8)buf[0];
if ( buf[0] == 27 )
{
read(0, &v1, 2uLL);
result = 0LL;
if ( v1 == 91 )
{
if ( v2 == 67 )
{
result = 108LL;
}
else if ( v2 > 67 )
{
result = 0LL;
if ( v2 == 68 )
result = 106LL;
}
else
{
result = 107LL;
if ( v2 != 65 )
{
LOBYTE(result) = v2 == 66;
result = (unsigned int)(32 * result);
}
}
}
}
return result;
}

original_code:
char
readch(void)
{
 char c, c2[2];
 read(0, &c, 1);
 if(c == "\033["[0]){
  read(0, c2, 2);
  if(c2[0] != "\033["[1])
   return '\0';
  switch(c2[1]){
  case 'A': return 'k';
  case 'B': return ' ';
  case 'C': return 'l';
  case 'D': return 'j';
  }
  return '\0';
 }
 return c;
}

canonical_original_code:
char readch()
{
 char c; char c2[2];
 read(0, &c, 1);
 if(c == "\033["[0]){
  read(0, c2, 2);
  if(c2[0] != "\033["[1])
   return '\0';
  switch(c2[1]){
  case 'A': return 'k';
  case 'B': return ' ';
  case 'C': return 'l';
  case 'D': return 'j';
  }
  return '\0';
 }
 return c;
}

variable_types: c char

return_type: char


drawtext

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

va_start(arg, format);
v1 = vsnprintf(buf_0, 0x50uLL, format, arg);
posxy(5 - v1 / 4, 12);
printf("%s", buf_0);
return flush();
}

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

va_start(arg, format);
v1 = vsnprintf(buf_0, 0x50uLL, format, arg);
func1(5 - v1 / 4, 12);
printf("%s", buf_0);
return func3();
}

original_code:
void
drawtext(char *fmt, ...)
{
 va_list ap;
 static char buf[80];
 int len;
 __builtin_va_start(ap,fmt);
 len = vsnprintf(buf, sizeof buf, fmt, ap);
 __builtin_va_end(ap);
 posxy(10/2 - len/4, (20 + 4)/2);
 printf("%s", buf);
 flush();
}

canonical_original_code:
void drawtext(char *fmt, ...)
{
 __builtin_va_list ap;
 static char buf[80];
 int len;
 __builtin_va_start(ap,fmt);
 len = vsnprintf(buf, sizeof buf, fmt, ap);
 __builtin_va_end(ap);
 posxy(10/2 - len/4, (20 + 4)/2);
 printf("%s", buf);
 flush();
}

variable_types:
fmt
char *

variable_types:
buf
char [80]

variable_types: len int


printUsage

repo: krendil/getverse

decompiled_code:
int printUsage()
{
return printf(
"Usage:\tgetverse -t <translation> [-f format] [<reference>]\n"
"\tgetverse [-h | -?]\n"
"Options:\n"
"\t-t\tA Bible translation you have installed in SWORD (e.g. KJV)\n"
"\t-f\tA format string that will determine the format of the output\n"
"\t\t Defaults to '%%p'\n"
"\t-h\n"
"\t-?\tDisplay this message and exit\n"
"Format variables:\n"
"\t%%p\tThe passage text\n"
"\t%%t\tThe translation name\n"
"\t%%r\tThe supplied reference\n"
"\t%%%%\tA literal %% character\n"
"\n"
"If no reference is supplied, getverse tries to read from stdin.\n");
}

canonical_decompiled_code:
int func0()
{
return printf(
"Usage:\tgetverse -t <translation> [-f format] [<reference>]\n"
"\tgetverse [-h | -?]\n"
"Options:\n"
"\t-t\tA Bible translation you have installed in SWORD (e.g. KJV)\n"
"\t-f\tA format string that will determine the format of the output\n"
"\t\t Defaults to '%%p'\n"
"\t-h\n"
"\t-?\tDisplay this message and exit\n"
"Format variables:\n"
"\t%%p\tThe passage text\n"
"\t%%t\tThe translation name\n"
"\t%%r\tThe supplied reference\n"
"\t%%%%\tA literal %% character\n"
"\n"
"If no reference is supplied, getverse tries to read from stdin.\n");
}

original_code:
void printUsage() {
    printf( "Usage:	getverse -t <translation> [-f format] [<reference>]\n" "	getverse [-h | -?]\n" "Options:\n" "	-t	A Bible translation you have installed in SWORD (e.g. KJV)\n" "	-f	A format string that will determine the format of the output\n" "		 Defaults to '%%p'\n" "	-h\n" "	-?	Display this message and exit" "\n" "Format variables:\n" "	%%p	The passage text\n" "	%%t	The translation name\n" "	%%r	The supplied reference\n" "	%%%%	A literal %% character\n" "\n" "If no reference is supplied, getverse tries to read from stdin.\n" );
}


setReference

decompiled_code:
char *__fastcall setReference(int a1, const char **a2)
{
const char *v2; // rbx
size_t v3; // rax
char *v4; // rax
char *v5; // rbx
const char **v6; // rbp
int v7; // er13
const char *v8; // r12

v2 = *a2;
v3 = strlen(*a2);
v4 = (char *)calloc(1uLL, v3);
v5 = strcat(v4, v2);
if ( a1 > 1 )
{
v6 = a2 + 1;
v7 = 0;
do
{
v8 = *v6;
v7 += strlen(*v6 + 1);
v5 = (char *)realloc(v5, v7);
*(_WORD *)&v5[strlen(v5)] = 32;
strcat(v5, v8);
++v6;
}
while ( v6 != &a2[(unsigned int)(a1 - 2) + 2] );
}
return v5;
}

canonical_decompiled_code:
char *__fastcall func1(int a1, const char **a2)
{
const char *v2; // rbx
size_t v3; // rax
char *v4; // rax
char *v5; // rbx
const char **v6; // rbp
int v7; // er13
const char *v8; // r12

v2 = *a2;
v3 = strlen(*a2);
v4 = (char *)calloc(1uLL, v3);
v5 = strcat(v4, v2);
if ( a1 > 1 )
{
v6 = a2 + 1;
v7 = 0;
do
{
v8 = *v6;
v7 += strlen(*v6 + 1);
v5 = (char *)realloc(v5, v7);
*(_WORD *)&v5[strlen(v5)] = 32;
strcat(v5, v8);
++v6;
}
while ( v6 != &a2[(unsigned int)(a1 - 2) + 2] );
}
return v5;
}

original_code:
char * setReference( int count, char ** words ) {
    int size = 0;
    char * reference = (char*)calloc(1, strlen(words[0]));
    reference = strcat(reference, words[0]);
    for( int i = 1; i < count; i++ ) {
        size += strlen(words[i]+1);
        reference = (char*)realloc(reference, size);
        reference = strcat(strcat(reference, " "), words[i]);
    }
    return reference;
}

canonical_original_code:
char *setReference(int count, char **words) {
    int size = 0;
    char *reference = (char*)calloc(1, strlen(words[0]));
    reference = strcat(reference, words[0]);
    for( int i = 1; i < count; i++ ) {
        size += strlen(words[i]+1);
        reference = (char *)realloc(reference, size);
        reference = strcat(strcat(reference, " "), words[i]);
    }
    return reference;
}

variable_types: count int

variable_types:
words
char **

variable_types: size int

variable_types:
reference
char *

variable_types: i int


readReference

decompiled_code:
void *readReference()
{
size_t v0; // rbp
void *v1; // rbx
char *v2; // rax

v0 = 32LL;
v1 = 0LL;
do
{
v1 = realloc(v1, v0);
v2 = fgets((char *)v1, v0, stdin);
v0 += 32LL;
}
while ( !v2 );
return v1;
}

canonical_decompiled_code:
void *func2()
{
size_t v0; // rbp
void *v1; // rbx
char *v2; // rax

v0 = 32LL;
v1 = 0LL;
do
{
v1 = realloc(v1, v0);
v2 = fgets((char *)v1, v0, stdin);
v0 += 32LL;
}
while ( !v2 );
return v1;
}

original_code:
char * readReference() {
    int size = 0;
    char * reference = ((void *)0);
    do {
         size += 32;
         reference = (char*)realloc(reference, size);
     } while (!fgets(reference, size, stdin));
     return reference;
}

canonical_original_code:
char *readReference() {
    int size = 0;
    char *reference = ((void *)0);
    do {
         size += 32;
         reference = (char *)realloc(reference, size);
     } while (!fgets(reference, size, stdin));
     return reference;
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v5; // ecx
const char *v6; // rbp
const char *v7; // rbx
char *v8; // rax
void *v9; // rdx
__int64 *v10; // rsi
__int64 v11; // rdx
const char *v12; // rsi
char v13; // al
const char *v14; // rax
char *v15; // rbx
char *v16; // rdi
__int64 v17; // rdx
__int64 v19; // [rsp+0h] [rbp-38h] BYREF
char *s; // [rsp+8h] [rbp-30h]

if ( argc <= 1 )
goto LABEL_18;
v5 = 1;
v6 = 0LL;
v7 = 0LL;
while ( 1 )
{
v11 = v5;
v12 = argv[v11];
if ( *v12 != 45 )
break;
v13 = v12[1];
if ( v13 != 114 )
{
if ( v13 > 114 )
{
if ( v13 != 116 )
goto LABEL_7;
++v5;
v7 = argv[v11 + 1];
}
else
{
if ( v13 != 102 )
{
if ( v13 == 104 || v13 == 63 )
{
printUsage();
exit(0);
}
LABEL_7:
fprintf(stderr, "Unknown option %s\n", argv[v5]);
exit(1);
}
++v5;
v6 = argv[v11 + 1];
}
}
if ( argc <= ++v5 )
{
if ( v7 )
{
LABEL_31:
v9 = readReference();
goto LABEL_5;
}
goto LABEL_18;
}
}
v8 = setReference(argc - v5, &argv[v11]);
v9 = v8;
if ( v7 )
{
if ( !v8 )
goto LABEL_31;
LABEL_5:
v10 = (__int64 *)v7;
switch ( (unsigned int)getPassage(&v19, v7, v9, 0LL) )
{
case 0xFFFFFFFC:
fwrite("That chapter doesn't have that many verses.\n", 1uLL, 0x2CuLL, stderr);
exit(1);
case 0xFFFFFFFD:
fwrite("That book doesn't have that many chapters.\n", 1uLL, 0x2BuLL, stderr);
exit(1);
case 0xFFFFFFFE:
fwrite("That passage does not exist.\n", 1uLL, 0x1DuLL, stderr);
exit(1);
case 0xFFFFFFFF:
fprintf(stderr, "Could not find translation '%s'. Are you sure it is installed?\n", v7);
exit(1);
case 0u:
if ( v6 )
{
v10 = &v19;
v14 = (const char *)formatPassage(v6, &v19);
v15 = (char *)v14;
if ( !v14 )
{
fwrite("Unknown variable in format string.\n", 1uLL, 0x23uLL, stderr);
exit(1);
}
puts(v14);
v16 = v15;
free(v15);
}
else
{
v16 = s;
puts(s);
}
deletePassage(v16, v10, v17);
exit(0);
default:
fwrite("Something when wrong. Not quit sure what.", 1uLL, 0x29uLL, stderr);
exit(1);
}
}
LABEL_18:
fwrite("Please specify a translation with the -t option\n", 1uLL, 0x30uLL, stderr);
exit(1);
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
int v5; // ecx
const char *v6; // rbp
const char *v7; // rbx
char *v8; // rax
void *v9; // rdx
__int64 *v10; // rsi
__int64 v11; // rdx
const char *v12; // rsi
char v13; // al
const char *v14; // rax
char *v15; // rbx
char *v16; // rdi
__int64 v17; // rdx
__int64 v19; // [rsp+0h] [rbp-38h] BYREF
char *s; // [rsp+8h] [rbp-30h]

if ( argc <= 1 )
goto LABEL_18;
v5 = 1;
v6 = 0LL;
v7 = 0LL;
while ( 1 )
{
v11 = v5;
v12 = argv[v11];
if ( *v12 != 45 )
break;
v13 = v12[1];
if ( v13 != 114 )
{
if ( v13 > 114 )
{
if ( v13 != 116 )
goto LABEL_7;
++v5;
v7 = argv[v11 + 1];
}
else
{
if ( v13 != 102 )
{
if ( v13 == 104 || v13 == 63 )
{
func0();
exit(0);
}
LABEL_7:
fprintf(stderr, "Unknown option %s\n", argv[v5]);
exit(1);
}
++v5;
v6 = argv[v11 + 1];
}
}
if ( argc <= ++v5 )
{
if ( v7 )
{
LABEL_31:
v9 = func2();
goto LABEL_5;
}
goto LABEL_18;
}
}
v8 = func1(argc - v5, &argv[v11]);
v9 = v8;
if ( v7 )
{
if ( !v8 )
goto LABEL_31;
LABEL_5:
v10 = (__int64 *)v7;
switch ( (unsigned int)getPassage(&v19, v7, v9, 0LL) )
{
case 0xFFFFFFFC:
fwrite("That chapter doesn't have that many verses.\n", 1uLL, 0x2CuLL, stderr);
exit(1);
case 0xFFFFFFFD:
fwrite("That book doesn't have that many chapters.\n", 1uLL, 0x2BuLL, stderr);
exit(1);
case 0xFFFFFFFE:
fwrite("That passage does not exist.\n", 1uLL, 0x1DuLL, stderr);
exit(1);
case 0xFFFFFFFF:
fprintf(stderr, "Could not find translation '%s'. Are you sure it is installed?\n", v7);
exit(1);
case 0u:
if ( v6 )
{
v10 = &v19;
v14 = (const char *)formatPassage(v6, &v19);
v15 = (char *)v14;
if ( !v14 )
{
fwrite("Unknown variable in format string.\n", 1uLL, 0x23uLL, stderr);
exit(1);
}
puts(v14);
v16 = v15;
free(v15);
}
else
{
v16 = s;
puts(s);
}
deletePassage(v16, v10, v17);
exit(0);
default:
fwrite("Something when wrong. Not quit sure what.", 1uLL, 0x29uLL, stderr);
exit(1);
}
}
LABEL_18:
fwrite("Please specify a translation with the -t option\n", 1uLL, 0x30uLL, stderr);
exit(1);
}

original_code:
int main(int argc, char ** argv) {
    char * translation = ((void *)0);
    char * format = ((void *)0);
    char * reference = ((void *)0);
    int red = 0;
    for( int i = 1; i < argc; i++ ) {
        if( argv[i][0] != '-' ) {
            reference = setReference( argc - i, argv + i );
            break;
        }
        switch( argv[i][1] ) {
            case 'f':
                format = argv[++i];
                break;
            case 't':
                translation = argv[++i];
                break;
            case 'r':
                red = 1;
                break;
            case 'h':
            case '?':
                printUsage();
                exit(0);
                break;
            default:
                fprintf(stderr, "Unknown option %s\n", argv[i]);
                exit(1);
                break;
        }
    }
    if(translation == ((void *)0) ) {
        fprintf(stderr, "Please specify a translation with the -t option\n");
        exit(1);
    }
    if( reference == ((void *)0) ) {
        reference = readReference();
    }
    Passage result;
    int err = getPassage(&result, translation, reference, 0);
    switch(err) {
        case -1:
            fprintf(stderr, "Could not find translation '%s'. Are you sure it is installed?\n",
                    translation);
            exit(1); break;
        case -2:
            fprintf(stderr, "That passage does not exist.\n");
            exit(1); break;
        case -3:
            fprintf(stderr, "That book doesn't have that many chapters.\n");
            exit(1); break;
        case -4:
            fprintf(stderr, "That chapter doesn't have that many verses.\n");
            exit(1); break;
        case 0:
            if( format ) {
                char * out = formatPassage(format, &result);
                if(!out) {
                    fprintf(stderr, "Unknown variable in format string.\n");
                    exit(1);
                }
                printf("%s\n", out);
                free(out);
            } else {
                printf("%s\n", result.text);
            }
            deletePassage(result);
            exit(0); break;
        default:
            fprintf(stderr, "Something when wrong. Not quit sure what.");
            exit(1);
    }
}

canonical_original_code:
int main(int argc, char **argv) {
    char *translation = ((void *)0);
    char *format = ((void *)0);
    char *reference = ((void *)0);
    int red = 0;
    for( int i = 1; i < argc; i++ ) {
        if( argv[i][0] != '-' ) {
            reference = setReference( argc - i, argv + i );
            break;
        }
        switch( argv[i][1] ) {
            case 'f':
                format = argv[++i];
                break;
            case 't':
                translation = argv[++i];
                break;
            case 'r':
                red = 1;
                break;
            case 'h':
            case '?':
                printUsage();
                exit(0);
                break;
            default:
                fprintf(stderr, "Unknown option %s\n", argv[i]);
                exit(1);
                break;
        }
    }
    if(translation == ((void *)0) ) {
        fprintf(stderr, "Please specify a translation with the -t option\n");
        exit(1);
    }
    if( reference == ((void *)0) ) {
        reference = readReference();
    }
    struct Passage result;
    int err = getPassage(&result, translation, reference, 0);
    switch(err) {
        case -1:
            fprintf(stderr, "Could not find translation '%s'. Are you sure it is installed?\n",
                    translation);
            exit(1); break;
        case -2:
            fprintf(stderr, "That passage does not exist.\n");
            exit(1); break;
        case -3:
            fprintf(stderr, "That book doesn't have that many chapters.\n");
            exit(1); break;
        case -4:
            fprintf(stderr, "That chapter doesn't have that many verses.\n");
            exit(1); break;
        case 0:
            if( format ) {
                char *out = formatPassage(format, &result);
                if(!out) {
                    fprintf(stderr, "Unknown variable in format string.\n");
                    exit(1);
                }
                printf("%s\n", out);
                free(out);
            } else {
                printf("%s\n", result.text);
            }
            deletePassage(result);
            exit(0); break;
        default:
            fprintf(stderr, "Something when wrong. Not quit sure what.");
            exit(1);
    }
}

variable_types: argc int

variable_types:
argv
char **

variable_types:
translation
char *

variable_types:
format
char *

variable_types: red int

variable_types: struct Passage { char *reference; char *text; char *translation; }

variable_types: err int

variable_types:
out
char *


repo: krig/LISP

decompiled_code:
__int64 __fastcall init_file_stream(FILE **a1, const char *a2)
{
FILE *v2; // rax

v2 = fopen(a2, "r");
*a1 = v2;
if ( !v2 )
return 0LL;
a1[1] = (FILE *)file_close_stream;
a1[2] = (FILE *)file_get_char;
a1[3] = (FILE *)file_put_char;
return 1LL;
}

canonical_decompiled_code:
__int64 __fastcall func3(FILE **a1, const char *a2)
{
FILE *v2; // rax

v2 = fopen(a2, "r");
*a1 = v2;
if ( !v2 )
return 0LL;
a1[1] = (FILE *)file_close_stream;
a1[2] = (FILE *)file_get_char;
a1[3] = (FILE *)file_put_char;
return 1LL;
}

original_code:
int init_file_stream(struct instream* stream, const char* filename) {
        stream->hstream = fopen(filename, "r");
        if (!stream->hstream)
                return 0;
        stream->close_stream = &file_close_stream;
        stream->get_char = &file_get_char;
        stream->put_char = &file_put_char;
        return 1;
}

canonical_original_code:
int init_file_stream(struct instream *stream, char *filename) {
        stream->hstream = fopen(filename, "r");
        if (!stream->hstream)
                return 0;
        stream->close_stream = &file_close_stream;
        stream->get_char = &file_get_char;
        stream->put_char = &file_put_char;
        return 1;
}

variable_types: struct instream { void *hstream; void (*close_stream)(struct instream *); int (*get_char)(struct instream *); void (*put_char)(struct instream *, int); } *

variable_types:
filename
char *


logseg

repo: krisrm/socketdirmonitor

decompiled_code:
void __noreturn logseg()
{
slog("Segmentation fault\n");
exit(1);
}

canonical_decompiled_code:
void __noreturn func1()
{
slog("Segmentation fault\n");
exit(1);
}

original_code:
void logseg(){
    slog("Segmentation fault\n");
    exit(1);
}


ssRestart

decompiled_code:
void ssRestart()
{
error = 1;
_sysv_signal(1, ssRestart);
}

canonical_decompiled_code:
void func2()
{
error = 1;
_sysv_signal(1, ssRestart);
}

original_code:
void ssRestart(){
    error= 1;
    signal(1,&ssRestart);
}


stopChecking

decompiled_code:
void __noreturn stopChecking()
{
close(fds[0]);
freeAll(&diff);
exit(0);
}

canonical_decompiled_code:
void __noreturn func3()
{
close(fds[0]);
freeAll(&diff);
exit(0);
}

original_code:
void stopChecking(){
    close(fds[0]);
    freeAll(&diff);
    exit(0);
}


daemonize

decompiled_code:
int daemonize()
{
__pid_t v0; // eax

v0 = fork();
if ( v0 < 0 )
exit(1);
if ( v0 > 0 )
{
printf(
"Server successfully daemonized. Kill process id %d and %d when done.\n",
(unsigned int)v0,
(unsigned int)dirmonitor);
exit(0);
}
umask(0);
if ( setsid() < 0 )
exit(1);
if ( chdir("/") < 0 )
exit(1);
_sysv_signal(11, (__sighandler_t)logseg);
close(0);
close(1);
return close(2);
}

canonical_decompiled_code:
int func4()
{
__pid_t v0; // eax

v0 = fork();
if ( v0 < 0 )
exit(1);
if ( v0 > 0 )
{
printf(
"Server successfully daemonized. Kill process id %d and %d when done.\n",
(unsigned int)v0,
(unsigned int)dirmonitor);
exit(0);
}
umask(0);
if ( setsid() < 0 )
exit(1);
if ( chdir("/") < 0 )
exit(1);
_sysv_signal(11, (__sighandler_t)logseg);
close(0);
close(1);
return close(2);
}

original_code:
void daemonize(){
    pid_t pid, sid;
 pid = fork();
 if (pid < 0) {
  exit(1);
 }
 if (pid > 0) {
  printf("Server successfully daemonized. Kill process id %d and %d when done.\n",pid,dirmonitor);
  exit(0);
 }
 umask(0);
 sid = setsid();
 if (sid < 0) {
  exit(1);
 }
 if ((chdir("/")) < 0) {
  exit(1);
 }
    signal(11, &logseg);
 close(0);
 close(1);
 close(2);
}

canonical_original_code:
void daemonize(){
    int pid; int sid;
 pid = fork();
 if (pid < 0) {
  exit(1);
 }
 if (pid > 0) {
  printf("Server successfully daemonized. Kill process id %d and %d when done.\n",pid,dirmonitor);
  exit(0);
 }
 umask(0);
 sid = setsid();
 if (sid < 0) {
  exit(1);
 }
 if ((chdir("/")) < 0) {
  exit(1);
 }
    signal(11, &logseg);
 close(0);
 close(1);
 close(2);
}

variable_types: pid int

variable_types: sid int


checkDirectory

decompiled_code:
__int64 checkDirectory()
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

if ( (unsigned int)diffDirectory(&diff) )
{
buf[0] = -1;
write(fds[1], buf, 1uLL);
stopChecking();
}
writeDiffEntries((unsigned int)fds[1], &diff);
sleep(period);
return checkDirectory();
}

canonical_decompiled_code:
__int64 func5()
{
char buf[9]; // [rsp+Fh] [rbp-9h] BYREF

if ( (unsigned int)diffDirectory(&diff) )
{
buf[0] = -1;
write(fds[1], buf, 1uLL);
func3();
}
writeDiffEntries((unsigned int)fds[1], &diff);
sleep(period);
return func5();
}

original_code:
void checkDirectory(){
    int worked = diffDirectory(&diff);
    if (worked != 0){
        unsigned char w = 255;
        write(fds[1],&w,1);
        stopChecking();
    }
    writeDiffEntries(fds[1],&diff);
    sleep(period);
    checkDirectory();
}

variable_types: worked int

variable_types:
w
unsigned char


openDirmonitor

decompiled_code:
__sighandler_t openDirmonitor()
{
__pid_t v0; // eax

if ( pipe(fds) == -1 )
slog("Could not open pipe\n");
v0 = fork();
dirmonitor = v0;
if ( v0 < 0 )
{
slog("Could not fork directory monitor\n");
goto LABEL_5;
}
if ( v0 )
{
LABEL_5:
close(fds[1]);
_sysv_signal(2, terminate);
return _sysv_signal(1, (__sighandler_t)ssRestart);
}
close(fds[0]);
_sysv_signal(2, (__sighandler_t)stopChecking);
_sysv_signal(15, (__sighandler_t)stopChecking);
_sysv_signal(1, (__sighandler_t)stopChecking);
return (__sighandler_t)checkDirectory();
}

canonical_decompiled_code:
__sighandler_t func6()
{
__pid_t v0; // eax

if ( pipe(fds) == -1 )
slog("Could not open pipe\n");
v0 = fork();
dirmonitor = v0;
if ( v0 < 0 )
{
slog("Could not fork directory monitor\n");
goto LABEL_5;
}
if ( v0 )
{
LABEL_5:
close(fds[1]);
_sysv_signal(2, terminate);
return _sysv_signal(1, (__sighandler_t)ssRestart);
}
close(fds[0]);
_sysv_signal(2, (__sighandler_t)stopChecking);
_sysv_signal(15, (__sighandler_t)stopChecking);
_sysv_signal(1, (__sighandler_t)stopChecking);
return (__sighandler_t)func5();
}

original_code:
void openDirmonitor(){
    if (pipe(fds) == -1){
        slog("Could not open pipe\n");
    }
    dirmonitor = fork();
    if (dirmonitor < 0){
        slog("Could not fork directory monitor\n");
    } else if (dirmonitor == 0){
        close(fds[0]);
  signal(2, &stopChecking);
        signal(15, &stopChecking);
  signal(1, &stopChecking);
        checkDirectory();
        return;
    }
    close(fds[1]);
    signal(2,&terminate);
    signal(1,&ssRestart);
}


clientAllDisconnect

decompiled_code:
ssize_t __fastcall clientAllDisconnect(const char *a1)
{
int *v1; // rbx
ssize_t result; // rax

v1 = hosts;
do
{
if ( *v1 == 2 )
result = clientDisconnect(v1, a1);
v1 += 16328;
}
while ( v1 != (int *)(unsigned int)((_DWORD)hosts + 195936) );
return result;
}

canonical_decompiled_code:
ssize_t __fastcall func9(const char *a1)
{
int *v1; // rbx
ssize_t result; // rax

v1 = hosts;
do
{
if ( *v1 == 2 )
result = func8(v1, a1);
v1 += 16328;
}
while ( v1 != (int *)(unsigned int)((_DWORD)hosts + 195936) );
return result;
}

original_code:
void clientAllDisconnect(char* message){
    for (int n = 0; n < 3; n++){
        if (hosts[n].status == 2)
            clientDisconnect(&hosts[n],message);
    }
}

canonical_original_code:
void clientAllDisconnect(char *message){
    for (int n = 0; n < 3; n++){
        if (hosts[n].status == 2)
            clientDisconnect(&hosts[n],message);
    }
}

variable_types:
message
char *

variable_types: n int


terminate

decompiled_code:
void __noreturn terminate()
{
int v0; // ebx

clientAllDisconnect("Server interrupted");
kill(dirmonitor, 15);
if ( fdmax >= 0 )
{
v0 = 0;
do
{
if ( (*((_QWORD *)&inputs + v0 / 64) & (1LL << (v0 % 64))) != 0 )
close(v0);
++v0;
}
while ( fdmax >= v0 );
}
exit(0);
}

canonical_decompiled_code:
void __noreturn func10()
{
int v0; // ebx

func9("Server interrupted");
kill(dirmonitor, 15);
if ( fdmax >= 0 )
{
v0 = 0;
do
{
if ( (*((_QWORD *)&inputs + v0 / 64) & (1LL << (v0 % 64))) != 0 )
close(v0);
++v0;
}
while ( fdmax >= v0 );
}
exit(0);
}

original_code:
void terminate(){
    clientAllDisconnect("Server interrupted");
    kill(dirmonitor,15);
    for(int i = 0; i <= fdmax; i++){
        if(((((&inputs)->__fds_bits)[((i) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((i) % (8 * (int) sizeof (__fd_mask)))))) != 0)){
            close(i);
        }
    }
    exit(0);
}

variable_types: i int


writeUpdates

decompiled_code:
__int64 writeUpdates()
{
int v0; // er12
__int64 result; // rax
int i; // ebp
unsigned __int8 buf; // [rsp+Fh] [rbp-119h] BYREF
char v4[248]; // [rsp+10h] [rbp-118h] BYREF
int v5; // [rsp+108h] [rbp-20h]
__int16 v6; // [rsp+10Ch] [rbp-1Ch]
char v7; // [rsp+10Eh] [rbp-1Ah]

read(fds[0], &buf, 1uLL);
v0 = buf;
if ( buf == 255 )
{
clientAllDisconnect("Directory removed or inaccessible");
terminate();
}
result = writeToClients(hosts, 3LL, &buf, 1LL);
if ( v0 > 0 )
{
for ( i = 0; i != v0; ++i )
{
buf = 1;
read(fds[0], &buf, 1uLL);
writeToClients(hosts, 3LL, &buf, 1LL);
memset(v4, 0, sizeof(v4));
v5 = 0;
v6 = 0;
v7 = 0;
read(fds[0], v4, buf);
result = writeToClients(hosts, 3LL, v4, buf);
}
}
return result;
}

canonical_decompiled_code:
__int64 func11()
{
int v0; // er12
__int64 result; // rax
int i; // ebp
unsigned __int8 buf; // [rsp+Fh] [rbp-119h] BYREF
char v4[248]; // [rsp+10h] [rbp-118h] BYREF
int v5; // [rsp+108h] [rbp-20h]
__int16 v6; // [rsp+10Ch] [rbp-1Ch]
char v7; // [rsp+10Eh] [rbp-1Ah]

read(fds[0], &buf, 1uLL);
v0 = buf;
if ( buf == 255 )
{
func9("Directory removed or inaccessible");
func10();
}
result = writeToClients(hosts, 3LL, &buf, 1LL);
if ( v0 > 0 )
{
for ( i = 0; i != v0; ++i )
{
buf = 1;
read(fds[0], &buf, 1uLL);
writeToClients(hosts, 3LL, &buf, 1LL);
memset(v4, 0, sizeof(v4));
v5 = 0;
v6 = 0;
v7 = 0;
read(fds[0], v4, buf);
result = writeToClients(hosts, 3LL, v4, buf);
}
}
return result;
}

original_code:
void writeUpdates(){
    char buf[255];
    int expected = 1;
    int total = 0;
    unsigned char w;
    read(fds[0], &w, expected);
    total = (int) w;
    if (total == 255){
        clientAllDisconnect("Directory removed or inaccessible");
        terminate();
        return;
    }
    writeToClients(hosts,3,&w,1);
    for(int a =0; a < total; a++){
        w = 1;
        read(fds[0], &w,w);
        writeToClients(hosts,3,&w,1);
        memset(buf,0,sizeof(buf));
        read(fds[0], &buf, w);
        writeToClients(hosts,3,buf,w);
    }
}

canonical_original_code:
void writeUpdates(){
    char buf[255];
    int expected = 1;
    int total = 0;
    unsigned char w;
    read(fds[0], &w, expected);
    total = (int) w;
    if (total == 255){
        clientAllDisconnect("Directory removed or inaccessible");
        terminate();
        return;
    }
    writeToClients(hosts,3,&w,1);
    for(int a = 0; a < total; a++){
        w = 1;
        read(fds[0], &w,w);
        writeToClients(hosts,3,&w,1);
        memset(buf,0,sizeof(buf));
        read(fds[0], &buf, w);
        writeToClients(hosts,3,buf,w);
    }
}

variable_types:
buf
char [255]

variable_types: expected int

variable_types: total int

variable_types: a int