decompiled_code:
__int64 __fastcall binary_heap_decrease_storage(__int64 a1)
{
int v1; // eax
unsigned int v2; // er12
int v3; // ebp
__int64 result; // rax
void *v5; // rax

v1 = *(_DWORD *)(a1 + 4);
if ( !v1 )
return (unsigned int)-2;
if ( v1 == 1 )
{
free(*(void **)(a1 + 8));
*(_QWORD *)(a1 + 8) = 0LL;
result = 0LL;
*(_DWORD *)(a1 + 4) = 0;
}
else
{
v2 = 0;
v3 = v1 / 2;
if ( *(_DWORD *)a1 > v1 / 2 )
return v2;
v5 = realloc(*(void **)(a1 + 8), 8LL * v3);
if ( !v5 )
return (unsigned int)-1;
*(_QWORD *)(a1 + 8) = v5;
result = 0LL;
*(_DWORD *)(a1 + 4) = v3;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func4(__int64 a1)
{
int v1; // eax
unsigned int v2; // er12
int v3; // ebp
__int64 result; // rax
void *v5; // rax

v1 = *(_DWORD *)(a1 + 4);
if ( !v1 )
return (unsigned int)-2;
if ( v1 == 1 )
{
free(*(void **)(a1 + 8));
*(_QWORD *)(a1 + 8) = 0LL;
result = 0LL;
*(_DWORD *)(a1 + 4) = 0;
}
else
{
v2 = 0;
v3 = v1 / 2;
if ( *(_DWORD *)a1 > v1 / 2 )
return v2;
v5 = realloc(*(void **)(a1 + 8), 8LL * v3);
if ( !v5 )
return (unsigned int)-1;
*(_QWORD *)(a1 + 8) = v5;
result = 0LL;
*(_DWORD *)(a1 + 4) = v3;
}
return result;
}

original_code:
static int binary_heap_decrease_storage(binary_heap* heap) {
    if(heap->storage_size == 0) {
        return -2;
    }
    else if(heap->storage_size == 1) {
        free(heap->tree);
        heap->tree = 0;
        heap->storage_size = 0;
    }
    else {
        if(heap->size - 1 < heap->storage_size / 2) {
            int new_storage_size = heap->storage_size / 2;
            void **temp_storage = realloc(heap->tree, sizeof(void *) * new_storage_size);
            if(temp_storage == ((void *)0)) {
                return -1;
            }
            heap->tree = temp_storage;
            heap->storage_size = new_storage_size;
        }
    }
    return 0;
}

canonical_original_code:
static int binary_heap_decrease_storage(struct binary_heap *heap) {
    if(heap->storage_size == 0) {
        return -2;
    }
    else if(heap->storage_size == 1) {
        free(heap->tree);
        heap->tree = 0;
        heap->storage_size = 0;
    }
    else {
        if(heap->size - 1 < heap->storage_size / 2) {
            int new_storage_size = heap->storage_size / 2;
            void **temp_storage = realloc(heap->tree, sizeof(void *) * new_storage_size);
            if(temp_storage == ((void *)0)) {
                return -1;
            }
            heap->tree = temp_storage;
            heap->storage_size = new_storage_size;
        }
    }
    return 0;
}

user_defined_types: struct binary_heap { int size; int storage_size; void **tree; int (*comparator)(void *node1, void *node2); void (*destroy)(void *data); }


repo: vdaras/Huffman-Compression

decompiled_code:
__int64 __fastcall binary_heap_create(_QWORD *a1, __int64 a2, __int64 a3)
{
_QWORD *v4; // rax

v4 = malloc(0x20uLL);
*a1 = v4;
if ( !v4 )
return 0xFFFFFFFFLL;
*v4 = 0LL;
v4[1] = 0LL;
v4[2] = a2;
v4[3] = a3;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func5(_QWORD *a1, __int64 a2, __int64 a3)
{
_QWORD *v4; // rax

v4 = malloc(0x20uLL);
*a1 = v4;
if ( !v4 )
return 0xFFFFFFFFLL;
*v4 = 0LL;
v4[1] = 0LL;
v4[2] = a2;
v4[3] = a3;
return 0LL;
}

original_code:
int binary_heap_create(binary_heap** heap, int (*comparator)(void*, void*), void (*destroy)(void*)) {
    (*heap) = malloc(sizeof(binary_heap));
    if((*heap) == ((void *)0)) {
        return -1;
    }
    (*heap)->size = 0;
    (*heap)->storage_size = 0;
    (*heap)->tree = ((void *)0);
    (*heap)->comparator = comparator;
    (*heap)->destroy = destroy;
    return 0;
}

canonical_original_code:
int binary_heap_create(struct binary_heap **heap, int (*comparator)(void *, void *), void (*destroy)(void *)) {
    (*heap) = malloc(sizeof(binary_heap));
    if((*heap) == ((void *)0)) {
        return -1;
    }
    (*heap)->size = 0;
    (*heap)->storage_size = 0;
    (*heap)->tree = ((void *)0);
    (*heap)->comparator = comparator;
    (*heap)->destroy = destroy;
    return 0;
}

variable_types: struct binary_heap { int size; int storage_size; void **tree; int (*comparator)(void *node1, void *node2); void (*destroy)(void *data); } **

variable_types:
comparator
int (*)(void *, void *)

variable_types:
destroy
void (*)(void *)

return_type: int


original_code:
void binary_heap_destroy(binary_heap** heap) {
    binary_heap* temp = (*heap);
    if(temp->tree != ((void *)0)) {
        if(temp->destroy != ((void *)0)) {
            for(int i = 0; i < temp->size; i++) {
                temp->destroy(temp->tree[i]);
            }
        }
        free(temp->tree);
    }
    free(temp);
    (*heap) = ((void *)0);
}

canonical_original_code:
void binary_heap_destroy(struct binary_heap **heap) {
    struct binary_heap *temp = (*heap);
    if(temp->tree != ((void *)0)) {
        if(temp->destroy != ((void *)0)) {
            for(int i = 0; i < temp->size; i++) {
                temp->destroy(temp->tree[i]);
            }
        }
        free(temp->tree);
    }
    free(temp);
    (*heap) = ((void *)0);
}

variable_types: struct binary_heap { int size; int storage_size; void **tree; int (*comparator)(void *node1, void *node2); void (*destroy)(void *data); } *

variable_types: i int

return_type: void


original_code:
int binary_heap_insert(binary_heap* heap, const void* data) {
    int storage_status = binary_heap_increase_storage(heap);
    if(storage_status != 0) {
        return storage_status;
    }
    int new_element_pos = heap->size;
    int parent_element_pos = binary_heap_get_parent(new_element_pos);
    heap->tree[new_element_pos] = (void *) data;
    while(new_element_pos > 0
       && heap->comparator(heap->tree[parent_element_pos], heap->tree[new_element_pos]) < 0) {
        void* temp = heap->tree[parent_element_pos];
        heap->tree[parent_element_pos] = heap->tree[new_element_pos];
        heap->tree[new_element_pos] = temp;
        new_element_pos = parent_element_pos;
        parent_element_pos = binary_heap_get_parent(new_element_pos);
    }
    heap->size++;
    return 0;
}

canonical_original_code:
int binary_heap_insert(struct binary_heap *heap, void *data) {
    int storage_status = binary_heap_increase_storage(heap);
    if(storage_status != 0) {
        return storage_status;
    }
    int new_element_pos = heap->size;
    int parent_element_pos = binary_heap_get_parent(new_element_pos);
    heap->tree[new_element_pos] = (void *) data;
    while(new_element_pos > 0
       && heap->comparator(heap->tree[parent_element_pos], heap->tree[new_element_pos]) < 0) {
        void *temp = heap->tree[parent_element_pos];
        heap->tree[parent_element_pos] = heap->tree[new_element_pos];
        heap->tree[new_element_pos] = temp;
        new_element_pos = parent_element_pos;
        parent_element_pos = binary_heap_get_parent(new_element_pos);
    }
    heap->size++;
    return 0;
}

variable_types:
data
void *

variable_types:
temp
void *


original_code:
int binary_heap_extract(binary_heap* heap, void** data) {
    if(heap->size == 0) {
        return 1;
    }
    (*data) = heap->tree[0];
    void* save = heap->tree[heap->size - 1];
    int storage_status = binary_heap_decrease_storage(heap);
    if(storage_status != 0) {
        return storage_status;
    }
    heap->size--;
    int m_pos = 0;
    if(heap->size > 0) {
        heap->tree[0] = save;
        int curr_pos = 0;
        while(1) {
            int left_pos = binary_heap_get_left(curr_pos);
            int right_pos = binary_heap_get_right(curr_pos);
            if(left_pos < heap->size
            && heap->comparator(heap->tree[left_pos], heap->tree[curr_pos]) > 0) {
                m_pos = left_pos;
            } else {
                m_pos = curr_pos;
            }
            if(right_pos < heap->size
            && heap->comparator(heap->tree[right_pos], heap->tree[m_pos]) > 0) {
                m_pos = right_pos;
            }
            if(m_pos == curr_pos) {
                break;
            } else {
                void* temp = heap->tree[m_pos];
                heap->tree[m_pos] = heap->tree[curr_pos];
                heap->tree[curr_pos] = temp;
                curr_pos = m_pos;
            }
        }
    }
    return 0;
}

canonical_original_code:
int binary_heap_extract(struct binary_heap *heap, void **data) {
    if(heap->size == 0) {
        return 1;
    }
    (*data) = heap->tree[0];
    void *save = heap->tree[heap->size - 1];
    int storage_status = binary_heap_decrease_storage(heap);
    if(storage_status != 0) {
        return storage_status;
    }
    heap->size--;
    int m_pos = 0;
    if(heap->size > 0) {
        heap->tree[0] = save;
        int curr_pos = 0;
        while(1) {
            int left_pos = binary_heap_get_left(curr_pos);
            int right_pos = binary_heap_get_right(curr_pos);
            if(left_pos < heap->size
            && heap->comparator(heap->tree[left_pos], heap->tree[curr_pos]) > 0) {
                m_pos = left_pos;
            } else {
                m_pos = curr_pos;
            }
            if(right_pos < heap->size
            && heap->comparator(heap->tree[right_pos], heap->tree[m_pos]) > 0) {
                m_pos = right_pos;
            }
            if(m_pos == curr_pos) {
                break;
            } else {
                void *temp = heap->tree[m_pos];
                heap->tree[m_pos] = heap->tree[curr_pos];
                heap->tree[curr_pos] = temp;
                curr_pos = m_pos;
            }
        }
    }
    return 0;
}

variable_types:
data
void **

variable_types:
save
void *


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

v2 = a1 + 15;
if ( a1 >= 0 )
v2 = a1;
return (unsigned int)(2 * (v2 >> 4) + 2);
}

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

v2 = a1 + 15;
if ( a1 >= 0 )
v2 = a1;
return (unsigned int)(2 * (v2 >> 4) + 2);
}

original_code:
static int calculate_buffer_size(int bitset_size) {
    int total_buckets = (bitset_size / BUCKET_SIZE) + 1;
    return total_buckets * SHORT_INT_SIZE;
}


decompiled_code:
__int64 __fastcall bitset_create(_QWORD *a1, int a2)
{
_DWORD *v2; // rax
_DWORD *v3; // rbp
int v4; // eax
void *v5; // rax
__int64 result; // rax

v2 = malloc(0x10uLL);
if ( v2 )
{
v3 = v2;
v4 = calculate_buffer_size(a2);
v5 = calloc(v4, 1uLL);
*(_QWORD *)v3 = v5;
if ( v5 )
{
v3[2] = a2;
result = 0LL;
*a1 = v3;
return result;
}
free(v3);
}
*a1 = 0LL;
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, int a2)
{
_DWORD *v2; // rax
_DWORD *v3; // rbp
int v4; // eax
void *v5; // rax
__int64 result; // rax

v2 = malloc(0x10uLL);
if ( v2 )
{
v3 = v2;
v4 = func0(a2);
v5 = calloc(v4, 1uLL);
*(_QWORD *)v3 = v5;
if ( v5 )
{
v3[2] = a2;
result = 0LL;
*a1 = v3;
return result;
}
free(v3);
}
*a1 = 0LL;
return 0xFFFFFFFFLL;
}

original_code:
int bitset_create(bitset** bset, unsigned int size) {
    bitset* retval = malloc(sizeof(bitset));
    if(retval == ((void *)0)) {
        (*bset) = ((void *)0);
        return -1;
    }
    retval->total_bits = 0;
    int buffer_size = calculate_buffer_size(size);
    retval->bit_buffer = malloc(buffer_size);
    if(retval->bit_buffer == ((void *)0)) {
        free(retval);
        (*bset) = ((void *)0);
        return -1;
    }
    memset(retval->bit_buffer, 0, buffer_size);
    retval->total_bits = size;
    (*bset) = retval;
    return 0;
}

canonical_original_code:
int bitset_create(struct bitset **bset, unsigned int size) {
    struct bitset *retval = malloc(sizeof(bitset));
    if(retval == ((void *)0)) {
        (*bset) = ((void *)0);
        return -1;
    }
    retval->total_bits = 0;
    int buffer_size = calculate_buffer_size(size);
    retval->bit_buffer = malloc(buffer_size);
    if(retval->bit_buffer == ((void *)0)) {
        free(retval);
        (*bset) = ((void *)0);
        return -1;
    }
    memset(retval->bit_buffer, 0, buffer_size);
    retval->total_bits = size;
    (*bset) = retval;
    return 0;
}

variable_types: struct bitset { unsigned short *bit_buffer; unsigned int total_bits; } **

variable_types:
size
unsigned int


decompiled_code:
void __fastcall bitset_destroy(void ***a1)
{
free(**a1);
free(*a1);
*a1 = 0LL;
}

canonical_decompiled_code:
void __fastcall func2(void ***a1)
{
free(**a1);
free(*a1);
*a1 = 0LL;
}

original_code:
void bitset_destroy(bitset** bset) {
    free((*bset)->bit_buffer);
    free((*bset));
    (*bset) = ((void *)0);
}

canonical_original_code:
void bitset_destroy(struct bitset **bset) {
    free((*bset)->bit_buffer);
    free((*bset));
    (*bset) = ((void *)0);
}

user_defined_types: struct bitset { unsigned short *bit_buffer; unsigned int total_bits; }


original_code:
int bitset_set_bit(bitset* bset, unsigned int bit) {
    if(bit >= bset->total_bits) {
        return -2;
    }
    int bucket = bit / BUCKET_SIZE;
    int bit_index = BUCKET_SIZE - (bit - bucket * BUCKET_SIZE);
    int mask = 1 << (bit_index - 1);
    bset->bit_buffer[bucket] |= mask;
    return 0;
}

canonical_original_code:
int bitset_set_bit(struct bitset *bset, unsigned int bit) {
    if(bit >= bset->total_bits) {
        return -2;
    }
    int bucket = bit / BUCKET_SIZE;
    int bit_index = BUCKET_SIZE - (bit - bucket * BUCKET_SIZE);
    int mask = 1 << (bit_index - 1);
    bset->bit_buffer[bucket] |= mask;
    return 0;
}

variable_types: struct bitset { unsigned short *bit_buffer; unsigned int total_bits; } *

variable_types:
bit
unsigned int

variable_types: bucket int

variable_types: mask int


original_code:
int bitset_clear_bit(bitset* bset, unsigned int bit) {
    if(bit >= bset->total_bits) {
        return -2;
    }
    int bucket = bit / BUCKET_SIZE;
    int bit_index = BUCKET_SIZE - (bit - bucket * BUCKET_SIZE);
    int mask = ~(1 << (bit_index - 1));
    bset->bit_buffer[bucket] &= mask;
    return 0;
}

canonical_original_code:
int bitset_clear_bit(struct bitset *bset, unsigned int bit) {
    if(bit >= bset->total_bits) {
        return -2;
    }
    int bucket = bit / BUCKET_SIZE;
    int bit_index = BUCKET_SIZE - (bit - bucket * BUCKET_SIZE);
    int mask = ~(1 << (bit_index - 1));
    bset->bit_buffer[bucket] &= mask;
    return 0;
}


original_code:
int bitset_get_bit(bitset* bset, unsigned int bit, unsigned int* value) {
    if(bit >= bset->total_bits) {
        return -2;
    }
    int bucket = bit / BUCKET_SIZE;
    int bit_index = BUCKET_SIZE - (bit - bucket * BUCKET_SIZE);
    int mask = 1 << (bit_index - 1);
    (*value) = (bset->bit_buffer[bucket] & mask) != 0;
    return 0;
}

canonical_original_code:
int bitset_get_bit(struct bitset *bset, unsigned int bit, unsigned int *value) {
    if(bit >= bset->total_bits) {
        return -2;
    }
    int bucket = bit / BUCKET_SIZE;
    int bit_index = BUCKET_SIZE - (bit - bucket * BUCKET_SIZE);
    int mask = 1 << (bit_index - 1);
    (*value) = (bset->bit_buffer[bucket] & mask) != 0;
    return 0;
}

variable_types:
value
unsigned int *


decompiled_code:
__int64 __fastcall bitset_resize(__int64 a1, int a2)
{
int v2; // er13
int v3; // eax
int v5; // er12
char *v6; // r14

v2 = calculate_buffer_size(*(_DWORD *)(a1 + 8));
v3 = calculate_buffer_size(a2);
if ( v2 >= v3 )
goto LABEL_2;
v5 = v3;
v6 = (char *)realloc(*(void **)a1, v3);
if ( v6 )
{
memset(&v6[2 * (v2 / 2)], 0, 2 * (v5 / 2 - v2 / 2));
*(_QWORD *)a1 = v6;
LABEL_2:
*(_DWORD *)(a1 + 8) = a2;
return 0LL;
}
return 0xFFFFFFFFLL;
}

canonical_decompiled_code:
__int64 __fastcall func6(__int64 a1, int a2)
{
int v2; // er13
int v3; // eax
int v5; // er12
char *v6; // r14

v2 = func0(*(_DWORD *)(a1 + 8));
v3 = func0(a2);
if ( v2 >= v3 )
goto LABEL_2;
v5 = v3;
v6 = (char *)realloc(*(void **)a1, v3);
if ( v6 )
{
memset(&v6[2 * (v2 / 2)], 0, 2 * (v5 / 2 - v2 / 2));
*(_QWORD *)a1 = v6;
LABEL_2:
*(_DWORD *)(a1 + 8) = a2;
return 0LL;
}
return 0xFFFFFFFFLL;
}

original_code:
int bitset_resize(bitset* bset, int new_size) {
    int prev_buffer_size = calculate_buffer_size(bset->total_bits);
    int buffer_size = calculate_buffer_size(new_size);
    if(prev_buffer_size < buffer_size) {
        unsigned short int* temp = realloc(bset->bit_buffer, buffer_size);
        if(temp == ((void *)0)) {
             return -1;
        }
        int prev_total_buckets = (prev_buffer_size / SHORT_INT_SIZE);
        int new_buckets = (buffer_size / SHORT_INT_SIZE) - prev_total_buckets;
        memset(temp + prev_total_buckets, 0, new_buckets * SHORT_INT_SIZE);
        bset->bit_buffer = temp;
    }
    bset->total_bits = new_size;
    return 0;
}

canonical_original_code:
int bitset_resize(struct bitset *bset, int new_size) {
    int prev_buffer_size = calculate_buffer_size(bset->total_bits);
    int buffer_size = calculate_buffer_size(new_size);
    if(prev_buffer_size < buffer_size) {
        unsigned short *temp = realloc(bset->bit_buffer, buffer_size);
        if(temp == ((void *)0)) {
             return -1;
        }
        int prev_total_buckets = (prev_buffer_size / SHORT_INT_SIZE);
        int new_buckets = (buffer_size / SHORT_INT_SIZE) - prev_total_buckets;
        memset(temp + prev_total_buckets, 0, new_buckets * SHORT_INT_SIZE);
        bset->bit_buffer = temp;
    }
    bset->total_bits = new_size;
    return 0;
}

variable_types:
temp
unsigned short *


decompiled_code:
__int64 __fastcall bitset_copy(__int64 a1, void ***a2)
{
unsigned int v2; // er12

v2 = bitset_create(a2, *(_DWORD *)(a1 + 8));
if ( !v2 )
memcpy(**a2, *(const void **)a1, 2 * (*(_DWORD *)(a1 + 8) >> 4) + 2);
return v2;
}

original_code:
int bitset_copy(bitset* copy_of, bitset** copy) {
    int creation_status = bitset_create(copy, copy_of->total_bits);
    if(creation_status != 0) {
        return creation_status;
    }
    int num_buckets = (copy_of->total_bits / BUCKET_SIZE) + 1;
    memcpy((*copy)->bit_buffer, copy_of->bit_buffer, num_buckets * SHORT_INT_SIZE);
    return 0;
}

canonical_original_code:
int bitset_copy(struct bitset *copy_of, struct bitset **copy) {
    int creation_status = bitset_create(copy, copy_of->total_bits);
    if(creation_status != 0) {
        return creation_status;
    }
    int num_buckets = (copy_of->total_bits / BUCKET_SIZE) + 1;
    memcpy((*copy)->bit_buffer, copy_of->bit_buffer, num_buckets * SHORT_INT_SIZE);
    return 0;
}

variable_types:
copy
struct bitset **


decompiled_code:
__int64 __fastcall bitset_copy_bits(__int64 a1, void ***a2, unsigned int a3)
{
int v4; // ebx
int v5; // esi
unsigned int v6; // er12

if ( *(_DWORD *)(a1 + 8) >= a3 )
{
v4 = a3;
v5 = a3;
}
else
{
v4 = *(_DWORD *)(a1 + 8);
v5 = v4;
}
v6 = bitset_create(a2, v5);
if ( !v6 )
memcpy(**a2, *(const void **)a1, 2 * (v4 / 16) + 2);
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, void ***a2, unsigned int a3)
{
int v4; // ebx
int v5; // esi
unsigned int v6; // er12

if ( *(_DWORD *)(a1 + 8) >= a3 )
{
v4 = a3;
v5 = a3;
}
else
{
v4 = *(_DWORD *)(a1 + 8);
v5 = v4;
}
v6 = func1(a2, v5);
if ( !v6 )
memcpy(**a2, *(const void **)a1, 2 * (v4 / 16) + 2);
return v6;
}

original_code:
int bitset_copy_bits(bitset* copy_of, bitset** copy, int n_bits) {
    if(n_bits > copy_of->total_bits) {
        n_bits = copy_of->total_bits;
    }
    int creation_status = bitset_create(copy, n_bits);
    if(creation_status != 0) {
        return creation_status;
    }
    int num_buckets = (n_bits / BUCKET_SIZE) + 1;
    memcpy((*copy)->bit_buffer, copy_of->bit_buffer, num_buckets * SHORT_INT_SIZE);
    return 0;
}

canonical_original_code:
int bitset_copy_bits(struct bitset *copy_of, struct bitset **copy, int n_bits) {
    if(n_bits > copy_of->total_bits) {
        n_bits = copy_of->total_bits;
    }
    int creation_status = bitset_create(copy, n_bits);
    if(creation_status != 0) {
        return creation_status;
    }
    int num_buckets = (n_bits / BUCKET_SIZE) + 1;
    memcpy((*copy)->bit_buffer, copy_of->bit_buffer, num_buckets * SHORT_INT_SIZE);
    return 0;
}


decompiled_code:
__int64 __fastcall compare_huffman_nodes(__int64 a1, __int64 a2)
{
int v2; // edx
__int64 result; // rax

v2 = *(_DWORD *)(a2 + 4);
result = 1LL;
if ( *(_DWORD *)(a1 + 4) >= v2 )
result = (unsigned int)-(*(_DWORD *)(a1 + 4) != v2);
return result;
}

canonical_decompiled_code:
__int64 __fastcall func0(__int64 a1, __int64 a2)
{
int v2; // edx
__int64 result; // rax

v2 = *(_DWORD *)(a2 + 4);
result = 1LL;
if ( *(_DWORD *)(a1 + 4) >= v2 )
result = (unsigned int)-(*(_DWORD *)(a1 + 4) != v2);
return result;
}

original_code:
static int compare_huffman_nodes(void* node1, void* node2) {
    huffman_node* huff_node_1 = (huffman_node*) node1;
    huffman_node* huff_node_2 = (huffman_node*) node2;
    return huff_node_1->frequency < huff_node_2->frequency ? 1 :
            huff_node_1->frequency == huff_node_2->frequency ? 0 : -1;
}

canonical_original_code:
static int compare_huffman_nodes(void *node1, void *node2) {
    struct huffman_node_t *huff_node_1 = (huffman_node*) node1;
    struct huffman_node_t *huff_node_2 = (huffman_node*) node2;
    return huff_node_1->frequency < huff_node_2->frequency ? 1 :
            huff_node_1->frequency == huff_node_2->frequency ? 0 : -1;
}

variable_types: struct huffman_node_t { unsigned char which_char; int frequency; int is_leaf; struct huffman_node_t *parent; struct huffman_node_t *left; } *


decompiled_code:
void __fastcall huffman_node_destroy(void *ptr)
{
if ( ptr )
{
huffman_node_destroy(*((void **)ptr + 4));
huffman_node_destroy(*((void **)ptr + 3));
free(ptr);
}
}

canonical_decompiled_code:
void __fastcall func1(void *ptr)
{
if ( ptr )
{
func1(*((void **)ptr + 4));
func1(*((void **)ptr + 3));
free(ptr);
}
}

original_code:
static void huffman_node_destroy(void* node) {
    if(node == ((void *)0))
        return;
    huffman_node* h_node = (huffman_node*) node;
    huffman_node_destroy(h_node->right);
    huffman_node_destroy(h_node->left);
    free(h_node);
}

canonical_original_code:
static void huffman_node_destroy(void *node) {
    if(node == ((void *)0))
        return;
    struct huffman_node_t *h_node = (huffman_node*) node;
    huffman_node_destroy(h_node->right);
    huffman_node_destroy(h_node->left);
    free(h_node);
}

variable_types:
node
void *

user_defined_types: struct huffman_node_t { unsigned char which_char; int frequency; int is_leaf; struct huffman_node_t *parent; struct huffman_node_t *left; }


decompiled_code:
__int64 __fastcall check_and_resize_bitset(__int64 a1, unsigned int a2)
{
unsigned int v3; // esi
__int64 result; // rax

v3 = *(_DWORD *)(a1 + 8);
if ( v3 <= a2 )
result = (unsigned int)-((unsigned int)bitset_resize(a1, 2 * v3) == -1);
else
result = 0LL;
return result;
}

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

v3 = *(_DWORD *)(a1 + 8);
if ( v3 <= a2 )
result = (unsigned int)-((unsigned int)bitset_resize(a1, 2 * v3) == -1);
else
result = 0LL;
return result;
}

original_code:
static int check_and_resize_bitset(bitset* bset, int bits_stored) {
    if(bits_stored >= bset->total_bits) {
        int resize_status = bitset_resize(bset, bset->total_bits * 2);
        if(resize_status == -1) {
            return -1;
        }
    }
    return 0;
}

canonical_original_code:
static int check_and_resize_bitset(struct bitset *bset, int bits_stored) {
    if(bits_stored >= bset->total_bits) {
        int resize_status = bitset_resize(bset, bset->total_bits * 2);
        if(resize_status == -1) {
            return -1;
        }
    }
    return 0;
}


decompiled_code:
__int64 __fastcall huffman_tree_serialize_recurse(unsigned __int8 *a1, __int64 a2, unsigned int *a3)
{
unsigned int v6; // er12
__int64 v7; // rsi
unsigned int v8; // esi
unsigned __int8 v9; // r15
int v10; // ebp
bool v11; // cf
__int64 v12; // rsi

while ( 1 )
{
v6 = check_and_resize_bitset(a2, *a3);
if ( v6 )
break;
v7 = *a3;
if ( *((_DWORD *)a1 + 2) )
{
bitset_set_bit(a2, v7);
v8 = *a3 + 1;
*a3 = v8;
v9 = *a1;
v10 = 7;
while ( 1 )
{
v6 = check_and_resize_bitset(a2, v8);
if ( v6 )
break;
v12 = *a3;
if ( (v9 & (unsigned __int8)(1 << v10)) != 0 )
{
bitset_set_bit(a2, v12);
v8 = *a3 + 1;
*a3 = v8;
v11 = v10-- == 0;
if ( v11 )
return v6;
}
else
{
bitset_clear_bit(a2, v12);
v8 = *a3 + 1;
*a3 = v8;
v11 = v10-- == 0;
if ( v11 )
return v6;
}
}
return v6;
}
bitset_clear_bit(a2, v7);
++*a3;
v6 = huffman_tree_serialize_recurse(*((_QWORD *)a1 + 3), a2, a3);
if ( v6 )
return v6;
a1 = (unsigned __int8 *)*((_QWORD *)a1 + 4);
}
return v6;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 *a1, __int64 a2, unsigned int *a3)
{
unsigned int v6; // er12
__int64 v7; // rsi
unsigned int v8; // esi
unsigned __int8 v9; // r15
int v10; // ebp
bool v11; // cf
__int64 v12; // rsi

while ( 1 )
{
v6 = func2(a2, *a3);
if ( v6 )
break;
v7 = *a3;
if ( *((_DWORD *)a1 + 2) )
{
bitset_set_bit(a2, v7);
v8 = *a3 + 1;
*a3 = v8;
v9 = *a1;
v10 = 7;
while ( 1 )
{
v6 = func2(a2, v8);
if ( v6 )
break;
v12 = *a3;
if ( (v9 & (unsigned __int8)(1 << v10)) != 0 )
{
bitset_set_bit(a2, v12);
v8 = *a3 + 1;
*a3 = v8;
v11 = v10-- == 0;
if ( v11 )
return v6;
}
else
{
bitset_clear_bit(a2, v12);
v8 = *a3 + 1;
*a3 = v8;
v11 = v10-- == 0;
if ( v11 )
return v6;
}
}
return v6;
}
bitset_clear_bit(a2, v7);
++*a3;
v6 = func3(*((_QWORD *)a1 + 3), a2, a3);
if ( v6 )
return v6;
a1 = (unsigned __int8 *)*((_QWORD *)a1 + 4);
}
return v6;
}

original_code:
static int huffman_tree_serialize_recurse(huffman_node* curr_node, bitset* tree_binary_rep, int *bits_stored) {
    int resize_status = check_and_resize_bitset(tree_binary_rep, *bits_stored);
    if(resize_status != 0) {
        return resize_status;
    }
    if(curr_node->is_leaf) {
        bitset_set_bit(tree_binary_rep, *bits_stored);
        (*bits_stored)++;
        unsigned char byte = curr_node->which_char;
        for(int i = 0; i < 8; i++) {
            unsigned char mask = 1 << (8 - i - 1);
            int bit = (byte & mask) != 0;
            resize_status = check_and_resize_bitset(tree_binary_rep, *bits_stored);
            if(resize_status != 0) {
                return resize_status;
            }
            if(bit == 1) {
                bitset_set_bit(tree_binary_rep, *bits_stored);
            } else {
                bitset_clear_bit(tree_binary_rep, *bits_stored);
            }
            (*bits_stored)++;
        }
    } else {
        bitset_clear_bit(tree_binary_rep, *bits_stored);
        (*bits_stored)++;
        int left_status = huffman_tree_serialize_recurse(curr_node->left, tree_binary_rep, bits_stored);
        if(left_status != 0) {
            return left_status;
        }
        int right_status = huffman_tree_serialize_recurse(curr_node->right, tree_binary_rep, bits_stored);
        if(right_status != 0) {
            return right_status;
        }
    }
    return 0;
}

canonical_original_code:
static int huffman_tree_serialize_recurse(struct huffman_node_t *curr_node, struct bitset *tree_binary_rep, int *bits_stored) {
    int resize_status = check_and_resize_bitset(tree_binary_rep, *bits_stored);
    if(resize_status != 0) {
        return resize_status;
    }
    if(curr_node->is_leaf) {
        bitset_set_bit(tree_binary_rep, *bits_stored);
        (*bits_stored)++;
        unsigned char byte = curr_node->which_char;
        for(int i = 0; i < 8; i++) {
            unsigned char mask = 1 << (8 - i - 1);
            int bit = (byte & mask) != 0;
            resize_status = check_and_resize_bitset(tree_binary_rep, *bits_stored);
            if(resize_status != 0) {
                return resize_status;
            }
            if(bit == 1) {
                bitset_set_bit(tree_binary_rep, *bits_stored);
            } else {
                bitset_clear_bit(tree_binary_rep, *bits_stored);
            }
            (*bits_stored)++;
        }
    } else {
        bitset_clear_bit(tree_binary_rep, *bits_stored);
        (*bits_stored)++;
        int left_status = huffman_tree_serialize_recurse(curr_node->left, tree_binary_rep, bits_stored);
        if(left_status != 0) {
            return left_status;
        }
        int right_status = huffman_tree_serialize_recurse(curr_node->right, tree_binary_rep, bits_stored);
        if(right_status != 0) {
            return right_status;
        }
    }
    return 0;
}

variable_types:
byte
unsigned char

variable_types:
mask
unsigned char

variable_types: bit int


decompiled_code:
__int64 __fastcall huffman_node_create(_QWORD *a1)
{
_QWORD *v1; // rax

v1 = malloc(0x28uLL);
*a1 = v1;
if ( !v1 )
return 0xFFFFFFFFLL;
*(_BYTE *)v1 = 0;
*(_QWORD *)((char *)v1 + 4) = 0x100000000LL;
v1[4] = 0LL;
v1[3] = 0LL;
v1[2] = 0LL;
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func4(_QWORD *a1)
{
_QWORD *v1; // rax

v1 = malloc(0x28uLL);
*a1 = v1;
if ( !v1 )
return 0xFFFFFFFFLL;
*(_BYTE *)v1 = 0;
*(_QWORD *)((char *)v1 + 4) = 0x100000000LL;
v1[4] = 0LL;
v1[3] = 0LL;
v1[2] = 0LL;
return 0LL;
}

original_code:
int huffman_node_create(huffman_node** node) {
    (*node) = malloc(sizeof(huffman_node));
    if((*node) == ((void *)0)) {
        return -1;
    }
    (*node)->which_char = 0;
    (*node)->frequency = 0;
    (*node)->is_leaf = 1;
    (*node)->left = (*node)->right = ((void *)0);
    (*node)->parent = ((void *)0);
    return 0;
}

canonical_original_code:
int huffman_node_create(struct huffman_node_t **node) {
    (*node) = malloc(sizeof(huffman_node));
    if((*node) == ((void *)0)) {
        return -1;
    }
    (*node)->which_char = 0;
    (*node)->frequency = 0;
    (*node)->is_leaf = 1;
    (*node)->left = (*node)->right = ((void *)0);
    (*node)->parent = ((void *)0);
    return 0;
}

variable_types: struct huffman_node_t { unsigned char which_char; int frequency; int is_leaf; struct huffman_node_t *parent; struct huffman_node_t *left; } **


decompiled_code:
__int64 __fastcall huffman_tree_deserialize_recurse(__int64 a1, __int64 a2, _DWORD *a3)
{
__int64 result; // rax
__int64 v5; // rsi
__int64 v7; // rsi
int v8; // eax
int v9; // er15
int v10; // ebp
bool v11; // zf
void *v13; // rdx
unsigned int v14; // [rsp+Ch] [rbp-4Ch]
int v15; // [rsp+14h] [rbp-44h] BYREF
void *ptr[8]; // [rsp+18h] [rbp-40h] BYREF

result = 0LL;
v5 = (unsigned int)*a3;
if ( (unsigned int)v5 < *(_DWORD *)(a2 + 8) )
{
bitset_get_bit(a2, v5, &v15);
v7 = (unsigned int)(*a3 + 1);
v8 = v15;
*a3 = v7;
if ( !v8 )
{
result = huffman_node_create(ptr);
if ( (_DWORD)result )
return result;
LODWORD(result) = huffman_tree_deserialize_recurse(ptr[0], a2, a3);
if ( !(_DWORD)result )
{
*(void **)(a1 + 24) = ptr[0];
result = huffman_node_create(ptr);
if ( (_DWORD)result )
return result;
result = huffman_tree_deserialize_recurse(ptr[0], a2, a3);
if ( !(_DWORD)result )
{
v13 = ptr[0];
*(_DWORD *)(a1 + 8) = 0;
*(_QWORD *)(a1 + 32) = v13;
return result;
}
}
v14 = result;
huffman_node_destroy(ptr[0]);
return v14;
}
v9 = 7;
v10 = 0;
while ( 1 )
{
result = bitset_get_bit(a2, v7, ptr);
if ( (_DWORD)result == -2 )
break;
v11 = LODWORD(ptr[0]) == 1;
v7 = (unsigned int)(*a3 + 1);
*a3 = v7;
if ( v11 )
v10 |= 1 << v9;
if ( v9-- == 0 )
{
*(_BYTE *)a1 = v10;
return 0LL;
}
}
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, _DWORD *a3)
{
__int64 result; // rax
__int64 v5; // rsi
__int64 v7; // rsi
int v8; // eax
int v9; // er15
int v10; // ebp
bool v11; // zf
void *v13; // rdx
unsigned int v14; // [rsp+Ch] [rbp-4Ch]
int v15; // [rsp+14h] [rbp-44h] BYREF
void *ptr[8]; // [rsp+18h] [rbp-40h] BYREF

result = 0LL;
v5 = (unsigned int)*a3;
if ( (unsigned int)v5 < *(_DWORD *)(a2 + 8) )
{
bitset_get_bit(a2, v5, &v15);
v7 = (unsigned int)(*a3 + 1);
v8 = v15;
*a3 = v7;
if ( !v8 )
{
result = func4(ptr);
if ( (_DWORD)result )
return result;
LODWORD(result) = func5(ptr[0], a2, a3);
if ( !(_DWORD)result )
{
*(void **)(a1 + 24) = ptr[0];
result = func4(ptr);
if ( (_DWORD)result )
return result;
result = func5(ptr[0], a2, a3);
if ( !(_DWORD)result )
{
v13 = ptr[0];
*(_DWORD *)(a1 + 8) = 0;
*(_QWORD *)(a1 + 32) = v13;
return result;
}
}
v14 = result;
func1(ptr[0]);
return v14;
}
v9 = 7;
v10 = 0;
while ( 1 )
{
result = bitset_get_bit(a2, v7, ptr);
if ( (_DWORD)result == -2 )
break;
v11 = LODWORD(ptr[0]) == 1;
v7 = (unsigned int)(*a3 + 1);
*a3 = v7;
if ( v11 )
v10 |= 1 << v9;
if ( v9-- == 0 )
{
*(_BYTE *)a1 = v10;
return 0LL;
}
}
}
return result;
}

original_code:
static int huffman_tree_deserialize_recurse(huffman_node* curr_node, bitset* tree_binary_rep, int *bits_read) {
    if((*bits_read) >= tree_binary_rep->total_bits) {
        return 0;
    }
    int node_creation_status, deserialization_status;
    unsigned int curr_bit;
    bitset_get_bit(tree_binary_rep, *bits_read, &curr_bit);
    (*bits_read)++;
    if(curr_bit == 0) {
        huffman_node* new_node;
        node_creation_status = huffman_node_create(&new_node);
        if(node_creation_status != 0) {
            return node_creation_status;
        }
        deserialization_status = huffman_tree_deserialize_recurse(new_node, tree_binary_rep, bits_read);
        if(deserialization_status != 0) {
            huffman_node_destroy(new_node);
            return deserialization_status;
        }
        curr_node->left = new_node;
        node_creation_status = huffman_node_create(&new_node);
        if(node_creation_status != 0) {
            return node_creation_status;
        }
        deserialization_status = huffman_tree_deserialize_recurse(new_node, tree_binary_rep, bits_read);
        if(deserialization_status != 0) {
            huffman_node_destroy(new_node);
            return deserialization_status;
        }
        curr_node->right = new_node;
        curr_node->is_leaf = 0;
    } else {
        unsigned char byte = 0;
        for(int i = 0; i < 8; i++) {
            unsigned int bit;
            int bit_read_status = bitset_get_bit(tree_binary_rep, *bits_read, &bit);
            if(bit_read_status == -2) {
                return -2;
            }
            (*bits_read)++;
            if(bit == 1) {
                unsigned char mask = 1 << (8 - i - 1);
                byte |= mask;
            }
        }
        curr_node->which_char = byte;
    }
    return 0;
}

canonical_original_code:
static int huffman_tree_deserialize_recurse(struct huffman_node_t *curr_node, struct bitset *tree_binary_rep, int *bits_read) {
    if((*bits_read) >= tree_binary_rep->total_bits) {
        return 0;
    }
    int node_creation_status; int deserialization_status;
    unsigned int curr_bit;
    bitset_get_bit(tree_binary_rep, *bits_read, &curr_bit);
    (*bits_read)++;
    if(curr_bit == 0) {
        struct huffman_node_t *new_node;
        node_creation_status = huffman_node_create(&new_node);
        if(node_creation_status != 0) {
            return node_creation_status;
        }
        deserialization_status = huffman_tree_deserialize_recurse(new_node, tree_binary_rep, bits_read);
        if(deserialization_status != 0) {
            huffman_node_destroy(new_node);
            return deserialization_status;
        }
        curr_node->left = new_node;
        node_creation_status = huffman_node_create(&new_node);
        if(node_creation_status != 0) {
            return node_creation_status;
        }
        deserialization_status = huffman_tree_deserialize_recurse(new_node, tree_binary_rep, bits_read);
        if(deserialization_status != 0) {
            huffman_node_destroy(new_node);
            return deserialization_status;
        }
        curr_node->right = new_node;
        curr_node->is_leaf = 0;
    } else {
        unsigned char byte = 0;
        for(int i = 0; i < 8; i++) {
            unsigned int bit;
            int bit_read_status = bitset_get_bit(tree_binary_rep, *bits_read, &bit);
            if(bit_read_status == -2) {
                return -2;
            }
            (*bits_read)++;
            if(bit == 1) {
                unsigned char mask = 1 << (8 - i - 1);
                byte |= mask;
            }
        }
        curr_node->which_char = byte;
    }
    return 0;
}

variable_types: i int


decompiled_code:
int print_usage()
{
return puts("Usage: huffman_encoding -c[-d] infile outfile.");
}

canonical_decompiled_code:
int func0()
{
return puts("Usage: huffman_encoding -c[-d] infile outfile.");
}

original_code:
void print_usage() {
    printf("Usage: huffman_encoding -c[-d] infile outfile.\n");
}


decompiled_code:
__int64 __fastcall destroy_lookup(_QWORD *a1)
{
_QWORD *v1; // rbx
__int64 result; // rax

v1 = a1;
do
{
if ( *v1 )
result = bitset_destroy(v1);
++v1;
}
while ( v1 != a1 + 256 );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1)
{
_QWORD *v1; // rbx
__int64 result; // rax

v1 = a1;
do
{
if ( *v1 )
result = bitset_destroy(v1);
++v1;
}
while ( v1 != a1 + 256 );
return result;
}

original_code:
static void destroy_lookup(bitset* lookup[256]) {
    for(int i = 0; i < 256; i++) {
        if(lookup[i] != ((void *)0)) {
            bitset_destroy(&lookup[i]);
        }
    }
}

canonical_original_code:
static void destroy_lookup(struct bitset *lookup[256]) {
    for(int i = 0; i < 256; i++) {
        if(lookup[i] != ((void *)0)) {
            bitset_destroy(&lookup[i]);
        }
    }
}

variable_types: struct bitset { unsigned short *bit_buffer; unsigned int total_bits; } *[256]


decompiled_code:
__int64 __fastcall create_lookup_recurse(unsigned __int8 *a1, __int64 a2, __int64 a3, int a4, unsigned int a5)
{
unsigned int v7; // er12

if ( a4 )
{
bitset_clear_bit(a3, a5);
v7 = a5 + 1;
if ( *((_DWORD *)a1 + 2) )
{
LABEL_3:
bitset_copy_bits(a3, a2 + 8LL * *a1, v7);
return 0LL;
}
}
else
{
bitset_set_bit(a3, a5);
v7 = a5 + 1;
if ( *((_DWORD *)a1 + 2) )
goto LABEL_3;
}
create_lookup_recurse(*((_QWORD *)a1 + 3), a2, a3, 1LL, v7);
create_lookup_recurse(*((_QWORD *)a1 + 4), a2, a3, 0LL, v7);
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned __int8 *a1, __int64 a2, __int64 a3, int a4, unsigned int a5)
{
unsigned int v7; // er12

if ( a4 )
{
bitset_clear_bit(a3, a5);
v7 = a5 + 1;
if ( *((_DWORD *)a1 + 2) )
{
LABEL_3:
bitset_copy_bits(a3, a2 + 8LL * *a1, v7);
return 0LL;
}
}
else
{
bitset_set_bit(a3, a5);
v7 = a5 + 1;
if ( *((_DWORD *)a1 + 2) )
goto LABEL_3;
}
func2(*((_QWORD *)a1 + 3), a2, a3, 1LL, v7);
func2(*((_QWORD *)a1 + 4), a2, a3, 0LL, v7);
return 0LL;
}

original_code:
static int create_lookup_recurse(huffman_node* curr_node, bitset* lookup[256], bitset* curr_path, int is_left, int depth) {
    if(is_left) {
        bitset_clear_bit(curr_path, depth);
    } else {
        bitset_set_bit(curr_path, depth);
    }
    if(curr_node->is_leaf) {
        bitset_copy_bits(curr_path, &lookup[curr_node->which_char], depth + 1);
    } else {
        create_lookup_recurse(curr_node->left, lookup, curr_path, 1, depth + 1);
        create_lookup_recurse(curr_node->right, lookup, curr_path, 0, depth + 1);
    }
    return 0;
}

canonical_original_code:
static int create_lookup_recurse(struct huffman_node_t *curr_node, struct bitset *lookup[256], struct bitset *curr_path, int is_left, int depth) {
    if(is_left) {
        bitset_clear_bit(curr_path, depth);
    } else {
        bitset_set_bit(curr_path, depth);
    }
    if(curr_node->is_leaf) {
        bitset_copy_bits(curr_path, &lookup[curr_node->which_char], depth + 1);
    } else {
        create_lookup_recurse(curr_node->left, lookup, curr_path, 1, depth + 1);
        create_lookup_recurse(curr_node->right, lookup, curr_path, 0, depth + 1);
    }
    return 0;
}

variable_types: depth int


decompiled_code:
__int64 __fastcall create_lookup(__int64 a1, _QWORD *a2)
{
unsigned int v2; // er12
__int64 v4[4]; // [rsp+8h] [rbp-20h] BYREF

v2 = bitset_create(v4, 30LL);
if ( v2 == -1 )
return v2;
v2 = create_lookup_recurse(*(unsigned __int8 **)(a1 + 24), (__int64)a2, v4[0], 1, 0);
if ( !v2 )
{
v2 = create_lookup_recurse(*(unsigned __int8 **)(a1 + 32), (__int64)a2, v4[0], 0, 0);
if ( !v2 )
{
bitset_destroy(v4);
return v2;
}
}
bitset_destroy(v4);
destroy_lookup(a2);
return v2;
}

canonical_decompiled_code:
__int64 __fastcall func3(__int64 a1, _QWORD *a2)
{
unsigned int v2; // er12
__int64 v4[4]; // [rsp+8h] [rbp-20h] BYREF

v2 = bitset_create(v4, 30LL);
if ( v2 == -1 )
return v2;
v2 = func2(*(unsigned __int8 **)(a1 + 24), (__int64)a2, v4[0], 1, 0);
if ( !v2 )
{
v2 = func2(*(unsigned __int8 **)(a1 + 32), (__int64)a2, v4[0], 0, 0);
if ( !v2 )
{
bitset_destroy(v4);
return v2;
}
}
bitset_destroy(v4);
func1(a2);
return v2;
}

original_code:
static int create_lookup(huffman_node* huffman_root, bitset* lookup[256]) {
    bitset *curr_path;
    int bitset_creation_status = bitset_create(&curr_path, 30);
    if(bitset_creation_status == -1) {
        return -1;
    }
    int lookup_creation_status = create_lookup_recurse(huffman_root->left, lookup, curr_path, 1, 0);
    if(lookup_creation_status != 0) {
        bitset_destroy(&curr_path);
        destroy_lookup(lookup);
        return lookup_creation_status;
    }
    lookup_creation_status = create_lookup_recurse(huffman_root->right, lookup, curr_path, 0, 0);
    if(lookup_creation_status != 0) {
        bitset_destroy(&curr_path);
        destroy_lookup(lookup);
        return lookup_creation_status;
    }
    bitset_destroy(&curr_path);
    return 0;
}

canonical_original_code:
static int create_lookup(struct huffman_node_t *huffman_root, struct bitset *lookup[256]) {
    struct bitset *curr_path;
    int bitset_creation_status = bitset_create(&curr_path, 30);
    if(bitset_creation_status == -1) {
        return -1;
    }
    int lookup_creation_status = create_lookup_recurse(huffman_root->left, lookup, curr_path, 1, 0);
    if(lookup_creation_status != 0) {
        bitset_destroy(&curr_path);
        destroy_lookup(lookup);
        return lookup_creation_status;
    }
    lookup_creation_status = create_lookup_recurse(huffman_root->right, lookup, curr_path, 0, 0);
    if(lookup_creation_status != 0) {
        bitset_destroy(&curr_path);
        destroy_lookup(lookup);
        return lookup_creation_status;
    }
    bitset_destroy(&curr_path);
    return 0;
}


printLog

repo: vdeconinck/SMysqLogger

decompiled_code:
void printLog(int a1, const char *a2, ...)
{
struct tm *v2; // rax
time_t v3; // [rsp+0h] [rbp-F8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF
char s[32]; // [rsp+20h] [rbp-D8h] BYREF

if ( loggingLevel <= a1 )
{
v3 = time(0LL);
v2 = localtime(&v3);
strftime(s, 0x1EuLL, "%Y-%m-%d %H:%M:%S", v2);
fprintf(logfile, "%s - ", s);
va_start(arg, a2);
vfprintf(logfile, a2, arg);
fflush(logfile);
}
}

canonical_decompiled_code:
void func1(int a1, const char *a2, ...)
{
struct tm *v2; // rax
time_t v3; // [rsp+0h] [rbp-F8h] BYREF
gcc_va_list arg; // [rsp+8h] [rbp-F0h] BYREF
char s[32]; // [rsp+20h] [rbp-D8h] BYREF

if ( loggingLevel <= a1 )
{
v3 = time(0LL);
v2 = localtime(&v3);
strftime(s, 0x1EuLL, "%Y-%m-%d %H:%M:%S", v2);
fprintf(logfile, "%s - ", s);
va_start(arg, a2);
vfprintf(logfile, a2, arg);
fflush(logfile);
}
}

original_code:
int printLog(int level, const char *format, ...) {
    if (level >= _loggingLevel) {
        char buf[30];
        time_t now = time(((void *)0));
        struct tm *ts = localtime(&now);
        strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ts);
        fprintf(_logfile, "%s - ", buf);
        va_list args;
        __builtin_va_start(args,format);
        vfprintf(_logfile, format, args);
        __builtin_va_end(args);
        fflush(_logfile);
    }
}

canonical_original_code:
int printLog(int level, char *format, ...) {
    if (level >= _loggingLevel) {
        char buf[30];
        long now = time(((void *)0));
        struct tm *ts = localtime(&now);
        strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ts);
        fprintf(_logfile, "%s - ", buf);
        __builtin_va_list args;
        __builtin_va_start(args,format);
        vfprintf(_logfile, format, args);
        __builtin_va_end(args);
        fflush(_logfile);
    }
}

variable_types: level int

variable_types:
format
char *

variable_types:
buf
char [30]

variable_types: now long

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


lightLog

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

if ( loggingLevel <= a1 )
{
va_start(arg, a2);
vfprintf(logfile, a2, arg);
result = fflush(logfile);
}
return result;
}

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

if ( loggingLevel <= a1 )
{
va_start(arg, a2);
vfprintf(logfile, a2, arg);
result = fflush(logfile);
}
return result;
}

original_code:
int lightLog(int level, const char *format, ...) {
    if (level >= _loggingLevel) {
        va_list args;
        __builtin_va_start(args,format);
        vfprintf(_logfile, format, args);
        __builtin_va_end(args);
        fflush(_logfile);
    }
}

canonical_original_code:
int lightLog(int level, char *format, ...) {
    if (level >= _loggingLevel) {
        __builtin_va_list args;
        __builtin_va_start(args,format);
        vfprintf(_logfile, format, args);
        __builtin_va_end(args);
        fflush(_logfile);
    }
}


trimEnd

decompiled_code:
char *__fastcall trimEnd(char *s)
{
__int64 v1; // rbx
char *v2; // rax
unsigned __int64 v3; // rdx

v1 = 0x100002600LL;
if ( *s )
{
while ( 1 )
{
v2 = &s[strlen(s) - 1];
v3 = (unsigned __int8)*v2;
if ( (unsigned __int8)v3 > 0x20u || !_bittest64(&v1, v3) )
break;
*v2 = 0;
if ( !*s )
return s;
}
}
return s;
}

canonical_decompiled_code:
char *__fastcall func3(char *s)
{
__int64 v1; // rbx
char *v2; // rax
unsigned __int64 v3; // rdx

v1 = 0x100002600LL;
if ( *s )
{
while ( 1 )
{
v2 = &s[strlen(s) - 1];
v3 = (unsigned __int8)*v2;
if ( (unsigned __int8)v3 > 0x20u || !_bittest64(&v1, v3) )
break;
*v2 = 0;
if ( !*s )
return s;
}
}
return s;
}

original_code:
char* trimEnd (char* str) {
 while((str[0] != '\0') && ((str[strlen(str)-1] == ' ') || (str[strlen(str)-1] == '\t') || (str[strlen(str)-1] == '\r') || (str[strlen(str)-1] == '\n'))) str[strlen(str)-1]='\0';
 return str;
}

canonical_original_code:
char *trimEnd(char *str) {
 while((str[0] != '\0') && ((str[strlen(str)-1] == ' ') || (str[strlen(str)-1] == '\t') || (str[strlen(str)-1] == '\r') || (str[strlen(str)-1] == '\n'))) str[strlen(str)-1]='\0';
 return str;
}

variable_types:
str
char *

return_type: char *


trimStart

decompiled_code:
char *__fastcall trimStart(char *s)
{
unsigned __int64 v1; // rax
__int64 v2; // rbp
size_t v3; // rax
size_t i; // rbx
unsigned __int64 v5; // rdx

v1 = (unsigned __int8)*s;
if ( (unsigned __int8)v1 <= 0x20u )
{
v2 = 0x100002600LL;
if ( _bittest64(&v2, v1) )
{
v3 = strlen(s);
while ( 1 )
{
for ( i = 0LL; v3 > i; ++i )
{
s[i] = s[i + 1];
v3 = strlen(s);
}
v5 = (unsigned __int8)*s;
if ( (unsigned __int8)v5 > 0x20u )
break;
if ( !_bittest64(&v2, v5) )
return s;
}
}
}
return s;
}

canonical_decompiled_code:
char *__fastcall func4(char *s)
{
unsigned __int64 v1; // rax
__int64 v2; // rbp
size_t v3; // rax
size_t i; // rbx
unsigned __int64 v5; // rdx

v1 = (unsigned __int8)*s;
if ( (unsigned __int8)v1 <= 0x20u )
{
v2 = 0x100002600LL;
if ( _bittest64(&v2, v1) )
{
v3 = strlen(s);
while ( 1 )
{
for ( i = 0LL; v3 > i; ++i )
{
s[i] = s[i + 1];
v3 = strlen(s);
}
v5 = (unsigned __int8)*s;
if ( (unsigned __int8)v5 > 0x20u )
break;
if ( !_bittest64(&v2, v5) )
return s;
}
}
}
return s;
}


trim

decompiled_code:
char *__fastcall trim(char *a1)
{
char *v1; // rax

v1 = trimStart(a1);
return trimEnd(v1);
}

canonical_decompiled_code:
char *__fastcall func5(char *a1)
{
char *v1; // rax

v1 = func4(a1);
return func3(v1);
}

original_code:
char* trim(char* str) {
 return trimEnd(trimStart(str));
}

canonical_original_code:
char *trim(char *str) {
 return trimEnd(trimStart(str));
}


checkArrayConsistency

decompiled_code:
__int64 checkArrayConsistency()
{
__int64 v0; // rbx
const char *v1; // rcx
const char *v2; // rdx
const char *v3; // r8

v0 = 0LL;
printLog(1LL, "List of requested channels:\n");
lightLog(1LL, " channel_name -> db_column (db_type)\n");
lightLog(1LL, "------------------------------------------------------------------------------------\n");
do
{
v1 = &dbColumns[v0];
v2 = &channelNames[v0];
v3 = &dbTypes[v0];
v0 += 30LL;
lightLog(1LL, "%20s -> %s (%s)\n", v2, v1, v3);
}
while ( v0 != 510 );
return 17LL;
}

original_code:
int checkArrayConsistency() {
 int channelCount = sizeof(channelNames)/30;
 int columnCount = sizeof(dbColumns)/30;
 int typeCount = sizeof(dbTypes)/30;
 int i;
 printLog(1, "List of requested channels:\n");
    lightLog(1, "        channel_name -> db_column (db_type)\n");
    lightLog(1, "------------------------------------------------------------------------------------\n");
 for (i = 0; i < ((channelCount)>(((columnCount)>(typeCount)?(columnCount):(typeCount)))?(channelCount):(((columnCount)>(typeCount)?(columnCount):(typeCount)))); i++) {
  lightLog(1, "%20s -> %s (%s)\n", (i < channelCount ? channelNames[i]:"?"), (i < columnCount ? dbColumns[i]:"?"), (i < typeCount ? dbTypes[i]:"?"));
 }
 if (channelCount != columnCount) {
  printLog(6, "Error : inconsistent number of channel_names (%d) and db_columns (%d)!\n", channelCount, columnCount);
  exit(-20);
 }
 if (columnCount != typeCount) {
  printLog(6, "Error : inconsistent number of db_columns (%d) and db_types (%d)!\n", columnCount, typeCount);
  exit(-21);
 }
 return channelCount;
}

variable_types: channelCount int

variable_types: columnCount int

variable_types: typeCount int


logValues

decompiled_code:
__int64 __fastcall logValues(int a1, __int64 a2, int a3)
{
void *v3; // rsp
void *v4; // rsp
unsigned int *v5; // r12
char *v6; // r13
double v7; // rbx
const char *v8; // r15
unsigned int v9; // eax
unsigned int v10; // er14
const char *v11; // rdx
struct tm *v12; // rax
time_t v13; // rax
__int64 v14; // r12
char *v15; // r15
int v16; // er14
unsigned int *v17; // r13
char *v18; // rbx
double v19; // r12
char *v20; // rdi
unsigned int v21; // eax
int v23; // ebx
double v24; // xmm0_8
struct tm *v25; // rax
int v26; // er13
char *v27; // rbx
__int64 v28; // rdi
__int64 v29; // rdi
_BYTE v30[4]; // [rsp+0h] [rbp-5B0h] BYREF
int v31; // [rsp+4h] [rbp-5ACh]
double v32; // [rsp+8h] [rbp-5A8h]
unsigned int *v33; // [rsp+10h] [rbp-5A0h]
int v34; // [rsp+18h] [rbp-598h]
int v35; // [rsp+1Ch] [rbp-594h]
unsigned int *v36; // [rsp+20h] [rbp-590h]
char *dest; // [rsp+28h] [rbp-588h]
__int64 v38; // [rsp+30h] [rbp-580h]
double v39; // [rsp+38h] [rbp-578h]
double v40; // [rsp+48h] [rbp-568h] BYREF
time_t time0[4]; // [rsp+50h] [rbp-560h] BYREF
char v42[32]; // [rsp+70h] [rbp-540h] BYREF
time_t timer[8]; // [rsp+90h] [rbp-520h] BYREF
char src[1248]; // [rsp+D0h] [rbp-4E0h] BYREF

v34 = a3;
v3 = alloca(30LL * a3);
v35 = a1;
dest = v30;
v33 = (unsigned int *)a2;
v38 = 4LL * a3;
v4 = alloca(4 * a1 * (__int64)a3);
v32 = COERCE_DOUBLE(v30);
if ( a1 <= 0 )
{
v31 = v34 - 1;
}
else
{
v39 = v32;
v5 = v33;
v36 = &v33[v35 - 1 + 1];
v31 = v34 - 1;
v6 = &channelNames[30 * (v34 - 1) + 30];
do
{
v7 = v39;
v8 = channelNames;
if ( v34 > 0 )
{
do
{
v9 = FindChannelName(*v5, v8);
**(_DWORD **)&v7 = v9;
v10 = v9;
if ( !v9 )
{
printLog(6LL, "Error : cannot determine handle of channel name '%s' for device %d\n", v8, *v5);
v26 = GetChannelHandlesEx(*v5, src, 300LL, 0LL);
GetDeviceName(*v5, timer, 49LL);
lightLog(
5LL,
"Here are the %d available channels for device '%s':\n",
(unsigned int)v26,
(const char *)timer);
lightLog(5LL, "---------------------------------------\n");
lightLog(5LL, "| Channel name | Channel Unit |\n");
lightLog(5LL, "---------------------------------------\n");
if ( v26 > 0 )
{
v27 = src;
do
{
v28 = *(unsigned int *)v27;
v27 += 4;
GetChannelName(v28, v42, 29LL);
v29 = *((unsigned int *)v27 - 1);
LOBYTE(time0[0]) = 0;
GetChannelUnit(v29, time0, 16LL);
lightLog(5LL, "| %16s | %-16s |\n", v42, (const char *)time0);
}
while ( &src[4 * (v26 - 1) + 4] != v27 );
}
lightLog(5LL, "---------------------------------------\n");
exit(-50);
}
*(_QWORD *)&v7 += 4LL;
GetChannelName(v9, v42, 29LL);
v11 = v8;
v8 += 30;
printLog(1LL, "Channel '%s' has handle %d on device %d\n", v11, v10, *v5);
}
while ( v8 != v6 );
}
++v5;
*(_QWORD *)&v39 += v38;
}
while ( v5 != v36 );
}
timer[0] = time(0LL);
v12 = localtime(timer);
*(_QWORD *)&v12->tm_sec = 0LL;
v12->tm_hour = 0;
v13 = mktime(v12);
v14 = requestedInterval - (int)difftime(timer[0], v13) % (int)requestedInterval;
printLog(2LL, "Waiting for %d seconds...\n", v14);
sleep(v14);
v15 = &dest[30 * v31 + 30];
while ( 1 )
{
time0[0] = time(0LL);
if ( v35 <= 0 )
goto LABEL_30;
v16 = 0;
if ( v34 > 0 )
break;
LABEL_26:
v23 = v34;
while ( 1 )
{
++v16;
insertValues((unsigned int)v23, dbColumns, dest);
lightLog(2LL, ".");
if ( v35 <= v16 )
break;
if ( v23 > 0 )
goto LABEL_10;
}
LABEL_30:
timer[0] = time(0LL);
v24 = difftime(timer[0], time0[0]);
if ( (double)(int)requestedInterval <= v24 )
{
printLog(
4LL,
"Warning : Processing took %f seconds. Can't keep up with the %d seconds requested interval. Please choose a higher value.\n",
v24,
requestedInterval);
time0[0] = time(0LL);
}
else
{
v39 = v24;
v25 = localtime(time0);
v25->tm_sec += requestedInterval;
time0[0] = mktime(v25);
sleep((int)((double)(int)requestedInterval - v39));
}
}
LABEL_10:
LODWORD(v36) = 1;
while ( 2 )
{
v17 = &v33[v16];
*(_QWORD *)&v39 = *(_QWORD *)&v32 + v16 * v38;
LABEL_12:
v18 = dest;
v19 = v39;
do
{
v21 = GetChannelValue((unsigned int)**(_DWORD **)&v19, *v17, &v40, src, 29LL, (unsigned int)maxChannelValueAge);
if ( v21 )
{
++v16;
if ( v21 != -3 )
printLog(4LL, "Error reading channel value....error code=%d\n", v21);
if ( v35 > v16 )
{
++v17;
*(_QWORD *)&v39 += v38;
LODWORD(v36) = 0;
goto LABEL_12;
}
return 0LL;
}
if ( !src[0] )
sprintf(src, "%.6f", v40);
v20 = v18;
v18 += 30;
*(_QWORD *)&v19 += 4LL;
strcpy(v20, src);
}
while ( v15 != v18 );
if ( (_DWORD)v36 )
goto LABEL_26;
if ( v35 > ++v16 )
{
while ( v34 <= 0 )
{
if ( v35 <= ++v16 )
return 0LL;
}
continue;
}
return 0LL;
}
}

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int64 a2, int a3)
{
void *v3; // rsp
void *v4; // rsp
unsigned int *v5; // r12
char *v6; // r13
double v7; // rbx
const char *v8; // r15
unsigned int v9; // eax
unsigned int v10; // er14
const char *v11; // rdx
struct tm *v12; // rax
time_t v13; // rax
__int64 v14; // r12
char *v15; // r15
int v16; // er14
unsigned int *v17; // r13
char *v18; // rbx
double v19; // r12
char *v20; // rdi
unsigned int v21; // eax
int v23; // ebx
double v24; // xmm0_8
struct tm *v25; // rax
int v26; // er13
char *v27; // rbx
__int64 v28; // rdi
__int64 v29; // rdi
_BYTE v30[4]; // [rsp+0h] [rbp-5B0h] BYREF
int v31; // [rsp+4h] [rbp-5ACh]
double v32; // [rsp+8h] [rbp-5A8h]
unsigned int *v33; // [rsp+10h] [rbp-5A0h]
int v34; // [rsp+18h] [rbp-598h]
int v35; // [rsp+1Ch] [rbp-594h]
unsigned int *v36; // [rsp+20h] [rbp-590h]
char *dest; // [rsp+28h] [rbp-588h]
__int64 v38; // [rsp+30h] [rbp-580h]
double v39; // [rsp+38h] [rbp-578h]
double v40; // [rsp+48h] [rbp-568h] BYREF
time_t time0[4]; // [rsp+50h] [rbp-560h] BYREF
char v42[32]; // [rsp+70h] [rbp-540h] BYREF
time_t timer[8]; // [rsp+90h] [rbp-520h] BYREF
char src[1248]; // [rsp+D0h] [rbp-4E0h] BYREF

v34 = a3;
v3 = alloca(30LL * a3);
v35 = a1;
dest = v30;
v33 = (unsigned int *)a2;
v38 = 4LL * a3;
v4 = alloca(4 * a1 * (__int64)a3);
v32 = COERCE_DOUBLE(v30);
if ( a1 <= 0 )
{
v31 = v34 - 1;
}
else
{
v39 = v32;
v5 = v33;
v36 = &v33[v35 - 1 + 1];
v31 = v34 - 1;
v6 = &channelNames[30 * (v34 - 1) + 30];
do
{
v7 = v39;
v8 = channelNames;
if ( v34 > 0 )
{
do
{
v9 = FindChannelName(*v5, v8);
**(_DWORD **)&v7 = v9;
v10 = v9;
if ( !v9 )
{
printLog(6LL, "Error : cannot determine handle of channel name '%s' for device %d\n", v8, *v5);
v26 = GetChannelHandlesEx(*v5, src, 300LL, 0LL);
GetDeviceName(*v5, timer, 49LL);
lightLog(
5LL,
"Here are the %d available channels for device '%s':\n",
(unsigned int)v26,
(const char *)timer);
lightLog(5LL, "---------------------------------------\n");
lightLog(5LL, "| Channel name | Channel Unit |\n");
lightLog(5LL, "---------------------------------------\n");
if ( v26 > 0 )
{
v27 = src;
do
{
v28 = *(unsigned int *)v27;
v27 += 4;
GetChannelName(v28, v42, 29LL);
v29 = *((unsigned int *)v27 - 1);
LOBYTE(time0[0]) = 0;
GetChannelUnit(v29, time0, 16LL);
lightLog(5LL, "| %16s | %-16s |\n", v42, (const char *)time0);
}
while ( &src[4 * (v26 - 1) + 4] != v27 );
}
lightLog(5LL, "---------------------------------------\n");
exit(-50);
}
*(_QWORD *)&v7 += 4LL;
GetChannelName(v9, v42, 29LL);
v11 = v8;
v8 += 30;
printLog(1LL, "Channel '%s' has handle %d on device %d\n", v11, v10, *v5);
}
while ( v8 != v6 );
}
++v5;
*(_QWORD *)&v39 += v38;
}
while ( v5 != v36 );
}
timer[0] = time(0LL);
v12 = localtime(timer);
*(_QWORD *)&v12->tm_sec = 0LL;
v12->tm_hour = 0;
v13 = mktime(v12);
v14 = requestedInterval - (int)difftime(timer[0], v13) % (int)requestedInterval;
printLog(2LL, "Waiting for %d seconds...\n", v14);
sleep(v14);
v15 = &dest[30 * v31 + 30];
while ( 1 )
{
time0[0] = time(0LL);
if ( v35 <= 0 )
goto LABEL_30;
v16 = 0;
if ( v34 > 0 )
break;
LABEL_26:
v23 = v34;
while ( 1 )
{
++v16;
insertValues((unsigned int)v23, dbColumns, dest);
lightLog(2LL, ".");
if ( v35 <= v16 )
break;
if ( v23 > 0 )
goto LABEL_10;
}
LABEL_30:
timer[0] = time(0LL);
v24 = difftime(timer[0], time0[0]);
if ( (double)(int)requestedInterval <= v24 )
{
printLog(
4LL,
"Warning : Processing took %f seconds. Can't keep up with the %d seconds requested interval. Please choose a higher value.\n",
v24,
requestedInterval);
time0[0] = time(0LL);
}
else
{
v39 = v24;
v25 = localtime(time0);
v25->tm_sec += requestedInterval;
time0[0] = mktime(v25);
sleep((int)((double)(int)requestedInterval - v39));
}
}
LABEL_10:
LODWORD(v36) = 1;
while ( 2 )
{
v17 = &v33[v16];
*(_QWORD *)&v39 = *(_QWORD *)&v32 + v16 * v38;
LABEL_12:
v18 = dest;
v19 = v39;
do
{
v21 = GetChannelValue((unsigned int)**(_DWORD **)&v19, *v17, &v40, src, 29LL, (unsigned int)maxChannelValueAge);
if ( v21 )
{
++v16;
if ( v21 != -3 )
printLog(4LL, "Error reading channel value....error code=%d\n", v21);
if ( v35 > v16 )
{
++v17;
*(_QWORD *)&v39 += v38;
LODWORD(v36) = 0;
goto LABEL_12;
}
return 0LL;
}
if ( !src[0] )
sprintf(src, "%.6f", v40);
v20 = v18;
v18 += 30;
*(_QWORD *)&v19 += 4LL;
strcpy(v20, src);
}
while ( v15 != v18 );
if ( (_DWORD)v36 )
goto LABEL_26;
if ( v35 > ++v16 )
{
while ( v34 <= 0 )
{
if ( v35 <= ++v16 )
return 0LL;
}
continue;
}
return 0LL;
}
}

original_code:
BOOL logValues(int deviceCount, DWORD deviceHandles[], int channelCount) {
 int res;
 char channelValues[channelCount][30];
 char chanName[30];
 DWORD channelHandles[deviceCount][channelCount];
 double value;
 char textValue[30];
 time_t loopStart, dayStart, now;
 double elapsedSecs;
 int deviceNr, channelNr, delay;
 for (deviceNr = 0; deviceNr < deviceCount; deviceNr++) {
  for (channelNr = 0; channelNr < channelCount; channelNr++) {
   channelHandles[deviceNr][channelNr] = FindChannelName(deviceHandles[deviceNr], channelNames[channelNr]);
   if (channelHandles[deviceNr][channelNr] <= 0) {
    DWORD chanHandle[300];
    char deviceName[50];
    char cUnit[17];
    int realChannelNr;
    printLog(6, "Error : cannot determine handle of channel name '%s' for device %d\n", channelNames[channelNr], deviceHandles[deviceNr]);
    int realChannelCount = GetChannelHandlesEx(deviceHandles[deviceNr], chanHandle, 300, SPOTCHANNELS);
    GetDeviceName(deviceHandles[deviceNr], deviceName, sizeof(deviceName)-1);
    lightLog(5, "Here are the %d available channels for device '%s':\n", realChannelCount, deviceName);
    lightLog(5, "---------------------------------------\n");
    lightLog(5, "|   Channel name   |   Channel Unit   |\n");
    lightLog(5, "---------------------------------------\n");
    for(realChannelNr = 0; realChannelNr < realChannelCount; realChannelNr++)
    {
     GetChannelName(chanHandle[realChannelNr], chanName, sizeof(chanName)-1);
     cUnit[0]=0;
     GetChannelUnit(chanHandle[realChannelNr], cUnit, sizeof(cUnit)-1);
     lightLog(5, "| %16s | %-16s |\n", chanName, cUnit);
    }
    lightLog(5, "---------------------------------------\n");
    exit(-50);
   }
   GetChannelName(channelHandles[deviceNr][channelNr], chanName, sizeof(chanName)-1);
   printLog(1, "Channel '%s' has handle %d on device %d\n", channelNames[channelNr], channelHandles[deviceNr][channelNr], deviceHandles[deviceNr]);
  }
 }
 BOOL loop = (1 == 1);
 BOOL readOK = (1 == 1);
 now = time(((void *)0));
 struct tm *cal = localtime(&now);
 cal->tm_hour = 0;
 cal->tm_min = 0;
 cal->tm_sec = 0;
 dayStart = mktime(cal);
 elapsedSecs = difftime(now, dayStart);
 delay = requestedInterval - ((int)(elapsedSecs) % requestedInterval);
 printLog(2, "Waiting for %d seconds...\n", delay);
 sleep (delay);
 while (loop) {
  loopStart = time(((void *)0));
  for (deviceNr = 0; deviceNr < deviceCount; deviceNr++) {
   for (channelNr=0; channelNr<channelCount; channelNr++) {
    res = GetChannelValue(channelHandles[deviceNr][channelNr], deviceHandles[deviceNr], &value, textValue, sizeof(textValue)-1, maxChannelValueAge);
    if (res == 0) {
     if (strlen(textValue) == 0) {
      sprintf(textValue, "%.6f", value);
     }
    }
    else {
                    if (res != -3) {
                        printLog(4, "Error reading channel value....error code=%d\n", res);
                    }
                    readOK = (1 == 0);
                    loop = (1 == 0);
     break;
    }
    strcpy(channelValues[channelNr], textValue);
   }
   if (readOK) {
    insertValues(channelCount, dbColumns, channelValues);
    lightLog(2, ".");
   }
  }
  if (loop) {
            now = time(((void *)0));
            elapsedSecs = difftime(now, loopStart);
            if (elapsedSecs < requestedInterval) {
                struct tm *cal = localtime(&loopStart);
                cal->tm_sec += requestedInterval;
                loopStart = mktime(cal);
                delay = requestedInterval - elapsedSecs;
                sleep(delay);
            }
            else {
                printLog(4, "Warning : Processing took %f seconds. Can't keep up with the %d seconds requested interval. Please choose a higher value.\n", elapsedSecs, requestedInterval);
                loopStart = time(((void *)0));
            }
        }
 }
    return readOK;
}

canonical_original_code:
int logValues(int deviceCount, unsigned int deviceHandles[], int channelCount) {
 int res;
 char channelValues[-1][30];
 char chanName[30];
 unsigned int channelHandles[-1][-1];
 double value;
 char textValue[30];
 long loopStart; long dayStart; long now;
 double elapsedSecs;
 int deviceNr; int channelNr; int delay;
 for (deviceNr = 0; deviceNr < deviceCount; deviceNr++) {
  for (channelNr = 0; channelNr < channelCount; channelNr++) {
   channelHandles[deviceNr][channelNr] = FindChannelName(deviceHandles[deviceNr], channelNames[channelNr]);
   if (channelHandles[deviceNr][channelNr] <= 0) {
    unsigned int chanHandle[300];
    char deviceName[50];
    char cUnit[17];
    int realChannelNr;
    printLog(6, "Error : cannot determine handle of channel name '%s' for device %d\n", channelNames[channelNr], deviceHandles[deviceNr]);
    int realChannelCount = GetChannelHandlesEx(deviceHandles[deviceNr], chanHandle, 300, SPOTCHANNELS);
    GetDeviceName(deviceHandles[deviceNr], deviceName, sizeof(deviceName)-1);
    lightLog(5, "Here are the %d available channels for device '%s':\n", realChannelCount, deviceName);
    lightLog(5, "---------------------------------------\n");
    lightLog(5, "|   Channel name   |   Channel Unit   |\n");
    lightLog(5, "---------------------------------------\n");
    for(realChannelNr = 0; realChannelNr < realChannelCount; realChannelNr++)
    {
     GetChannelName(chanHandle[realChannelNr], chanName, sizeof(chanName)-1);
     cUnit[0]=0;
     GetChannelUnit(chanHandle[realChannelNr], cUnit, sizeof(cUnit)-1);
     lightLog(5, "| %16s | %-16s |\n", chanName, cUnit);
    }
    lightLog(5, "---------------------------------------\n");
    exit(-50);
   }
   GetChannelName(channelHandles[deviceNr][channelNr], chanName, sizeof(chanName)-1);
   printLog(1, "Channel '%s' has handle %d on device %d\n", channelNames[channelNr], channelHandles[deviceNr][channelNr], deviceHandles[deviceNr]);
  }
 }
 int loop = (1 == 1);
 int readOK = (1 == 1);
 now = time(((void *)0));
 struct tm *cal = localtime(&now);
 cal->tm_hour = 0;
 cal->tm_min = 0;
 cal->tm_sec = 0;
 dayStart = mktime(cal);
 elapsedSecs = difftime(now, dayStart);
 delay = requestedInterval - ((int)(elapsedSecs) % requestedInterval);
 printLog(2, "Waiting for %d seconds...\n", delay);
 sleep (delay);
 while (loop) {
  loopStart = time(((void *)0));
  for (deviceNr = 0; deviceNr < deviceCount; deviceNr++) {
   for (channelNr=0; channelNr<channelCount; channelNr++) {
    res = GetChannelValue(channelHandles[deviceNr][channelNr], deviceHandles[deviceNr], &value, textValue, sizeof(textValue)-1, maxChannelValueAge);
    if (res == 0) {
     if (strlen(textValue) == 0) {
      sprintf(textValue, "%.6f", value);
     }
    }
    else {
                    if (res != -3) {
                        printLog(4, "Error reading channel value....error code=%d\n", res);
                    }
                    readOK = (1 == 0);
                    loop = (1 == 0);
     break;
    }
    strcpy(channelValues[channelNr], textValue);
   }
   if (readOK) {
    insertValues(channelCount, dbColumns, channelValues);
    lightLog(2, ".");
   }
  }
  if (loop) {
            now = time(((void *)0));
            elapsedSecs = difftime(now, loopStart);
            if (elapsedSecs < requestedInterval) {
                struct tm *cal = localtime(&loopStart);
                cal->tm_sec += requestedInterval;
                loopStart = mktime(cal);
                delay = requestedInterval - elapsedSecs;
                sleep(delay);
            }
            else {
                printLog(4, "Warning : Processing took %f seconds. Can't keep up with the %d seconds requested interval. Please choose a higher value.\n", elapsedSecs, requestedInterval);
                loopStart = time(((void *)0));
            }
        }
 }
    return readOK;
}

variable_types: deviceCount int

variable_types:
deviceHandles
unsigned int []

variable_types: res int

variable_types:
channelValues
char [-1][30]

variable_types:
chanName
char [30]

variable_types:
channelHandles
unsigned int [-1][-1]

variable_types: value double

variable_types:
textValue
char [30]

variable_types: loopStart long

variable_types: dayStart long

variable_types: elapsedSecs double

variable_types: deviceNr int

variable_types: channelNr int

variable_types: delay int

variable_types:
chanHandle
unsigned int [300]

variable_types:
deviceName
char [50]

variable_types:
cUnit
char [17]

variable_types: realChannelNr int

variable_types: realChannelCount int

variable_types: loop int

variable_types: readOK int

user_defined_types: struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; long tm_gmtoff; char *tm_zone; }


startDeviceLogging

decompiled_code:
__int64 __fastcall startDeviceLogging(unsigned int a1, __int64 a2, int a3)
{
__int64 v5; // rbp
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int *v8; // rbx
__int64 v9; // rdi
__int64 result; // rax
char v11[104]; // [rsp+0h] [rbp-68h] BYREF

printLog(2LL, "Starting detection of %d device(s). Please wait...\n", a1);
v5 = a2 + 4LL * (a1 - 1) + 4;
do
{
while ( 1 )
{
v6 = DoStartDeviceDetection(a1, 1LL);
if ( v6 != -1 )
break;
lightLog(2LL, "x");
sleep(requestedInterval);
}
if ( v6 )
{
if ( v6 != -9 )
{
printLog(6LL, "\nUnknown error : %d\n", v6);
exit(-41);
}
printLog(6LL, "\nError: there is already an running device detection.\n");
exit(-40);
}
lightLog(2LL, "\n");
v7 = GetDeviceHandles(a2, a1);
if ( a1 != v7 )
{
printLog(6LL, "Error : GetDeviceHandles returned %d device(s) while we expect %d\n", v7, a1);
exit(-42);
}
lightLog(0LL, "-------------------------------------------\n");
lightLog(0LL, "Device handle | Device Name \n");
lightLog(0LL, "-------------------------------------------\n");
if ( (int)a1 > 0 )
{
v8 = (unsigned int *)a2;
do
{
v9 = *v8++;
GetDeviceName(v9, v11, 49LL);
lightLog(0LL, " %3lu | '%s'\n", *(v8 - 1), v11);
}
while ( v8 != (unsigned int *)v5 );
}
lightLog(0LL, "-------------------------------------------\n\n");
result = logValues(a1, a2, a3);
}
while ( !(_DWORD)result );
return result;
}

canonical_decompiled_code:
__int64 __fastcall func2(unsigned int a1, __int64 a2, int a3)
{
__int64 v5; // rbp
unsigned int v6; // eax
unsigned int v7; // eax
unsigned int *v8; // rbx
__int64 v9; // rdi
__int64 result; // rax
char v11[104]; // [rsp+0h] [rbp-68h] BYREF

printLog(2LL, "Starting detection of %d device(s). Please wait...\n", a1);
v5 = a2 + 4LL * (a1 - 1) + 4;
do
{
while ( 1 )
{
v6 = DoStartDeviceDetection(a1, 1LL);
if ( v6 != -1 )
break;
lightLog(2LL, "x");
sleep(requestedInterval);
}
if ( v6 )
{
if ( v6 != -9 )
{
printLog(6LL, "\nUnknown error : %d\n", v6);
exit(-41);
}
printLog(6LL, "\nError: there is already an running device detection.\n");
exit(-40);
}
lightLog(2LL, "\n");
v7 = GetDeviceHandles(a2, a1);
if ( a1 != v7 )
{
printLog(6LL, "Error : GetDeviceHandles returned %d device(s) while we expect %d\n", v7, a1);
exit(-42);
}
lightLog(0LL, "-------------------------------------------\n");
lightLog(0LL, "Device handle | Device Name \n");
lightLog(0LL, "-------------------------------------------\n");
if ( (int)a1 > 0 )
{
v8 = (unsigned int *)a2;
do
{
v9 = *v8++;
GetDeviceName(v9, v11, 49LL);
lightLog(0LL, " %3lu | '%s'\n", *(v8 - 1), v11);
}
while ( v8 != (unsigned int *)v5 );
}
lightLog(0LL, "-------------------------------------------\n\n");
result = func1(a1, a2, a3);
}
while ( !(_DWORD)result );
return result;
}

original_code:
void startDeviceLogging(int deviceCount, DWORD deviceHandles[], int channelCount) {
 int iErrorCode, deviceNr;
 char nameBuffer[50];
 printLog(2, "Starting detection of %d device(s). Please wait...\n", deviceCount);
 BOOL deviceOK = (1 == 0);
 while (!deviceOK) {
  iErrorCode = DoStartDeviceDetection(deviceCount, (1 == 1) );
  switch(iErrorCode) {
   case YE_NOT_ALL_DEVS_FOUND:
    lightLog(2, "x");
    sleep(requestedInterval);
    deviceOK = (1 == 0);
    break;
   case YE_DEV_DETECT_IN_PROGRESS:
    printLog(6, "\nError: there is already an running device detection.\n");
    exit(-40);
   case YE_OK:
    deviceOK = (1 == 1);
    break;
   default:
    printLog(6, "\nUnknown error : %d\n", iErrorCode);
    exit(-41);
  }
        if (deviceOK) {
   lightLog(2, "\n");
            DWORD devCount = GetDeviceHandles(deviceHandles, deviceCount);
            if (devCount != deviceCount) {
                printLog(6, "Error : GetDeviceHandles returned %d device(s) while we expect %d\n", devCount, deviceCount);
                exit(-42);
            }
            lightLog(0, "-------------------------------------------\n");
            lightLog(0, "Device handle | Device Name  \n");
            lightLog(0, "-------------------------------------------\n");
            for (deviceNr = 0; deviceNr < deviceCount; deviceNr++) {
                GetDeviceName(deviceHandles[deviceNr], nameBuffer, sizeof(nameBuffer)-1);
                lightLog(0, "   %3lu        | '%s'\n", (unsigned long)deviceHandles[deviceNr], nameBuffer);
            }
            lightLog(0, "-------------------------------------------\n\n");
            deviceOK = logValues(deviceCount, deviceHandles, channelCount);
        }
    }
}

canonical_original_code:
void startDeviceLogging(int deviceCount, unsigned int deviceHandles[], int channelCount) {
 int iErrorCode; int deviceNr;
 char nameBuffer[50];
 printLog(2, "Starting detection of %d device(s). Please wait...\n", deviceCount);
 int deviceOK = (1 == 0);
 while (!deviceOK) {
  iErrorCode = DoStartDeviceDetection(deviceCount, (1 == 1) );
  switch(iErrorCode) {
   case YE_NOT_ALL_DEVS_FOUND:
    lightLog(2, "x");
    sleep(requestedInterval);
    deviceOK = (1 == 0);
    break;
   case YE_DEV_DETECT_IN_PROGRESS:
    printLog(6, "\nError: there is already an running device detection.\n");
    exit(-40);
   case YE_OK:
    deviceOK = (1 == 1);
    break;
   default:
    printLog(6, "\nUnknown error : %d\n", iErrorCode);
    exit(-41);
  }
        if (deviceOK) {
   lightLog(2, "\n");
            unsigned int devCount = GetDeviceHandles(deviceHandles, deviceCount);
            if (devCount != deviceCount) {
                printLog(6, "Error : GetDeviceHandles returned %d device(s) while we expect %d\n", devCount, deviceCount);
                exit(-42);
            }
            lightLog(0, "-------------------------------------------\n");
            lightLog(0, "Device handle | Device Name  \n");
            lightLog(0, "-------------------------------------------\n");
            for (deviceNr = 0; deviceNr < deviceCount; deviceNr++) {
                GetDeviceName(deviceHandles[deviceNr], nameBuffer, sizeof(nameBuffer)-1);
                lightLog(0, "   %3lu        | '%s'\n", (unsigned long)deviceHandles[deviceNr], nameBuffer);
            }
            lightLog(0, "-------------------------------------------\n\n");
            deviceOK = logValues(deviceCount, deviceHandles, channelCount);
        }
    }
}

variable_types: deviceCount int

variable_types:
deviceHandles
unsigned int []

variable_types: channelCount int

variable_types: iErrorCode int

variable_types:
nameBuffer
char [50]

variable_types: deviceOK int

variable_types:
devCount
unsigned int


main

canonical_name: main

decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er12
__int64 v4; // rbx
__int64 v5; // r12
signed int v6; // eax
void *v7; // rsp
__int64 v8; // rbx
__int64 v9; // rdi
int v11; // [rsp+0h] [rbp-70h] BYREF
char v12[42]; // [rsp+6h] [rbp-6Ah] BYREF
int v13[16]; // [rsp+30h] [rbp-40h] BYREF

strcpy(v12, "yasdi.ini");
v3 = GetPrivateProfileInt_("Log", "level", 3LL, v12);
requestedInterval = GetPrivateProfileInt_("Params", "requestedInterval", 300LL, v12);
maxChannelValueAge = GetPrivateProfileInt_("Params", "maxChannelValueAge", 5LL, v12);
initLog(v3, stdout);
printLog(1LL, "SMySqLogger was started\n");
if ( (int)yasdiMasterInitialize(v12, &v11) < 0 )
{
printLog(6LL, "ERROR: ini file '%s' was not found or is unreadable!\n", v12);
exit(-1);
}
v11 = yasdiMasterGetDriver(v13, 10LL);
if ( v11 <= 0 )
{
lightLog(2LL, "\n");
printLog(6LL, "Error: No drivers are online! YASDI can't communicate with devices!\n");
exit(-3);
}
v4 = 0LL;
do
{
yasdiGetDriverName((unsigned int)v13[v4], &v12[10], 30LL);
printLog(2LL, "Switching on driver '%s'... ", &v12[10]);
if ( !(unsigned int)yasdiSetDriverOnline((unsigned int)v13[v4]) )
{
lightLog(6LL, "Failure ! Please check config file\n");
exit(-2);
}
++v4;
lightLog(2LL, "Done\n");
}
while ( v11 > (int)v4 );
lightLog(2LL, "\n");
v5 = (unsigned int)checkArrayConsistency();
dbInit(v12, v5, dbColumns, dbTypes);
v6 = GetPrivateProfileInt_("Plant", "deviceCount", 0xFFFFFFFFLL, v12);
if ( v6 == -1 )
{
printLog(6LL, "Error: 'deviceCount=xx' must be specified under section '[Plant]' of %s !\n", v12);
exit(-4);
}
v7 = alloca(4LL * v6);
startDeviceLogging(v6, (__int64)&v11, v5);
if ( v11 > 0 )
{
v8 = 0LL;
do
{
yasdiGetDriverName((unsigned int)v13[v8], &v12[10], 30LL);
printLog(2LL, "Switching off driver '%s'...\n", &v12[10]);
v9 = (unsigned int)v13[v8++];
yasdiSetDriverOffline(v9);
}
while ( v11 > (int)v8 );
}
dbShutdown();
yasdiMasterShutdown();
return 0;
}

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // er12
__int64 v4; // rbx
__int64 v5; // r12
signed int v6; // eax
void *v7; // rsp
__int64 v8; // rbx
__int64 v9; // rdi
int v11; // [rsp+0h] [rbp-70h] BYREF
char v12[42]; // [rsp+6h] [rbp-6Ah] BYREF
int v13[16]; // [rsp+30h] [rbp-40h] BYREF

strcpy(v12, "yasdi.ini");
v3 = GetPrivateProfileInt_("Log", "level", 3LL, v12);
requestedInterval = GetPrivateProfileInt_("Params", "requestedInterval", 300LL, v12);
maxChannelValueAge = GetPrivateProfileInt_("Params", "maxChannelValueAge", 5LL, v12);
initLog(v3, stdout);
printLog(1LL, "SMySqLogger was started\n");
if ( (int)yasdiMasterInitialize(v12, &v11) < 0 )
{
printLog(6LL, "ERROR: ini file '%s' was not found or is unreadable!\n", v12);
exit(-1);
}
v11 = yasdiMasterGetDriver(v13, 10LL);
if ( v11 <= 0 )
{
lightLog(2LL, "\n");
printLog(6LL, "Error: No drivers are online! YASDI can't communicate with devices!\n");
exit(-3);
}
v4 = 0LL;
do
{
yasdiGetDriverName((unsigned int)v13[v4], &v12[10], 30LL);
printLog(2LL, "Switching on driver '%s'... ", &v12[10]);
if ( !(unsigned int)yasdiSetDriverOnline((unsigned int)v13[v4]) )
{
lightLog(6LL, "Failure ! Please check config file\n");
exit(-2);
}
++v4;
lightLog(2LL, "Done\n");
}
while ( v11 > (int)v4 );
lightLog(2LL, "\n");
v5 = (unsigned int)func0();
dbInit(v12, v5, dbColumns, dbTypes);
v6 = GetPrivateProfileInt_("Plant", "deviceCount", 0xFFFFFFFFLL, v12);
if ( v6 == -1 )
{
printLog(6LL, "Error: 'deviceCount=xx' must be specified under section '[Plant]' of %s !\n", v12);
exit(-4);
}
v7 = alloca(4LL * v6);
func2(v6, (__int64)&v11, v5);
if ( v11 > 0 )
{
v8 = 0LL;
do
{
yasdiGetDriverName((unsigned int)v13[v8], &v12[10], 30LL);
printLog(2LL, "Switching off driver '%s'...\n", &v12[10]);
v9 = (unsigned int)v13[v8++];
yasdiSetDriverOffline(v9);
}
while ( v11 > (int)v8 );
}
dbShutdown();
yasdiMasterShutdown();
return 0;
}

original_code:
int main(int argv, char **argc)
{
 int driverCount, driverNr;
 DWORD drivers[10];
 char driverName[30];
 BOOL bOnDriverOnline = (1 == 0);
 char iniFile[]="yasdi.ini";
    int logLevel;
 logLevel = GetPrivateProfileInt_("Log", "level", 3, iniFile);
 requestedInterval = GetPrivateProfileInt_("Params", "requestedInterval", 300, iniFile);
    maxChannelValueAge = GetPrivateProfileInt_("Params", "maxChannelValueAge", 5, iniFile);
    initLog(logLevel, stdout);
    printLog(1, "SMySqLogger was started\n");
 if (0 > yasdiMasterInitialize(iniFile, &driverCount)) {
  printLog(6, "ERROR: ini file '%s' was not found or is unreadable!\n", iniFile);
  exit(-1);
 }
 driverCount = yasdiMasterGetDriver(drivers, 10);
 for(driverNr = 0; driverNr < driverCount; driverNr++) {
  yasdiGetDriverName(drivers[driverNr], driverName, sizeof(driverName));
  printLog(2, "Switching on driver '%s'... ", driverName);
  if (yasdiSetDriverOnline(drivers[driverNr])) {
   lightLog(2, "Done\n");
   bOnDriverOnline = (1 == 1);
  }
  else {
   lightLog(6, "Failure ! Please check config file\n");
   exit(-2);
  }
 }
 lightLog(2, "\n");
 if ((1 == 0) == bOnDriverOnline) {
  printLog(6, "Error: No drivers are online! YASDI can't communicate with devices!\n");
  exit(-3);
 }
 int channelCount = checkArrayConsistency();
 dbInit(iniFile, channelCount, dbColumns, dbTypes);
    int deviceCount = GetPrivateProfileInt_("Plant", "deviceCount", -1, iniFile);
 if (deviceCount == -1) {
  printLog(6, "Error: 'deviceCount=xx' must be specified under section '[Plant]' of %s !\n", iniFile);
  exit(-4);
 }
    DWORD deviceHandles[deviceCount];
 startDeviceLogging(deviceCount, deviceHandles, channelCount);
 for (driverNr = 0; driverNr < driverCount; driverNr++) {
  yasdiGetDriverName(drivers[driverNr], driverName, sizeof(driverName));
  printLog(2, "Switching off driver '%s'...\n", driverName);
  yasdiSetDriverOffline(drivers[driverNr]);
 }
 dbShutdown();
 yasdiMasterShutdown();
 return 0;
}

canonical_original_code:
int main(int argv, char **argc)
{
 int driverCount; int driverNr;
 unsigned int drivers[10];
 char driverName[30];
 int bOnDriverOnline = (1 == 0);
 char iniFile[] = "yasdi.ini";
    int logLevel;
 logLevel = GetPrivateProfileInt_("Log", "level", 3, iniFile);
 requestedInterval = GetPrivateProfileInt_("Params", "requestedInterval", 300, iniFile);
    maxChannelValueAge = GetPrivateProfileInt_("Params", "maxChannelValueAge", 5, iniFile);
    initLog(logLevel, stdout);
    printLog(1, "SMySqLogger was started\n");
 if (0 > yasdiMasterInitialize(iniFile, &driverCount)) {
  printLog(6, "ERROR: ini file '%s' was not found or is unreadable!\n", iniFile);
  exit(-1);
 }
 driverCount = yasdiMasterGetDriver(drivers, 10);
 for(driverNr = 0; driverNr < driverCount; driverNr++) {
  yasdiGetDriverName(drivers[driverNr], driverName, sizeof(driverName));
  printLog(2, "Switching on driver '%s'... ", driverName);
  if (yasdiSetDriverOnline(drivers[driverNr])) {
   lightLog(2, "Done\n");
   bOnDriverOnline = (1 == 1);
  }
  else {
   lightLog(6, "Failure ! Please check config file\n");
   exit(-2);
  }
 }
 lightLog(2, "\n");
 if ((1 == 0) == bOnDriverOnline) {
  printLog(6, "Error: No drivers are online! YASDI can't communicate with devices!\n");
  exit(-3);
 }
 int channelCount = checkArrayConsistency();
 dbInit(iniFile, channelCount, dbColumns, dbTypes);
    int deviceCount = GetPrivateProfileInt_("Plant", "deviceCount", -1, iniFile);
 if (deviceCount == -1) {
  printLog(6, "Error: 'deviceCount=xx' must be specified under section '[Plant]' of %s !\n", iniFile);
  exit(-4);
 }
    unsigned int deviceHandles[-1];
 startDeviceLogging(deviceCount, deviceHandles, channelCount);
 for (driverNr = 0; driverNr < driverCount; driverNr++) {
  yasdiGetDriverName(drivers[driverNr], driverName, sizeof(driverName));
  printLog(2, "Switching off driver '%s'...\n", driverName);
  yasdiSetDriverOffline(drivers[driverNr]);
 }
 dbShutdown();
 yasdiMasterShutdown();
 return 0;
}

variable_types: argv int

variable_types:
argc
char **

variable_types: driverCount int

variable_types: driverNr int

variable_types:
drivers
unsigned int [10]

variable_types:
driverName
char [30]

variable_types: bOnDriverOnline int

variable_types:
iniFile
char []

variable_types: logLevel int

variable_types:
deviceHandles
unsigned int [-1]