decompiled_code:
int *__fastcall random_shuffle(__int64 a1)
{
__int64 i; // rbx
int *result; // rax
int v3; // edx

for ( i = 0LL; i != 39; ++i )
{
result = (int *)(a1 + 4LL * ((int)i + rand() % (40 - (int)i)));
v3 = *result;
*result = *(_DWORD *)(a1 + 4 * i);
*(_DWORD *)(a1 + 4 * i) = v3;
}
return result;
}

canonical_decompiled_code:
int *__fastcall func10(__int64 a1)
{
__int64 i; // rbx
int *result; // rax
int v3; // edx

for ( i = 0LL; i != 39; ++i )
{
result = (int *)(a1 + 4LL * ((int)i + rand() % (40 - (int)i)));
v3 = *result;
*result = *(_DWORD *)(a1 + 4 * i);
*(_DWORD *)(a1 + 4 * i) = v3;
}
return result;
}

original_code:
void random_shuffle(int items[]) {
    int i;
    for(i = 0; i < 40 -1; i++) {
        int ind = i + (rand() % (40 -i));
        int tmp = items[ind];
        items[ind] = items[i];
        items[i] = tmp;
    }
}

variable_types:
items
int []

variable_types: ind int

variable_types: tmp int


execute

repo: mchalek/euler

decompiled_code:
__int64 execute()
{
__int64 i; // rax
char *v1; // rbx
unsigned int v2; // ebp
unsigned __int64 v3; // r12
int v4; // eax
int v6[40]; // [rsp+0h] [rbp-C8h] BYREF
char v7; // [rsp+A0h] [rbp-28h] BYREF

for ( i = 0LL; i != 40; ++i )
v6[i] = i;
random_shuffle((__int64)v6);
v1 = (char *)v6;
v2 = 0;
v3 = 0LL;
do
{
v3 |= 1LL << *(_DWORD *)v1;
v4 = count_pieces(v3);
if ( (int)v2 < v4 )
v2 = v4;
v1 += 4;
}
while ( &v7 != v1 );
return v2;
}

canonical_decompiled_code:
__int64 func11()
{
__int64 i; // rax
char *v1; // rbx
unsigned int v2; // ebp
unsigned __int64 v3; // r12
int v4; // eax
int v6[40]; // [rsp+0h] [rbp-C8h] BYREF
char v7; // [rsp+A0h] [rbp-28h] BYREF

for ( i = 0LL; i != 40; ++i )
v6[i] = i;
func10((__int64)v6);
v1 = (char *)v6;
v2 = 0;
v3 = 0LL;
do
{
v3 |= 1LL << *(_DWORD *)v1;
v4 = func8(v3);
if ( (int)v2 < v4 )
v2 = v4;
v1 += 4;
}
while ( &v7 != v1 );
return v2;
}

original_code:
int execute() {
    int i;
    int pieces[40];
    for(i = 0; i < 40; i++)
        pieces[i] = i;
    random_shuffle(pieces);
    uint64_t state = 0;
    int max_count = 0;
    for(i = 0; i < 40; i++) {
        state |= 1ul << pieces[i];
        int count = count_pieces(state);
        if(count > max_count) {
            max_count = count;
        }
    }
    return max_count;
}

canonical_original_code:
int execute() {
    int i;
    int pieces[40];
    for(i = 0; i < 40; i++)
        pieces[i] = i;
    random_shuffle(pieces);
    unsigned long state = 0;
    int max_count = 0;
    for(i = 0; i < 40; i++) {
        state |= 1ul << pieces[i];
        int count = count_pieces(state);
        if(count > max_count) {
            max_count = count;
        }
    }
    return max_count;
}

variable_types: i int

variable_types:
pieces
int [40]

variable_types:
state
unsigned long

variable_types: count int

return_type: int


main

canonical_name: main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi
__int64 v4; // rbp
__int64 i; // rbx

v3 = (const char *)time(0LL);
srand((unsigned int)v3);
v4 = (int)execute(v3, argv);
for ( i = 1LL; ; printf("after %ld iterations: %.6f\n", i, (double)(int)v4 / (double)(int)i) )
{
do
{
v4 += (int)execute(v3, argv);
++i;
}
while ( i != 1000000 * (i / 1000000) );
argv = (const char **)i;
v3 = "after %ld iterations: %.6f\n";
}
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
const char *v3; // rdi
__int64 v4; // rbp
__int64 i; // rbx

v3 = (const char *)time(0LL);
srand((unsigned int)v3);
v4 = (int)func11(v3, argv);
for ( i = 1LL; ; printf("after %ld iterations: %.6f\n", i, (double)(int)v4 / (double)(int)i) )
{
do
{
v4 += (int)func11(v3, argv);
++i;
}
while ( i != 1000000 * (i / 1000000) );
argv = (const char **)i;
v3 = "after %ld iterations: %.6f\n";
}
}

original_code:
int main(void) {
    long sum = 0l;
    long num_trials = 0l;
    srand(time(((void *)0)));
    while(1) {
        sum += execute();
        num_trials += 1;
        if(!(num_trials % 1000000)) {
            printf("after %ld iterations: %.6f\n", num_trials, ((double) sum) / num_trials);
        }
    }
    return 0;
}

canonical_original_code:
int main() {
    long sum = 0l;
    long num_trials = 0l;
    srand(time(((void *)0)));
    while(1) {
        sum += execute();
        num_trials += 1;
        if(!(num_trials % 1000000)) {
            printf("after %ld iterations: %.6f\n", num_trials, ((double) sum) / num_trials);
        }
    }
    return 0;
}

variable_types: sum long


mkprimes

original_code:
void mkprimes(int max_n, uint64_t **primes, int *np)
{
    _Bool *iscomp = calloc(1 + max_n, sizeof(_Bool));
    *primes = malloc((1 + max_n)*sizeof(uint64_t));
    int i;
    *np = 0;
    for(i = 2; i <= max_n; i++) {
        if(!iscomp[i]) {
            (*primes)[*np] = i;
            (*np)++;
            int j = 2*i;
            while(j <= max_n) {
                iscomp[j] = 1;
                j += i;
            }
        }
    }
    *primes = realloc(*primes, (*np)*sizeof(uint64_t));
}

canonical_original_code:
void mkprimes(int max_n, unsigned long **primes, int *np)
{
    _Bool *iscomp = calloc(1 + max_n, sizeof(_Bool));
    *primes = malloc((1 + max_n)*sizeof(unsigned long));
    int i;
    *np = 0;
    for(i = 2; i <= max_n; i++) {
        if(!iscomp[i]) {
            (*primes)[*np] = i;
            (*np)++;
            int j = 2*i;
            while(j <= max_n) {
                iscomp[j] = 1;
                j += i;
            }
        }
    }
    *primes = realloc(*primes, (*np)*sizeof(unsigned long));
}

variable_types:
primes
unsigned long **

variable_types:
np
int *

variable_types:
iscomp
_Bool *

variable_types: j int

return_type: void


setem

original_code:
void setem(uint64_t cur, uint64_t min, uint64_t *items, uint64_t factors[], int powers[], int nfact, uint64_t cumprod) {
    if(cur > (61ul*61ul*61ul))
        return;
    if(!nfact) {
        items[cur] = min;
        return;
    }
    uint64_t fact = factors[nfact-1];
    int i;
    uint64_t new_cur = cur;
    uint64_t rem_prod = cumprod;
    for(i = 0; i < powers[nfact-1]; i++)
        rem_prod /= fact;
    uint64_t min_cur = (1+min) / rem_prod;
    for(i = 0; i <= powers[nfact-1]; i++) {
        if(new_cur > (61ul*61ul*61ul))
            break;
        if(new_cur > min_cur)
            setem(new_cur, min, items, factors, powers, nfact-1, rem_prod);
        new_cur *= fact;
    }
    return;
}

canonical_original_code:
void setem(unsigned long cur, unsigned long min, unsigned long *items, unsigned long factors[], int powers[], int nfact, unsigned long cumprod) {
    if(cur > (61ul*61ul*61ul))
        return;
    if(!nfact) {
        items[cur] = min;
        return;
    }
    unsigned long fact = factors[nfact-1];
    int i;
    unsigned long new_cur = cur;
    unsigned long rem_prod = cumprod;
    for(i = 0; i < powers[nfact-1]; i++)
        rem_prod /= fact;
    unsigned long min_cur = (1+min) / rem_prod;
    for(i = 0; i <= powers[nfact-1]; i++) {
        if(new_cur > (61ul*61ul*61ul))
            break;
        if(new_cur > min_cur)
            setem(new_cur, min, items, factors, powers, nfact-1, rem_prod);
        new_cur *= fact;
    }
    return;
}

variable_types:
cur
unsigned long

variable_types:
min
unsigned long

variable_types:
items
unsigned long *

variable_types:
factors
unsigned long []

variable_types:
powers
int []

variable_types: nfact int

variable_types:
cumprod
unsigned long

variable_types:
fact
unsigned long


main

original_code:
int main(void) {
    uint64_t *primes;
    int nprimes;
    mkprimes((61ul*61ul*61ul), &primes, &nprimes);
    uint64_t *zz = calloc(20000001, sizeof(uint64_t));
    uint64_t z;
    uint64_t factors[20];
    int powers[20];
    int nfact;
    for(z = 3; z <= (61ul*61ul*61ul); z++) {
        if(!(z % 100000)) {
            printf("z == %ld\n", z);
        }
        uint64_t z2 = z*z-1ul;
        factorize(z2, factors, powers, &nfact, primes, nprimes);
        fflush(stdout);
        setem(1ul, z, zz, factors, powers, nfact, z2);
    }
    uint64_t sum = 0ul;
    for(z = 3; z <= (61ul*61ul*61ul); z++) {
        uint64_t I = zz[z] ? zz[z] : 1ul;
        printf("I(%ld) == %ld\n", z, I);
        sum += I;
    }
    printf("SUM: %ld\n", sum);
    free(zz);
    return 0;
}

canonical_original_code:
int main() {
    unsigned long *primes;
    int nprimes;
    mkprimes((61ul*61ul*61ul), &primes, &nprimes);
    unsigned long *zz = calloc(20000001, sizeof(uint64_t));
    unsigned long z;
    unsigned long factors[20];
    int powers[20];
    int nfact;
    for(z = 3; z <= (61ul*61ul*61ul); z++) {
        if(!(z % 100000)) {
            printf("z == %ld\n", z);
        }
        unsigned long z2 = z*z-1ul;
        factorize(z2, factors, powers, &nfact, primes, nprimes);
        fflush(stdout);
        setem(1ul, z, zz, factors, powers, nfact, z2);
    }
    unsigned long sum = 0ul;
    for(z = 3; z <= (61ul*61ul*61ul); z++) {
        unsigned long I = zz[z] ? zz[z] : 1ul;
        printf("I(%ld) == %ld\n", z, I);
        sum += I;
    }
    printf("SUM: %ld\n", sum);
    free(zz);
    return 0;
}

variable_types:
primes
unsigned long *

variable_types: nprimes int

variable_types:
zz
unsigned long *

variable_types:
z
unsigned long

variable_types:
factors
unsigned long [20]

variable_types:
powers
int [20]

variable_types:
sum
unsigned long

variable_types:
I
unsigned long


encode

decompiled_code:
__int64 __fastcall encode(_DWORD *a1)
{
_DWORD *v1; // rdx
__int64 result; // rax

v1 = a1 + 10;
LODWORD(result) = 0;
do
result = (unsigned int)(*a1++ + 3 * result);
while ( a1 != v1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(_DWORD *a1)
{
_DWORD *v1; // rdx
__int64 result; // rax

v1 = a1 + 10;
LODWORD(result) = 0;
do
result = (unsigned int)(*a1++ + 3 * result);
while ( a1 != v1 );
return result;
}

original_code:
int encode(int dcounts[])
{
    int i;
    int ret = 0;
    for(i = 0; i < 10; i++) {
        ret *= 3;
        ret += dcounts[i];
    }
    return ret;
}

variable_types:
dcounts
int []

variable_types: i int

variable_types: ret int


decode

decompiled_code:
__int64 __fastcall decode(signed int a1, _QWORD *a2)
{
int *v2; // rsi
__int64 result; // rax

*a2 = 0LL;
a2[1] = 0LL;
a2[2] = 0LL;
a2[3] = 0LL;
a2[4] = 0LL;
if ( a1 )
{
v2 = (int *)a2 + 9;
do
{
*v2 = a1 % 3;
a1 /= 3;
result = (unsigned int)a1;
--v2;
}
while ( a1 );
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(signed int a1, _QWORD *a2)
{
int *v2; // rsi
__int64 result; // rax

*a2 = 0LL;
a2[1] = 0LL;
a2[2] = 0LL;
a2[3] = 0LL;
a2[4] = 0LL;
if ( a1 )
{
v2 = (int *)a2 + 9;
do
{
*v2 = a1 % 3;
a1 /= 3;
result = (unsigned int)a1;
--v2;
}
while ( a1 );
}
return result;
}

original_code:
void decode(int encoding, int dcounts[])
{
    memset(dcounts, 0, 10*sizeof(int));
    int i = 9;
    while(encoding) {
        dcounts[i] = encoding % 3;
        encoding /= 3;
        i--;
    }
}

variable_types: encoding int


complement

decompiled_code:
__int64 __fastcall complement(signed int a1)
{
__int64 i; // rax
__int64 v3[6]; // [rsp+0h] [rbp-60h] BYREF
_QWORD v4[6]; // [rsp+30h] [rbp-30h] BYREF

decode(a1, v4);
v3[0] = 0LL;
v3[1] = 0LL;
v3[2] = 0LL;
v3[3] = 0LL;
v3[4] = 0LL;
for ( i = 0LL; i != 40; i += 4LL )
*(_DWORD *)((char *)v3 + i) = 2 - *(_DWORD *)((char *)v4 + i);
return encode(v3);
}

original_code:
int complement(int encoding)
{
    int dcounts[10];
    decode(encoding, dcounts);
    int ccounts[10];
    memset(ccounts, 0, sizeof(ccounts));
    int i;
    for(i = 0; i < 10; i++)
        ccounts[i] = 2 - dcounts[i];
    return encode(ccounts);
}

variable_types:
dcounts
int [10]

variable_types:
ccounts
int [10]


isvalid

decompiled_code:
_BOOL8 __fastcall isvalid(signed int a1)
{
char *v1; // rax
int v2; // ecx
_QWORD v4[5]; // [rsp+0h] [rbp-30h] BYREF
char v5; // [rsp+28h] [rbp-8h] BYREF

decode(a1, v4);
v1 = (char *)v4;
v2 = 0;
while ( *(int *)v1 <= 2 )
{
v2 += *(_DWORD *)v1;
v1 += 4;
if ( v1 == &v5 )
return v2 == 10;
}
return 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func11(signed int a1)
{
char *v1; // rax
int v2; // ecx
_QWORD v4[5]; // [rsp+0h] [rbp-30h] BYREF
char v5; // [rsp+28h] [rbp-8h] BYREF

func8(a1, v4);
v1 = (char *)v4;
v2 = 0;
while ( *(int *)v1 <= 2 )
{
v2 += *(_DWORD *)v1;
v1 += 4;
if ( v1 == &v5 )
return v2 == 10;
}
return 0LL;
}

original_code:
int isvalid(int encoding)
{
    int ndig[10];
    decode(encoding, ndig);
    int i;
    int sum = 0;
    for(i = 0; i < 10; i++) {
        if(ndig[i] > 2)
            return 0;
        sum += ndig[i];
    }
    return sum == 10;
}

variable_types:
ndig
int [10]

variable_types: sum int


count

original_code:
long count(long num, int depth, int dcounts[]) {
    if(depth == 10) {
        long lead = num / 1000000000l;
        int e = encode(dcounts);
        long mod11 = num % 11;
        if(lead)
            encoding_counts_mod_11[mod11][e]++;
        else
            em11_lz[mod11][e]++;
        return 1;
    }
    long ret = 0;
    int dig;
    for(dig = 0; dig < 10; dig++) {
        if(dcounts[dig] == 2)
            continue;
        dcounts[dig]++;
        ret += count(num * 10 + dig, 1 + depth, dcounts);
        dcounts[dig]--;
    }
    return ret;
}

variable_types: num long

variable_types: depth int

variable_types: lead long

variable_types: e int

variable_types: ret long

variable_types: dig int

return_type: long


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 i; // rbx
void **v4; // rbp
void **v5; // rbp
__int64 v6; // rax
__int64 j; // rbp
signed int v8; // er12
char *v9; // rbx
char *v10; // rbx
__int64 v12; // [rsp+0h] [rbp-E8h]
__int64 v13; // [rsp+10h] [rbp-D8h]
__int64 v14; // [rsp+18h] [rbp-D0h]
_QWORD v15[5]; // [rsp+20h] [rbp-C8h] BYREF
char v16; // [rsp+48h] [rbp-A0h] BYREF
_QWORD v17[5]; // [rsp+50h] [rbp-98h] BYREF
char v18; // [rsp+78h] [rbp-70h] BYREF
__int64 v19[13]; // [rsp+80h] [rbp-68h] BYREF

encoding_counts_mod_11 = (__int64)malloc(0x58uLL);
em11_lz = (__int64)malloc(0x58uLL);
for ( i = 0LL; i != 88; i += 8LL )
{
v4 = (void **)(encoding_counts_mod_11 + i);
*v4 = calloc(0xE5B7uLL, 8uLL);
v5 = (void **)(em11_lz + i);
*v5 = calloc(0xE5B7uLL, 8uLL);
}
v19[0] = 0LL;
v19[1] = 0LL;
v19[2] = 0LL;
v19[3] = 0LL;
v19[4] = 0LL;
v6 = count(0LL, 0, v19);
printf("count: %ld\n", v6);
v14 = 0LL;
v13 = 0LL;
do
{
v12 = (11 - v14 % 11) % 11;
for ( j = 0LL; j != 58807; ++j )
{
if ( isvalid(j) )
{
decode(j, v15);
v8 = complement(j);
decode(v8, v17);
v9 = (char *)v15;
do
{
fprintf(stderr, "%d ", *(unsigned int *)v9);
v9 += 4;
}
while ( &v16 != v9 );
fwrite("| ", 1uLL, 2uLL, stderr);
v10 = (char *)v17;
do
{
fprintf(stderr, "%d ", *(unsigned int *)v10);
v10 += 4;
}
while ( &v18 != v10 );
fputc(10, stderr);
fprintf(stderr, "(%d, %d) * (%ld, %d)\n", (unsigned int)v14, (unsigned int)j, v12, (unsigned int)v8);
fprintf(
stderr,
"%ld lhs, %ld rhs\n",
*(_QWORD *)(*(_QWORD *)(encoding_counts_mod_11 + 8 * v14) + 8 * j),
*(_QWORD *)(*(_QWORD *)(encoding_counts_mod_11 + 8 * v12) + 8LL * v8));
v13 += *(_QWORD *)(*(_QWORD *)(encoding_counts_mod_11 + 8 * v14) + 8 * j)
* (*(_QWORD *)(*(_QWORD *)(em11_lz + 8 * v12) + 8LL * v8)
+ *(_QWORD *)(*(_QWORD *)(encoding_counts_mod_11 + 8 * v12) + 8LL * v8));
}
}
++v14;
}
while ( v14 != 11 );
printf("result? %ld\n", v13);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 i; // rbx
void **v4; // rbp
void **v5; // rbp
__int64 v6; // rax
__int64 j; // rbp
signed int v8; // er12
char *v9; // rbx
char *v10; // rbx
__int64 v12; // [rsp+0h] [rbp-E8h]
__int64 v13; // [rsp+10h] [rbp-D8h]
__int64 v14; // [rsp+18h] [rbp-D0h]
_QWORD v15[5]; // [rsp+20h] [rbp-C8h] BYREF
char v16; // [rsp+48h] [rbp-A0h] BYREF
_QWORD v17[5]; // [rsp+50h] [rbp-98h] BYREF
char v18; // [rsp+78h] [rbp-70h] BYREF
__int64 v19[13]; // [rsp+80h] [rbp-68h] BYREF

encoding_counts_mod_11 = (__int64)malloc(0x58uLL);
em11_lz = (__int64)malloc(0x58uLL);
for ( i = 0LL; i != 88; i += 8LL )
{
v4 = (void **)(encoding_counts_mod_11 + i);
*v4 = calloc(0xE5B7uLL, 8uLL);
v5 = (void **)(em11_lz + i);
*v5 = calloc(0xE5B7uLL, 8uLL);
}
v19[0] = 0LL;
v19[1] = 0LL;
v19[2] = 0LL;
v19[3] = 0LL;
v19[4] = 0LL;
v6 = func10(0LL, 0, v19);
printf("count: %ld\n", v6);
v14 = 0LL;
v13 = 0LL;
do
{
v12 = (11 - v14 % 11) % 11;
for ( j = 0LL; j != 58807; ++j )
{
if ( func11(j) )
{
func8(j, v15);
v8 = func9(j);
func8(v8, v17);
v9 = (char *)v15;
do
{
fprintf(stderr, "%d ", *(unsigned int *)v9);
v9 += 4;
}
while ( &v16 != v9 );
fwrite("| ", 1uLL, 2uLL, stderr);
v10 = (char *)v17;
do
{
fprintf(stderr, "%d ", *(unsigned int *)v10);
v10 += 4;
}
while ( &v18 != v10 );
fputc(10, stderr);
fprintf(stderr, "(%d, %d) * (%ld, %d)\n", (unsigned int)v14, (unsigned int)j, v12, (unsigned int)v8);
fprintf(
stderr,
"%ld lhs, %ld rhs\n",
*(_QWORD *)(*(_QWORD *)(encoding_counts_mod_11 + 8 * v14) + 8 * j),
*(_QWORD *)(*(_QWORD *)(encoding_counts_mod_11 + 8 * v12) + 8LL * v8));
v13 += *(_QWORD *)(*(_QWORD *)(encoding_counts_mod_11 + 8 * v14) + 8 * j)
* (*(_QWORD *)(*(_QWORD *)(em11_lz + 8 * v12) + 8LL * v8)
+ *(_QWORD *)(*(_QWORD *)(encoding_counts_mod_11 + 8 * v12) + 8LL * v8));
}
}
++v14;
}
while ( v14 != 11 );
printf("result? %ld\n", v13);
return 0;
}

original_code:
int main()
{
    encoding_counts_mod_11 = malloc(11*sizeof(long *));
    em11_lz = malloc(11*sizeof(long *));
    int i;
    for(i = 0; i < 11; i++) {
        encoding_counts_mod_11[i] = calloc(1 + 58806, sizeof(long));
        em11_lz[i] = calloc(1 + 58806, sizeof(long));
    }
    int dcounts[10];
    memset(dcounts, 0, sizeof(dcounts));
    long c = count(0, 0, dcounts);
    printf("count: %ld\n", c);
    long z = 10000000000l % 11;
    long result = 0;
    for(i = 0; i < 11; i++) {
        long lhs_mod_11 = (i * z) % 11;
        long mod_complement = (11 - lhs_mod_11) % 11;
        int j;
        for(j = 0; j <= 58806; j++) {
            if(!isvalid(j))
                continue;
            int jj[10], kk[10];
            decode(j, jj);
            int comp = complement(j);
            decode(comp, kk);
            int k;
            for(k = 0; k < 10; k++) {
                fprintf(stderr, "%d ", jj[k]);
            }
            fprintf(stderr, "| ");
            for(k = 0; k < 10; k++) {
                fprintf(stderr, "%d ", kk[k]);
            }
            fprintf(stderr, "\n");
            fprintf(stderr, "(%d, %d) * (%ld, %d)\n", i, j, mod_complement, comp);
            fprintf(stderr, "%ld lhs, %ld rhs\n", encoding_counts_mod_11[i][j], encoding_counts_mod_11[mod_complement][comp]);
            result += encoding_counts_mod_11[i][j] *
                (encoding_counts_mod_11[mod_complement][comp] + em11_lz[mod_complement][comp]);
        }
    }
    printf("result? %ld\n", result);
    return 0;
}

canonical_original_code:
int main()
{
    encoding_counts_mod_11 = malloc(11*sizeof(long *));
    em11_lz = malloc(11*sizeof(long *));
    int i;
    for(i = 0; i < 11; i++) {
        encoding_counts_mod_11[i] = calloc(1 + 58806, sizeof(long));
        em11_lz[i] = calloc(1 + 58806, sizeof(long));
    }
    int dcounts[10];
    memset(dcounts, 0, sizeof(dcounts));
    long c = count(0, 0, dcounts);
    printf("count: %ld\n", c);
    long z = 10000000000l % 11;
    long result = 0;
    for(i = 0; i < 11; i++) {
        long lhs_mod_11 = (i * z) % 11;
        long mod_complement = (11 - lhs_mod_11) % 11;
        int j;
        for(j = 0; j <= 58806; j++) {
            if(!isvalid(j))
                continue;
            int jj[10]; int kk[10];
            decode(j, jj);
            int comp = complement(j);
            decode(comp, kk);
            int k;
            for(k = 0; k < 10; k++) {
                fprintf(stderr, "%d ", jj[k]);
            }
            fprintf(stderr, "| ");
            for(k = 0; k < 10; k++) {
                fprintf(stderr, "%d ", kk[k]);
            }
            fprintf(stderr, "\n");
            fprintf(stderr, "(%d, %d) * (%ld, %d)\n", i, j, mod_complement, comp);
            fprintf(stderr, "%ld lhs, %ld rhs\n", encoding_counts_mod_11[i][j], encoding_counts_mod_11[mod_complement][comp]);
            result += encoding_counts_mod_11[i][j] *
                (encoding_counts_mod_11[mod_complement][comp] + em11_lz[mod_complement][comp]);
        }
    }
    printf("result? %ld\n", result);
    return 0;
}

variable_types: c long

variable_types: z long

variable_types: result long

variable_types: j int

variable_types:
jj
int [10]

variable_types:
kk
int [10]

variable_types: comp int

variable_types: k int


original_code:
int main(int argc, char *argv[])
{
    if(argc < 2) {
        printf("SYNTAX: %s ndig [ndig == 40 solves problem]\n", argv[0]);
        exit(0);
    }
    int max_length = atoi(argv[1]);
    long *hit_0 = calloc(10, sizeof(long));
    long *hit_9 = calloc(10, sizeof(long));
    long *hit_nil = calloc(10, sizeof(long));
    long *pandigital = calloc(10, sizeof(long));
    initialize(hit_0, hit_9, hit_nil, pandigital);
    int current_length;
    long result = 0;
    for(current_length = 1; current_length < max_length; current_length++) {
        int j;
        long *new_hit_0 = calloc(10, sizeof(long));
        long *new_hit_9 = calloc(10, sizeof(long));
        long *new_hit_nil = calloc(10, sizeof(long));
        long *new_pandigital = calloc(10, sizeof(long));
        new_hit_0[0] += hit_nil[1];
        new_hit_nil[2] += hit_nil[1];
        for(j = 2; j < 8; j++) {
           new_hit_nil[j-1] += hit_nil[j];
           new_hit_nil[j+1] += hit_nil[j];
        }
        new_hit_9[9] += hit_nil[8];
        new_hit_nil[7] += hit_nil[8];
        new_hit_0[1] += hit_0[0];
        for(j = 1; j < 8; j++) {
            new_hit_0[j-1] += hit_0[j];
            new_hit_0[j+1] += hit_0[j];
        }
        new_pandigital[9] += hit_0[8];
        new_hit_0[7] += hit_0[8];
        new_pandigital[0] += hit_9[1];
        new_hit_9[2] += hit_9[1];
        for(j = 2; j < 9; j++) {
            new_hit_9[j-1] += hit_9[j];
            new_hit_9[j+1] += hit_9[j];
        }
        new_hit_9[8] += hit_9[9];
        new_pandigital[1] += pandigital[0];
        for(j = 1; j < 9; j++) {
            new_pandigital[j-1] += pandigital[j];
            new_pandigital[j+1] += pandigital[j];
        }
        new_pandigital[8] += pandigital[9];
        for(j = 0; j < 10; j++) {
            result += new_pandigital[j];
        }
        free(hit_0);
        free(hit_9);
        free(hit_nil);
        free(pandigital);
        hit_0 = new_hit_0;
        hit_9 = new_hit_9;
        hit_nil = new_hit_nil;
        pandigital = new_pandigital;
    }
    printf("result: %ld\n", result);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
pandigital
long *


decompiled_code:
__int64 __fastcall is_present(int a1, __int64 a2, _DWORD *a3)
{
__int64 v4; // rax
int v5; // edx
__int64 result; // rax

v4 = 0LL;
while ( 1 )
{
v5 = v4;
if ( *(_DWORD *)(a2 + 4 * v4) == a1 )
break;
if ( ++v4 == 5 )
return 0LL;
}
result = 1LL;
if ( a3 )
*a3 = v5;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func9(int a1, __int64 a2, _DWORD *a3)
{
__int64 v4; // rax
int v5; // edx
__int64 result; // rax

v4 = 0LL;
while ( 1 )
{
v5 = v4;
if ( *(_DWORD *)(a2 + 4 * v4) == a1 )
break;
if ( ++v4 == 5 )
return 0LL;
}
result = 1LL;
if ( a3 )
*a3 = v5;
return result;
}

original_code:
_Bool is_present(int draw, int mem[], int *pos) {
    int i;
    for(i = 0; i < 5; i++)
        if(mem[i] == draw) {
            if(pos)
                *pos = i;
            return 1;
        }
    return 0;
}

variable_types: draw int

variable_types:
mem
int []

variable_types:
pos
int *

variable_types: i int


decompiled_code:
__int64 __fastcall update_robin(int a1, const __m128i *a2, __m128i *a3)
{
__int64 result; // rax

result = is_present(a1, (__int64)a2, 0LL);
*a3 = _mm_loadu_si128(a2);
if ( (_BYTE)result )
a3[1].m128i_i32[0] = a2[1].m128i_i32[0];
else
a3[1].m128i_i32[0] = a1;
return result;
}

original_code:
_Bool update_robin(int draw, int mem_0[], int mem_1[])
{
    _Bool hit = is_present(draw, mem_0, ((void *)0));
    if(hit) {
        memcpy(mem_1, mem_0, 5*sizeof(int));
        return 1;
    } else {
        memcpy(mem_1, mem_0, 4*sizeof(int));
        mem_1[4] = draw;
        return 0;
    }
}


original_code:
_Bool update_larry(int draw, int mem_0[], int mem_1[])
{
    int hit_pos;
    _Bool hit = is_present(draw, mem_0, &hit_pos);
    if(hit) {
        memcpy(mem_1, mem_0, hit_pos*sizeof(int));
        memcpy(mem_1 + hit_pos, mem_0 + hit_pos + 1, (5 - hit_pos - 1)*sizeof(int));
        mem_1[4] = draw;
        return 1;
    } else {
        memcpy(mem_1, mem_0, 4*sizeof(int));
        mem_1[4] = draw;
        return 0;
    }
}


unpack

decompiled_code:
int *__fastcall unpack(int a1, int *a2)
{
int *v2; // rdx
int *result; // rax

v2 = a2 + 4;
do
{
*v2 = a1 % 11;
a1 /= 11;
result = v2--;
}
while ( result != a2 );
return result;
}

canonical_decompiled_code:
int *__fastcall func7(int a1, int *a2)
{
int *v2; // rdx
int *result; // rax

v2 = a2 + 4;
do
{
*v2 = a1 % 11;
a1 /= 11;
result = v2--;
}
while ( result != a2 );
return result;
}

original_code:
static inline void unpack(int n, int state[5]) {
    int i = 0;
    for(i = 0; i < 5; i++) {
        state[4-i] = n % 11;
        n /= 11;
    }
}

variable_types: n int

variable_types:
state
int [5]


pack

decompiled_code:
__int64 __fastcall pack(_DWORD *a1)
{
_DWORD *v1; // rcx
__int64 result; // rax

v1 = a1 + 5;
LODWORD(result) = 0;
do
result = (unsigned int)(*a1++ + 11 * result);
while ( a1 != v1 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(_DWORD *a1)
{
_DWORD *v1; // rcx
__int64 result; // rax

v1 = a1 + 5;
LODWORD(result) = 0;
do
result = (unsigned int)(*a1++ + 11 * result);
while ( a1 != v1 );
return result;
}

original_code:
static inline int pack(int state[5]) {
    int ret = 0;
    int i;
    for(i = 0; i < 5; i++) {
        ret *= 11;
        ret += state[i];
    }
    return ret;
}

variable_types: ret int


main

original_code:
int main(void)
{
    state_t *larry = calloc(161051, sizeof(state_t));
    state_t *robin = calloc(161051, sizeof(state_t));
    larry[0].score[0] = 1;
    robin[0].score[0] = 1;
    int draw;
    for(draw = 0; draw < 50; draw++) {
        printf("doing draw %d\n", draw);
        state_t *larry_new = calloc(161051, sizeof(state_t));
        state_t *robin_new = calloc(161051, sizeof(state_t));
        int prev_state;
        for(prev_state = 0; prev_state < 161051; prev_state++) {
            int extracted[5];
            unpack(prev_state, extracted);
            int n;
            for(n = 1; n <= 10; n++) {
                int updated[5];
                int score = update_larry(n, extracted, updated);
                int updated_index = pack(updated);
                int i;
                for(i = 0; i + score < 50; i++) {
                    larry_new[updated_index].score[i+score] += larry[prev_state].score[i];
                }
                score = update_robin(n, extracted, updated);
                updated_index = pack(updated);
                for(i = 0; i + score < 50; i++) {
                    robin_new[updated_index].score[i + score] += robin[prev_state].score[i];
                }
            }
        }
        state_t *tmp = larry;
        larry = larry_new;
        free(tmp);
        tmp = robin;
        robin = robin_new;
        free(tmp);
    }
    return 0;
}

canonical_original_code:
int main()
{
    struct _state *larry = calloc(161051, sizeof(state_t));
    struct _state *robin = calloc(161051, sizeof(state_t));
    larry[0].score[0] = 1;
    robin[0].score[0] = 1;
    int draw;
    for(draw = 0; draw < 50; draw++) {
        printf("doing draw %d\n", draw);
        struct _state *larry_new = calloc(161051, sizeof(state_t));
        struct _state *robin_new = calloc(161051, sizeof(state_t));
        int prev_state;
        for(prev_state = 0; prev_state < 161051; prev_state++) {
            int extracted[5];
            unpack(prev_state, extracted);
            int n;
            for(n = 1; n <= 10; n++) {
                int updated[5];
                int score = update_larry(n, extracted, updated);
                int updated_index = pack(updated);
                int i;
                for(i = 0; i + score < 50; i++) {
                    larry_new[updated_index].score[i+score] += larry[prev_state].score[i];
                }
                score = update_robin(n, extracted, updated);
                updated_index = pack(updated);
                for(i = 0; i + score < 50; i++) {
                    robin_new[updated_index].score[i + score] += robin[prev_state].score[i];
                }
            }
        }
        struct _state *tmp = larry;
        larry = larry_new;
        free(tmp);
        tmp = robin;
        robin = robin_new;
        free(tmp);
    }
    return 0;
}

variable_types:
larry
struct _state { long score[50]; } *

variable_types:
robin
struct _state { long score[50]; } *

variable_types:
extracted
int [5]

variable_types:
updated
int [5]

variable_types: score int

variable_types:
tmp
struct _state { long score[50]; } *


cube

original_code:
long cube(long x, long m)
{
    long xmod = x % m;
    long x3 = xmod*xmod*xmod;
    return x3 % m;
}

variable_types: x long

variable_types: m long

variable_types: xmod long


main

original_code:
int main()
{
    int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43};
    int np = sizeof(primes) / sizeof(int);
    long *cur_items = malloc(primes[np-1]*sizeof(long));
    int n_items = 0;
    long *tmp = ((void *)0);
    long i;
    for(i = 1; i < primes[np-1]; i++) {
        if(cube(i, primes[np-1]) == 1) {
            cur_items[n_items++] = i;
        }
    }
    uint64_t cur_modulus = primes[np-1];
    for(i = np-2; i >= 0; i--) {
        tmp = realloc(tmp, n_items*sizeof(long));
        int tmp_items = n_items;
        memcpy(tmp, cur_items, n_items*sizeof(long));
        cur_items = realloc(cur_items, n_items * primes[i] * sizeof(long));
        n_items = 0;
        int j;
        for(j = 0; j < tmp_items; j++) {
            int k;
            uint64_t test = tmp[j];
            for(k = 0; k < primes[i]; k++) {
                if(cube(test, primes[i]) == 1) {
                    cur_items[n_items++] = test;
                }
                test += cur_modulus;
            }
        }
        cur_modulus *= primes[i];
    }
    uint64_t sum = 0;
    for(i = 0; i < n_items; i++) {
        if(cur_items[i] > 1)
            sum += cur_items[i];
    }
    printf("S(%ld) == %ld\n", cur_modulus, sum);
    return 0;
}

canonical_original_code:
int main()
{
    int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43};
    int np = sizeof(primes) / sizeof(int);
    long *cur_items = malloc(primes[np-1]*sizeof(long));
    int n_items = 0;
    long *tmp = ((void *)0);
    long i;
    for(i = 1; i < primes[np-1]; i++) {
        if(cube(i, primes[np-1]) == 1) {
            cur_items[n_items++] = i;
        }
    }
    unsigned long cur_modulus = primes[np-1];
    for(i = np-2; i >= 0; i--) {
        tmp = realloc(tmp, n_items*sizeof(long));
        int tmp_items = n_items;
        memcpy(tmp, cur_items, n_items*sizeof(long));
        cur_items = realloc(cur_items, n_items * primes[i] * sizeof(long));
        n_items = 0;
        int j;
        for(j = 0; j < tmp_items; j++) {
            int k;
            unsigned long test = tmp[j];
            for(k = 0; k < primes[i]; k++) {
                if(cube(test, primes[i]) == 1) {
                    cur_items[n_items++] = test;
                }
                test += cur_modulus;
            }
        }
        cur_modulus *= primes[i];
    }
    unsigned long sum = 0;
    for(i = 0; i < n_items; i++) {
        if(cur_items[i] > 1)
            sum += cur_items[i];
    }
    printf("S(%ld) == %ld\n", cur_modulus, sum);
    return 0;
}

variable_types:
primes
int []

variable_types: np int

variable_types:
tmp
long *

variable_types: i long

variable_types: j int

variable_types: k int

variable_types:
test
unsigned long

variable_types:
sum
unsigned long


decompiled_code:
__int64 __fastcall find_closest(__int64 a1, __int64 a2, __int64 a3, double a4)
{
unsigned int v4; // ecx
__int64 result; // rax
double v6; // xmm1_8
__int64 v7; // rsi
double v8; // xmm1_8
__int64 v9; // rsi

if ( (_DWORD)a2 == (_DWORD)a3 )
return a1 + 16LL * (int)a2;
if ( (_DWORD)a2 + 1 == (_DWORD)a3 )
{
v7 = 16LL * (int)a2;
result = a1 + 16LL * (int)a3;
v8 = fabs(a4 - *(double *)(a1 + v7 + 8));
v9 = a1 + v7;
if ( fabs(a4 - *(double *)(result + 8)) > v8 )
result = v9;
}
else
{
v4 = ((int)a2 + (int)a3) / 2;
result = a1 + 16LL * (int)v4;
v6 = a4 - *(double *)(result + 8);
if ( v6 != 0.0 )
{
if ( v6 < 0.0 )
{
result = find_closest(a1, a2, v4);
}
else
{
result = 0LL;
if ( v6 > 0.0 )
result = find_closest(a1, v4, a3);
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, __int64 a2, __int64 a3, double a4)
{
unsigned int v4; // ecx
__int64 result; // rax
double v6; // xmm1_8
__int64 v7; // rsi
double v8; // xmm1_8
__int64 v9; // rsi

if ( (_DWORD)a2 == (_DWORD)a3 )
return a1 + 16LL * (int)a2;
if ( (_DWORD)a2 + 1 == (_DWORD)a3 )
{
v7 = 16LL * (int)a2;
result = a1 + 16LL * (int)a3;
v8 = fabs(a4 - *(double *)(a1 + v7 + 8));
v9 = a1 + v7;
if ( fabs(a4 - *(double *)(result + 8)) > v8 )
result = v9;
}
else
{
v4 = ((int)a2 + (int)a3) / 2;
result = a1 + 16LL * (int)v4;
v6 = a4 - *(double *)(result + 8);
if ( v6 != 0.0 )
{
if ( v6 < 0.0 )
{
result = func8(a1, a2, v4);
}
else
{
result = 0LL;
if ( v6 > 0.0 )
result = func8(a1, v4, a3);
}
}
}
return result;
}

original_code:
d_t *find_closest(double x, d_t *pairs, int left, int right)
{
    if(left == right) {
        return pairs + left;
    }
    if(left + 1 == right) {
        if(fabs(x - pairs[left].fafb) < fabs(x - pairs[right].fafb))
            return pairs + left;
        else
            return pairs + right;
    }
    int mid = (left + right) / 2;
    double dmid = x - pairs[mid].fafb;
    if(dmid == 0.0)
        return pairs + mid;
    if(dmid < 0)
        return find_closest(x, pairs, left, mid);
    if(dmid > 0)
        return find_closest(x, pairs, mid, right);
    return ((void *)0);
}

canonical_original_code:
struct _d *find_closest(double x, struct _d *pairs, int left, int right)
{
    if(left == right) {
        return pairs + left;
    }
    if(left + 1 == right) {
        if(fabs(x - pairs[left].fafb) < fabs(x - pairs[right].fafb))
            return pairs + left;
        else
            return pairs + right;
    }
    int mid = (left + right) / 2;
    double dmid = x - pairs[mid].fafb;
    if(dmid == 0.0)
        return pairs + mid;
    if(dmid < 0)
        return find_closest(x, pairs, left, mid);
    if(dmid > 0)
        return find_closest(x, pairs, mid, right);
    return ((void *)0);
}

variable_types: x double

variable_types:
pairs
struct _d *

variable_types: left int

variable_types: right int

variable_types: mid int

variable_types: dmid double

return_type: struct _d { int a; double fafb; } *


cmp

decompiled_code:
__int64 __fastcall cmp(const void *a1, const void *a2)
{
double v2; // xmm1_8
double v3; // xmm0_8
__int64 result; // rax

v2 = *((double *)a1 + 1);
v3 = *((double *)a2 + 1);
if ( v2 == v3 )
result = 0LL;
else
result = 2 * (unsigned int)(v2 > v3) - 1;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func7(const void *a1, const void *a2)
{
double v2; // xmm1_8
double v3; // xmm0_8
__int64 result; // rax

v2 = *((double *)a1 + 1);
v3 = *((double *)a2 + 1);
if ( v2 == v3 )
result = 0LL;
else
result = 2 * (unsigned int)(v2 > v3) - 1;
return result;
}

original_code:
int cmp(const void *va, const void *vb)
{
    d_t *a = (d_t *) va;
    d_t *b = (d_t *) vb;
    if(a->fafb == b->fafb)
        return 0;
    return 2*(a->fafb > b->fafb) - 1;
}

canonical_original_code:
int cmp(void *va, void *vb)
{
    struct _d *a = (d_t *) va;
    struct _d *b = (d_t *) vb;
    if(a->fafb == b->fafb)
        return 0;
    return 2*(a->fafb > b->fafb) - 1;
}

variable_types:
va
void *

variable_types:
vb
void *

variable_types:
a
struct _d { int a; double fafb; } *

variable_types:
b
struct _d { int a; double fafb; } *


main

original_code:
int main()
{
    double *f = malloc(((int) (1.43 * 10000))*sizeof(double));
    int i;
    for(i = 0; i < ((int) (1.43 * 10000)); i++) {
        f[i] = exp(((double) i) / 10000) - 1;
    }
    int n_pairs = (((int) (1.43 * 10000))*(((int) (1.43 * 10000)) + 1)) / 2;
    d_t *pairs = malloc(n_pairs * sizeof(d_t));
    int k = 0;
    for(i = 0; i < ((int) (1.43 * 10000)); i++) {
        int j;
        double fi = f[i];
        for(j = 0; j <= i; j++) {
            pairs[k].a = i;
            pairs[k].b = j;
            pairs[k].fafb = fi + f[j];
            k++;
        }
    }
    qsort(pairs, n_pairs, sizeof(d_t), cmp);
    for(i = 0; i < 100; i += 10) {
        printf("pairs[%d]: %d, %d, %g\n", i, pairs[i].a, pairs[i].b, pairs[i].fafb);
    }
    double min_dist = 20.0;
    int abcd[4] = {0, 0, 0, 0};
    for(i = 0; i < n_pairs; i++) {
        double remainder = 3.14159265358979323846 - pairs[i].fafb;
        d_t *best = find_closest(remainder, pairs, 0, n_pairs - 1);
        double dist = fabs(pairs[i].fafb + best->fafb - 3.14159265358979323846);
        if(dist < min_dist) {
            min_dist = dist;
            abcd[0] = pairs[i].a;
            abcd[1] = pairs[i].b;
            abcd[2] = best->a;
            abcd[3] = best->b;
        }
    }
    int a = abcd[0];
    int b = abcd[1];
    int c = abcd[2];
    int d = abcd[3];
    printf("best solution at (%d, %d, %d, %d) => %g\n", abcd[0], abcd[1], abcd[2], abcd[3], min_dist);
    printf("result: %d\n", a*a + b*b + c*c + d*d);
    return 0;
}

canonical_original_code:
int main()
{
    double *f = malloc(((int) (1.43 * 10000))*sizeof(double));
    int i;
    for(i = 0; i < ((int) (1.43 * 10000)); i++) {
        f[i] = exp(((double) i) / 10000) - 1;
    }
    int n_pairs = (((int) (1.43 * 10000))*(((int) (1.43 * 10000)) + 1)) / 2;
    struct _d *pairs = malloc(n_pairs * sizeof(d_t));
    int k = 0;
    for(i = 0; i < ((int) (1.43 * 10000)); i++) {
        int j;
        double fi = f[i];
        for(j = 0; j <= i; j++) {
            pairs[k].a = i;
            pairs[k].b = j;
            pairs[k].fafb = fi + f[j];
            k++;
        }
    }
    qsort(pairs, n_pairs, sizeof(d_t), cmp);
    for(i = 0; i < 100; i += 10) {
        printf("pairs[%d]: %d, %d, %g\n", i, pairs[i].a, pairs[i].b, pairs[i].fafb);
    }
    double min_dist = 20.0;
    int abcd[4] = {0, 0, 0, 0};
    for(i = 0; i < n_pairs; i++) {
        double remainder = 3.14159265358979323846 - pairs[i].fafb;
        struct _d *best = find_closest(remainder, pairs, 0, n_pairs - 1);
        double dist = fabs(pairs[i].fafb + best->fafb - 3.14159265358979323846);
        if(dist < min_dist) {
            min_dist = dist;
            abcd[0] = pairs[i].a;
            abcd[1] = pairs[i].b;
            abcd[2] = best->a;
            abcd[3] = best->b;
        }
    }
    int a = abcd[0];
    int b = abcd[1];
    int c = abcd[2];
    int d = abcd[3];
    printf("best solution at (%d, %d, %d, %d) => %g\n", abcd[0], abcd[1], abcd[2], abcd[3], min_dist);
    printf("result: %d\n", a*a + b*b + c*c + d*d);
    return 0;
}

variable_types:
f
double *

variable_types: i int

variable_types:
pairs
struct _d { int a; double fafb; } *

variable_types: fi double

variable_types:
abcd
int [4]

variable_types: remainder double

variable_types:
best
struct _d { int a; double fafb; } *

variable_types: dist double

variable_types: a int

variable_types: b int

variable_types: c int

variable_types: d int


original_code:
long sum_squarefree(long *values, int n, long *sqprimes, int num_primes) {
    int i;
    long ret = 0;
    for(i = 0; i < n; i++) {
        long value = values[i];
        int j;
        _Bool sqfree = 1;
        for(j = 0; j < num_primes; j++) {
            if(sqprimes[j] > value)
                break;
            if(!(value % sqprimes[j])) {
                sqfree = 0;
                break;
            }
        }
        ret += sqfree ? value : 0;
    }
    return ret;
}

variable_types:
values
long *

variable_types: n int

variable_types:
sqprimes
long *

variable_types: ret long

variable_types: value long

variable_types: j int


original_code:
int main(void) {
    int nalloc = 1000;
    long *values = malloc(nalloc*sizeof(long));
    char buf[256];
    int n = 0;
    long max = -1;
    while(((void *)0) != fgets(buf, sizeof(buf), stdin)) {
        values[n] = strtol(buf, ((void *)0), 10);
        if(values[n] > max) {
            max = values[n];
        }
        n++;
    }
    int max_prime = (int) ceil(sqrt(max));
    while(((long) max_prime) * ((long) max_prime) < max)
        max_prime++;
    long *sqprimes;
    int num_primes;
    get_primes(max_prime, &sqprimes, &num_primes);
    int i;
    for(i = 0; i < num_primes; i++)
        sqprimes[i] *= sqprimes[i];
    long result = sum_squarefree(values, n, sqprimes, num_primes);
    printf("result: %ld\n", result);
    return 0;
}

canonical_original_code:
int main() {
    int nalloc = 1000;
    long *values = malloc(nalloc*sizeof(long));
    char buf[256];
    int n = 0;
    long max = -1;
    while(((void *)0) != fgets(buf, sizeof(buf), stdin)) {
        values[n] = strtol(buf, ((void *)0), 10);
        if(values[n] > max) {
            max = values[n];
        }
        n++;
    }
    int max_prime = (int) ceil(sqrt(max));
    while(((long) max_prime) * ((long) max_prime) < max)
        max_prime++;
    long *sqprimes;
    int num_primes;
    get_primes(max_prime, &sqprimes, &num_primes);
    int i;
    for(i = 0; i < num_primes; i++)
        sqprimes[i] *= sqprimes[i];
    long result = sum_squarefree(values, n, sqprimes, num_primes);
    printf("result: %ld\n", result);
    return 0;
}

variable_types: nalloc int

variable_types:
buf
char [256]

variable_types: max long

variable_types: i int

variable_types: result long


original_code:
_Bool mr_isprime(uint64_t n) {
    if(!(n & 1))
        return 0;
    uint64_t d = n-1;
    int r = _trailz(d);
    d >>= r;
    int i;
    _Bool possibly_prime = 1;
    for(i = 0; i < 5 && possibly_prime; i++) {
        uint64_t a = rand_int(2, (((2147483647) > (n-1)) ? (n-1) : (2147483647)));
        uint64_t x = uipow(a, d, n);
        if(x == 1ul || x == (n-1))
            continue;
        int j;
        possibly_prime = 0;
        for(j = 0; j < r-1; j++) {
            x = uipow(x, 2, n);
            if(x == 1ul) {
                possibly_prime = 0;
                break;
            }
            if(x == (n-1)) {
                possibly_prime = 1;
                break;
            }
        }
    }
    return possibly_prime;
}

canonical_original_code:
_Bool mr_isprime(unsigned long n) {
    if(!(n & 1))
        return 0;
    unsigned long d = n-1;
    int r = _trailz(d);
    d >>= r;
    int i;
    _Bool possibly_prime = 1;
    for(i = 0; i < 5 && possibly_prime; i++) {
        unsigned long a = rand_int(2, (((2147483647) > (n-1)) ? (n-1) : (2147483647)));
        unsigned long x = uipow(a, d, n);
        if(x == 1ul || x == (n-1))
            continue;
        int j;
        possibly_prime = 0;
        for(j = 0; j < r-1; j++) {
            x = uipow(x, 2, n);
            if(x == 1ul) {
                possibly_prime = 0;
                break;
            }
            if(x == (n-1)) {
                possibly_prime = 1;
                break;
            }
        }
    }
    return possibly_prime;
}

variable_types:
n
unsigned long

variable_types:
d
unsigned long

variable_types: r int

variable_types:
a
unsigned long

variable_types:
x
unsigned long


check

decompiled_code:
__int64 __fastcall check(__int64 a1)
{
__int64 v1; // rbx
__int64 result; // rax
unsigned __int64 v3; // rbx
int v4; // er12
int v5; // ebp

v1 = a1 * a1;
result = mr_isprime(a1 * a1 + 1);
if ( (_BYTE)result )
{
result = mr_isprime(v1 + 3);
if ( (_BYTE)result )
{
v3 = v1 + 5;
v4 = 0;
v5 = 5;
do
{
if ( seq[v4] == v5 )
{
result = mr_isprime(v3);
++v4;
}
else
{
result = (unsigned int)mr_isprime(v3) ^ 1;
}
v5 += 2;
v3 += 2LL;
}
while ( v5 <= 27 && (_BYTE)result );
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(__int64 a1)
{
__int64 v1; // rbx
__int64 result; // rax
unsigned __int64 v3; // rbx
int v4; // er12
int v5; // ebp

v1 = a1 * a1;
result = func11(a1 * a1 + 1);
if ( (_BYTE)result )
{
result = func11(v1 + 3);
if ( (_BYTE)result )
{
v3 = v1 + 5;
v4 = 0;
v5 = 5;
do
{
if ( seq[v4] == v5 )
{
result = func11(v3);
++v4;
}
else
{
result = (unsigned int)func11(v3) ^ 1;
}
v5 += 2;
v3 += 2LL;
}
while ( v5 <= 27 && (_BYTE)result );
}
}
return result;
}

original_code:
_Bool check(long n) {
    long n2 = n*n;
    _Bool hit = 0;
    if(mr_isprime(n2+1) && mr_isprime(n2+3)) {
        int j, k;
        hit = 1;
        k = 0;
        for(j = 5; hit && j <= 27; j += 2) {
            if(seq[k] == j) {
                hit &= mr_isprime(n2+j);
                k++;
            } else {
                hit &= !mr_isprime(n2+j);
            }
        }
    }
    return hit;
}

canonical_original_code:
_Bool check(long n) {
    long n2 = n*n;
    _Bool hit = 0;
    if(mr_isprime(n2+1) && mr_isprime(n2+3)) {
        int j; int k;
        hit = 1;
        k = 0;
        for(j = 5; hit && j <= 27; j += 2) {
            if(seq[k] == j) {
                hit &= mr_isprime(n2+j);
                k++;
            } else {
                hit &= !mr_isprime(n2+j);
            }
        }
    }
    return hit;
}

variable_types: n long

variable_types: k int


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er15
__int64 v4; // rbx
__int64 v6; // [rsp+8h] [rbp-40h]

v3 = 0;
v6 = 0LL;
v4 = 10LL;
while ( 1 )
{
if ( v4 != 3 * (v4 / 3)
&& (unsigned __int64)(v4 % 7 - 3) <= 1
&& (unsigned __int64)(v4 % 11 - 2) > 1
&& (unsigned __int64)(v4 % 11 - 8) > 1
&& (unsigned __int64)(v4 % 13 - 5) > 3
&& v4 % 13
&& (unsigned __int8)check(v4) )
{
printf("hit for i == %ld\n", v4);
v6 += v4;
++v3;
}
v4 += 10LL;
if ( v4 == 150000000 )
break;
if ( v4 == 10000000 * (v4 / 10000000) )
printf("done with i == %ld\n", v4);
}
printf("final i: %ld\n", 150000000LL);
printf("sum of target numbers: %ld\n", v6);
printf("%d primes of this form below %ld\n", v3, 150000000LL);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er15
__int64 v4; // rbx
__int64 v6; // [rsp+8h] [rbp-40h]

v3 = 0;
v6 = 0LL;
v4 = 10LL;
while ( 1 )
{
if ( v4 != 3 * (v4 / 3)
&& (unsigned __int64)(v4 % 7 - 3) <= 1
&& (unsigned __int64)(v4 % 11 - 2) > 1
&& (unsigned __int64)(v4 % 11 - 8) > 1
&& (unsigned __int64)(v4 % 13 - 5) > 3
&& v4 % 13
&& (unsigned __int8)func12(v4) )
{
printf("hit for i == %ld\n", v4);
v6 += v4;
++v3;
}
v4 += 10LL;
if ( v4 == 150000000 )
break;
if ( v4 == 10000000 * (v4 / 10000000) )
printf("done with i == %ld\n", v4);
}
printf("final i: %ld\n", 150000000LL);
printf("sum of target numbers: %ld\n", v6);
printf("%d primes of this form below %ld\n", v3, 150000000LL);
return 0;
}

original_code:
int main()
{
    long i;
    long sum = 0l;
    int nz = 0;
    for(i = 10; i < 150000000l; i += 10) {
        if(!(i % 10000000))
            printf("done with i == %ld\n", i);
        if(!(i % 3))
            continue;
        long mod7 = i % 7;
        if(!(mod7 == 3 || mod7 == 4))
            continue;
        long mod11 = i % 11;
        if(mod11 == 2 || mod11 == 3 || mod11 == 8 || mod11 == 9)
            continue;
        long mod13 = i % 13;
        if((mod13 == 0) || (mod13 > 4 && mod13 < 9))
            continue;
        _Bool hit = check(i);
        if(hit) {
            printf("hit for i == %ld\n", i);
            sum += i;
            nz++;
        }
    }
    printf("final i: %ld\n", i);
    printf("sum of target numbers: %ld\n", sum);
    printf("%d primes of this form below %ld\n", nz, 150000000l);
    return 0;
}

variable_types: i long

variable_types: sum long

variable_types: nz int


isprime

decompiled_code:
bool __fastcall prime::isprime(prime *this, int a2)
{
__int64 v2; // r10
__int64 v5; // r9
__int64 v6; // rdi
int v7; // ecx

v2 = *((int *)this + 5);
if ( (int)v2 > a2 )
return *(_BYTE *)(*(_QWORD *)this + a2) == 0;
v5 = *((_QWORD *)this + 1);
v6 = 0LL;
do
{
v7 = *(_DWORD *)(v5 + 4 * v6);
if ( !(a2 % v7) )
return 0;
if ( v7 * v7 >= a2 )
break;
++v6;
}
while ( *((_DWORD *)this + 4) > (int)v6 );
if ( v2 * v2 < a2 )
std::__ostream_insert<char,std::char_traits<char>>(
&std::cout,
"isprime() warning: number is too big to be certain!\n",
52LL);
return 1;
}

canonical_decompiled_code:
bool __fastcall prime::func3(prime *this, int a2)
{
__int64 v2; // r10
__int64 v5; // r9
__int64 v6; // rdi
int v7; // ecx

v2 = *((int *)this + 5);
if ( (int)v2 > a2 )
return *(_BYTE *)(*(_QWORD *)this + a2) == 0;
v5 = *((_QWORD *)this + 1);
v6 = 0LL;
do
{
v7 = *(_DWORD *)(v5 + 4 * v6);
if ( !(a2 % v7) )
return 0;
if ( v7 * v7 >= a2 )
break;
++v6;
}
while ( *((_DWORD *)this + 4) > (int)v6 );
if ( v2 * v2 < a2 )
std::__ostream_insert<char,std::char_traits<char>>(
&std::cout,
"isprime() warning: number is too big to be certain!\n",
52LL);
return 1;
}

original_code:
_Bool isprime(long x, long *p, long np)
{
    if(np < 1) {
        fprintf(stderr, "ERROR: no primes!\n");
        exit(-1);
    }
    if(x > p[np-1]*p[np-1]) {
        fprintf(stderr, "ERROR: %ld is beyond the maximum testable for primes up to %ld\n", x, p[np-1]);
        exit(-1);
    }
    if(x <= p[np-1])
        return ((void *)0) != bsearch(&x, p, np, sizeof(long), lcmp);
    if(!mr_isprime(x))
        return 0;
    long i = 0;
    for(i = 0; i < np; i++) {
        if(p[i]*p[i] >= x)
            break;
        if(0 == (x % p[i]))
            return 0;
    }
    return 1;
}

variable_types: x long

variable_types:
p
long *

variable_types: np long


repo: mckellyln/inotify-server

decompiled_code:
void __fastcall sig_hdlr(unsigned int a1)
{
if ( a1 == 10 )
{
dump_hash_info = 1;
}
else
{
if ( a1 != 12 )
{
if ( !background )
{
fflush(0LL);
fprintf(_bss_start, "\nexiting on signal %d ...\n\n", a1);
}
exit(0);
}
dump_hash_info = 2;
}
}

canonical_decompiled_code:
void __fastcall func7(unsigned int a1)
{
if ( a1 == 10 )
{
dump_hash_info = 1;
}
else
{
if ( a1 != 12 )
{
if ( !background )
{
fflush(0LL);
fprintf(_bss_start, "\nexiting on signal %d ...\n\n", a1);
}
exit(0);
}
dump_hash_info = 2;
}
}

original_code:
static void sig_hdlr(int sig)
{
    if (sig == 10) {
        dump_hash_info = 1;
    }else if (sig == 12) {
        dump_hash_info = 2;
    } else {
        if (!background) {
            fflush(((void *)0));
            fprintf(stderr,"\nexiting on signal %d ...\n\n", sig);
        }
        exit(0);
    }
    return;
}

variable_types: sig int


decompiled_code:
void __fastcall __noreturn work_loop(void *a1)
{
const char *v1; // rbx
const char *i; // rbp
int v3; // eax
int v4; // ebx
time_t v5; // r12
struct timespec v6; // [rsp+0h] [rbp-28h] BYREF

while ( 1 )
{
v5 = time(0LL);
pthread_rwlock_rdlock(&rwlock);
v1 = (const char *)purge;
if ( purge )
{
for ( i = (const char *)*((_QWORD *)purge + 41); ; i = (const char *)*((_QWORD *)i + 41) )
{
if ( *((__int64 *)v1 + 38) > 1 && v5 - *((_QWORD *)v1 + 38) >= lifespan )
{
*((_QWORD *)v1 + 38) = 1LL;
if ( debug > 1 )
{
pthread_mutex_lock(&lock);
fprintf(_bss_start, "debug: purging <%s> from page cache ...\n", v1);
pthread_mutex_unlock(&lock);
}
v3 = open(v1, 0);
v4 = v3;
if ( v3 >= 0 )
{
posix_fadvise(v3, 0LL, 0LL, 4);
close(v4);
}
}
if ( !i )
break;
v1 = i;
}
}
pthread_rwlock_unlock(&rwlock);
v6.tv_sec = 0LL;
v6.tv_nsec = 50000000LL;
nanosleep(&v6, 0LL);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func8(void *a1)
{
const char *v1; // rbx
const char *i; // rbp
int v3; // eax
int v4; // ebx
time_t v5; // r12
struct timespec v6; // [rsp+0h] [rbp-28h] BYREF

while ( 1 )
{
v5 = time(0LL);
pthread_rwlock_rdlock(&rwlock);
v1 = (const char *)purge;
if ( purge )
{
for ( i = (const char *)*((_QWORD *)purge + 41); ; i = (const char *)*((_QWORD *)i + 41) )
{
if ( *((__int64 *)v1 + 38) > 1 && v5 - *((_QWORD *)v1 + 38) >= lifespan )
{
*((_QWORD *)v1 + 38) = 1LL;
if ( debug > 1 )
{
pthread_mutex_lock(&lock);
fprintf(_bss_start, "debug: purging <%s> from page cache ...\n", v1);
pthread_mutex_unlock(&lock);
}
v3 = open(v1, 0);
v4 = v3;
if ( v3 >= 0 )
{
posix_fadvise(v3, 0LL, 0LL, 4);
close(v4);
}
}
if ( !i )
break;
v1 = i;
}
}
pthread_rwlock_unlock(&rwlock);
v6.tv_sec = 0LL;
v6.tv_nsec = 50000000LL;
nanosleep(&v6, 0LL);
}
}

original_code:
static void *work_loop(void *bogus)
{
    struct timespec ts;
    PURGE *pp = ((void *)0);
    PURGE *ptmp = ((void *)0);
    while (1) {
        time_t time_now = time(((void *)0));
        pthread_rwlock_rdlock(&rwlock);
        for((pp)=(purge),(ptmp)=(__typeof(pp))((purge)?(purge)->hh.next:((void *)0)); pp; (pp)=(ptmp),(ptmp)=(__typeof(pp))((ptmp)?(ptmp)->hh.next:((void *)0))) {
            if ((pp->stime > 1) && ((time_now - pp->stime) >= lifespan)) {
                pp->stime = 1;
                if (debug > 1) {
                    pthread_mutex_lock(&lock);
                    fprintf(stderr,"debug: purging <%s> from page cache ...\n", pp->filename);
                    pthread_mutex_unlock(&lock);
                }
                int fd = open(pp->filename, 00);
                if (fd >= 0) {
                    posix_fadvise(fd, 0, 0, 4);
                    close(fd);
                }
            }
        }
        pthread_rwlock_unlock(&rwlock);
        ts.tv_sec = 0;
        ts.tv_nsec = 50000000;
        nanosleep(&ts, ((void *)0));
    }
    return ((void *)0);
}

canonical_original_code:
static void *work_loop(void *bogus)
{
    struct timespec ts;
    struct PURGE *pp = ((void *)0);
    struct PURGE *ptmp = ((void *)0);
    while (1) {
        long time_now = time(((void *)0));
        pthread_rwlock_rdlock(&rwlock);
        for((pp)=(purge),(ptmp)=(__typeof(pp))((purge)?(purge)->hh.next:((void *)0)); pp; (pp)=(ptmp),(ptmp)=(__typeof(pp))((ptmp)?(ptmp)->hh.next:((void *)0))) {
            if ((pp->stime > 1) && ((time_now - pp->stime) >= lifespan)) {
                pp->stime = 1;
                if (debug > 1) {
                    pthread_mutex_lock(&lock);
                    fprintf(stderr,"debug: purging <%s> from page cache ...\n", pp->filename);
                    pthread_mutex_unlock(&lock);
                }
                int fd = open(pp->filename, 00);
                if (fd >= 0) {
                    posix_fadvise(fd, 0, 0, 4);
                    close(fd);
                }
            }
        }
        pthread_rwlock_unlock(&rwlock);
        ts.tv_sec = 0;
        ts.tv_nsec = 50000000;
        nanosleep(&ts, ((void *)0));
    }
    return ((void *)0);
}

variable_types:
bogus
void *

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

variable_types: struct PURGE { char filename[300]; long stime; struct UT_hash_handle { struct UT_hash_table { struct UT_hash_bucket { struct UT_hash_handle *hh_head; unsigned int count; unsigned int expand_mult; } *buckets; unsigned int num_buckets; unsigned int num_items; struct UT_hash_handle *tail; long hho; unsigned int ideal_chain_maxlen; unsigned int nonideal_items; unsigned int ineff_expands; unsigned int signature; } *tbl; void *prev; void *next; struct UT_hash_handle *hh_prev; struct UT_hash_handle *hh_next; void *key; unsigned int keylen; unsigned int hashv; } hh; } *

variable_types: fd int

return_type: void *

user_defined_types: struct PURGE { char filename[300]; long stime; struct UT_hash_handle hh; }

user_defined_types: struct UT_hash_handle { struct UT_hash_table *tbl; void *prev; void *next; struct UT_hash_handle *hh_prev; struct UT_hash_handle *hh_next; void *key; unsigned int keylen; unsigned int hashv; }

user_defined_types: struct UT_hash_table { struct UT_hash_bucket *buckets; unsigned int num_buckets; unsigned int num_items; struct UT_hash_handle *tail; long hho; unsigned int ideal_chain_maxlen; unsigned int nonideal_items; unsigned int ineff_expands; unsigned int signature; }


main

original_code:
int main(int argc,char *argv[])
{
    char tfile[200] = { "testfile" };
    unlink(tfile);
    int fd = open(tfile, 0100|02, 0644);
    if (fd < 0) {
        fprintf(stderr,"unable to open test file %s, errno = %d\n", tfile, (*__errno_location ()));
        return 0;
    }
    char buf[200] = { "Now is the time for all good men ...\n" };
    int len = strlen(buf);
    if (write(fd, buf, len) != len) {
        fprintf(stderr,"unable to write out test file %s correctly\n", tfile);
        unlink(tfile);
        return 0;
    }
    int ret = close(fd);
    if (ret != 0)
        fprintf(stderr,"unable to close testfile, errno = %d\n", (*__errno_location ()));
    return 0;
}

canonical_original_code:
int main(int argc, char *argv[])
{
    char tfile[200] = { "testfile" };
    unlink(tfile);
    int fd = open(tfile, 0100|02, 0644);
    if (fd < 0) {
        fprintf(stderr,"unable to open test file %s, errno = %d\n", tfile, (*__errno_location ()));
        return 0;
    }
    char buf[200] = { "Now is the time for all good men ...\n" };
    int len = strlen(buf);
    if (write(fd, buf, len) != len) {
        fprintf(stderr,"unable to write out test file %s correctly\n", tfile);
        unlink(tfile);
        return 0;
    }
    int ret = close(fd);
    if (ret != 0)
        fprintf(stderr,"unable to close testfile, errno = %d\n", (*__errno_location ()));
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types:
tfile
char [200]

variable_types:
buf
char [200]

variable_types: len int

variable_types: ret int


prefix

repo: mcpherrinm/webscale

decompiled_code:
_BOOL8 __fastcall prefix(char *s1, char *s2)
{
int v2; // er12
int v3; // eax

v2 = strlen(s1);
v3 = strlen(s2);
if ( v3 >= v2 )
v3 = v2;
return strncmp(s1, s2, v3) == 0;
}

canonical_decompiled_code:
_BOOL8 __fastcall func6(char *s1, char *s2)
{
int v2; // er12
int v3; // eax

v2 = strlen(s1);
v3 = strlen(s2);
if ( v3 >= v2 )
v3 = v2;
return strncmp(s1, s2, v3) == 0;
}

original_code:
int prefix(const char *a, const char*b) {
  int la = strlen(a);
  int lb = strlen(b);
  return 0 == strncmp(a, b, la > lb ? lb : la);
}

canonical_original_code:
int prefix(char *a, char *b) {
  int la = strlen(a);
  int lb = strlen(b);
  return 0 == strncmp(a, b, la > lb ? lb : la);
}

variable_types:
a
char *

variable_types:
b
char *

variable_types: la int

variable_types: lb int


repo: mcsf/postman

decompiled_code:
_QWORD *lst_new()
{
_QWORD *result; // rax

result = malloc(8uLL);
*result = 0LL;
return result;
}

canonical_decompiled_code:
_QWORD *func7()
{
_QWORD *result; // rax

result = malloc(8uLL);
*result = 0LL;
return result;
}

original_code:
list *lst_new() {
 list *l = (list *) malloc(sizeof(list));
 l->head = ((void *)0);
 return l;
}

canonical_original_code:
struct list *lst_new() {
 struct list *l = (list *) malloc(sizeof(list));
 l->head = ((void *)0);
 return l;
}

variable_types: struct list { struct _node { int value; struct _node *next; } *head; } *


decompiled_code:
void __fastcall lst_remove(_QWORD **a1, int a2)
{
_QWORD *v3; // rdi
_QWORD *v4; // rax

v3 = *a1;
if ( v3 )
{
if ( *(_DWORD *)v3 == a2 )
{
*a1 = (_QWORD *)v3[1];
free(v3);
}
else
{
while ( 1 )
{
v4 = v3;
v3 = (_QWORD *)v3[1];
if ( !v3 )
break;
if ( *(_DWORD *)v3 == a2 )
{
v4[1] = v3[1];
free(v3);
return;
}
}
}
}
}

original_code:
void lst_remove(list *l, int value) {
 node *pre, *cur;
 pre = cur = l->head;
 if (!cur) return;
 if (cur->value == value) {
  l->head = cur->next;
  free(cur);
  return;
 }
 while ((cur = cur->next)) {
  if (cur->value == value) {
   pre->next = cur->next;
   free(cur);
   return;
  }
  pre = cur;
 }
}

canonical_original_code:
void lst_remove(struct list *l, int value) {
 struct _node *pre; struct _node *cur;
 pre = cur = l->head;
 if (!cur) return;
 if (cur->value == value) {
  l->head = cur->next;
  free(cur);
  return;
 }
 while ((cur = cur->next)) {
  if (cur->value == value) {
   pre->next = cur->next;
   free(cur);
   return;
  }
  pre = cur;
 }
}

variable_types: value int

variable_types: struct _node { int value; struct _node *next; } *


decompiled_code:
_BOOL8 __fastcall lst_empty(_QWORD *a1)
{
return *a1 == 0LL;
}

canonical_decompiled_code:
_BOOL8 __fastcall func9(_QWORD *a1)
{
return *a1 == 0LL;
}

original_code:
int lst_empty(list *l) {
 return (l->head == ((void *)0));
}

canonical_original_code:
int lst_empty(struct list *l) {
 return (l->head == ((void *)0));
}

user_defined_types: struct _node { int value; struct _node *next; }


decompiled_code:
__int64 __fastcall lst_size(__int64 a1)
{
__int64 v1; // rax
unsigned int v2; // edx

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

canonical_decompiled_code:
__int64 __fastcall func10(__int64 a1)
{
__int64 v1; // rax
unsigned int v2; // edx

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

original_code:
int lst_size(list *l) {
 int siz = 1;
 node *n = l->head;
 if (!n) return 0;
 while ((n = n->next)) siz++;
 return siz;
}

canonical_original_code:
int lst_size(struct list *l) {
 int siz = 1;
 struct _node *n = l->head;
 if (!n) return 0;
 while ((n = n->next)) siz++;
 return siz;
}

variable_types: siz int


decompiled_code:
__int64 __fastcall lst_pop(void **a1)
{
void *v1; // rax
unsigned int v2; // ebx

v1 = *a1;
if ( !*a1 )
return (unsigned int)-1;
v2 = *(_DWORD *)v1;
*a1 = (void *)*((_QWORD *)v1 + 1);
free(v1);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func11(void **a1)
{
void *v1; // rax
unsigned int v2; // ebx

v1 = *a1;
if ( !*a1 )
return (unsigned int)-1;
v2 = *(_DWORD *)v1;
*a1 = (void *)*((_QWORD *)v1 + 1);
free(v1);
return v2;
}

original_code:
int lst_pop(list *l) {
 int res;
 node *n = l->head;
 if (!n) return -1;
 res = n->value;
 l->head = n->next;
 free(n);
 return res;
}

canonical_original_code:
int lst_pop(struct list *l) {
 int res;
 struct _node *n = l->head;
 if (!n) return -1;
 res = n->value;
 l->head = n->next;
 free(n);
 return res;
}

variable_types: res int


decompiled_code:
__int64 __fastcall lst_first(_QWORD *a1)
{
__int64 result; // rax

if ( *a1 )
result = *(unsigned int *)*a1;
else
result = 0xFFFFFFFFLL;
return result;
}

canonical_decompiled_code:
__int64 __fastcall func12(_QWORD *a1)
{
__int64 result; // rax

if ( *a1 )
result = *(unsigned int *)*a1;
else
result = 0xFFFFFFFFLL;
return result;
}

original_code:
int lst_first(list *l) {
 if (!l->head) return -1;
 return l->head->value;
}

canonical_original_code:
int lst_first(struct list *l) {
 if (!l->head) return -1;
 return l->head->value;
}


decompiled_code:
void __fastcall lst_reverse(void **a1)
{
void **v1; // r12
_QWORD *v2; // rbx
int v3; // eax

v1 = (void **)lst_new();
v2 = *a1;
if ( *a1 )
{
do
{
v2 = (_QWORD *)v2[1];
v3 = lst_pop(a1);
lst_push(v1, v3);
}
while ( v2 );
if ( !lst_empty(a1) )
puts("L IS NOT EMPTY");
*a1 = *v1;
free(v1);
}
}

canonical_decompiled_code:
void __fastcall func13(void **a1)
{
void **v1; // r12
_QWORD *v2; // rbx
int v3; // eax

v1 = (void **)func7();
v2 = *a1;
if ( *a1 )
{
do
{
v2 = (_QWORD *)v2[1];
v3 = func11(a1);
lst_push(v1, v3);
}
while ( v2 );
if ( !func9(a1) )
puts("L IS NOT EMPTY");
*a1 = *v1;
free(v1);
}
}

original_code:
void lst_reverse(list *l) {
 list *new = lst_new();
 node *n = l->head;
 node *next;
 if (!n) return;
 do {
  next = n->next;
  lst_push(new, lst_pop(l));
 } while ((n = next));
 if (!lst_empty(l)) printf("L IS NOT EMPTY\n");
 l->head = new->head;
 free(new);
}

canonical_original_code:
void lst_reverse(struct list *l) {
 struct list *new = lst_new();
 struct _node *n = l->head;
 struct _node *next;
 if (!n) return;
 do {
  next = n->next;
  lst_push(new, lst_pop(l));
 } while ((n = next));
 if (!lst_empty(l)) printf("L IS NOT EMPTY\n");
 l->head = new->head;
 free(new);
}


original_code:
int lst_subst(list *l, int old, list *new) {
 node *pre, *cur, *new_head, *new_tail;
 if (!l->head || !new->head) return 0;
 pre = cur = new_head = new->head;
 while ((cur = cur->next)) pre = cur;
 new_tail = pre;
 pre = cur = l->head;
 while ((cur = cur->next)) {
  if (cur->value == old) {
   pre->next = new_head;
   new_tail->next = cur->next;
   free(cur);
   return 1;
  }
  pre = cur;
 }
 return 0;
}

canonical_original_code:
int lst_subst(struct list *l, int old, struct list *new) {
 struct _node *pre; struct _node *cur; struct _node *new_head; struct _node *new_tail;
 if (!l->head || !new->head) return 0;
 pre = cur = new_head = new->head;
 while ((cur = cur->next)) pre = cur;
 new_tail = pre;
 pre = cur = l->head;
 while ((cur = cur->next)) {
  if (cur->value == old) {
   pre->next = new_head;
   new_tail->next = cur->next;
   free(cur);
   return 1;
  }
  pre = cur;
 }
 return 0;
}

variable_types: old int

variable_types:
new
struct list *


decompiled_code:
int __fastcall lst_dump(unsigned int **a1)
{
unsigned int *i; // rbx
const char *v2; // rdx

for ( i = *a1; i; i = (unsigned int *)*((_QWORD *)i + 1) )
{
v2 = "";
if ( *((_QWORD *)i + 1) )
v2 = " ";
printf("%d%s", *i, v2);
}
return putchar(10);
}

canonical_decompiled_code:
int __fastcall func15(unsigned int **a1)
{
unsigned int *i; // rbx
const char *v2; // rdx

for ( i = *a1; i; i = (unsigned int *)*((_QWORD *)i + 1) )
{
v2 = "";
if ( *((_QWORD *)i + 1) )
v2 = " ";
printf("%d%s", *i, v2);
}
return putchar(10);
}

original_code:
void lst_dump(list *l) {
 node *n;
 for (n = l->head; n; n = n->next)
  printf("%d%s", n->value, n->next ? " " : "");
 printf("\n");
}

canonical_original_code:
void lst_dump(struct list *l) {
 struct _node *n;
 for (n = l->head; n; n = n->next)
  printf("%d%s", n->value, n->next ? " " : "");
 printf("\n");
}


setup

decompiled_code:
int setup()
{
int v0; // ebx
__int64 v1; // rbx
_QWORD *v2; // rbp
int result; // eax
__int64 v4; // rbp
int v5; // ebx
int v6; // [rsp+8h] [rbp-20h] BYREF
int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

scanf("%d\n%d\n%d\n", &n, &m, &init);
v0 = n;
edges = (__int64)malloc(8LL * n);
if ( v0 > 0 )
{
v1 = 0LL;
do
{
v2 = (_QWORD *)(edges + 8 * v1);
*v2 = lst_new();
++v1;
}
while ( n > (int)v1 );
}
while ( scanf("%d %d\n", v7, &v6) != -1 )
{
lst_insert_ordered(*(__int64 **)(edges + 8LL * v7[0] - 8), v6);
lst_insert_ordered(*(__int64 **)(edges + 8LL * v6 - 8), v7[0]);
}
result = puts("Edge lists:");
if ( n > 0 )
{
v4 = 0LL;
v5 = 0;
do
{
printf("%d\t: ", (unsigned int)++v5);
result = lst_dump(*(unsigned int ***)(edges + v4));
v4 += 8LL;
}
while ( v5 < n );
}
return result;
}

canonical_decompiled_code:
int func16()
{
int v0; // ebx
__int64 v1; // rbx
_QWORD *v2; // rbp
int result; // eax
__int64 v4; // rbp
int v5; // ebx
int v6; // [rsp+8h] [rbp-20h] BYREF
int v7[7]; // [rsp+Ch] [rbp-1Ch] BYREF

scanf("%d\n%d\n%d\n", &n, &m, &init);
v0 = n;
edges = (__int64)malloc(8LL * n);
if ( v0 > 0 )
{
v1 = 0LL;
do
{
v2 = (_QWORD *)(edges + 8 * v1);
*v2 = func7();
++v1;
}
while ( n > (int)v1 );
}
while ( scanf("%d %d\n", v7, &v6) != -1 )
{
lst_insert_ordered(*(__int64 **)(edges + 8LL * v7[0] - 8), v6);
lst_insert_ordered(*(__int64 **)(edges + 8LL * v6 - 8), v7[0]);
}
result = puts("Edge lists:");
if ( n > 0 )
{
v4 = 0LL;
v5 = 0;
do
{
printf("%d\t: ", (unsigned int)++v5);
result = func15(*(unsigned int ***)(edges + v4));
v4 += 8LL;
}
while ( v5 < n );
}
return result;
}

original_code:
void setup() {
 int i, a, b;
 scanf("%d\n%d\n%d\n", &n, &m, &init);
 edges = malloc(sizeof(list *) * n);
 for (i = 0; i < n; i++)
  edges[i] = lst_new();
 while (scanf("%d %d\n", &a, &b) != (-1)) {
  lst_insert_ordered(edges[a-1], b);
  lst_insert_ordered(edges[b-1], a);
 }
 printf("Edge lists:\n");
 for (i = 0; i < n; i++) {
  printf("%d\t: ", i+1);
  lst_dump(edges[i]);
 }
}

canonical_original_code:
void setup() {
 int i; int a; int b;
 scanf("%d\n%d\n%d\n", &n, &m, &init);
 edges = malloc(sizeof(struct list *) * n);
 for (i = 0; i < n; i++)
  edges[i] = lst_new();
 while (scanf("%d %d\n", &a, &b) != (-1)) {
  lst_insert_ordered(edges[a-1], b);
  lst_insert_ordered(edges[b-1], a);
 }
 printf("Edge lists:\n");
 for (i = 0; i < n; i++) {
  printf("%d\t: ", i+1);
  lst_dump(edges[i]);
 }
}

variable_types: i int

variable_types: a int

variable_types: b int


decompiled_code:
__int64 check_solvable()
{
__int64 *v0; // rbx
__int64 v1; // rbp
__int64 result; // rax

if ( n <= 0 )
return 1LL;
v0 = (__int64 *)edges;
v1 = edges + 8LL * (unsigned int)(n - 1) + 8;
while ( 1 )
{
result = lst_size(*v0);
if ( !(_DWORD)result )
break;
if ( (int)result % 2 == 1 )
return 0LL;
if ( ++v0 == (__int64 *)v1 )
return 1LL;
}
return result;
}

canonical_decompiled_code:
__int64 func17()
{
__int64 *v0; // rbx
__int64 v1; // rbp
__int64 result; // rax

if ( n <= 0 )
return 1LL;
v0 = (__int64 *)edges;
v1 = edges + 8LL * (unsigned int)(n - 1) + 8;
while ( 1 )
{
result = func10(*v0);
if ( !(_DWORD)result )
break;
if ( (int)result % 2 == 1 )
return 0LL;
if ( ++v0 == (__int64 *)v1 )
return 1LL;
}
return result;
}

original_code:
int check_solvable() {
 int i;
 for (i = 0; i < n; i++) {
  int size = lst_size(edges[i]);
  if (size == 0 || size % 2 == 1)
   return 0;
 }
 return 1;
}

variable_types: size int


decompiled_code:
__int64 __fastcall initial_vertex(unsigned int **a1)
{
unsigned int *v1; // rbx
__int64 v2; // r12
unsigned int v3; // ebp

v1 = *a1;
if ( !*a1 )
return 0;
v2 = edges;
while ( 1 )
{
v3 = *v1;
if ( !lst_empty(*(_QWORD **)(v2 + 8LL * (int)*v1 - 8)) )
break;
v1 = (unsigned int *)*((_QWORD *)v1 + 1);
if ( !v1 )
return 0;
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func18(unsigned int **a1)
{
unsigned int *v1; // rbx
__int64 v2; // r12
unsigned int v3; // ebp

v1 = *a1;
if ( !*a1 )
return 0;
v2 = edges;
while ( 1 )
{
v3 = *v1;
if ( !func9(*(_QWORD **)(v2 + 8LL * (int)*v1 - 8)) )
break;
v1 = (unsigned int *)*((_QWORD *)v1 + 1);
if ( !v1 )
return 0;
}
return v3;
}

original_code:
int initial_vertex(list *cycle) {
 node *n;
 for (n = cycle->head; n; n = n->next) {
  int v = n->value;
  if (!lst_empty(edges[v-1]))
   return v;
 }
 return 0;
}

canonical_original_code:
int initial_vertex(struct list *cycle) {
 struct _node *n;
 for (n = cycle->head; n; n = n->next) {
  int v = n->value;
  if (!lst_empty(edges[v-1]))
   return v;
 }
 return 0;
}

variable_types: v int


decompiled_code:
__int64 collect_cycles()
{
int v0; // ebx
int v1; // ebp
void **v2; // rbx
__int64 result; // rax
unsigned int **v4; // r12

cycles = (__int64)lst_new();
do
{
printf("NEW CYCLE, init=%d\n", (unsigned int)init);
v1 = init;
v4 = (unsigned int **)lst_new();
lst_push(v4, v1);
if ( lst_empty((_QWORD *)cycles) )
c_0 = (__int64)v4;
while ( 1 )
{
v2 = *(void ***)(edges + 8LL * v1 - 8);
if ( lst_empty(v2) )
break;
v0 = lst_pop(v2);
lst_remove(*(_QWORD ***)(edges + 8LL * v0 - 8), v1);
lst_push(v4, v0);
v1 = v0;
}
lst_dump(v4);
lst_push((_QWORD *)cycles, (int)v4);
result = initial_vertex(v4);
init = result;
}
while ( (_DWORD)result );
return result;
}

canonical_decompiled_code:
__int64 func19()
{
int v0; // ebx
int v1; // ebp
void **v2; // rbx
__int64 result; // rax
unsigned int **v4; // r12

cycles = (__int64)func7();
do
{
printf("NEW CYCLE, init=%d\n", (unsigned int)init);
v1 = init;
v4 = (unsigned int **)func7();
lst_push(v4, v1);
if ( func9((_QWORD *)cycles) )
c_0 = (__int64)v4;
while ( 1 )
{
v2 = *(void ***)(edges + 8LL * v1 - 8);
if ( func9(v2) )
break;
v0 = func11(v2);
func8(*(_QWORD ***)(edges + 8LL * v0 - 8), v1);
lst_push(v4, v0);
v1 = v0;
}
func15(v4);
lst_push((_QWORD *)cycles, (int)v4);
result = func18(v4);
init = result;
}
while ( (_DWORD)result );
return result;
}

original_code:
void collect_cycles() {
 int cur;
 list *cycle;
 cycles = lst_new();
 do {
  printf("NEW CYCLE, init=%d\n", init);
  cur = init;
  cycle = lst_new();
  lst_push(cycle, cur);
  if (lst_empty(cycles)) c_0 = cycle;
  while (!lst_empty(edges[cur-1])) {
   int next = lst_pop(edges[cur-1]);
   lst_remove(edges[next-1], cur);
   lst_push(cycle, next);
   cur = next;
  }
  lst_dump(cycle);
  lst_push(cycles, (int) cycle);
 } while ((init = initial_vertex(cycle)));
}

canonical_original_code:
void collect_cycles() {
 int cur;
 struct list *cycle;
 cycles = lst_new();
 do {
  printf("NEW CYCLE, init=%d\n", init);
  cur = init;
  cycle = lst_new();
  lst_push(cycle, cur);
  if (lst_empty(cycles)) c_0 = cycle;
  while (!lst_empty(edges[cur-1])) {
   int next = lst_pop(edges[cur-1]);
   lst_remove(edges[next-1], cur);
   lst_push(cycle, next);
   cur = next;
  }
  lst_dump(cycle);
  lst_push(cycles, (int) cycle);
 } while ((init = initial_vertex(cycle)));
}

variable_types: cur int

variable_types: next int


decompiled_code:
void merge_cycles()
{
unsigned int v0; // er12
_QWORD *v1; // r13
int v2; // ebp
__int64 v3; // rbx
__int64 v4; // r12
__int64 *v5; // r14
__int64 v6; // rbx
unsigned int **v7; // r15

v0 = lst_size(cycles);
v1 = malloc(8LL * (int)v0);
v2 = v0 - 1;
if ( (int)(v0 - 1) >= 0 )
{
v3 = v2;
do
v1[v3--] = (int)lst_pop((void **)cycles);
while ( (int)v3 >= 0 );
}
printf("SIZE: %d\n", v0);
if ( v2 > 0 )
{
v4 = (__int64)&v1[(int)v0 - 2];
do
{
v5 = *(__int64 **)(v4 + 8);
v6 = (unsigned int)lst_first(v5);
printf("<-- MERGING AT %d\n", v6);
v7 = *(unsigned int ***)v4;
lst_dump(*(unsigned int ***)v4);
if ( !(unsigned int)lst_subst((_QWORD **)v7, v6, v5) )
{
puts("-+-+-+- OOPS");
raise(10);
}
lst_dump(v7);
printf("--> MERGED INTO %d\n", (unsigned int)--v2);
v4 -= 8LL;
}
while ( v2 );
}
free(v1);
}

canonical_decompiled_code:
void func20()
{
unsigned int v0; // er12
_QWORD *v1; // r13
int v2; // ebp
__int64 v3; // rbx
__int64 v4; // r12
__int64 *v5; // r14
__int64 v6; // rbx
unsigned int **v7; // r15

v0 = func10(cycles);
v1 = malloc(8LL * (int)v0);
v2 = v0 - 1;
if ( (int)(v0 - 1) >= 0 )
{
v3 = v2;
do
v1[v3--] = (int)func11((void **)cycles);
while ( (int)v3 >= 0 );
}
printf("SIZE: %d\n", v0);
if ( v2 > 0 )
{
v4 = (__int64)&v1[(int)v0 - 2];
do
{
v5 = *(__int64 **)(v4 + 8);
v6 = (unsigned int)func12(v5);
printf("<-- MERGING AT %d\n", v6);
v7 = *(unsigned int ***)v4;
func15(*(unsigned int ***)v4);
if ( !(unsigned int)func14((_QWORD **)v7, v6, v5) )
{
puts("-+-+-+- OOPS");
raise(10);
}
func15(v7);
printf("--> MERGED INTO %d\n", (unsigned int)--v2);
v4 -= 8LL;
}
while ( v2 );
}
free(v1);
}

original_code:
void merge_cycles() {
 int i, n_cycles = lst_size(cycles);
 list **cycles_arr = malloc(sizeof(list *) * n_cycles);
 for (i = n_cycles - 1; i >= 0; i--)
  cycles_arr[i] = (list *) lst_pop(cycles);
 printf("SIZE: %d\n", n_cycles);
 for (i = n_cycles - 1; i > 0; i--) {
  list *c_i = cycles_arr[i];
  int end = lst_first(c_i);
  printf("<-- MERGING AT %d\n", end);
  lst_dump(cycles_arr[i-1]);
  if (!lst_subst(cycles_arr[i-1], end, c_i)) {
   printf("-+-+-+- OOPS\n");
   raise(10);
  }
  lst_dump(cycles_arr[i-1]);
  printf("--> MERGED INTO %d\n", i-1);
 }
 free(cycles_arr);
}

canonical_original_code:
void merge_cycles() {
 int i; int n_cycles = lst_size(cycles);
 struct list **cycles_arr = malloc(sizeof(list *) * n_cycles);
 for (i = n_cycles - 1; i >= 0; i--)
  cycles_arr[i] = (struct list *) lst_pop(cycles);
 printf("SIZE: %d\n", n_cycles);
 for (i = n_cycles - 1; i > 0; i--) {
  struct list *c_i = cycles_arr[i];
  int end = lst_first(c_i);
  printf("<-- MERGING AT %d\n", end);
  lst_dump(cycles_arr[i-1]);
  if (!lst_subst(cycles_arr[i-1], end, c_i)) {
   printf("-+-+-+- OOPS\n");
   raise(10);
  }
  lst_dump(cycles_arr[i-1]);
  printf("--> MERGED INTO %d\n", i-1);
 }
 free(cycles_arr);
}

variable_types: struct list { struct _node { int value; struct _node *next; } *head; } **

variable_types: end int


output

decompiled_code:
int output()
{
lst_reverse((void **)c_0);
return lst_dump((unsigned int **)c_0);
}

canonical_decompiled_code:
int func21()
{
func13((void **)c_0);
return func15((unsigned int **)c_0);
}

original_code:
void output() {
 lst_reverse(c_0);
 lst_dump(c_0);
}


main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
setup();
if ( (unsigned int)check_solvable(argc, argv) )
{
collect_cycles();
merge_cycles();
output();
}
else
{
puts("-1");
}
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
func16();
if ( (unsigned int)func17(argc, argv) )
{
func19();
func20();
func21();
}
else
{
puts("-1");
}
return 0;
}

original_code:
int main() {
 setup();
 if (!check_solvable()) {
  printf("-1\n");
  return 0;
 }
 collect_cycles();
 merge_cycles();
 output();
 return 0;
}


repo: mczoden/hems

decompiled_code:
void __noreturn sig_hdl()
{
pthread_cancel(tid_uld);
pthread_cancel(tid_gen);
pthread_mutex_destroy(&mtx_gen);
pthread_mutex_destroy(&mtx_uld);
sleep(1u);
uld_deinit();
exit(0);
}

canonical_decompiled_code:
void __noreturn func0()
{
pthread_cancel(tid_uld);
pthread_cancel(tid_gen);
pthread_mutex_destroy(&mtx_gen);
pthread_mutex_destroy(&mtx_uld);
sleep(1u);
uld_deinit();
exit(0);
}

original_code:
static void sig_hdl(int sig)
{
    pthread_cancel(tid_uld);
    pthread_cancel(tid_gen);
    pthread_mutex_destroy(&mtx_gen);
    pthread_mutex_destroy(&mtx_uld);
    sleep(1);
    uld_deinit();
    exit(0);
}

variable_types: sig int


decompiled_code:
void __fastcall __noreturn gen_thread_hdl(void *a1)
{
pthread_setcancelstate(0, 0LL);
pthread_setcanceltype(1, 0LL);
while ( 1 )
{
pthread_mutex_lock(&mtx_gen);
if ( (unsigned int)gen(a1) )
dbg(2LL, "%s.", "Generate kpi file failed");
else
pthread_mutex_unlock(&mtx_uld);
}
}

canonical_decompiled_code:
void __fastcall __noreturn func1(void *a1)
{
pthread_setcancelstate(0, 0LL);
pthread_setcanceltype(1, 0LL);
while ( 1 )
{
pthread_mutex_lock(&mtx_gen);
if ( (unsigned int)gen(a1) )
dbg(2LL, "%s.", "Generate kpi file failed");
else
pthread_mutex_unlock(&mtx_uld);
}
}

original_code:
static void *gen_thread_hdl(void *arg)
{
    const cheer_ctx_t *ctx = ((cheer_ctx_t *)arg);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, ((void *)0));
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, ((void *)0));
    while (1) {
        pthread_mutex_lock(&mtx_gen);
        if (gen(ctx) != 0) {
            dbg((1 << 1), "%s.", "Generate kpi file failed");
        } else {
            pthread_mutex_unlock(&mtx_uld);
        }
    }
    pthread_exit(((void *)0));
}

canonical_original_code:
static void *gen_thread_hdl(void *arg)
{
    struct cheer_ctx *ctx = ((cheer_ctx_t *)arg);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, ((void *)0));
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, ((void *)0));
    while (1) {
        pthread_mutex_lock(&mtx_gen);
        if (gen(ctx) != 0) {
            dbg((1 << 1), "%s.", "Generate kpi file failed");
        } else {
            pthread_mutex_unlock(&mtx_uld);
        }
    }
    pthread_exit(((void *)0));
}

variable_types:
arg
void *

variable_types: struct cheer_ctx { long t_beg_pro; char cfg[256]; struct { int h; int m; } tz; int interval; long t_uld_time; char sn[256]; char url[256]; int port; char username[256]; char password[256]; _Bool need_bak; char bak_dir[256]; int dbg_lvl; } *


decompiled_code:
void __fastcall __noreturn uld_thread_hdl(void *a1)
{
pthread_setcancelstate(0, 0LL);
pthread_setcanceltype(1, 0LL);
while ( 1 )
{
do
pthread_mutex_lock(&mtx_uld);
while ( !(unsigned int)uld(a1) );
dbg(2LL, "%s.", "Upload kpi file failed, retry at next intval");
}
}

canonical_decompiled_code:
void __fastcall __noreturn func2(void *a1)
{
pthread_setcancelstate(0, 0LL);
pthread_setcanceltype(1, 0LL);
while ( 1 )
{
do
pthread_mutex_lock(&mtx_uld);
while ( !(unsigned int)uld(a1) );
dbg(2LL, "%s.", "Upload kpi file failed, retry at next intval");
}
}

original_code:
static void *uld_thread_hdl(void *arg)
{
    const cheer_ctx_t *ctx = ((cheer_ctx_t *)arg);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, ((void *)0));
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, ((void *)0));
    while (1) {
        pthread_mutex_lock(&mtx_uld);
        if (uld(ctx) != 0) {
            dbg((1 << 1), "%s.", "Upload kpi file failed, retry at next intval");
        }
    }
    pthread_exit(((void *)0));
}

canonical_original_code:
static void *uld_thread_hdl(void *arg)
{
    struct cheer_ctx *ctx = ((cheer_ctx_t *)arg);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, ((void *)0));
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, ((void *)0));
    while (1) {
        pthread_mutex_lock(&mtx_uld);
        if (uld(ctx) != 0) {
            dbg((1 << 1), "%s.", "Upload kpi file failed, retry at next intval");
        }
    }
    pthread_exit(((void *)0));
}

user_defined_types: struct cheer_ctx { long t_beg_pro; char cfg[256]; struct { int h; int m; } tz; int interval; long t_uld_time; char sn[256]; char url[256]; int port; char username[256]; char password[256]; _Bool need_bak; char bak_dir[256]; int dbg_lvl; }


decompiled_code:
int __fastcall print_usage(const char *a1)
{
printf("Usage: %s [OPTIONS]\n", a1);
puts(" --config CONFIG specify config file path");
return puts(" --help display this help and exit");
}

canonical_decompiled_code:
int __fastcall func3(const char *a1)
{
printf("Usage: %s [OPTIONS]\n", a1);
puts(" --config CONFIG specify config file path");
return puts(" --help display this help and exit");
}

original_code:
static void print_usage(const char *argv0)
{
    printf("Usage: %s [OPTIONS]\n", argv0);
    printf("  --config CONFIG   specify config file path\n");
    printf("  --help            display this help and exit\n");
}

canonical_original_code:
static void print_usage(char *argv0)
{
    printf("Usage: %s [OPTIONS]\n", argv0);
    printf("  --config CONFIG   specify config file path\n");
    printf("  --help            display this help and exit\n");
}


original_code:
static int parser_para(cheer_ctx_t *ctx, int argc, char *argv[])
{
    int opt = 0;
    struct option long_opts[] = {
        { "help", 0, 0, 'h' },
        { "config", 1, 0, 'f' },
        { 0, 0, 0, 0 }
    };
    do {
        opt = getopt_long(argc, argv, "hf:", long_opts, ((void *)0));
        switch (opt) {
        case 'h':
            print_usage(argv[0]);
            exit(0);
            break;
        case 'f':
            strncpy(ctx->cfg, optarg, sizeof(ctx->cfg) - 1);
            break;
        case '?':
            print_usage(argv[0]);
            exit(1);
            break;
        case -1:
            break;
        default:
            return -1;
        }
    } while (opt != -1);
    return 0;
}

canonical_original_code:
static int parser_para(struct cheer_ctx *ctx, int argc, char *argv[])
{
    int opt = 0;
    struct option long_opts[] = {
        { "help", 0, 0, 'h' },
        { "config", 1, 0, 'f' },
        { 0, 0, 0, 0 }
    };
    do {
        opt = getopt_long(argc, argv, "hf:", long_opts, ((void *)0));
        switch (opt) {
        case 'h':
            print_usage(argv[0]);
            exit(0);
            break;
        case 'f':
            strncpy(ctx->cfg, optarg, sizeof(ctx->cfg) - 1);
            break;
        case '?':
            print_usage(argv[0]);
            exit(1);
            break;
        case -1:
            break;
        default:
            return -1;
        }
    } while (opt != -1);
    return 0;
}

variable_types: argc int

variable_types:
argv
char *[]

variable_types: opt int

variable_types: struct option { char *name; int has_arg; int *flag; int val; } []


original_code:
static int time_init(cheer_ctx_t *ctx)
{
    struct tm tm_beg;
    long int gmt_h = 0, gmt_m = 0;
    ctx->t_beg_pro = time(((void *)0));
    memcpy(&tm_beg, localtime(&(ctx->t_beg_pro)), sizeof(tm_beg));
    gmt_h = tm_beg.__tm_gmtoff / 3600;
    gmt_m = abs((tm_beg.__tm_gmtoff - gmt_h * 3600) / 60);
    ctx->tz.h = gmt_h;
    ctx->tz.m = gmt_m;
    return 0;
}

canonical_original_code:
static int time_init(struct cheer_ctx *ctx)
{
    struct tm tm_beg;
    long gmt_h = 0; long gmt_m = 0;
    ctx->t_beg_pro = time(((void *)0));
    memcpy(&tm_beg, localtime(&(ctx->t_beg_pro)), sizeof(tm_beg));
    gmt_h = tm_beg.__tm_gmtoff / 3600;
    gmt_m = abs((tm_beg.__tm_gmtoff - gmt_h * 3600) / 60);
    ctx->tz.h = gmt_h;
    ctx->tz.m = gmt_m;
    return 0;
}

variable_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long __tm_gmtoff; char *__tm_zone; }


decompiled_code:
int __fastcall chk_and_prep_bak_dir(char *path)
{
int result; // eax
int v3; // edi
char *v4; // rax
struct stat v5; // [rsp+0h] [rbp-98h] BYREF

memset(&v5, 0, sizeof(v5));
result = stat(path, &v5);
if ( result )
{
v3 = *_errno_location();
if ( v3 == 2 )
{
result = mkdir(path, 0x1FFu);
if ( result )
{
dbg(1LL, "Create directory: %s failed.", path);
result = -1;
}
}
else
{
v4 = strerror(v3);
dbg(1LL, "Invoke stat() failed: %s.", v4);
result = -1;
}
}
else if ( (v5.st_mode & 0xF000) != 0x4000 )
{
dbg(1LL, "%s has been existed but not a directory.", path);
result = -1;
}
return result;
}

canonical_decompiled_code:
int __fastcall func6(char *path)
{
int result; // eax
int v3; // edi
char *v4; // rax
struct stat v5; // [rsp+0h] [rbp-98h] BYREF

memset(&v5, 0, sizeof(v5));
result = stat(path, &v5);
if ( result )
{
v3 = *_errno_location();
if ( v3 == 2 )
{
result = mkdir(path, 0x1FFu);
if ( result )
{
dbg(1LL, "Create directory: %s failed.", path);
result = -1;
}
}
else
{
v4 = strerror(v3);
dbg(1LL, "Invoke stat() failed: %s.", v4);
result = -1;
}
}
else if ( (v5.st_mode & 0xF000) != 0x4000 )
{
dbg(1LL, "%s has been existed but not a directory.", path);
result = -1;
}
return result;
}

original_code:
static int chk_and_prep_bak_dir(const char *dirname)
{
    struct stat st = { 0 };
    if (stat(dirname, &st) != 0) {
        if ((*__errno_location ()) == 2) {
            if (mkdir(dirname, 0777) != 0) {
                dbg((1 << 0), "Create directory: %s failed.", dirname);
                return -1;
            }
        } else {
            dbg((1 << 0), "Invoke stat() failed: %s.", strerror((*__errno_location ())));
            return -1;
        }
    } else {
        if (!((((st.st_mode)) & 0170000) == (0040000))) {
            dbg((1 << 0), "%s has been existed but not a directory.", dirname);
            return -1;
        }
    }
    return 0;
}

canonical_original_code:
static int chk_and_prep_bak_dir(char *dirname)
{
    struct stat st = { 0 };
    if (stat(dirname, &st) != 0) {
        if ((*__errno_location ()) == 2) {
            if (mkdir(dirname, 0777) != 0) {
                dbg((1 << 0), "Create directory: %s failed.", dirname);
                return -1;
            }
        } else {
            dbg((1 << 0), "Invoke stat() failed: %s.", strerror((*__errno_location ())));
            return -1;
        }
    } else {
        if (!((((st.st_mode)) & 0170000) == (0040000))) {
            dbg((1 << 0), "%s has been existed but not a directory.", dirname);
            return -1;
        }
    }
    return 0;
}

variable_types:
dirname
char *

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; long st_atime; unsigned long st_atimensec; long st_mtime; unsigned long st_mtimensec; long st_ctime; unsigned long st_ctimensec; long __glibc_reserved[3]; }


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

if ( *(int *)(a1 + 272) <= 1 )
{
dbg(1LL, "%s.", "Upload interval too short");
result = 0LL;
}
else if ( *(_BYTE *)(a1 + 288) )
{
if ( *(_BYTE *)(a1 + 544) )
{
if ( !*(_BYTE *)(a1 + 1316) )
goto LABEL_17;
if ( !*(_BYTE *)(a1 + 1317) )
{
dbg(2LL, "Backup directory is not specified, default: %s.", "./backup/");
strncpy((char *)(a1 + 1317), "./backup/", 0xFFuLL);
}
if ( chk_and_prep_bak_dir((char *)(a1 + 1317)) )
{
dbg(1LL, "%s.", "Backup directory can't be used");
result = 0LL;
}
else
{
LABEL_17:
if ( !*(_DWORD *)(a1 + 1576) )
dbg(4LL, "%s.", "Debug level is 0, no dbg will be displayed from now");
set_dbg_lvl(*(unsigned int *)(a1 + 1576));
result = 1LL;
}
}
else
{
dbg(1LL, "%s.", "Upload URL should not be empty");
result = 0LL;
}
}
else
{
dbg(1LL, "%s.", "Serial number should not be empty");
result = 0LL;
}
return result;
}

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

if ( *(int *)(a1 + 272) <= 1 )
{
dbg(1LL, "%s.", "Upload interval too short");
result = 0LL;
}
else if ( *(_BYTE *)(a1 + 288) )
{
if ( *(_BYTE *)(a1 + 544) )
{
if ( !*(_BYTE *)(a1 + 1316) )
goto LABEL_17;
if ( !*(_BYTE *)(a1 + 1317) )
{
dbg(2LL, "Backup directory is not specified, default: %s.", "./backup/");
strncpy((char *)(a1 + 1317), "./backup/", 0xFFuLL);
}
if ( func6((char *)(a1 + 1317)) )
{
dbg(1LL, "%s.", "Backup directory can't be used");
result = 0LL;
}
else
{
LABEL_17:
if ( !*(_DWORD *)(a1 + 1576) )
dbg(4LL, "%s.", "Debug level is 0, no dbg will be displayed from now");
set_dbg_lvl(*(unsigned int *)(a1 + 1576));
result = 1LL;
}
}
else
{
dbg(1LL, "%s.", "Upload URL should not be empty");
result = 0LL;
}
}
else
{
dbg(1LL, "%s.", "Serial number should not be empty");
result = 0LL;
}
return result;
}

original_code:
static _Bool permit_to_run(cheer_ctx_t *ctx)
{
    if (ctx->interval <= 1) {
        dbg((1 << 0), "%s.", "Upload interval too short");
        return 0;
    }
    if (strlen(ctx->sn) == 0) {
        dbg((1 << 0), "%s.", "Serial number should not be empty");
        return 0;
    }
    if (strlen(ctx->url) == 0) {
        dbg((1 << 0), "%s.", "Upload URL should not be empty");
        return 0;
    }
    if (ctx->need_bak) {
        if (strlen(ctx->bak_dir) == 0) {
            dbg((1 << 1), "Backup directory is not specified, default: %s.", "./backup/");
            strncpy(ctx->bak_dir, "./backup/", sizeof(ctx->bak_dir) - 1);
        }
        if (chk_and_prep_bak_dir(ctx->bak_dir) != 0) {
            dbg((1 << 0), "%s.", "Backup directory can't be used");
            return 0;
        }
    }
    if (ctx->dbg_lvl == (0)) {
        dbg((1 << 2), "%s.", "Debug level is 0, no dbg will be displayed from now");
    }
    set_dbg_lvl(ctx->dbg_lvl);
    return 1;
}

canonical_original_code:
static _Bool permit_to_run(struct cheer_ctx *ctx)
{
    if (ctx->interval <= 1) {
        dbg((1 << 0), "%s.", "Upload interval too short");
        return 0;
    }
    if (strlen(ctx->sn) == 0) {
        dbg((1 << 0), "%s.", "Serial number should not be empty");
        return 0;
    }
    if (strlen(ctx->url) == 0) {
        dbg((1 << 0), "%s.", "Upload URL should not be empty");
        return 0;
    }
    if (ctx->need_bak) {
        if (strlen(ctx->bak_dir) == 0) {
            dbg((1 << 1), "Backup directory is not specified, default: %s.", "./backup/");
            strncpy(ctx->bak_dir, "./backup/", sizeof(ctx->bak_dir) - 1);
        }
        if (chk_and_prep_bak_dir(ctx->bak_dir) != 0) {
            dbg((1 << 0), "%s.", "Backup directory can't be used");
            return 0;
        }
    }
    if (ctx->dbg_lvl == (0)) {
        dbg((1 << 2), "%s.", "Debug level is 0, no dbg will be displayed from now");
    }
    set_dbg_lvl(ctx->dbg_lvl);
    return 1;
}