decompiled_code:
__int64 __fastcall density_lion_form_model_update(__int64 a1, __int64 a2, unsigned __int8 a3, __int64 a4, unsigned __int8 a5)
{
__int64 v5; // rdx
__int64 result; // rax

if ( a5 < a3 )
{
v5 = *(unsigned int *)(a2 + 8);
result = *(unsigned int *)(a4 + 8);
*(_QWORD *)(a1 + 8 * v5 + 200) = a4;
*(_DWORD *)(a4 + 8) = v5;
*(_DWORD *)(a2 + 8) = result;
*(_QWORD *)(a1 + 8 * result + 200) = a2;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(__int64 a1, __int64 a2, unsigned __int8 a3, __int64 a4, unsigned __int8 a5)
{
__int64 v5; // rdx
__int64 result; // rax

if ( a5 < a3 )
{
v5 = *(unsigned int *)(a2 + 8);
result = *(unsigned int *)(a4 + 8);
*(_QWORD *)(a1 + 8 * v5 + 200) = a4;
*(_DWORD *)(a4 + 8) = v5;
*(_DWORD *)(a2 + 8) = result;
*(_QWORD *)(a1 + 8 * result + 200) = a2;
}
return result;
}

original_code:
inline __attribute__((always_inline)) void density_lion_form_model_update(density_lion_form_data *const restrict data, density_lion_form_node *const restrict form, const uint8_t usage, density_lion_form_node *const restrict previous_form, const uint8_t previous_usage) {
    if (__builtin_expect(!!(previous_usage < usage), 0)) {
        const DENSITY_LION_FORM form_value = form->form;
        const DENSITY_LION_FORM previous_form_value = previous_form->form;
        previous_form->form = form_value;
        form->form = previous_form_value;
        data->formsIndex[form_value] = previous_form;
        data->formsIndex[previous_form_value] = form;
    }
}

canonical_original_code:
inline void density_lion_form_model_update(struct density_lion_form_data *data, struct density_lion_form_node *form, unsigned char usage, struct density_lion_form_node *previous_form, unsigned char previous_usage) {
    if (__builtin_expect(!!(previous_usage < usage), 0)) {
        enum DENSITY_LION_FORM form_value = form->form;
        enum DENSITY_LION_FORM previous_form_value = previous_form->form;
        previous_form->form = form_value;
        form->form = previous_form_value;
        data->formsIndex[form_value] = previous_form;
        data->formsIndex[previous_form_value] = form;
    }
}

variable_types:
form
struct density_lion_form_node *

variable_types:
usage
unsigned char

variable_types: enum DENSITY_LION_FORM { DENSITY_LION_FORM_PREDICTIONS_A=0, DENSITY_LION_FORM_PREDICTIONS_B=1, DENSITY_LION_FORM_PREDICTIONS_C=2, DENSITY_LION_FORM_DICTIONARY_A=3, DENSITY_LION_FORM_DICTIONARY_B=4, DENSITY_LION_FORM_DICTIONARY_C=5, DENSITY_LION_FORM_DICTIONARY_D=6, DENSITY_LION_FORM_PLAIN=7 }


repo: centaurean/density

original_code:
inline __attribute__((always_inline)) DENSITY_LION_FORM density_lion_form_model_increment_usage(density_lion_form_data *const data, density_lion_form_node *const restrict form) {
    const DENSITY_LION_FORM form_value = form->form;
    const uint8_t usage = ++data->usages.usages_as_uint8_t[form_value];
    density_lion_form_node *const previous_form = form->previousForm;
    if (previous_form)
        density_lion_form_model_update(data, form, usage, previous_form, data->usages.usages_as_uint8_t[previous_form->form]);
    else
        density_lion_form_model_flatten(data, usage);
    return form_value;
}

canonical_original_code:
inline enum DENSITY_LION_FORM density_lion_form_model_increment_usage(struct density_lion_form_data *data, struct density_lion_form_node *form) {
    enum DENSITY_LION_FORM form_value = form->form;
    unsigned char usage = ++data->usages.usages_as_uint8_t[form_value];
    struct density_lion_form_node *previous_form = form->previousForm;
    if (previous_form)
        density_lion_form_model_update(data, form, usage, previous_form, data->usages.usages_as_uint8_t[previous_form->form]);
    else
        density_lion_form_model_flatten(data, usage);
    return form_value;
}

variable_types: struct density_lion_form_data { union { unsigned char usages_as_uint8_t[8]; unsigned long usages_as_uint64_t; } usages; void (*attachments[8])(unsigned char **, unsigned char **, unsigned long *, void *, unsigned short *, unsigned int *); struct density_lion_form_node { void *previousForm; enum DENSITY_LION_FORM { DENSITY_LION_FORM_PREDICTIONS_A=0, DENSITY_LION_FORM_PREDICTIONS_B=1, DENSITY_LION_FORM_PREDICTIONS_C=2, DENSITY_LION_FORM_DICTIONARY_A=3, DENSITY_LION_FORM_DICTIONARY_B=4, DENSITY_LION_FORM_DICTIONARY_C=5, DENSITY_LION_FORM_DICTIONARY_D=6, DENSITY_LION_FORM_PLAIN=7 } form; unsigned char rank; } formsPool[8]; struct density_lion_form_node *formsIndex[8]; unsigned char nextAvailableForm; } *

variable_types: struct density_lion_form_node { void *previousForm; enum DENSITY_LION_FORM { DENSITY_LION_FORM_PREDICTIONS_A=0, DENSITY_LION_FORM_PREDICTIONS_B=1, DENSITY_LION_FORM_PREDICTIONS_C=2, DENSITY_LION_FORM_DICTIONARY_A=3, DENSITY_LION_FORM_DICTIONARY_B=4, DENSITY_LION_FORM_DICTIONARY_C=5, DENSITY_LION_FORM_DICTIONARY_D=6, DENSITY_LION_FORM_PLAIN=7 } form; unsigned char rank; } *

user_defined_types: struct density_lion_form_data { union { unsigned char usages_as_uint8_t[8]; unsigned long usages_as_uint64_t; } usages; void (*attachments[8])(unsigned char **, unsigned char **, unsigned long *, void *, unsigned short *, unsigned int *); struct density_lion_form_node formsPool[8]; struct density_lion_form_node *formsIndex[8]; unsigned char nextAvailableForm; }

user_defined_types: struct density_lion_form_node { void *previousForm; enum DENSITY_LION_FORM form; unsigned char rank; }


decompiled_code:
__int64 __fastcall density_lion_form_model_get_encoding(__int64 *a1, unsigned int a2)
{
__int64 v2; // rdx
__int64 v3; // rcx
char v4; // al
__int64 v5; // rsi
__int64 result; // rax
__int64 v7; // rax

v2 = a1[a2 + 25];
v3 = *(_QWORD *)v2;
v4 = *((_BYTE *)a1 + a2) + 1;
*((_BYTE *)a1 + a2) = v4;
if ( v3 )
{
v5 = *(unsigned int *)(v3 + 8);
if ( (unsigned __int8)v4 > *((_BYTE *)a1 + v5) )
{
v7 = *(unsigned int *)(v2 + 8);
*(_DWORD *)(v2 + 8) = v5;
a1[v7 + 25] = v3;
*(_DWORD *)(v3 + 8) = v7;
a1[v5 + 25] = v2;
}
result = density_lion_form_entropy_codes[*(unsigned __int8 *)(v2 + 12)];
}
else
{
if ( v4 < 0 )
*a1 = ((unsigned __int64)*a1 >> 1) & 0x7F7F7F7F7F7F7F7FLL;
result = 257LL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 *a1, unsigned int a2)
{
__int64 v2; // rdx
__int64 v3; // rcx
char v4; // al
__int64 v5; // rsi
__int64 result; // rax
__int64 v7; // rax

v2 = a1[a2 + 25];
v3 = *(_QWORD *)v2;
v4 = *((_BYTE *)a1 + a2) + 1;
*((_BYTE *)a1 + a2) = v4;
if ( v3 )
{
v5 = *(unsigned int *)(v3 + 8);
if ( (unsigned __int8)v4 > *((_BYTE *)a1 + v5) )
{
v7 = *(unsigned int *)(v2 + 8);
*(_DWORD *)(v2 + 8) = v5;
a1[v7 + 25] = v3;
*(_DWORD *)(v3 + 8) = v7;
a1[v5 + 25] = v2;
}
result = density_lion_form_entropy_codes[*(unsigned __int8 *)(v2 + 12)];
}
else
{
if ( v4 < 0 )
*a1 = ((unsigned __int64)*a1 >> 1) & 0x7F7F7F7F7F7F7F7FLL;
result = 257LL;
}
return result;
}

original_code:
inline __attribute__((always_inline)) density_lion_entropy_code density_lion_form_model_get_encoding(density_lion_form_data *const data, const DENSITY_LION_FORM form) {
    const uint8_t usage = ++data->usages.usages_as_uint8_t[form];
    density_lion_form_node *const form_found = data->formsIndex[form];
    density_lion_form_node *const previous_form = form_found->previousForm;
    if (previous_form) {
        density_lion_form_model_update(data, form_found, usage, previous_form, data->usages.usages_as_uint8_t[previous_form->form]);
        return density_lion_form_entropy_codes[form_found->rank];
    } else {
        density_lion_form_model_flatten(data, usage);
        return density_lion_form_entropy_codes[0];
    }
}

canonical_original_code:
inline struct density_lion_entropy_code density_lion_form_model_get_encoding(struct density_lion_form_data *data, enum DENSITY_LION_FORM form) {
    unsigned char usage = ++data->usages.usages_as_uint8_t[form];
    struct density_lion_form_node *form_found = data->formsIndex[form];
    struct density_lion_form_node *previous_form = form_found->previousForm;
    if (previous_form) {
        density_lion_form_model_update(data, form_found, usage, previous_form, data->usages.usages_as_uint8_t[previous_form->form]);
        return density_lion_form_entropy_codes[form_found->rank];
    } else {
        density_lion_form_model_flatten(data, usage);
        return density_lion_form_entropy_codes[0];
    }
}

return_type: struct density_lion_entropy_code { unsigned char value; unsigned char bitLength; }

user_defined_types: enum DENSITY_LION_FORM { DENSITY_LION_FORM_PREDICTIONS_A=0, DENSITY_LION_FORM_PREDICTIONS_B=1, DENSITY_LION_FORM_PREDICTIONS_C=2, DENSITY_LION_FORM_DICTIONARY_A=3, DENSITY_LION_FORM_DICTIONARY_B=4, DENSITY_LION_FORM_DICTIONARY_C=5, DENSITY_LION_FORM_DICTIONARY_D=6, DENSITY_LION_FORM_PLAIN=7 }


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

result = 256LL;
*(_QWORD *)a1 = a2;
*(_WORD *)(a1 + 8) = 256;
*(_BYTE *)(a1 + 10) = 0;
*(_QWORD *)(a1 + 16) = 0LL;
return result;
}

original_code:
inline __attribute__((always_inline)) void density_algorithms_prepare_state(density_algorithm_state *const restrict state, void *const restrict dictionary) {
    state->dictionary = dictionary;
    state->copy_penalty = 0;
    state->copy_penalty_start = 1;
    state->previous_incompressible = 0;
    state->counter = 0;
}

canonical_original_code:
inline void density_algorithms_prepare_state(struct density_algorithm_state *state, void *dictionary) {
    state->dictionary = dictionary;
    state->copy_penalty = 0;
    state->copy_penalty_start = 1;
    state->previous_incompressible = 0;
    state->counter = 0;
}

variable_types: struct density_algorithm_state { void *dictionary; unsigned char copy_penalty; unsigned char copy_penalty_start; _Bool previous_incompressible; unsigned long counter; } *

variable_types:
dictionary
void *

return_type: void


decompiled_code:
double __fastcall cputime_timeval_to_time(int a1, int a2)
{
return (double)a1 * 1000000.0 + (double)a2;
}

canonical_decompiled_code:
double __fastcall func0(int a1, int a2)
{
return (double)a1 * 1000000.0 + (double)a2;
}

original_code:
double cputime_timeval_to_time(struct timeval tv) {
    return tv.tv_sec * 1000000.0 + tv.tv_usec;
}

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

return_type: double


decompiled_code:
__time_t cputime_get_current_total_usertime()
{
struct rusage v1; // [rsp+0h] [rbp-98h] BYREF

getrusage(RUSAGE_SELF, &v1);
return v1.ru_utime.tv_sec;
}

canonical_decompiled_code:
__time_t func1()
{
struct rusage v1; // [rsp+0h] [rbp-98h] BYREF

getrusage(RUSAGE_SELF, &v1);
return v1.ru_utime.tv_sec;
}

original_code:
struct timeval cputime_get_current_total_usertime() {
    struct rusage usage;
    getrusage(RUSAGE_SELF, &usage);
    return usage.ru_utime;
}

variable_types: struct rusage { struct timeval { long tv_sec; long tv_usec; } ru_utime; struct timeval ru_stime; union { long ru_maxrss; long __ru_maxrss_word; }; union { long ru_ixrss; long __ru_ixrss_word; }; union { long ru_idrss; long __ru_idrss_word; }; union { long ru_isrss; long __ru_isrss_word; }; union { long ru_minflt; long __ru_minflt_word; }; union { long ru_majflt; long __ru_majflt_word; }; union { long ru_nswap; long __ru_nswap_word; }; union { long ru_inblock; long __ru_inblock_word; }; union { long ru_oublock; long __ru_oublock_word; }; union { long ru_msgsnd; long __ru_msgsnd_word; }; union { long ru_msgrcv; long __ru_msgrcv_word; }; union { long ru_nsignals; long __ru_nsignals_word; }; union { long ru_nvcsw; long __ru_nvcsw_word; }; union { long ru_nivcsw; long __ru_nivcsw_word; }; }

user_defined_types: struct rusage { struct timeval ru_utime; struct timeval ru_stime; union { long ru_maxrss; long __ru_maxrss_word; }; union { long ru_ixrss; long __ru_ixrss_word; }; union { long ru_idrss; long __ru_idrss_word; }; union { long ru_isrss; long __ru_isrss_word; }; union { long ru_minflt; long __ru_minflt_word; }; union { long ru_majflt; long __ru_majflt_word; }; union { long ru_nswap; long __ru_nswap_word; }; union { long ru_inblock; long __ru_inblock_word; }; union { long ru_oublock; long __ru_oublock_word; }; union { long ru_msgsnd; long __ru_msgsnd_word; }; union { long ru_msgrcv; long __ru_msgrcv_word; }; union { long ru_nsignals; long __ru_nsignals_word; }; union { long ru_nvcsw; long __ru_nvcsw_word; }; union { long ru_nivcsw; long __ru_nivcsw_word; }; }


decompiled_code:
__time_t __fastcall cputime_chronometer_start(__int64 a1)
{
__time_t result; // rax
__int64 v2; // rdx

*(_DWORD *)(a1 + 48) = 1;
result = cputime_get_current_total_usertime();
*(_QWORD *)a1 = result;
*(_QWORD *)(a1 + 8) = v2;
*(_QWORD *)(a1 + 16) = result;
*(_QWORD *)(a1 + 24) = v2;
return result;
}

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

*(_DWORD *)(a1 + 48) = 1;
result = func1();
*(_QWORD *)a1 = result;
*(_QWORD *)(a1 + 8) = v2;
*(_QWORD *)(a1 + 16) = result;
*(_QWORD *)(a1 + 24) = v2;
return result;
}

original_code:
void cputime_chronometer_start(cputime_chronometer *chrono) {
    chrono->state = CPUTIME_CHRONOMETER_STATE_RUNNING;
    chrono->start = cputime_get_current_total_usertime();
    chrono->lap = chrono->start;
}

canonical_original_code:
void cputime_chronometer_start(struct cputime_chronometer *chrono) {
    chrono->state = CPUTIME_CHRONOMETER_STATE_RUNNING;
    chrono->start = cputime_get_current_total_usertime();
    chrono->lap = chrono->start;
}

variable_types: struct cputime_chronometer { struct timeval { long tv_sec; long tv_usec; } start; struct timeval lap; struct timeval stop; enum CPUTIME_CHRONOMETER_STATE { CPUTIME_CHRONOMETER_STATE_IDLE=0, CPUTIME_CHRONOMETER_STATE_RUNNING=1 } state; } *

user_defined_types: struct cputime_chronometer { struct timeval start; struct timeval lap; struct timeval stop; enum CPUTIME_CHRONOMETER_STATE state; }


decompiled_code:
void __fastcall cputime_chronometer_lap(__int64 a1, __m128 a2)
{
int v2; // eax
int v3; // edx
__int64 v4; // r8
__int64 v5; // rax

if ( *(_DWORD *)(a1 + 48) == 1 )
{
v2 = cputime_get_current_total_usertime();
cputime_substract(v2, v3, a2);
*(_QWORD *)(a1 + 16) = v4;
*(_QWORD *)(a1 + 24) = v5;
}
}

original_code:
double cputime_chronometer_lap(cputime_chronometer *chrono) {
    if (chrono->state == CPUTIME_CHRONOMETER_STATE_RUNNING) {
        struct timeval new_lap = cputime_get_current_total_usertime();
        double elapsed = cputime_substract(new_lap, chrono->lap);
        chrono->lap = new_lap;
        return elapsed;
    } else
        return 0.0;
}

canonical_original_code:
double cputime_chronometer_lap(struct cputime_chronometer *chrono) {
    if (chrono->state == CPUTIME_CHRONOMETER_STATE_RUNNING) {
        struct timeval new_lap = cputime_get_current_total_usertime();
        double elapsed = cputime_substract(new_lap, chrono->lap);
        chrono->lap = new_lap;
        return elapsed;
    } else
        return 0.0;
}

variable_types: elapsed double

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

user_defined_types: enum CPUTIME_CHRONOMETER_STATE { CPUTIME_CHRONOMETER_STATE_IDLE=0, CPUTIME_CHRONOMETER_STATE_RUNNING=1 }


original_code:
inline __attribute__((always_inline)) density_algorithm_exit_status density_chameleon_encode(density_algorithm_state *const restrict state, const uint8_t **restrict in, const uint_fast64_t in_size, uint8_t **restrict out, const uint_fast64_t out_size) {
    if (out_size < (sizeof(density_chameleon_signature) + ((8 * sizeof(density_chameleon_signature)) * sizeof(uint32_t))))
        return DENSITY_ALGORITHMS_EXIT_STATUS_OUTPUT_STALL;
    density_chameleon_signature signature;
    density_chameleon_signature *signature_pointer;
    uint32_t unit;
    uint8_t *out_limit = *out + out_size - (sizeof(density_chameleon_signature) + ((8 * sizeof(density_chameleon_signature)) * sizeof(uint32_t)));
    uint_fast64_t limit_256 = (in_size >> 8);
    while (__builtin_expect(!!(limit_256-- && *out <= out_limit), 1)) {
        if (__builtin_expect(!!(!(state->counter & 0xf)), 0)) {
            if (state->copy_penalty_start & ~0x1) state->copy_penalty_start >>= 1;;
        }
        state->counter++;
        if (__builtin_expect(!!(state->copy_penalty), 0)) {
            __builtin_memcpy(*out, *in, 256); *in += 256; *out += 256;;
            if(!(--state->copy_penalty)) state->copy_penalty_start++;;
        } else {
            const uint8_t *out_start = *out;
            density_chameleon_encode_prepare_signature(out, &signature_pointer, &signature);
            __builtin_prefetch(*in + 256);
            density_chameleon_encode_256(in, out, &signature, (density_chameleon_dictionary *const) state->dictionary, &unit);
            __builtin_memcpy(signature_pointer, &signature, sizeof(density_chameleon_signature));
            if (__builtin_expect(!!((*out - out_start) & ~(256 - 1)), 0)) { if (state->previous_incompressible) state->copy_penalty = state->copy_penalty_start; state->previous_incompressible = 1; } else state->previous_incompressible = 0;;
        }
    }
    if (*out > out_limit)
        return DENSITY_ALGORITHMS_EXIT_STATUS_OUTPUT_STALL;
    uint_fast64_t remaining;
    switch (in_size & 0xff) {
        case 0:
        case 1:
        case 2:
        case 3:
            density_chameleon_encode_prepare_signature(out, &signature_pointer, &signature);
            signature = ((uint64_t) DENSITY_CHAMELEON_SIGNATURE_FLAG_CHUNK);
            __builtin_memcpy(signature_pointer, &signature, sizeof(density_chameleon_signature));
            goto process_remaining_bytes;
        default:
            break;
    }
    const uint_fast64_t limit_4 = (in_size & 0xff) >> 2;
    density_chameleon_encode_prepare_signature(out, &signature_pointer, &signature);
    for (uint_fast8_t shift = 0; shift != limit_4; shift++)
        density_chameleon_encode_4(in, out, shift, &signature, (density_chameleon_dictionary *const) state->dictionary, &unit);
    signature |= ((uint64_t) DENSITY_CHAMELEON_SIGNATURE_FLAG_CHUNK << limit_4);
    __builtin_memcpy(signature_pointer, &signature, sizeof(density_chameleon_signature));
    process_remaining_bytes:
    remaining = in_size & 0x3;
    if (remaining)
        __builtin_memcpy(*out, *in, remaining); *in += remaining; *out += remaining;;
    return DENSITY_ALGORITHMS_EXIT_STATUS_FINISHED;
}

canonical_original_code:
inline enum density_algorithm_exit_status density_chameleon_encode(struct density_algorithm_state *state, unsigned char **in, unsigned long in_size, unsigned char **out, unsigned long out_size) {
    if (out_size < (sizeof(density_chameleon_signature) + ((8 * sizeof(density_chameleon_signature)) * sizeof(unsigned int))))
        return DENSITY_ALGORITHMS_EXIT_STATUS_OUTPUT_STALL;
    unsigned long signature;
    unsigned long *signature_pointer;
    unsigned int unit;
    unsigned char *out_limit = *out + out_size - (sizeof(density_chameleon_signature) + ((8 * sizeof(density_chameleon_signature)) * sizeof(uint32_t)));
    unsigned long limit_256 = (in_size >> 8);
    while (__builtin_expect(!!(limit_256-- && *out <= out_limit), 1)) {
        if (__builtin_expect(!!(!(state->counter & 0xf)), 0)) {
            if (state->copy_penalty_start & ~0x1) state->copy_penalty_start >>= 1;;
        }
        state->counter++;
        if (__builtin_expect(!!(state->copy_penalty), 0)) {
            __builtin_memcpy(*out, *in, 256); *in += 256; *out += 256;;
            if(!(--state->copy_penalty)) state->copy_penalty_start++;;
        } else {
            unsigned char *out_start = *out;
            density_chameleon_encode_prepare_signature(out, &signature_pointer, &signature);
            __builtin_prefetch(*in + 256);
            density_chameleon_encode_256(in, out, &signature, (struct density_chameleon_dictionary *) state->dictionary, &unit);
            __builtin_memcpy(signature_pointer, &signature, sizeof(density_chameleon_signature));
            if (__builtin_expect(!!((*out - out_start) & ~(256 - 1)), 0)) { if (state->previous_incompressible) state->copy_penalty = state->copy_penalty_start; state->previous_incompressible = 1; } else state->previous_incompressible = 0;;
        }
    }
    if (*out > out_limit)
        return DENSITY_ALGORITHMS_EXIT_STATUS_OUTPUT_STALL;
    unsigned long remaining;
    switch (in_size & 0xff) {
        case 0:
        case 1:
        case 2:
        case 3:
            density_chameleon_encode_prepare_signature(out, &signature_pointer, &signature);
            signature = ((unsigned long) DENSITY_CHAMELEON_SIGNATURE_FLAG_CHUNK);
            __builtin_memcpy(signature_pointer, &signature, sizeof(density_chameleon_signature));
            goto process_remaining_bytes;
        default:
            break;
    }
    unsigned long limit_4 = (in_size & 0xff) >> 2;
    density_chameleon_encode_prepare_signature(out, &signature_pointer, &signature);
    for (unsigned char shift = 0; shift != limit_4; shift++)
        density_chameleon_encode_4(in, out, shift, &signature, (struct density_chameleon_dictionary *) state->dictionary, &unit);
    signature |= ((unsigned long) DENSITY_CHAMELEON_SIGNATURE_FLAG_CHUNK << limit_4);
    __builtin_memcpy(signature_pointer, &signature, sizeof(density_chameleon_signature));
    process_remaining_bytes:
    remaining = in_size & 0x3;
    if (remaining)
        __builtin_memcpy(*out, *in, remaining); *in += remaining; *out += remaining;;
    return DENSITY_ALGORITHMS_EXIT_STATUS_FINISHED;
}

variable_types:
in
unsigned char **

variable_types:
out
unsigned char **

variable_types:
signature
unsigned long

variable_types:
unit
unsigned int

variable_types:
remaining
unsigned long

variable_types:
shift
unsigned char

return_type: enum density_algorithm_exit_status { DENSITY_ALGORITHMS_EXIT_STATUS_FINISHED=0, DENSITY_ALGORITHMS_EXIT_STATUS_ERROR_DURING_PROCESSING=1, DENSITY_ALGORITHMS_EXIT_STATUS_INPUT_STALL=2, DENSITY_ALGORITHMS_EXIT_STATUS_OUTPUT_STALL=3 }

user_defined_types: struct density_algorithm_state { void *dictionary; unsigned char copy_penalty; unsigned char copy_penalty_start; _Bool previous_incompressible; unsigned long counter; }

user_defined_types: struct density_chameleon_dictionary { struct density_chameleon_dictionary_entry entries[-1]; }

user_defined_types: struct density_chameleon_dictionary_entry { unsigned int as_uint32_t; }


decompiled_code:
__int64 density_version_major()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 0LL;
}

original_code:
uint8_t density_version_major() {
    return 0;
}

canonical_original_code:
unsigned char density_version_major() {
    return 0;
}

return_type: unsigned char


decompiled_code:
__int64 density_version_minor()
{
return 14LL;
}

canonical_decompiled_code:
__int64 func1()
{
return 14LL;
}

original_code:
uint8_t density_version_minor() {
    return 14;
}

canonical_original_code:
unsigned char density_version_minor() {
    return 14;
}


decompiled_code:
__int64 density_version_revision()
{
return 2LL;
}

canonical_decompiled_code:
__int64 func2()
{
return 2LL;
}

original_code:
uint8_t density_version_revision() {
    return 2;
}

canonical_original_code:
unsigned char density_version_revision() {
    return 2;
}


decompiled_code:
__int64 spookyhash_version_major()
{
return 1LL;
}

canonical_decompiled_code:
__int64 func0()
{
return 1LL;
}

original_code:
uint8_t spookyhash_version_major() {
    return 1;
}

canonical_original_code:
unsigned char spookyhash_version_major() {
    return 1;
}


decompiled_code:
__int64 spookyhash_version_minor()
{
return 0LL;
}

canonical_decompiled_code:
__int64 func1()
{
return 0LL;
}

original_code:
uint8_t spookyhash_version_minor() {
    return 0;
}

canonical_original_code:
unsigned char spookyhash_version_minor() {
    return 0;
}


decompiled_code:
__int64 spookyhash_version_revision()
{
return 6LL;
}

canonical_decompiled_code:
__int64 func2()
{
return 6LL;
}

original_code:
uint8_t spookyhash_version_revision() {
    return 6;
}

canonical_original_code:
unsigned char spookyhash_version_revision() {
    return 6;
}


original_code:
inline __attribute__((always_inline)) density_algorithm_exit_status density_cheetah_encode(density_algorithm_state *const restrict state, const uint8_t **restrict in, const uint_fast64_t in_size, uint8_t **restrict out, const uint_fast64_t out_size) {
    if (out_size < (sizeof(density_cheetah_signature) + (((8 * sizeof(density_cheetah_signature)) >> 1) * sizeof(uint32_t))))
        return DENSITY_ALGORITHMS_EXIT_STATUS_OUTPUT_STALL;
    density_cheetah_signature signature;
    density_cheetah_signature *signature_pointer;
    uint_fast16_t last_hash = 0;
    uint32_t unit;
    uint8_t *out_limit = *out + out_size - (sizeof(density_cheetah_signature) + (((8 * sizeof(density_cheetah_signature)) >> 1) * sizeof(uint32_t)));
    uint_fast64_t limit_128 = (in_size >> 7);
    while (__builtin_expect(!!(limit_128-- && *out <= out_limit), 1)) {
        if (__builtin_expect(!!(!(state->counter & 0x1f)), 0)) {
            if (state->copy_penalty_start & ~0x1) state->copy_penalty_start >>= 1;;
        }
        state->counter++;
        if (__builtin_expect(!!(state->copy_penalty), 0)) {
            __builtin_memcpy(*out, *in, 128); *in += 128; *out += 128;;
            if(!(--state->copy_penalty)) state->copy_penalty_start++;;
        } else {
            const uint8_t *out_start = *out;
            density_cheetah_encode_prepare_signature(out, &signature_pointer, &signature);
            __builtin_prefetch(*in + 128);
            density_cheetah_encode_128(in, out, &last_hash, &signature, (density_cheetah_dictionary *const) state->dictionary, &unit);
            __builtin_memcpy(signature_pointer, &signature, sizeof(density_cheetah_signature));
            if (__builtin_expect(!!((*out - out_start) & ~(128 - 1)), 0)) { if (state->previous_incompressible) state->copy_penalty = state->copy_penalty_start; state->previous_incompressible = 1; } else state->previous_incompressible = 0;;
        }
    }
    if (*out > out_limit)
        return DENSITY_ALGORITHMS_EXIT_STATUS_OUTPUT_STALL;
    uint_fast64_t remaining;
    switch (in_size & 0x7f) {
        case 0:
        case 1:
        case 2:
        case 3:
            density_cheetah_encode_prepare_signature(out, &signature_pointer, &signature);
            signature = (uint64_t) DENSITY_CHEETAH_SIGNATURE_FLAG_CHUNK;
            __builtin_memcpy(signature_pointer, &signature, sizeof(density_cheetah_signature));
            goto process_remaining_bytes;
        default:
            break;
    }
    const uint_fast64_t limit_4 = ((in_size & 0x7f) >> 2) << 1;
    density_cheetah_encode_prepare_signature(out, &signature_pointer, &signature);
    for (uint_fast8_t shift = 0; shift != limit_4; shift += 2)
        density_cheetah_encode_4(in, out, &last_hash, shift, &signature, (density_cheetah_dictionary *const) state->dictionary, &unit);
    signature |= ((uint64_t) DENSITY_CHEETAH_SIGNATURE_FLAG_CHUNK << limit_4);
    __builtin_memcpy(signature_pointer, &signature, sizeof(density_cheetah_signature));
    process_remaining_bytes:
    remaining = in_size & 0x3;
    if (remaining)
    __builtin_memcpy(*out, *in, remaining); *in += remaining; *out += remaining;;
    return DENSITY_ALGORITHMS_EXIT_STATUS_FINISHED;
}

canonical_original_code:
inline enum density_algorithm_exit_status density_cheetah_encode(struct density_algorithm_state *state, unsigned char **in, unsigned long in_size, unsigned char **out, unsigned long out_size) {
    if (out_size < (sizeof(density_cheetah_signature) + (((8 * sizeof(density_cheetah_signature)) >> 1) * sizeof(unsigned int))))
        return DENSITY_ALGORITHMS_EXIT_STATUS_OUTPUT_STALL;
    unsigned long signature;
    unsigned long *signature_pointer;
    unsigned long last_hash = 0;
    unsigned int unit;
    unsigned char *out_limit = *out + out_size - (sizeof(density_cheetah_signature) + (((8 * sizeof(density_cheetah_signature)) >> 1) * sizeof(uint32_t)));
    unsigned long limit_128 = (in_size >> 7);
    while (__builtin_expect(!!(limit_128-- && *out <= out_limit), 1)) {
        if (__builtin_expect(!!(!(state->counter & 0x1f)), 0)) {
            if (state->copy_penalty_start & ~0x1) state->copy_penalty_start >>= 1;;
        }
        state->counter++;
        if (__builtin_expect(!!(state->copy_penalty), 0)) {
            __builtin_memcpy(*out, *in, 128); *in += 128; *out += 128;;
            if(!(--state->copy_penalty)) state->copy_penalty_start++;;
        } else {
            unsigned char *out_start = *out;
            density_cheetah_encode_prepare_signature(out, &signature_pointer, &signature);
            __builtin_prefetch(*in + 128);
            density_cheetah_encode_128(in, out, &last_hash, &signature, (struct density_cheetah_dictionary *) state->dictionary, &unit);
            __builtin_memcpy(signature_pointer, &signature, sizeof(density_cheetah_signature));
            if (__builtin_expect(!!((*out - out_start) & ~(128 - 1)), 0)) { if (state->previous_incompressible) state->copy_penalty = state->copy_penalty_start; state->previous_incompressible = 1; } else state->previous_incompressible = 0;;
        }
    }
    if (*out > out_limit)
        return DENSITY_ALGORITHMS_EXIT_STATUS_OUTPUT_STALL;
    unsigned long remaining;
    switch (in_size & 0x7f) {
        case 0:
        case 1:
        case 2:
        case 3:
            density_cheetah_encode_prepare_signature(out, &signature_pointer, &signature);
            signature = (unsigned long) DENSITY_CHEETAH_SIGNATURE_FLAG_CHUNK;
            __builtin_memcpy(signature_pointer, &signature, sizeof(density_cheetah_signature));
            goto process_remaining_bytes;
        default:
            break;
    }
    unsigned long limit_4 = ((in_size & 0x7f) >> 2) << 1;
    density_cheetah_encode_prepare_signature(out, &signature_pointer, &signature);
    for (unsigned char shift = 0; shift != limit_4; shift += 2)
        density_cheetah_encode_4(in, out, &last_hash, shift, &signature, (struct density_cheetah_dictionary *) state->dictionary, &unit);
    signature |= ((unsigned long) DENSITY_CHEETAH_SIGNATURE_FLAG_CHUNK << limit_4);
    __builtin_memcpy(signature_pointer, &signature, sizeof(density_cheetah_signature));
    process_remaining_bytes:
    remaining = in_size & 0x3;
    if (remaining)
    __builtin_memcpy(*out, *in, remaining); *in += remaining; *out += remaining;;
    return DENSITY_ALGORITHMS_EXIT_STATUS_FINISHED;
}

user_defined_types: enum density_algorithm_exit_status { DENSITY_ALGORITHMS_EXIT_STATUS_FINISHED=0, DENSITY_ALGORITHMS_EXIT_STATUS_ERROR_DURING_PROCESSING=1, DENSITY_ALGORITHMS_EXIT_STATUS_INPUT_STALL=2, DENSITY_ALGORITHMS_EXIT_STATUS_OUTPUT_STALL=3 }

user_defined_types: struct density_cheetah_dictionary { struct density_cheetah_dictionary_entry entries[-1]; struct density_cheetah_dictionary_prediction_entry prediction_entries[-1]; }

user_defined_types: struct density_cheetah_dictionary_entry { unsigned int chunk_a; unsigned int chunk_b; }

user_defined_types: struct density_cheetah_dictionary_prediction_entry { unsigned int next_chunk_prediction; }


original_code:
inline __attribute__((always_inline)) void spookyhash_context_init(spookyhash_context *context, uint64_t seed1, uint64_t seed2) {
    context->m_length = 0;
    context->m_remainder = 0;
    context->m_state[0] = seed1;
    context->m_state[1] = seed2;
}

canonical_original_code:
inline void spookyhash_context_init(struct spookyhash_context *context, unsigned long seed1, unsigned long seed2) {
    context->m_length = 0;
    context->m_remainder = 0;
    context->m_state[0] = seed1;
    context->m_state[1] = seed2;
}

variable_types: struct spookyhash_context { unsigned long m_data[-1]; unsigned long m_state[-1]; unsigned long m_length; unsigned char m_remainder; } *


original_code:
inline __attribute__((always_inline)) density_algorithm_exit_status density_chameleon_decode(density_algorithm_state *const restrict state, const uint8_t **restrict in, const uint_fast64_t in_size, uint8_t **restrict out, const uint_fast64_t out_size) {
    if (out_size < (((8 * sizeof(density_chameleon_signature)) * sizeof(uint32_t))))
        return DENSITY_ALGORITHMS_EXIT_STATUS_OUTPUT_STALL;
    density_chameleon_signature signature;
    uint_fast8_t shift;
    uint_fast64_t remaining;
    const uint8_t *start = *in;
    if (in_size < (sizeof(density_chameleon_signature) + ((8 * sizeof(density_chameleon_signature)) * sizeof(uint32_t)))) {
        goto read_signature;
    }
    const uint8_t *in_limit = *in + in_size - (sizeof(density_chameleon_signature) + ((8 * sizeof(density_chameleon_signature)) * sizeof(uint32_t)));
    uint8_t *out_limit = *out + out_size - (((8 * sizeof(density_chameleon_signature)) * sizeof(uint32_t)));
    while (__builtin_expect(!!(*in <= in_limit && *out <= out_limit), 1)) {
        if (__builtin_expect(!!(!(state->counter & 0xf)), 0)) {
            if (state->copy_penalty_start & ~0x1) state->copy_penalty_start >>= 1;;
        }
        state->counter++;
        if (__builtin_expect(!!(state->copy_penalty), 0)) {
            __builtin_memcpy(*out, *in, 256); *in += 256; *out += 256;;
            if(!(--state->copy_penalty)) state->copy_penalty_start++;;
        } else {
            const uint8_t *in_start = *in;
            density_chameleon_decode_read_signature(in, &signature);
            density_chameleon_decode_256(in, out, signature, (density_chameleon_dictionary *const) state->dictionary);
            if (__builtin_expect(!!((*in - in_start) & ~(256 - 1)), 0)) { if (state->previous_incompressible) state->copy_penalty = state->copy_penalty_start; state->previous_incompressible = 1; } else state->previous_incompressible = 0;;
        }
    }
    if (*out > out_limit)
        return DENSITY_ALGORITHMS_EXIT_STATUS_OUTPUT_STALL;
    read_signature:
    if (in_size - (*in - start) < sizeof(density_chameleon_signature))
        return DENSITY_ALGORITHMS_EXIT_STATUS_INPUT_STALL;
    shift = 0;
    density_chameleon_decode_read_signature(in, &signature);
    read_and_decode_4:
    switch (in_size - (*in - start)) {
        case 0:
        case 1:
            if (density_chameleon_decode_test_compressed(signature, shift))
                return DENSITY_ALGORITHMS_EXIT_STATUS_ERROR_DURING_PROCESSING;
            else
                goto process_remaining_bytes;
        case 2:
        case 3:
            if (density_chameleon_decode_test_compressed(signature, shift++))
                density_chameleon_decode_kernel(in, out, 1, (density_chameleon_dictionary *const) state->dictionary);
            else
                goto process_remaining_bytes;
            break;
        default:
            density_chameleon_decode_4(in, out, signature, shift++, (density_chameleon_dictionary *const) state->dictionary);
            break;
    }
    if (__builtin_expect(!!(shift == (8 * sizeof(density_chameleon_signature))), 0))
        goto read_signature;
    else
        goto read_and_decode_4;
    process_remaining_bytes:
    remaining = in_size - (*in - start);
    __builtin_memcpy(*out, *in, remaining); *in += remaining; *out += remaining;;
    return DENSITY_ALGORITHMS_EXIT_STATUS_FINISHED;
}

canonical_original_code:
inline enum density_algorithm_exit_status density_chameleon_decode(struct density_algorithm_state *state, unsigned char **in, unsigned long in_size, unsigned char **out, unsigned long out_size) {
    if (out_size < (((8 * sizeof(density_chameleon_signature)) * sizeof(unsigned int))))
        return DENSITY_ALGORITHMS_EXIT_STATUS_OUTPUT_STALL;
    unsigned long signature;
    unsigned char shift;
    unsigned long remaining;
    unsigned char *start = *in;
    if (in_size < (sizeof(density_chameleon_signature) + ((8 * sizeof(density_chameleon_signature)) * sizeof(unsigned int)))) {
        goto read_signature;
    }
    unsigned char *in_limit = *in + in_size - (sizeof(density_chameleon_signature) + ((8 * sizeof(density_chameleon_signature)) * sizeof(uint32_t)));
    unsigned char *out_limit = *out + out_size - (((8 * sizeof(density_chameleon_signature)) * sizeof(uint32_t)));
    while (__builtin_expect(!!(*in <= in_limit && *out <= out_limit), 1)) {
        if (__builtin_expect(!!(!(state->counter & 0xf)), 0)) {
            if (state->copy_penalty_start & ~0x1) state->copy_penalty_start >>= 1;;
        }
        state->counter++;
        if (__builtin_expect(!!(state->copy_penalty), 0)) {
            __builtin_memcpy(*out, *in, 256); *in += 256; *out += 256;;
            if(!(--state->copy_penalty)) state->copy_penalty_start++;;
        } else {
            unsigned char *in_start = *in;
            density_chameleon_decode_read_signature(in, &signature);
            density_chameleon_decode_256(in, out, signature, (struct density_chameleon_dictionary *) state->dictionary);
            if (__builtin_expect(!!((*in - in_start) & ~(256 - 1)), 0)) { if (state->previous_incompressible) state->copy_penalty = state->copy_penalty_start; state->previous_incompressible = 1; } else state->previous_incompressible = 0;;
        }
    }
    if (*out > out_limit)
        return DENSITY_ALGORITHMS_EXIT_STATUS_OUTPUT_STALL;
    read_signature:
    if (in_size - (*in - start) < sizeof(density_chameleon_signature))
        return DENSITY_ALGORITHMS_EXIT_STATUS_INPUT_STALL;
    shift = 0;
    density_chameleon_decode_read_signature(in, &signature);
    read_and_decode_4:
    switch (in_size - (*in - start)) {
        case 0:
        case 1:
            if (density_chameleon_decode_test_compressed(signature, shift))
                return DENSITY_ALGORITHMS_EXIT_STATUS_ERROR_DURING_PROCESSING;
            else
                goto process_remaining_bytes;
        case 2:
        case 3:
            if (density_chameleon_decode_test_compressed(signature, shift++))
                density_chameleon_decode_kernel(in, out, 1, (struct density_chameleon_dictionary *) state->dictionary);
            else
                goto process_remaining_bytes;
            break;
        default:
            density_chameleon_decode_4(in, out, signature, shift++, (struct density_chameleon_dictionary *) state->dictionary);
            break;
    }
    if (__builtin_expect(!!(shift == (8 * sizeof(density_chameleon_signature))), 0))
        goto read_signature;
    else
        goto read_and_decode_4;
    process_remaining_bytes:
    remaining = in_size - (*in - start);
    __builtin_memcpy(*out, *in, remaining); *in += remaining; *out += remaining;;
    return DENSITY_ALGORITHMS_EXIT_STATUS_FINISHED;
}

variable_types:
start
unsigned char *


decompiled_code:
__int64 __fastcall density_get_dictionary_size(int a1)
{
__int64 v1; // rdi
__int64 result; // rax

v1 = (unsigned int)(a1 - 1);
result = 0LL;
if ( (unsigned int)v1 <= 2 )
result = CSWTCH_1[v1];
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(int a1)
{
__int64 v1; // rdi
__int64 result; // rax

v1 = (unsigned int)(a1 - 1);
result = 0LL;
if ( (unsigned int)v1 <= 2 )
result = CSWTCH_1[v1];
return result;
}

original_code:
size_t density_get_dictionary_size(DENSITY_ALGORITHM algorithm) {
    switch(algorithm) {
        case DENSITY_ALGORITHM_CHAMELEON:
            return sizeof(density_chameleon_dictionary);
        case DENSITY_ALGORITHM_CHEETAH:
            return sizeof(density_cheetah_dictionary);
        case DENSITY_ALGORITHM_LION:
            return sizeof(density_lion_dictionary);
        default:
            return 0;
    }
}

canonical_original_code:
unsigned long density_get_dictionary_size(enum DENSITY_ALGORITHM algorithm) {
    switch(algorithm) {
        case DENSITY_ALGORITHM_CHAMELEON:
            return sizeof(density_chameleon_dictionary);
        case DENSITY_ALGORITHM_CHEETAH:
            return sizeof(density_cheetah_dictionary);
        case DENSITY_ALGORITHM_LION:
            return sizeof(density_lion_dictionary);
        default:
            return 0;
    }
}

variable_types: enum DENSITY_ALGORITHM { DENSITY_ALGORITHM_CHAMELEON=1, DENSITY_ALGORITHM_CHEETAH=2, DENSITY_ALGORITHM_LION=3 }

return_type: unsigned long


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
__int64 v3; // rbp
const char *v4; // rax
const char *v5; // rax
const char *v6; // rax
const char *v7; // rax

v3 = newDictionary(argc, argv, envp);
puts("Dictionary initalized.");
setContents(v3, "Test", "Other test");
setContents(v3, "Other Test", "Test");
setContents(v3, "My name", "centip3de");
setContents(v3, "Your name", "Not centip3de");
v4 = (const char *)getContents(v3, "Test");
printf("Output: %s\n", v4);
v5 = (const char *)getContents(v3, "Other Test");
printf("Output: %s\n", v5);
v6 = (const char *)getContents(v3, "My name");
printf("Output: %s\n", v6);
v7 = (const char *)getContents(v3, "Your name");
printf("Output: %s\n", v7);
freeDictionary(v3);
return 0;
}

original_code:
int main(int argc, char** argv) {
    Dictionary * dictionary = newDictionary();
    printf("Dictionary initalized.\n");
    setContents(dictionary, "Test", "Other test");
    setContents(dictionary, "Other Test", "Test");
    setContents(dictionary, "My name", "centip3de");
    setContents(dictionary, "Your name", "Not centip3de");
    printf("Output: %s\n", getContents(dictionary, "Test"));
    printf("Output: %s\n", getContents(dictionary, "Other Test"));
    printf("Output: %s\n", getContents(dictionary, "My name"));
    printf("Output: %s\n", getContents(dictionary, "Your name"));
    freeDictionary(dictionary);
    return 0;
}

canonical_original_code:
int main(int argc, char **argv) {
    struct Dictionary *dictionary = newDictionary();
    printf("Dictionary initalized.\n");
    setContents(dictionary, "Test", "Other test");
    setContents(dictionary, "Other Test", "Test");
    setContents(dictionary, "My name", "centip3de");
    setContents(dictionary, "Your name", "Not centip3de");
    printf("Output: %s\n", getContents(dictionary, "Test"));
    printf("Output: %s\n", getContents(dictionary, "Other Test"));
    printf("Output: %s\n", getContents(dictionary, "My name"));
    printf("Output: %s\n", getContents(dictionary, "Your name"));
    freeDictionary(dictionary);
    return 0;
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct Dictionary { char **contents; int currentSize; int totalSize; } *

return_type: int


getHash

decompiled_code:
__int64 __fastcall getHash(_BYTE *a1)
{
int v1; // eax
unsigned int i; // er8

v1 = (char)*a1;
for ( i = 0; *a1; v1 = (char)*a1 )
{
++a1;
i += v1;
}
return i;
}

canonical_decompiled_code:
__int64 __fastcall func0(_BYTE *a1)
{
int v1; // eax
unsigned int i; // er8

v1 = (char)*a1;
for ( i = 0; *a1; v1 = (char)*a1 )
{
++a1;
i += v1;
}
return i;
}

original_code:
int getHash(char * key) {
    int sum = 0;
    while(*key) {
        sum += *key;
        key++;
    }
    return sum;
}

canonical_original_code:
int getHash(char *key) {
    int sum = 0;
    while(*key) {
        sum += *key;
        key++;
    }
    return sum;
}

variable_types:
key
char *

variable_types: sum int


newDictionary

decompiled_code:
_DWORD *newDictionary()
{
_DWORD *v0; // r12
void *v1; // rax

v0 = malloc(0x10uLL);
v1 = malloc(0x800uLL);
*(_QWORD *)v0 = v1;
if ( !v1 )
printf("[ERROR]: Couldn't initialize dictionary.");
v0[3] = 256;
return v0;
}

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

v0 = malloc(0x10uLL);
v1 = malloc(0x800uLL);
*(_QWORD *)v0 = v1;
if ( !v1 )
printf("[ERROR]: Couldn't initialize dictionary.");
v0[3] = 256;
return v0;
}

original_code:
Dictionary * newDictionary() {
    int initialSize = 256;
    Dictionary * targetDict = (Dictionary*)malloc(sizeof(Dictionary));
    targetDict->contents = (char **)malloc(sizeof(char **) * initialSize);
    if(targetDict->contents == ((void *)0)) {
        printf("[ERROR]: Couldn't initialize dictionary.");
    }
    targetDict->totalSize = initialSize;
    return targetDict;
}

canonical_original_code:
struct Dictionary *newDictionary() {
    int initialSize = 256;
    struct Dictionary *targetDict = (Dictionary*)malloc(sizeof(Dictionary));
    targetDict->contents = (char **)malloc(sizeof(char **) * initialSize);
    if(targetDict->contents == ((void *)0)) {
        printf("[ERROR]: Couldn't initialize dictionary.");
    }
    targetDict->totalSize = initialSize;
    return targetDict;
}

variable_types: initialSize int

return_type: struct Dictionary { char **contents; int currentSize; int totalSize; } *


getContents

decompiled_code:
__int64 __fastcall getContents(__int64 a1, _BYTE *a2)
{
int v2; // eax
__int64 v3; // rcx

v2 = getHash(a2);
return *(_QWORD *)(*(_QWORD *)v3 + 8LL * (v2 % *(_DWORD *)(v3 + 12)));
}

original_code:
char * getContents(Dictionary * targetDict, char * key) {
    int hash = getHash(key);
    int index = hash % targetDict->totalSize;
    return targetDict->contents[index];
}

canonical_original_code:
char *getContents(struct Dictionary *targetDict, char *key) {
    int hash = getHash(key);
    int index = hash % targetDict->totalSize;
    return targetDict->contents[index];
}

variable_types: hash int

variable_types: index int

return_type: char *

user_defined_types: struct Dictionary { char **contents; int currentSize; int totalSize; }


setContents

decompiled_code:
const void *__fastcall setContents(__int64 a1, _BYTE *a2, __int64 a3)
{
int v4; // ebp
int v5; // edx
const void *result; // rax
const void *v7; // r15
void *v8; // rax

v4 = *(_DWORD *)(a1 + 8);
if ( v4 == *(_DWORD *)(a1 + 12) )
{
v7 = *(const void **)a1;
v8 = malloc(16LL * v4);
*(_QWORD *)a1 = v8;
memcpy(v8, v7, 8LL * v4);
*(_DWORD *)(a1 + 12) = 2 * v4;
}
v5 = (int)getHash(a2) % *(_DWORD *)(a1 + 12);
result = *(const void **)a1;
*(_QWORD *)(*(_QWORD *)a1 + 8LL * v5) = a3;
*(_DWORD *)(a1 + 8) = v4 + 1;
return result;
}

canonical_decompiled_code:
const void *__fastcall func3(__int64 a1, _BYTE *a2, __int64 a3)
{
int v4; // ebp
int v5; // edx
const void *result; // rax
const void *v7; // r15
void *v8; // rax

v4 = *(_DWORD *)(a1 + 8);
if ( v4 == *(_DWORD *)(a1 + 12) )
{
v7 = *(const void **)a1;
v8 = malloc(16LL * v4);
*(_QWORD *)a1 = v8;
memcpy(v8, v7, 8LL * v4);
*(_DWORD *)(a1 + 12) = 2 * v4;
}
v5 = (int)func0(a2) % *(_DWORD *)(a1 + 12);
result = *(const void **)a1;
*(_QWORD *)(*(_QWORD *)a1 + 8LL * v5) = a3;
*(_DWORD *)(a1 + 8) = v4 + 1;
return result;
}

original_code:
void setContents(Dictionary * targetDict, char * key, char * value) {
    if(targetDict->currentSize == targetDict->totalSize) {
        char ** currentContents = targetDict->contents;
        targetDict->contents = (char **)malloc(sizeof(char ** ) * targetDict->totalSize * 2);
        memcpy(targetDict->contents, currentContents, sizeof(char *)*targetDict->totalSize);
        targetDict->totalSize = targetDict->totalSize * 2;
    }
    int hash = getHash(key);
    int index = hash % targetDict->totalSize;
    targetDict->contents[index] = (char *)malloc(sizeof(char) * strlen(value));
    targetDict->contents[index] = value;
    targetDict->currentSize++;
}

canonical_original_code:
void setContents(struct Dictionary *targetDict, char *key, char *value) {
    if(targetDict->currentSize == targetDict->totalSize) {
        char **currentContents = targetDict->contents;
        targetDict->contents = (char **)malloc(sizeof(char ** ) * targetDict->totalSize * 2);
        memcpy(targetDict->contents, currentContents, sizeof(char *)*targetDict->totalSize);
        targetDict->totalSize = targetDict->totalSize * 2;
    }
    int hash = getHash(key);
    int index = hash % targetDict->totalSize;
    targetDict->contents[index] = (char *)malloc(sizeof(char) * strlen(value));
    targetDict->contents[index] = value;
    targetDict->currentSize++;
}

variable_types:
value
char *

variable_types:
currentContents
char **


freeDictionary

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

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

original_code:
void freeDictionary(Dictionary * targetDict) {
    free(targetDict->contents);
}

canonical_original_code:
void freeDictionary(struct Dictionary *targetDict) {
    free(targetDict->contents);
}


main

repo: certik/shootout-n-body

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
unsigned int v4; // er12

v4 = atoi(argv[1]);
offset_momentum(5LL, &bodies);
((void (*)(void))energy)();
printf("%.9f\n", v3);
advance(5LL, &bodies, v4, 0.01);
energy(5LL, &bodies);
printf("%.9f\n", 0.01);
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
double v3; // xmm0_8
unsigned int v4; // er12

v4 = atoi(argv[1]);
func9(5LL, &bodies);
((void (*)(void))energy)();
printf("%.9f\n", v3);
func7(5LL, &bodies, v4, 0.01);
func8(5LL, &bodies);
printf("%.9f\n", 0.01);
return 0;
}

original_code:
int main(int argc, char **argv)
{
   int n = atoi(argv[1]);
   offset_momentum(sizeof(bodies)/sizeof(planet), bodies);
   printf("%.9f\n", energy(sizeof(bodies)/sizeof(planet), bodies));
   advance(sizeof(bodies)/sizeof(planet), bodies, 0.01, n);
   printf("%.9f\n", energy(sizeof(bodies)/sizeof(planet), bodies));
   return 0;
}

variable_types: n int


advance

canonical_original_code:
void advance(int nbodies, struct planet *bodies, double dt, int steps)
{
   int i; int j;
   register struct planet *a; register struct planet *b;
   double d[3]; double d2; double mag;
   while (steps--) {
      for (a = bodies, i = 0; i < nbodies; a++, i++) {
         for (b = a + 1, j = i + 1; j < nbodies; b++, j++) {
            d[0] = a->x[0] - b->x[0];
            d[1] = a->x[1] - b->x[1];
            d[2] = a->x[2] - b->x[2];
            d2 = d[0] * d[0] + d[1] * d[1] + d[2] * d[2];
            mag = dt / (d2 * sqrt(d2));
            a->v[0] -= d[0] * b->mass * mag;
            a->v[1] -= d[1] * b->mass * mag;
            a->v[2] -= d[2] * b->mass * mag;
            b->v[0] += d[0] * a->mass * mag;
            b->v[1] += d[1] * a->mass * mag;
            b->v[2] += d[2] * a->mass * mag;
         }
      }
      for (a = bodies, i = 0; i < nbodies; i++, a++) {
         a->x[0] += dt * a->v[0];
         a->x[1] += dt * a->v[1];
         a->x[2] += dt * a->v[2];
      }
   }
}

variable_types: nbodies int

variable_types:
bodies
struct planet { double x[3]; } *

variable_types: dt double

variable_types: steps int

variable_types: i int

variable_types: j int

variable_types:
a
struct planet { double x[3]; } *

variable_types:
b
struct planet { double x[3]; } *

variable_types:
d
double [3]

variable_types: mag double


energy

original_code:
double energy(int nbodies, planet *bodies)
{
   double e, d[3];
   int i, j, k;
   planet *a, *b;
   e = 0.0;
   for (i = 0, a = bodies; i < nbodies; a++, i++) {
      for(k = 0; k < 3; k++) { e += a->mass * a->v[k] * a->v[k] / 2; }
      for (j = i + 1, b = a + 1; j < nbodies; b++, j++) {
         for(k = 0; k < 3; k++) { d[k] = a->x[k] - b->x[k]; }
         e -= (a->mass * b->mass) /
            sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
      }
   }
   return e;
}

canonical_original_code:
double energy(int nbodies, struct planet *bodies)
{
   double e; double d[3];
   int i; int j; int k;
   struct planet *a; struct planet *b;
   e = 0.0;
   for (i = 0, a = bodies; i < nbodies; a++, i++) {
      for(k = 0; k < 3; k++) { e += a->mass * a->v[k] * a->v[k] / 2; }
      for (j = i + 1, b = a + 1; j < nbodies; b++, j++) {
         for(k = 0; k < 3; k++) { d[k] = a->x[k] - b->x[k]; }
         e -= (a->mass * b->mass) /
            sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
      }
   }
   return e;
}

variable_types: e double

variable_types: k int


original_code:
void offset_momentum(int nbodies, planet *bodies)
{
   int i, k;
   for (i = 0; i < nbodies; i++)
      for(k = 0; k < 3; k++) { bodies[0].v[k] -= bodies[i].v[k] * bodies[i].mass / (4 * 3.141592653589793 * 3.141592653589793); }
}

canonical_original_code:
void offset_momentum(int nbodies, struct planet *bodies)
{
   int i; int k;
   for (i = 0; i < nbodies; i++)
      for(k = 0; k < 3; k++) { bodies[0].v[k] -= bodies[i].v[k] * bodies[i].mass / (4 * 3.141592653589793 * 3.141592653589793); }
}


repo: cesello/psentinel

decompiled_code:
void *safe_getcwd()
{
size_t v0; // r12
int v1; // ebx
void *i; // rbp

v0 = 100LL;
v1 = 100;
for ( i = malloc(0x64uLL); !getcwd((char *)i, v0); i = malloc(v1) )
{
v1 *= 2;
free(i);
v0 = v1;
}
return i;
}

canonical_decompiled_code:
void *func0()
{
size_t v0; // r12
int v1; // ebx
void *i; // rbp

v0 = 100LL;
v1 = 100;
for ( i = malloc(0x64uLL); !getcwd((char *)i, v0); i = malloc(v1) )
{
v1 *= 2;
free(i);
v0 = v1;
}
return i;
}

original_code:
char * safe_getcwd()
{
 int size = 100;
 char *buffer = (char *) malloc(size*sizeof(char));
 while (1)
 {
   char *value = getcwd (buffer, size);
   if (value != 0)
  return buffer;
   size *= 2;
   free (buffer);
   buffer = (char *) malloc (size*sizeof(char));
 }
}

canonical_original_code:
char *safe_getcwd()
{
 int size = 100;
 char *buffer = (char *) malloc(size*sizeof(char));
 while (1)
 {
   char *value = getcwd (buffer, size);
   if (value != 0)
  return buffer;
   size *= 2;
   free (buffer);
   buffer = (char *) malloc (size*sizeof(char));
 }
}

variable_types: size int

variable_types:
buffer
char *


Log

decompiled_code:
void Log(int pri, char *fmt, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, fmt);
if ( to_demonize )
vsyslog(pri, fmt, arg);
else
vprintf(fmt, arg);
}

canonical_decompiled_code:
void func2(int pri, char *fmt, ...)
{
gcc_va_list arg; // [rsp+8h] [rbp-D0h] BYREF

va_start(arg, fmt);
if ( to_demonize )
vsyslog(pri, fmt, arg);
else
vprintf(fmt, arg);
}

original_code:
void Log(int priority, char * format, ...)
{
 va_list ap;
 __builtin_va_start(ap,format);
 if (to_demonize == 0)
  vprintf(format, ap);
 else
  vsyslog(priority, format, ap);
 __builtin_va_end(ap);
}

canonical_original_code:
void Log(int priority, char *format, ...)
{
 __builtin_va_list ap;
 __builtin_va_start(ap,format);
 if (to_demonize == 0)
  vprintf(format, ap);
 else
  vsyslog(priority, format, ap);
 __builtin_va_end(ap);
}

variable_types: priority int

variable_types:
format
char *


decompiled_code:
void sleep_signal()
{
sleep_status = sleep_status == 0;
if ( sleep_status )
Log(7, "Sleep enabled\n");
else
Log(7, "Sleep disabled\n");
}

canonical_decompiled_code:
void func3()
{
sleep_status = sleep_status == 0;
if ( sleep_status )
func2(7, "Sleep enabled\n");
else
func2(7, "Sleep disabled\n");
}

original_code:
void sleep_signal(int sig)
{
 sleep_status = !sleep_status;
 if (sleep_status)
  Log(7, "Sleep enabled\n");
 else
  Log(7, "Sleep disabled\n");
}

variable_types: sig int


decompiled_code:
void __noreturn term_signal()
{
int v0; // ebp
int *v1; // rax
char *v2; // rax
char *v3; // rax

Log(7, "SIGTERM trapped.Clean all things\n");
clean_all(&connections);
if ( remove(pid_file) )
{
v1 = _errno_location();
v2 = strerror(*v1);
Log(3, "Error removing PID file %s, Err(%s)", pid_file, v2);
}
v0 = remove(socket_file);
if ( v0 )
{
v0 = *_errno_location();
v3 = strerror(v0);
Log(3, "Error removing Socket file %s, Err(%s)", socket_file, v3);
}
exit(v0);
}

canonical_decompiled_code:
void __noreturn func4()
{
int v0; // ebp
int *v1; // rax
char *v2; // rax
char *v3; // rax

func2(7, "SIGTERM trapped.Clean all things\n");
clean_all(&connections);
if ( remove(pid_file) )
{
v1 = _errno_location();
v2 = strerror(*v1);
func2(3, "Error removing PID file %s, Err(%s)", pid_file, v2);
}
v0 = remove(socket_file);
if ( v0 )
{
v0 = *_errno_location();
v3 = strerror(v0);
func2(3, "Error removing Socket file %s, Err(%s)", socket_file, v3);
}
exit(v0);
}

original_code:
void term_signal(int sig)
{
 int status;
 Log(7, "SIGTERM trapped.Clean all things\n");
 status = clean_all(&connections);
 status = remove(pid_file);
 if (status)
 {
  status = (*__errno_location ());
  Log(3, "Error removing PID file %s, Err(%s)", pid_file, strerror(status));
 }
 status = remove(socket_file);
 if (status)
 {
  status = (*__errno_location ());
  Log(3, "Error removing Socket file %s, Err(%s)", socket_file, strerror(status));
 }
 exit(status);
}

variable_types: status int


decompiled_code:
void pipe_signal()
{
Log(7, "SIGPIPE trapped.Ignore it\n");
}

canonical_decompiled_code:
void func5()
{
func2(7, "SIGPIPE trapped.Ignore it\n");
}

original_code:
void pipe_signal(int sig)
{
 Log(7, "SIGPIPE trapped.Ignore it\n");
}


decompiled_code:
__int64 __fastcall initialize_timers(__int64 a1)
{
int v1; // er13
__int64 v2; // rbx
unsigned int v3; // er12
int i; // ebp
const char *v5; // rdx

v1 = *(_DWORD *)(a1 + 8);
if ( v1 < 0 )
return 0LL;
v2 = a1 + 4240;
v3 = 0;
for ( i = 0; i <= v1; ++i )
{
while ( 1 )
{
if ( *(_QWORD *)(v2 + 40) )
{
v3 = make_timer(v2);
if ( v3 )
break;
}
++i;
v2 += 12552LL;
if ( v1 < i )
return v3;
}
v5 = *(const char **)v2;
v2 += 12552LL;
Log(3, "Error creating timer for service %s", v5);
}
return v3;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1)
{
int v1; // er13
__int64 v2; // rbx
unsigned int v3; // er12
int i; // ebp
const char *v5; // rdx

v1 = *(_DWORD *)(a1 + 8);
if ( v1 < 0 )
return 0LL;
v2 = a1 + 4240;
v3 = 0;
for ( i = 0; i <= v1; ++i )
{
while ( 1 )
{
if ( *(_QWORD *)(v2 + 40) )
{
v3 = make_timer(v2);
if ( v3 )
break;
}
++i;
v2 += 12552LL;
if ( v1 < i )
return v3;
}
v5 = *(const char **)v2;
v2 += 12552LL;
func2(3, "Error creating timer for service %s", v5);
}
return v3;
}

original_code:
int initialize_timers(configuration * connections)
{
 int configured_ports = connections->last_slot;
 int i;
 int status = 0;
 for (i = 0; i <= configured_ports; i++)
 {
  if (connections->single_conn[i].timer.chk_function)
  {
   status = make_timer(&(connections->single_conn[i]));
   if (status)
   {
    Log(3, "Error creating timer for service %s", connections->single_conn[i].service_name);
   }
  }
 }
 return status;
}

canonical_original_code:
int initialize_timers(struct all_connections *connections)
{
 int configured_ports = connections->last_slot;
 int i;
 int status = 0;
 for (i = 0; i <= configured_ports; i++)
 {
  if (connections->single_conn[i].timer.chk_function)
  {
   status = make_timer(&(connections->single_conn[i]));
   if (status)
   {
    Log(3, "Error creating timer for service %s", connections->single_conn[i].service_name);
   }
  }
 }
 return status;
}

variable_types: struct all_connections { int max_socket; int running_pid; int last_slot; struct fd_set { long __fds_bits[-1]; } socket_set; char command_line[4096]; struct single_connection { char *service_name; struct timer_action { void *timerid; long last_call; unsigned short disabled; int counter; int inqueue; int check_interval; unsigned short (*chk_function)(union sigval { int sival_int; void *sival_ptr; } args); union sigval chk_arguments; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } chk_mutex; unsigned short chk_status; } timer; int listening_socket; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } servaddr; struct sockaddr_un { unsigned short sun_family; char sun_path[108]; } servaddr_un; unsigned short port; unsigned short pausable; unsigned short disabled; unsigned int counter; int running_sockets[3]; char buffer[3][4096]; unsigned short (*reply_function)(void *, int, int); } single_conn[20]; } *

user_defined_types: struct all_connections { int max_socket; int running_pid; int last_slot; struct fd_set socket_set; char command_line[4096]; struct single_connection single_conn[20]; }

user_defined_types: struct single_connection { char *service_name; struct timer_action timer; int listening_socket; struct sockaddr_in servaddr; struct sockaddr_un servaddr_un; unsigned short port; unsigned short pausable; unsigned short disabled; unsigned int counter; int running_sockets[3]; char buffer[3][4096]; unsigned short (*reply_function)(void *, int, int); }

user_defined_types: struct timer_action { void *timerid; long last_call; unsigned short disabled; int counter; int inqueue; int check_interval; unsigned short (*chk_function)(union sigval args); union sigval chk_arguments; union pthread_mutex_t chk_mutex; unsigned short chk_status; }

user_defined_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr sin_addr; unsigned char sin_zero[-1]; }

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

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


main

decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // eax
__uid_t v5; // eax
int v6; // eax
int v7; // ebp
sigset_t *v8; // rsi
__int64 v9; // rcx
__sigset_t *v10; // rdi
sigset_t *v11; // rsi
__int64 v12; // rcx
__sigset_t *v13; // rdi
sigset_t *v14; // rsi
__int64 v15; // rcx
__sigset_t *v16; // rdi
int *v17; // rbp
int v18; // eax
char *v19; // rax
sigset_t v20; // [rsp+0h] [rbp-278h] BYREF
struct sigaction v21; // [rsp+80h] [rbp-1F8h] BYREF
struct sigaction v22; // [rsp+120h] [rbp-158h] BYREF
struct sigaction v23; // [rsp+1C0h] [rbp-B8h] BYREF

LODWORD(connections.fds_bits[1]) = -1;
HIDWORD(connections.fds_bits[0]) = getpid();
v3 = init((unsigned int)argc, argv, &connections);
if ( v3 != -2 )
{
if ( v3 == 2 && to_printhelp == 1 )
{
print_help((unsigned int)argc, argv);
exit(0);
}
if ( to_command_mode )
{
v4 = execute_command(&connections);
exit(v4);
}
v5 = getuid();
Log(5, "Program started by User %d \n", v5);
if ( to_demonize )
{
v6 = daemonize((__int64)&connections);
v7 = v6;
switch ( v6 )
{
case 0:
Log(6, "Starting as a daemon.Parent exit\n");
puts("Starting as a daemon.Parent exit");
exit(0);
case 1:
Log(6, "Running as Daemon.Log level: %d\n", (unsigned int)log_level);
setlogmask((v7 << (log_level + 1)) - 1);
openlog("psentinel", 11, 136);
Log(7, "Working dir: %s\n", (const char *)default_working_dir);
break;
case -1:
puts("Failed to daemonize.Check permissions");
goto LABEL_14;
}
}
else
{
default_working_dir = (__int64)safe_getcwd();
Log(7, "Working dir: %s\n", (const char *)default_working_dir);
}
sigfillset(&v20);
v8 = &v20;
v9 = 32LL;
v10 = &v21.sa_mask;
v21.sa_handler = (__sighandler_t)sleep_signal;
while ( v9 )
{
LODWORD(v10->__val[0]) = v8->__val[0];
v8 = (sigset_t *)((char *)v8 + 4);
v10 = (__sigset_t *)((char *)v10 + 4);
--v9;
}
v21.sa_flags = 0;
sigaction(10, &v21, 0LL);
v11 = &v20;
v12 = 32LL;
v13 = &v22.sa_mask;
v22.sa_handler = (__sighandler_t)term_signal;
while ( v12 )
{
LODWORD(v13->__val[0]) = v11->__val[0];
v11 = (sigset_t *)((char *)v11 + 4);
v13 = (__sigset_t *)((char *)v13 + 4);
--v12;
}
v22.sa_flags = 0;
sigaction(15, &v22, 0LL);
v14 = &v20;
v15 = 32LL;
v16 = &v23.sa_mask;
v23.sa_handler = (__sighandler_t)pipe_signal;
while ( v15 )
{
LODWORD(v16->__val[0]) = v14->__val[0];
v14 = (sigset_t *)((char *)v14 + 4);
v16 = (__sigset_t *)((char *)v16 + 4);
--v15;
}
v23.sa_flags = 0;
sigaction(13, &v23, 0LL);
Log(6, "Init networking\n");
initialize_network(&connections);
Log(6, "Init timers\n");
initialize_timers((__int64)&connections);
Log(6, "Init data file\n");
init_datafile(&connections);
v17 = _errno_location();
while ( 1 )
{
while ( 1 )
{
Log(7, "Main loop\n");
rebuild_connection_set(&connections);
v18 = select(
LODWORD(connections.fds_bits[0]) + 1,
(fd_set *)(unsigned int)((_DWORD)&connections + 16),
0LL,
0LL,
0LL);
if ( v18 < 0 )
break;
if ( v18 )
read_sockets(&connections);
}
if ( *v17 != 4 )
{
v19 = strerror(*v17);
Log(3, "Error in select socket (%s) \n", v19);
exit(1);
}
}
}
if ( to_printhelp == 1 )
print_help((unsigned int)argc, argv);
LABEL_14:
exit(1);
}

canonical_decompiled_code:
int __cdecl __noreturn main(int argc, const char **argv, const char **envp)
{
int v3; // eax
int v4; // eax
__uid_t v5; // eax
int v6; // eax
int v7; // ebp
sigset_t *v8; // rsi
__int64 v9; // rcx
__sigset_t *v10; // rdi
sigset_t *v11; // rsi
__int64 v12; // rcx
__sigset_t *v13; // rdi
sigset_t *v14; // rsi
__int64 v15; // rcx
__sigset_t *v16; // rdi
int *v17; // rbp
int v18; // eax
char *v19; // rax
sigset_t v20; // [rsp+0h] [rbp-278h] BYREF
struct sigaction v21; // [rsp+80h] [rbp-1F8h] BYREF
struct sigaction v22; // [rsp+120h] [rbp-158h] BYREF
struct sigaction v23; // [rsp+1C0h] [rbp-B8h] BYREF

LODWORD(connections.fds_bits[1]) = -1;
HIDWORD(connections.fds_bits[0]) = getpid();
v3 = init((unsigned int)argc, argv, &connections);
if ( v3 != -2 )
{
if ( v3 == 2 && to_printhelp == 1 )
{
print_help((unsigned int)argc, argv);
exit(0);
}
if ( to_command_mode )
{
v4 = execute_command(&connections);
exit(v4);
}
v5 = getuid();
func2(5, "Program started by User %d \n", v5);
if ( to_demonize )
{
v6 = func1((__int64)&connections);
v7 = v6;
switch ( v6 )
{
case 0:
func2(6, "Starting as a daemon.Parent exit\n");
puts("Starting as a daemon.Parent exit");
exit(0);
case 1:
func2(6, "Running as Daemon.Log level: %d\n", (unsigned int)log_level);
setlogmask((v7 << (log_level + 1)) - 1);
openlog("psentinel", 11, 136);
func2(7, "Working dir: %s\n", (const char *)default_working_dir);
break;
case -1:
puts("Failed to daemonize.Check permissions");
goto LABEL_14;
}
}
else
{
default_working_dir = (__int64)func0();
func2(7, "Working dir: %s\n", (const char *)default_working_dir);
}
sigfillset(&v20);
v8 = &v20;
v9 = 32LL;
v10 = &v21.sa_mask;
v21.sa_handler = (__sighandler_t)sleep_signal;
while ( v9 )
{
LODWORD(v10->__val[0]) = v8->__val[0];
v8 = (sigset_t *)((char *)v8 + 4);
v10 = (__sigset_t *)((char *)v10 + 4);
--v9;
}
v21.sa_flags = 0;
sigaction(10, &v21, 0LL);
v11 = &v20;
v12 = 32LL;
v13 = &v22.sa_mask;
v22.sa_handler = (__sighandler_t)term_signal;
while ( v12 )
{
LODWORD(v13->__val[0]) = v11->__val[0];
v11 = (sigset_t *)((char *)v11 + 4);
v13 = (__sigset_t *)((char *)v13 + 4);
--v12;
}
v22.sa_flags = 0;
sigaction(15, &v22, 0LL);
v14 = &v20;
v15 = 32LL;
v16 = &v23.sa_mask;
v23.sa_handler = (__sighandler_t)pipe_signal;
while ( v15 )
{
LODWORD(v16->__val[0]) = v14->__val[0];
v14 = (sigset_t *)((char *)v14 + 4);
v16 = (__sigset_t *)((char *)v16 + 4);
--v15;
}
v23.sa_flags = 0;
sigaction(13, &v23, 0LL);
func2(6, "Init networking\n");
initialize_network(&connections);
func2(6, "Init timers\n");
func6((__int64)&connections);
func2(6, "Init data file\n");
init_datafile(&connections);
v17 = _errno_location();
while ( 1 )
{
while ( 1 )
{
func2(7, "Main loop\n");
rebuild_connection_set(&connections);
v18 = select(
LODWORD(connections.fds_bits[0]) + 1,
(fd_set *)(unsigned int)((_DWORD)&connections + 16),
0LL,
0LL,
0LL);
if ( v18 < 0 )
break;
if ( v18 )
read_sockets(&connections);
}
if ( *v17 != 4 )
{
v19 = strerror(*v17);
func2(3, "Error in select socket (%s) \n", v19);
exit(1);
}
}
}
if ( to_printhelp == 1 )
print_help((unsigned int)argc, argv);
LABEL_14:
exit(1);
}

original_code:
int main(int argc, char **argv)
{
 int status;
 struct sigaction usr_action;
 struct sigaction term_action;
 struct sigaction pipe_action;
 sigset_t block_mask;
 int socks_status;
 connections.last_slot = -1;
 connections.running_pid = getpid();
 status = init(argc, argv, &connections);
 if (status == -2)
 {
  if (to_printhelp == 1)
   print_help(argc, argv);
  exit(1);
 }
 if (status == 2 && to_printhelp == 1)
 {
  print_help(argc, argv);
  exit(0);
 }
 if (to_command_mode)
 {
  status = execute_command(&connections);
  exit(status);
 }
 Log(5, "Program started by User %d \n", getuid());
 if (to_demonize)
 {
  status = daemonize(&connections);
  switch (status)
  {
   case -1:
    printf("Failed to daemonize.Check permissions\n");
    exit(1);
    break;
   case 0:
    Log(6, "Starting as a daemon.Parent exit\n");
    printf("Starting as a daemon.Parent exit\n");
    exit(0);
    break;
   case 1:
     Log(6, "Running as Daemon.Log level: %d\n",log_level);
     setlogmask(((1 << ((log_level)+1)) - 1));
     openlog("psentinel", 0x02 | 0x01 | 0x08, (17<<3));
     Log(7,"Working dir: %s\n",default_working_dir);
    break;
  }
 }
 else
 {
  default_working_dir=safe_getcwd();
  Log(7,"Working dir: %s\n",default_working_dir);
 }
 sigfillset(&block_mask);
 usr_action.__sigaction_handler.sa_handler = sleep_signal;
 usr_action.sa_mask = block_mask;
 usr_action.sa_flags = 0;
 sigaction(10, &usr_action, ((void *)0));
 term_action.__sigaction_handler.sa_handler = term_signal;
 term_action.sa_mask = block_mask;
 term_action.sa_flags = 0;
 sigaction(15, &term_action, ((void *)0));
 pipe_action.__sigaction_handler.sa_handler = pipe_signal;
 pipe_action.sa_mask = block_mask;
 pipe_action.sa_flags = 0;
 sigaction(13, &pipe_action, ((void *)0));
 Log(6, "Init networking\n");
 status = initialize_network(&connections);
 Log(6, "Init timers\n");
 status = initialize_timers(&connections);
 Log(6,"Init data file\n");
 status = init_datafile(&connections);
 while (1)
 {
  Log(7, "Main loop\n");
  rebuild_connection_set(&connections);
  socks_status = select(connections.max_socket + 1, &connections.socket_set, ((void *)0), ((void *)0), ((void *)0));
  if (socks_status < 0)
  {
   status = (*__errno_location ());
   if (status != 4)
   {
    Log(3, "Error in select socket (%s) \n", strerror(status));
    exit(1);
   }
  }
  else
   if (socks_status == 0)
   {
   }
   else
   {
    read_sockets(&connections);
   }
 }
 closelog();
 exit(0);
}

canonical_original_code:
int main(int argc, char **argv)
{
 int status;
 struct sigaction usr_action;
 struct sigaction term_action;
 struct sigaction pipe_action;
 struct __sigset_t block_mask;
 int socks_status;
 connections.last_slot = -1;
 connections.running_pid = getpid();
 status = init(argc, argv, &connections);
 if (status == -2)
 {
  if (to_printhelp == 1)
   print_help(argc, argv);
  exit(1);
 }
 if (status == 2 && to_printhelp == 1)
 {
  print_help(argc, argv);
  exit(0);
 }
 if (to_command_mode)
 {
  status = execute_command(&connections);
  exit(status);
 }
 Log(5, "Program started by User %d \n", getuid());
 if (to_demonize)
 {
  status = daemonize(&connections);
  switch (status)
  {
   case -1:
    printf("Failed to daemonize.Check permissions\n");
    exit(1);
    break;
   case 0:
    Log(6, "Starting as a daemon.Parent exit\n");
    printf("Starting as a daemon.Parent exit\n");
    exit(0);
    break;
   case 1:
     Log(6, "Running as Daemon.Log level: %d\n",log_level);
     setlogmask(((1 << ((log_level)+1)) - 1));
     openlog("psentinel", 0x02 | 0x01 | 0x08, (17<<3));
     Log(7,"Working dir: %s\n",default_working_dir);
    break;
  }
 }
 else
 {
  default_working_dir=safe_getcwd();
  Log(7,"Working dir: %s\n",default_working_dir);
 }
 sigfillset(&block_mask);
 usr_action.__sigaction_handler.sa_handler = sleep_signal;
 usr_action.sa_mask = block_mask;
 usr_action.sa_flags = 0;
 sigaction(10, &usr_action, ((void *)0));
 term_action.__sigaction_handler.sa_handler = term_signal;
 term_action.sa_mask = block_mask;
 term_action.sa_flags = 0;
 sigaction(15, &term_action, ((void *)0));
 pipe_action.__sigaction_handler.sa_handler = pipe_signal;
 pipe_action.sa_mask = block_mask;
 pipe_action.sa_flags = 0;
 sigaction(13, &pipe_action, ((void *)0));
 Log(6, "Init networking\n");
 status = initialize_network(&connections);
 Log(6, "Init timers\n");
 status = initialize_timers(&connections);
 Log(6,"Init data file\n");
 status = init_datafile(&connections);
 while (1)
 {
  Log(7, "Main loop\n");
  rebuild_connection_set(&connections);
  socks_status = select(connections.max_socket + 1, &connections.socket_set, ((void *)0), ((void *)0), ((void *)0));
  if (socks_status < 0)
  {
   status = (*__errno_location ());
   if (status != 4)
   {
    Log(3, "Error in select socket (%s) \n", strerror(status));
    exit(1);
   }
  }
  else
   if (socks_status == 0)
   {
   }
   else
   {
    read_sockets(&connections);
   }
 }
 closelog();
 exit(0);
}

variable_types: argc int

variable_types:
argv
char **

variable_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval { int sival_int; void *sival_ptr; } si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; } *, void *); } __sigaction_handler; struct __sigset_t { unsigned long __val[-1]; } sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct sigaction { union { void (*sa_handler)(int); void (*sa_sigaction)(int, struct siginfo_t *, void *); } __sigaction_handler; struct __sigset_t sa_mask; int sa_flags; void (*sa_restorer)(); }

user_defined_types: struct siginfo_t { int si_signo; int si_errno; int si_code; int __pad0; union { int _pad[-1]; struct { int si_pid; unsigned int si_uid; } _kill; struct { int si_tid; int si_overrun; union sigval si_sigval; } _timer; struct { int si_pid; unsigned int si_uid; union sigval si_sigval; } _rt; struct { int si_pid; unsigned int si_uid; int si_status; long si_utime; long si_stime; } _sigchld; struct { void *si_addr; short si_addr_lsb; union { struct { void *_lower; void *_upper; } _addr_bnd; unsigned int _pkey; } _bounds; } _sigfault; struct { long si_band; int si_fd; } _sigpoll; struct { void *_call_addr; int _syscall; unsigned int _arch; } _sigsys; } _sifields; }


setnonblocking

decompiled_code:
int __fastcall setnonblocking(int fd)
{
int v1; // ebx
__int64 v2; // rdx
int result; // eax

v1 = fcntl(fd, 3);
if ( v1 < 0 )
Log(3LL, "Error in fcntl(F_GETFL) \n");
v2 = (unsigned int)v1;
BYTE1(v2) = BYTE1(v1) | 8;
result = fcntl(fd, 4, v2);
if ( result < 0 )
result = Log(3LL, "Error in fcntl(F_SETFL) \n");
return result;
}

canonical_decompiled_code:
int __fastcall func2(int fd)
{
int v1; // ebx
__int64 v2; // rdx
int result; // eax

v1 = fcntl(fd, 3);
if ( v1 < 0 )
Log(3LL, "Error in fcntl(F_GETFL) \n");
v2 = (unsigned int)v1;
BYTE1(v2) = BYTE1(v1) | 8;
result = fcntl(fd, 4, v2);
if ( result < 0 )
result = Log(3LL, "Error in fcntl(F_SETFL) \n");
return result;
}

original_code:
void setnonblocking(int sock)
{
 int opts;
 opts = fcntl(sock,3);
 if (opts < 0)
 {
  Log(3,"Error in fcntl(F_GETFL) \n");
 }
 opts = (opts | 04000);
 if (fcntl(sock,4,opts) < 0)
 {
  Log(3,"Error in fcntl(F_SETFL) \n");
 }
 return;
}

variable_types: sock int

variable_types: opts int


decompiled_code:
__int64 __fastcall sock_read(int fd, void *buf, unsigned __int64 a3)
{
unsigned __int64 v4; // r14
int v6; // eax
unsigned int v7; // er12

v4 = 0LL;
if ( !a3 )
return (unsigned int)a3;
while ( 1 )
{
while ( 1 )
{
v6 = read(fd, buf, a3 - v4);
v7 = v6;
if ( v6 >= 0 )
break;
if ( *_errno_location() != 4 )
return v7;
}
if ( !v6 )
break;
v4 += v6;
buf = (char *)buf + v6;
if ( a3 <= v4 )
return (unsigned int)a3;
}
return (unsigned int)v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(int fd, void *buf, unsigned __int64 a3)
{
unsigned __int64 v4; // r14
int v6; // eax
unsigned int v7; // er12

v4 = 0LL;
if ( !a3 )
return (unsigned int)a3;
while ( 1 )
{
while ( 1 )
{
v6 = read(fd, buf, a3 - v4);
v7 = v6;
if ( v6 >= 0 )
break;
if ( *_errno_location() != 4 )
return v7;
}
if ( !v6 )
break;
v4 += v6;
buf = (char *)buf + v6;
if ( a3 <= v4 )
return (unsigned int)a3;
}
return (unsigned int)v4;
}

original_code:
int sock_read(int sockfd, char *buf, size_t count)
{
  size_t bytes_read = 0;
  int this_read;
  while (bytes_read < count)
  {
    do
    {
      this_read = read(sockfd, buf, count - bytes_read);
    }
    while ( (this_read < 0) && ((*__errno_location ()) == 4) );
    if (this_read < 0)
    {
      return this_read;
    }
    else
    if (this_read == 0)
    {
      return bytes_read;
    }
    bytes_read += this_read;
    buf += this_read;
  }
  return count;
}

canonical_original_code:
int sock_read(int sockfd, char *buf, unsigned long count)
{
  unsigned long bytes_read = 0;
  int this_read;
  while (bytes_read < count)
  {
    do
    {
      this_read = read(sockfd, buf, count - bytes_read);
    }
    while ( (this_read < 0) && ((*__errno_location ()) == 4) );
    if (this_read < 0)
    {
      return this_read;
    }
    else
    if (this_read == 0)
    {
      return bytes_read;
    }
    bytes_read += this_read;
    buf += this_read;
  }
  return count;
}

variable_types: sockfd int

variable_types:
buf
char *

variable_types:
count
unsigned long


decompiled_code:
__int64 __fastcall sock_write(int fd, void *buf, unsigned __int64 a3)
{
unsigned __int64 v4; // r14
int v6; // eax
unsigned int v7; // er12

v4 = 0LL;
if ( !a3 )
return (unsigned int)a3;
while ( 1 )
{
while ( 1 )
{
v6 = write(fd, buf, a3 - v4);
v7 = v6;
if ( v6 >= 0 )
break;
if ( *_errno_location() != 4 )
return v7;
}
if ( !v6 )
break;
v4 += v6;
buf = (char *)buf + v6;
if ( a3 <= v4 )
return (unsigned int)a3;
}
return v7;
}

canonical_decompiled_code:
__int64 __fastcall func4(int fd, void *buf, unsigned __int64 a3)
{
unsigned __int64 v4; // r14
int v6; // eax
unsigned int v7; // er12

v4 = 0LL;
if ( !a3 )
return (unsigned int)a3;
while ( 1 )
{
while ( 1 )
{
v6 = write(fd, buf, a3 - v4);
v7 = v6;
if ( v6 >= 0 )
break;
if ( *_errno_location() != 4 )
return v7;
}
if ( !v6 )
break;
v4 += v6;
buf = (char *)buf + v6;
if ( a3 <= v4 )
return (unsigned int)a3;
}
return v7;
}

original_code:
int sock_write(int sockfd, char *buf, size_t count)
{
  size_t bytes_sent = 0;
  int this_write;
  while (bytes_sent < count)
  {
    do
    {
      this_write = write(sockfd, buf, count - bytes_sent);
    }
    while ( (this_write < 0) && ((*__errno_location ()) == 4) );
    if (this_write <= 0)
    {
      return this_write;
    }
    bytes_sent += this_write;
    buf += this_write;
  }
  return count;
}

canonical_original_code:
int sock_write(int sockfd, char *buf, unsigned long count)
{
  unsigned long bytes_sent = 0;
  int this_write;
  while (bytes_sent < count)
  {
    do
    {
      this_write = write(sockfd, buf, count - bytes_sent);
    }
    while ( (this_write < 0) && ((*__errno_location ()) == 4) );
    if (this_write <= 0)
    {
      return this_write;
    }
    bytes_sent += this_write;
    buf += this_write;
  }
  return count;
}


decompiled_code:
__int64 __fastcall sock_puts(int fd, void *buf)
{
unsigned __int64 v2; // rax

v2 = strlen((const char *)buf);
return sock_write(fd, buf, v2);
}

canonical_decompiled_code:
__int64 __fastcall func5(int fd, void *buf)
{
unsigned __int64 v2; // rax

v2 = strlen((const char *)buf);
return func4(fd, buf, v2);
}

original_code:
int sock_puts(int sockfd, char *str)
{
  return sock_write(sockfd, str, strlen(str));
}

variable_types:
str
char *


original_code:
int initialize_network(configuration * connections)
{
 int temp_socket;
 struct sockaddr_in temp_servaddr;
 struct sockaddr_un temp_servaddr_un;
 int i;
 int configured_ports=connections->last_slot;
    for (i=0;i<=configured_ports;i++)
    {
     if (strcmp(connections->single_conn[i].service_name,"general")==0)
     {
      if ( ( temp_socket = socket(1, SOCK_STREAM, 0)) < 0 )
   {
    Log(3,"Error creating listening AF_UNIX socket.\n");
    exit(1);
   }
      connections->single_conn[i].listening_socket=temp_socket;
      if (temp_socket > connections->max_socket)
       connections->max_socket=temp_socket;
      memset(&temp_servaddr_un, 0, sizeof(temp_servaddr_un));
      temp_servaddr_un.sun_family = 1;
      strcpy(temp_servaddr_un.sun_path,socket_file);
      connections->single_conn[i].servaddr_un=temp_servaddr_un;
      if ( bind(temp_socket, (struct sockaddr*) &temp_servaddr_un, ((size_t) (((struct sockaddr_un *) 0)->sun_path) + strlen ((&temp_servaddr_un)->sun_path))) < 0 )
   {
    Log(3,"Error calling bind AF_UNIX (%s)\n",strerror((*__errno_location ())));
    exit(1);
   }
   if ( listen(temp_socket, 10) < 0 )
   {
    Log(3,"Error calling listen AF_UNIX (%s)\n",strerror((*__errno_location ())));
    exit(1);
   }
      continue;
     }
        if ( ( temp_socket = socket(2, SOCK_STREAM, 0)) < 0 )
     {
      Log(3,"Error creating listening socket.\n");
      exit(1);
     }
        connections->single_conn[i].listening_socket=temp_socket;
        if (temp_socket > connections->max_socket)
         connections->max_socket=temp_socket;
        memset(&temp_servaddr, 0, sizeof(temp_servaddr));
        temp_servaddr.sin_family = 2;
        temp_servaddr.sin_addr.s_addr = htonl(((in_addr_t) 0x00000000));
        temp_servaddr.sin_port = htons(connections->single_conn[i].port);
        connections->single_conn[i].servaddr=temp_servaddr;
     if ( bind(temp_socket, (struct sockaddr *) &temp_servaddr, sizeof(temp_servaddr)) < 0 )
  {
   Log(3,"Error calling bind(%s)\n",strerror((*__errno_location ())));
   exit(1);
  }
        if ( listen(temp_socket, 10) < 0 )
        {
         Log(3,"ECHOSERV: Error calling listen(%s)\n",strerror((*__errno_location ())));
         exit(1);
        }
    }
    return 3;
}

canonical_original_code:
int initialize_network(struct all_connections *connections)
{
 int temp_socket;
 struct sockaddr_in temp_servaddr;
 struct sockaddr_un temp_servaddr_un;
 int i;
 int configured_ports = connections->last_slot;
    for (i=0;i<=configured_ports;i++)
    {
     if (strcmp(connections->single_conn[i].service_name,"general")==0)
     {
      if ( ( temp_socket = socket(1, SOCK_STREAM, 0)) < 0 )
   {
    Log(3,"Error creating listening AF_UNIX socket.\n");
    exit(1);
   }
      connections->single_conn[i].listening_socket=temp_socket;
      if (temp_socket > connections->max_socket)
       connections->max_socket=temp_socket;
      memset(&temp_servaddr_un, 0, sizeof(temp_servaddr_un));
      temp_servaddr_un.sun_family = 1;
      strcpy(temp_servaddr_un.sun_path,socket_file);
      connections->single_conn[i].servaddr_un=temp_servaddr_un;
      if ( bind(temp_socket, (struct sockaddr *) &temp_servaddr_un, ((unsigned long) (((struct sockaddr_un *) 0)->sun_path) + strlen ((&temp_servaddr_un)->sun_path))) < 0 )
   {
    Log(3,"Error calling bind AF_UNIX (%s)\n",strerror((*__errno_location ())));
    exit(1);
   }
   if ( listen(temp_socket, 10) < 0 )
   {
    Log(3,"Error calling listen AF_UNIX (%s)\n",strerror((*__errno_location ())));
    exit(1);
   }
      continue;
     }
        if ( ( temp_socket = socket(2, SOCK_STREAM, 0)) < 0 )
     {
      Log(3,"Error creating listening socket.\n");
      exit(1);
     }
        connections->single_conn[i].listening_socket=temp_socket;
        if (temp_socket > connections->max_socket)
         connections->max_socket=temp_socket;
        memset(&temp_servaddr, 0, sizeof(temp_servaddr));
        temp_servaddr.sin_family = 2;
        temp_servaddr.sin_addr.s_addr = htonl(((unsigned int) 0x00000000));
        temp_servaddr.sin_port = htons(connections->single_conn[i].port);
        connections->single_conn[i].servaddr=temp_servaddr;
     if ( bind(temp_socket, (struct sockaddr *) &temp_servaddr, sizeof(temp_servaddr)) < 0 )
  {
   Log(3,"Error calling bind(%s)\n",strerror((*__errno_location ())));
   exit(1);
  }
        if ( listen(temp_socket, 10) < 0 )
        {
         Log(3,"ECHOSERV: Error calling listen(%s)\n",strerror((*__errno_location ())));
         exit(1);
        }
    }
    return 3;
}

variable_types: struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; }

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

variable_types: i int

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

user_defined_types: union sigval { int sival_int; void *sival_ptr; }

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


original_code:
void rebuild_connection_set(configuration * connections)
{
 int i,j;
 int configured_connections=connections->last_slot;
 do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&(connections->socket_set))->__fds_bits)[0]) : "memory"); } while (0);
 for (i=0;i<=configured_connections;i++)
 {
  ((void) (((&(connections->socket_set))->__fds_bits)[((connections->single_conn[i].listening_socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((connections->single_conn[i].listening_socket) % (8 * (int) sizeof (__fd_mask)))))));
  for (j=0;j<3;j++)
  {
   if (connections->single_conn[i].running_sockets[j]!=0)
   {
    ((void) (((&(connections->socket_set))->__fds_bits)[((connections->single_conn[i].running_sockets[j]) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((connections->single_conn[i].running_sockets[j]) % (8 * (int) sizeof (__fd_mask)))))));
    if (connections->single_conn[i].running_sockets[j] > connections->max_socket)
    {
     connections->max_socket = connections->single_conn[i].running_sockets[j];
    }
   }
  }
 }
}

canonical_original_code:
void rebuild_connection_set(struct all_connections *connections)
{
 int i; int j;
 int configured_connections = connections->last_slot;
 do { int __d0; int __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&(connections->socket_set))->__fds_bits)[0]) : "memory"); } while (0);
 for (i=0;i<=configured_connections;i++)
 {
  ((void) (((&(connections->socket_set))->__fds_bits)[((connections->single_conn[i].listening_socket) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((connections->single_conn[i].listening_socket) % (8 * (int) sizeof (__fd_mask)))))));
  for (j=0;j<3;j++)
  {
   if (connections->single_conn[i].running_sockets[j]!=0)
   {
    ((void) (((&(connections->socket_set))->__fds_bits)[((connections->single_conn[i].running_sockets[j]) / (8 * (int) sizeof (__fd_mask)))] |= ((__fd_mask) (1UL << ((connections->single_conn[i].running_sockets[j]) % (8 * (int) sizeof (__fd_mask)))))));
    if (connections->single_conn[i].running_sockets[j] > connections->max_socket)
    {
     connections->max_socket = connections->single_conn[i].running_sockets[j];
    }
   }
  }
 }
}

variable_types: j int

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


decompiled_code:
int __fastcall handle_new_connection(__int64 a1, int a2)
{
__int64 v2; // rbp
int v3; // er13
int *v4; // rax
__int64 v5; // rbp
int result; // eax
_QWORD *v7; // rbx
int *v8; // rax
char *v9; // rax
char v10[4136]; // [rsp+0h] [rbp-1028h] BYREF

v2 = a1 + 12552LL * a2;
v3 = accept(*(_DWORD *)(v2 + 4344), 0LL, 0LL);
if ( v3 >= 0
|| (v8 = _errno_location(),
v9 = strerror(*v8),
result = Log(
3LL,
"Error accepting new connections from port %d. Errno=%s\n",
*(unsigned __int16 *)(v2 + 4474),
v9),
v3 != -1) )
{
v4 = (int *)(a1 + 12552LL * a2);
v5 = v4[1121];
if ( (_DWORD)v5 )
{
if ( v4[1122] )
{
v5 = 2LL;
if ( v4[1123] )
{
Log(7LL, "Too many connections. Increase room for clients..\n");
sprintf(v10, "HTTP/1.0 %d %s - psentinel Ver.0.4.607 Rev.160\r\n", 500LL, "Too many clients");
sock_puts(v3, v10);
return close(v3);
}
}
else
{
v5 = 1LL;
}
}
Log(
7LL,
"Connection from port (%d) Accepted: FD=%d,Slot=%d\n",
*(unsigned __int16 *)(a1 + 12552LL * a2 + 4474),
(unsigned int)v3,
(unsigned int)v5);
*(_DWORD *)(a1 + 4 * (v5 + 3138LL * a2 + 1120) + 4) = v3;
setnonblocking(v3);
result = 0;
v7 = (_QWORD *)(12552LL * a2 + (v5 << 12) + 4496 + a1);
*v7 = 0LL;
v7[511] = 0LL;
memset(
(void *)((unsigned __int64)(v7 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v7 - (((_DWORD)v7 + 8) & 0xFFFFFFF8) + 4096) >> 3));
}
return result;
}

canonical_decompiled_code:
int __fastcall func8(__int64 a1, int a2)
{
__int64 v2; // rbp
int v3; // er13
int *v4; // rax
__int64 v5; // rbp
int result; // eax
_QWORD *v7; // rbx
int *v8; // rax
char *v9; // rax
char v10[4136]; // [rsp+0h] [rbp-1028h] BYREF

v2 = a1 + 12552LL * a2;
v3 = accept(*(_DWORD *)(v2 + 4344), 0LL, 0LL);
if ( v3 >= 0
|| (v8 = _errno_location(),
v9 = strerror(*v8),
result = Log(
3LL,
"Error accepting new connections from port %d. Errno=%s\n",
*(unsigned __int16 *)(v2 + 4474),
v9),
v3 != -1) )
{
v4 = (int *)(a1 + 12552LL * a2);
v5 = v4[1121];
if ( (_DWORD)v5 )
{
if ( v4[1122] )
{
v5 = 2LL;
if ( v4[1123] )
{
Log(7LL, "Too many connections. Increase room for clients..\n");
sprintf(v10, "HTTP/1.0 %d %s - psentinel Ver.0.4.607 Rev.160\r\n", 500LL, "Too many clients");
func5(v3, v10);
return close(v3);
}
}
else
{
v5 = 1LL;
}
}
Log(
7LL,
"Connection from port (%d) Accepted: FD=%d,Slot=%d\n",
*(unsigned __int16 *)(a1 + 12552LL * a2 + 4474),
(unsigned int)v3,
(unsigned int)v5);
*(_DWORD *)(a1 + 4 * (v5 + 3138LL * a2 + 1120) + 4) = v3;
func2(v3);
result = 0;
v7 = (_QWORD *)(12552LL * a2 + (v5 << 12) + 4496 + a1);
*v7 = 0LL;
v7[511] = 0LL;
memset(
(void *)((unsigned __int64)(v7 + 1) & 0xFFFFFFFFFFFFFFF8LL),
0,
8LL * (((unsigned int)v7 - (((_DWORD)v7 + 8) & 0xFFFFFFF8) + 4096) >> 3));
}
return result;
}

original_code:
void handle_new_connection(configuration * connections,int socket_index)
{
 int connection;
 int i;
 char temp_buffer[4096];
 connection = accept(connections->single_conn[socket_index].listening_socket,((void *)0),((void *)0));
 if (connection < 0 )
 {
  Log(3,"Error accepting new connections from port %d. Errno=%s\n", connections->single_conn[socket_index].port,strerror((*__errno_location ())));
 }
 for(i=0;(i<3)&&(connection!=-1);i++)
 {
  if (connections->single_conn[socket_index].running_sockets[i]==0)
  {
   Log(7,"Connection from port (%d) Accepted: FD=%d,Slot=%d\n",connections->single_conn[socket_index].port,connection,i);
   connections->single_conn[socket_index].running_sockets[i]=connection;
            setnonblocking(connection);
            memset(&connections->single_conn[socket_index].buffer[i][0], 0, 4096);
   connection=-1;
  }
 }
 if (connection !=-1)
 {
  Log(7,"Too many connections. Increase room for clients..\n");
  sprintf(temp_buffer,"HTTP/1.0 %d %s - " "psentinel" " Ver." "0.4.607 Rev.160" "\r\n",500,"Too many clients");
  sock_puts(connection,temp_buffer);
  close(connection);
 }
}

canonical_original_code:
void handle_new_connection(struct all_connections *connections, int socket_index)
{
 int connection;
 int i;
 char temp_buffer[4096];
 connection = accept(connections->single_conn[socket_index].listening_socket,((void *)0),((void *)0));
 if (connection < 0 )
 {
  Log(3,"Error accepting new connections from port %d. Errno=%s\n", connections->single_conn[socket_index].port,strerror((*__errno_location ())));
 }
 for(i=0;(i<3)&&(connection!=-1);i++)
 {
  if (connections->single_conn[socket_index].running_sockets[i]==0)
  {
   Log(7,"Connection from port (%d) Accepted: FD=%d,Slot=%d\n",connections->single_conn[socket_index].port,connection,i);
   connections->single_conn[socket_index].running_sockets[i]=connection;
            setnonblocking(connection);
            memset(&connections->single_conn[socket_index].buffer[i][0], 0, 4096);
   connection=-1;
  }
 }
 if (connection !=-1)
 {
  Log(7,"Too many connections. Increase room for clients..\n");
  sprintf(temp_buffer,"HTTP/1.0 %d %s - " "psentinel" " Ver." "0.4.607 Rev.160" "\r\n",500,"Too many clients");
  sock_puts(connection,temp_buffer);
  close(connection);
 }
}

variable_types: connection int


original_code:
void read_send_data(configuration * connections,int port_index,int socket_index)
{
 int connection;
 char * temp_buffer;
 connection=connections->single_conn[port_index].running_sockets[socket_index];
 temp_buffer=&(connections->single_conn[port_index].buffer[socket_index][0]);
 connections->single_conn[port_index].counter++;
 sock_read(connection,temp_buffer,4096);
 (*connections->single_conn[port_index].reply_function)(connections,port_index,socket_index);
 sock_puts(connection,temp_buffer);
 close(connection);
 connections->single_conn[port_index].running_sockets[socket_index]=0;
}

canonical_original_code:
void read_send_data(struct all_connections *connections, int port_index, int socket_index)
{
 int connection;
 char *temp_buffer;
 connection=connections->single_conn[port_index].running_sockets[socket_index];
 temp_buffer=&(connections->single_conn[port_index].buffer[socket_index][0]);
 connections->single_conn[port_index].counter++;
 sock_read(connection,temp_buffer,4096);
 (*connections->single_conn[port_index].reply_function)(connections,port_index,socket_index);
 sock_puts(connection,temp_buffer);
 close(connection);
 connections->single_conn[port_index].running_sockets[socket_index]=0;
}


original_code:
void read_sockets(configuration * connections)
{
 int i,j;
 int running_socket;
 int configured_connections = connections->last_slot;
 for (i=0;i<=configured_connections;i++)
 {
  if (((((&(connections->socket_set))->__fds_bits)[((connections->single_conn[i].listening_socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((connections->single_conn[i].listening_socket) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   handle_new_connection(connections,i);
  }
  for (j=0;j<3;j++)
  {
   running_socket = connections->single_conn[i].running_sockets[j];
   if (running_socket)
   {
    if (((((&(connections->socket_set))->__fds_bits)[((running_socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((running_socket) % (8 * (int) sizeof (__fd_mask)))))) != 0))
    {
     read_send_data(connections,i,j);
    }
   }
  }
 }
}

canonical_original_code:
void read_sockets(struct all_connections *connections)
{
 int i; int j;
 int running_socket;
 int configured_connections = connections->last_slot;
 for (i=0;i<=configured_connections;i++)
 {
  if (((((&(connections->socket_set))->__fds_bits)[((connections->single_conn[i].listening_socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((connections->single_conn[i].listening_socket) % (8 * (int) sizeof (__fd_mask)))))) != 0))
  {
   handle_new_connection(connections,i);
  }
  for (j=0;j<3;j++)
  {
   running_socket = connections->single_conn[i].running_sockets[j];
   if (running_socket)
   {
    if (((((&(connections->socket_set))->__fds_bits)[((running_socket) / (8 * (int) sizeof (__fd_mask)))] & ((__fd_mask) (1UL << ((running_socket) % (8 * (int) sizeof (__fd_mask)))))) != 0))
    {
     read_send_data(connections,i,j);
    }
   }
  }
 }
}


decompiled_code:
int __fastcall timer_handler(__int64 a1)
{
pthread_mutex_t *v1; // r12
__int16 v3; // bp
time_t v5; // rax
__int64 v6; // rdi

v1 = (pthread_mutex_t *)(a1 + 56);
++*(_DWORD *)(a1 + 32);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 56));
if ( *(_WORD *)(a1 + 24)
|| (++*(_DWORD *)(a1 + 28),
v5 = time(0LL),
v6 = *(_QWORD *)(a1 + 48),
*(_QWORD *)(a1 + 16) = v5,
(v3 = (*(__int64 (__fastcall **)(__int64))(a1 + 40))(v6)) == 0) )
{
v3 = *(_WORD *)(a1 + 96);
if ( v3 )
{
v3 = 0;
Log(3LL, "chk_function back for service [%s] returned to OK state. Re-enabling service\n", *(const char **)a1);
}
}
else if ( !*(_WORD *)(a1 + 96) )
{
Log(3LL, "chk_function for service [%s] returned an error state. Disabling service\n", *(const char **)a1);
}
--*(_DWORD *)(a1 + 32);
*(_WORD *)(a1 + 96) = v3;
return pthread_mutex_unlock(v1);
}

canonical_decompiled_code:
int __fastcall func0(__int64 a1)
{
pthread_mutex_t *v1; // r12
__int16 v3; // bp
time_t v5; // rax
__int64 v6; // rdi

v1 = (pthread_mutex_t *)(a1 + 56);
++*(_DWORD *)(a1 + 32);
pthread_mutex_lock((pthread_mutex_t *)(a1 + 56));
if ( *(_WORD *)(a1 + 24)
|| (++*(_DWORD *)(a1 + 28),
v5 = time(0LL),
v6 = *(_QWORD *)(a1 + 48),
*(_QWORD *)(a1 + 16) = v5,
(v3 = (*(__int64 (__fastcall **)(__int64))(a1 + 40))(v6)) == 0) )
{
v3 = *(_WORD *)(a1 + 96);
if ( v3 )
{
v3 = 0;
Log(3LL, "chk_function back for service [%s] returned to OK state. Re-enabling service\n", *(const char **)a1);
}
}
else if ( !*(_WORD *)(a1 + 96) )
{
Log(3LL, "chk_function for service [%s] returned an error state. Disabling service\n", *(const char **)a1);
}
--*(_DWORD *)(a1 + 32);
*(_WORD *)(a1 + 96) = v3;
return pthread_mutex_unlock(v1);
}

original_code:
void timer_handler(sigval_t args)
{
 struct single_connection * connection = (struct single_connection *) args.sival_ptr;
 unsigned short int status;
 connection->timer.inqueue++;
 pthread_mutex_lock(&(connection->timer.chk_mutex));
 if (connection->timer.disabled==0)
 {
  connection->timer.counter++;
  connection->timer.last_call=time(((void *)0));
  status=connection->timer.chk_function(connection->timer.chk_arguments);
 }
 else
 {
  status=0;
 }
 if (status)
 {
  if (connection->timer.chk_status==0)
  {
   Log (3,"chk_function for service [%s] returned an error state. Disabling service\n",connection->service_name);
  }
 }
 else
 {
  if (connection->timer.chk_status)
  {
   Log (3,"chk_function back for service [%s] returned to OK state. Re-enabling service\n",connection->service_name);
  }
 }
 connection->timer.chk_status=status;
 connection->timer.inqueue--;
 pthread_mutex_unlock(&(connection->timer.chk_mutex));
}

canonical_original_code:
void timer_handler(union sigval args)
{
 struct single_connection *connection = (struct single_connection *) args.sival_ptr;
 unsigned short status;
 connection->timer.inqueue++;
 pthread_mutex_lock(&(connection->timer.chk_mutex));
 if (connection->timer.disabled==0)
 {
  connection->timer.counter++;
  connection->timer.last_call=time(((void *)0));
  status=connection->timer.chk_function(connection->timer.chk_arguments);
 }
 else
 {
  status=0;
 }
 if (status)
 {
  if (connection->timer.chk_status==0)
  {
   Log (3,"chk_function for service [%s] returned an error state. Disabling service\n",connection->service_name);
  }
 }
 else
 {
  if (connection->timer.chk_status)
  {
   Log (3,"chk_function back for service [%s] returned to OK state. Re-enabling service\n",connection->service_name);
  }
 }
 connection->timer.chk_status=status;
 connection->timer.inqueue--;
 pthread_mutex_unlock(&(connection->timer.chk_mutex));
}

variable_types: struct single_connection { char *service_name; struct timer_action { void *timerid; long last_call; unsigned short disabled; int counter; int inqueue; int check_interval; unsigned short (*chk_function)(union sigval { int sival_int; void *sival_ptr; } args); union sigval chk_arguments; union pthread_mutex_t { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; unsigned int __nusers; int __kind; short __spins; short __elision; struct __pthread_internal_list { struct __pthread_internal_list *__prev; struct __pthread_internal_list *__next; } __list; } __data; char __size[40]; long __align; } chk_mutex; unsigned short chk_status; } timer; int listening_socket; struct sockaddr_in { unsigned short sin_family; unsigned short sin_port; struct in_addr { unsigned int s_addr; } sin_addr; unsigned char sin_zero[-1]; } servaddr; struct sockaddr_un { unsigned short sun_family; char sun_path[108]; } servaddr_un; unsigned short port; unsigned short pausable; unsigned short disabled; unsigned int counter; int running_sockets[3]; char buffer[3][4096]; unsigned short (*reply_function)(void *, int, int); } *

variable_types:
status
unsigned short