decompiled_code:
__int64 __fastcall bitset_create(_QWORD *a1, int a2)
{
_DWORD *v3; // rax
_DWORD *v4; // rbx
size_t v5; // r12
void *v6; // rdi
__int64 result; // rax

v3 = malloc(0x10uLL);
if ( v3 )
{
v4 = v3;
v5 = (int)calculate_buffer_size(a2);
v6 = malloc(v5);
*(_QWORD *)v4 = v6;
if ( v6 )
{
memset(v6, 0, v5);
v4[2] = a2;
*a1 = v4;
result = 0LL;
}
else
{
free(v4);
*a1 = 0LL;
result = 0xFFFFFFFFLL;
}
}
else
{
*a1 = 0LL;
result = 0xFFFFFFFFLL;
}
return result;
}

canonical_decompiled_code:
__int64 __fastcall func1(_QWORD *a1, int a2)
{
_DWORD *v3; // rax
_DWORD *v4; // rbx
size_t v5; // r12
void *v6; // rdi
__int64 result; // rax

v3 = malloc(0x10uLL);
if ( v3 )
{
v4 = v3;
v5 = (int)func0(a2);
v6 = malloc(v5);
*(_QWORD *)v4 = v6;
if ( v6 )
{
memset(v6, 0, v5);
v4[2] = a2;
*a1 = v4;
result = 0LL;
}
else
{
free(v4);
*a1 = 0LL;
result = 0xFFFFFFFFLL;
}
}
else
{
*a1 = 0LL;
result = 0xFFFFFFFFLL;
}
return result;
}

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


repo: vdaras/Huffman-Compression

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);
}

return_type: void

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

return_type: 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; // ebx

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

canonical_decompiled_code:
__int64 __fastcall func7(__int64 a1, void ***a2)
{
unsigned int v2; // ebx

v2 = func1(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 v3; // ebx
unsigned int v4; // ebp

v3 = a3;
if ( *(_DWORD *)(a1 + 8) < a3 )
v3 = *(_DWORD *)(a1 + 8);
v4 = bitset_create(a2, v3);
if ( !v4 )
memcpy(**a2, *(const void **)a1, 2 * (v3 / 16) + 2);
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func8(__int64 a1, void ***a2, unsigned int a3)
{
int v3; // ebx
unsigned int v4; // ebp

v3 = a3;
if ( *(_DWORD *)(a1 + 8) < a3 )
v3 = *(_DWORD *)(a1 + 8);
v4 = func1(a2, v3);
if ( !v4 )
memcpy(**a2, *(const void **)a1, 2 * (v3 / 16) + 2);
return v4;
}

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; // ecx
int v3; // edx
__int64 result; // rax

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

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

v2 = *(_DWORD *)(a1 + 4);
v3 = *(_DWORD *)(a2 + 4);
result = 1LL;
if ( v2 >= v3 )
result = (unsigned int)-(v2 != v3);
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);
result = 0LL;
if ( v3 <= a2 )
result = (unsigned int)-((unsigned int)bitset_resize(a1, 2 * v3) == -1);
return result;
}

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

v3 = *(_DWORD *)(a1 + 8);
result = 0LL;
if ( v3 <= a2 )
result = (unsigned int)-((unsigned int)bitset_resize(a1, 2 * v3) == -1);
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 v4; // ebx
int i; // er13
unsigned __int8 v7; // [rsp+Fh] [rbp-39h]

v4 = check_and_resize_bitset(a2, *a3);
if ( !v4 )
{
if ( *((_DWORD *)a1 + 2) )
{
bitset_set_bit(a2, *a3);
++*a3;
v7 = *a1;
for ( i = 7; i != -1; --i )
{
v4 = check_and_resize_bitset(a2, *a3);
if ( v4 )
break;
if ( (v7 & (unsigned __int8)(1 << i)) != 0 )
bitset_set_bit(a2, *a3);
else
bitset_clear_bit(a2, *a3);
++*a3;
}
}
else
{
bitset_clear_bit(a2, *a3);
++*a3;
v4 = huffman_tree_serialize_recurse(*((_QWORD *)a1 + 3), a2, a3);
if ( !v4 )
v4 = huffman_tree_serialize_recurse(*((_QWORD *)a1 + 4), a2, a3);
}
}
return v4;
}

canonical_decompiled_code:
__int64 __fastcall func3(unsigned __int8 *a1, __int64 a2, unsigned int *a3)
{
unsigned int v4; // ebx
int i; // er13
unsigned __int8 v7; // [rsp+Fh] [rbp-39h]

v4 = func2(a2, *a3);
if ( !v4 )
{
if ( *((_DWORD *)a1 + 2) )
{
bitset_set_bit(a2, *a3);
++*a3;
v7 = *a1;
for ( i = 7; i != -1; --i )
{
v4 = func2(a2, *a3);
if ( v4 )
break;
if ( (v7 & (unsigned __int8)(1 << i)) != 0 )
bitset_set_bit(a2, *a3);
else
bitset_clear_bit(a2, *a3);
++*a3;
}
}
else
{
bitset_clear_bit(a2, *a3);
++*a3;
v4 = func3(*((_QWORD *)a1 + 3), a2, a3);
if ( !v4 )
v4 = func3(*((_QWORD *)a1 + 4), a2, a3);
}
}
return v4;
}

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: i int

variable_types:
mask
unsigned char

variable_types: bit int


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

v1 = malloc(0x28uLL);
*a1 = v1;
if ( !v1 )
return 0xFFFFFFFFLL;
*(_BYTE *)v1 = 0;
v1[1] = 0;
v1[2] = 1;
*((_QWORD *)v1 + 4) = 0LL;
*((_QWORD *)v1 + 3) = 0LL;
*((_QWORD *)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 v4; // rsi
unsigned int v5; // ebx
int v8; // er13
int v9; // er15
void *ptr; // [rsp+0h] [rbp-48h] BYREF
int v11[15]; // [rsp+Ch] [rbp-3Ch] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func5(__int64 a1, __int64 a2, _DWORD *a3)
{
__int64 v4; // rsi
unsigned int v5; // ebx
int v8; // er13
int v9; // er15
void *ptr; // [rsp+0h] [rbp-48h] BYREF
int v11[15]; // [rsp+Ch] [rbp-3Ch] BYREF

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

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;
}


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 v8; // ebx

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

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

if ( a4 )
bitset_clear_bit(a3, a5);
else
bitset_set_bit(a3, a5);
if ( *((_DWORD *)a1 + 2) )
{
bitset_copy_bits(a3, a2 + 8LL * *a1, a5 + 1);
}
else
{
v8 = a5 + 1;
func2(*((_QWORD *)a1 + 3), a2, a3, 1LL, v8);
func2(*((_QWORD *)a1 + 4), a2, a3, 0LL, v8);
}
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; // ebx
__int64 v4[4]; // [rsp+8h] [rbp-20h] BYREF

v2 = bitset_create(v4, 30LL);
if ( v2 != -1 )
{
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)) != 0 )
{
bitset_destroy(v4);
destroy_lookup(a2);
}
else
{
bitset_destroy(v4);
}
}
return v2;
}

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

v2 = bitset_create(v4, 30LL);
if ( v2 != -1 )
{
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)) != 0 )
{
bitset_destroy(v4);
func1(a2);
}
else
{
bitset_destroy(v4);
}
}
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 i; // rbp
char *v2; // rdx
unsigned __int64 v3; // rax

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

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

for ( i = 0x100002600LL; *s; *v2 = 0 )
{
v2 = &s[strlen(s) - 1];
v3 = (unsigned __int8)*v2;
if ( (unsigned __int8)v3 > 0x20u )
break;
if ( !_bittest64(&i, v3) )
break;
}
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:
const char *__fastcall trimStart(const char *a1)
{
unsigned __int64 v1; // rax
__int64 v2; // rdx
__int64 v3; // r12
size_t i; // rbx
unsigned __int64 v5; // rax

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

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

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


trim

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

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

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

v1 = (char *)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

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
char *v5; // r15
int v6; // ebx
char *v7; // r12
char *v8; // rbx
unsigned int *v9; // r14
double v10; // r13
const char *v11; // r12
unsigned int v12; // eax
unsigned int v13; // ebx
struct tm *v14; // rax
time_t v15; // rax
__int64 v16; // rbx
char *v17; // r15
unsigned int *v18; // r12
char *v19; // rbx
unsigned int v20; // eax
__int64 v21; // r14
const char *v22; // r13
struct tm *v24; // rax
__int64 v25; // [rsp+0h] [rbp-5F0h] BYREF
__int64 *v26; // [rsp+8h] [rbp-5E8h]
const char *v27; // [rsp+10h] [rbp-5E0h]
unsigned int *v28; // [rsp+18h] [rbp-5D8h]
double v29; // [rsp+20h] [rbp-5D0h]
unsigned int *v30; // [rsp+28h] [rbp-5C8h]
char *v31; // [rsp+30h] [rbp-5C0h]
__int64 v32; // [rsp+38h] [rbp-5B8h]
int v33; // [rsp+40h] [rbp-5B0h]
int v34; // [rsp+44h] [rbp-5ACh]
const char *v35; // [rsp+48h] [rbp-5A8h]
char v36[32]; // [rsp+50h] [rbp-5A0h] BYREF
char v37[64]; // [rsp+70h] [rbp-580h] BYREF
char v38[1200]; // [rsp+B0h] [rbp-540h] BYREF
time_t timer; // [rsp+560h] [rbp-90h] BYREF
time_t time0; // [rsp+568h] [rbp-88h] BYREF
char src[40]; // [rsp+570h] [rbp-80h] BYREF
double v42; // [rsp+598h] [rbp-58h] BYREF
char v43[80]; // [rsp+5A0h] [rbp-50h] BYREF

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

canonical_decompiled_code:
__int64 __fastcall func1(int a1, __int64 a2, int a3)
{
void *v3; // rsp
void *v4; // rsp
char *v5; // r15
int v6; // ebx
char *v7; // r12
char *v8; // rbx
unsigned int *v9; // r14
double v10; // r13
const char *v11; // r12
unsigned int v12; // eax
unsigned int v13; // ebx
struct tm *v14; // rax
time_t v15; // rax
__int64 v16; // rbx
char *v17; // r15
unsigned int *v18; // r12
char *v19; // rbx
unsigned int v20; // eax
__int64 v21; // r14
const char *v22; // r13
struct tm *v24; // rax
__int64 v25; // [rsp+0h] [rbp-5F0h] BYREF
__int64 *v26; // [rsp+8h] [rbp-5E8h]
const char *v27; // [rsp+10h] [rbp-5E0h]
unsigned int *v28; // [rsp+18h] [rbp-5D8h]
double v29; // [rsp+20h] [rbp-5D0h]
unsigned int *v30; // [rsp+28h] [rbp-5C8h]
char *v31; // [rsp+30h] [rbp-5C0h]
__int64 v32; // [rsp+38h] [rbp-5B8h]
int v33; // [rsp+40h] [rbp-5B0h]
int v34; // [rsp+44h] [rbp-5ACh]
const char *v35; // [rsp+48h] [rbp-5A8h]
char v36[32]; // [rsp+50h] [rbp-5A0h] BYREF
char v37[64]; // [rsp+70h] [rbp-580h] BYREF
char v38[1200]; // [rsp+B0h] [rbp-540h] BYREF
time_t timer; // [rsp+560h] [rbp-90h] BYREF
time_t time0; // [rsp+568h] [rbp-88h] BYREF
char src[40]; // [rsp+570h] [rbp-80h] BYREF
double v42; // [rsp+598h] [rbp-58h] BYREF
char v43[80]; // [rsp+5A0h] [rbp-50h] BYREF

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

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)
{
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int *v6; // rbx
__int64 result; // rax
char v8[104]; // [rsp+0h] [rbp-68h] BYREF

printLog(2LL, "Starting detection of %d device(s). Please wait...\n", a1);
do
{
while ( 1 )
{
v4 = DoStartDeviceDetection(a1, 1LL);
if ( v4 != -1 )
break;
lightLog(2LL, "x");
sleep(requestedInterval);
}
if ( v4 )
{
if ( v4 != -9 )
{
printLog(6LL, "\nUnknown error : %d\n", v4);
exit(-41);
}
printLog(6LL, "\nError: there is already an running device detection.\n");
exit(-40);
}
lightLog(2LL, "\n");
v5 = GetDeviceHandles(a2, a1);
if ( v5 != a1 )
{
printLog(6LL, "Error : GetDeviceHandles returned %d device(s) while we expect %d\n", v5, a1);
exit(-42);
}
lightLog(0LL, "-------------------------------------------\n");
lightLog(0LL, "Device handle | Device Name \n");
lightLog(0LL, "-------------------------------------------\n");
if ( (int)a1 > 0 )
{
v6 = (unsigned int *)a2;
do
{
GetDeviceName(*v6, v8, 49LL);
lightLog(0LL, " %3lu | '%s'\n", *v6++, v8);
}
while ( v6 != (unsigned int *)(a2 + 4LL * (a1 - 1) + 4) );
}
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)
{
unsigned int v4; // eax
unsigned int v5; // eax
unsigned int *v6; // rbx
__int64 result; // rax
char v8[104]; // [rsp+0h] [rbp-68h] BYREF

printLog(2LL, "Starting detection of %d device(s). Please wait...\n", a1);
do
{
while ( 1 )
{
v4 = DoStartDeviceDetection(a1, 1LL);
if ( v4 != -1 )
break;
lightLog(2LL, "x");
sleep(requestedInterval);
}
if ( v4 )
{
if ( v4 != -9 )
{
printLog(6LL, "\nUnknown error : %d\n", v4);
exit(-41);
}
printLog(6LL, "\nError: there is already an running device detection.\n");
exit(-40);
}
lightLog(2LL, "\n");
v5 = GetDeviceHandles(a2, a1);
if ( v5 != a1 )
{
printLog(6LL, "Error : GetDeviceHandles returned %d device(s) while we expect %d\n", v5, a1);
exit(-42);
}
lightLog(0LL, "-------------------------------------------\n");
lightLog(0LL, "Device handle | Device Name \n");
lightLog(0LL, "-------------------------------------------\n");
if ( (int)a1 > 0 )
{
v6 = (unsigned int *)a2;
do
{
GetDeviceName(*v6, v8, 49LL);
lightLog(0LL, " %3lu | '%s'\n", *v6++, v8);
}
while ( v6 != (unsigned int *)(a2 + 4LL * (a1 - 1) + 4) );
}
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; // ebx
__int64 v4; // rbx
__int64 v5; // rbx
signed int v6; // edi
void *v7; // rsp
__int64 v8; // rbx
_BYTE v10[48]; // [rsp+0h] [rbp-70h] BYREF
int v11[11]; // [rsp+30h] [rbp-40h] BYREF
int v12[3]; // [rsp+5Ch] [rbp-14h] BYREF

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

canonical_decompiled_code:
int __cdecl main(int argc, const char **argv, const char **envp)
{
unsigned int v3; // ebx
__int64 v4; // rbx
__int64 v5; // rbx
signed int v6; // edi
void *v7; // rsp
__int64 v8; // rbx
_BYTE v10[48]; // [rsp+0h] [rbp-70h] BYREF
int v11[11]; // [rsp+30h] [rbp-40h] BYREF
int v12[3]; // [rsp+5Ch] [rbp-14h] BYREF

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


dbInit

decompiled_code:
__int64 __fastcall dbInit(char *src, int a2, const char *a3, const char *a4)
{
const char *v6; // r15
const char *v7; // r14
const char *v8; // rbx
const char *v9; // rax
__int64 v10; // rdi
const char *v11; // rbx
const char *v13; // rbx
unsigned int v14; // eax
unsigned int v15; // ebx
unsigned int *v16; // r14
__int64 v17; // rdi
const char *v18; // rax
const char *v19; // rbx
unsigned int v20; // eax
const char *v21; // rbx
unsigned int v22; // eax
const char *v23; // rbx
unsigned int v24; // eax
const char *v25; // rbx
unsigned int v26; // eax
const char *v27; // rbx
unsigned int v28; // eax
const char *v29; // rbx
unsigned int v30; // eax
const char *v31; // rbx
unsigned int v32; // eax
char v33[2000]; // [rsp+0h] [rbp-8C8h] BYREF
char v34[32]; // [rsp+7D0h] [rbp-F8h] BYREF
char v35[32]; // [rsp+7F0h] [rbp-D8h] BYREF
char v36[32]; // [rsp+810h] [rbp-B8h] BYREF
char v37[32]; // [rsp+830h] [rbp-98h] BYREF
char v38[32]; // [rsp+850h] [rbp-78h] BYREF
char v39[88]; // [rsp+870h] [rbp-58h] BYREF

strcpy(iniFilename, src);
channelCount = a2;
GetPrivateProfileString_("DB", "root_username", "root", v37, 30LL, iniFilename);
GetPrivateProfileString_("DB", "root_password", &unk_1C16, v36, 30LL, iniFilename);
GetPrivateProfileString_("DB", "username", "sma", v35, 30LL, iniFilename);
GetPrivateProfileString_("DB", "password", "smysqlogger", v34, 30LL, iniFilename);
GetPrivateProfileString_("DB", "server", "localhost", v39, 30LL, iniFilename);
GetPrivateProfileString_("DB", "database", "sma", v38, 30LL, iniFilename);
v6 = (const char *)mysql_get_client_info();
v7 = (const char *)trim(v38);
v8 = (const char *)trim(v39);
v9 = (const char *)trim(v35);
printLog(1LL, "Connecting to MySQL as %s@%s:%s (client version: %s)\n", v9, v8, v7, v6);
v10 = mysql_init(0LL);
conn = v10;
if ( !v10 )
{
v13 = (const char *)((__int64 (*)(void))mysql_error)();
v14 = mysql_errno(conn);
printLog(6LL, "Error %u initializing connection structure : %s\n", v14, v13);
exit(-10);
}
if ( !mysql_real_connect(v10, v39, v35, v34, v38, 0LL, 0LL, 0LL) )
{
v15 = mysql_errno(conn);
v16 = (unsigned int *)_errno_location();
*v16 = v15;
if ( ((v15 - 1045) & 0xFFFFFFFB) != 0 )
{
v18 = (const char *)mysql_error(conn);
printLog(6LL, "Error %u connecting to Database: %s\n", *v16, v18);
exit(-11);
}
printf("Database '%s' doesn't exist. Trying to create it...", v38);
if ( !mysql_real_connect(conn, v39, v37, v36, 0LL, 0LL, 0LL, 0LL) )
{
v19 = (const char *)mysql_error(conn);
v20 = mysql_errno(conn);
printf("Error %u connecting to Database: %s\n", v20, v19);
exit(-12);
}
sprintf(v33, "create database if not exists %s", v38);
if ( (unsigned int)mysql_query(conn, v33) )
{
v21 = (const char *)mysql_error(conn);
v22 = mysql_errno(conn);
printf("Error %u creating Database: %s\n", v22, v21);
exit(-13);
}
sprintf(v33, "create user %s@localhost identified by '%s'", v35, v34);
if ( (unsigned int)mysql_query(conn, v33) )
{
v23 = (const char *)mysql_error(conn);
v24 = mysql_errno(conn);
printf("Error %u creating user: %s\n", v24, v23);
exit(-14);
}
sprintf(v33, "GRANT ALL PRIVILEGES ON %s.* TO %s@localhost;", v38, v35);
if ( (unsigned int)mysql_query(conn, v33) )
{
v25 = (const char *)mysql_error(conn);
v26 = mysql_errno(conn);
printf("Error %u granting rights: %s\n", v26, v25);
exit(-15);
}
mysql_close(conn);
printf("Done. Reconnecting to newly created database...\n");
v17 = mysql_init(0LL);
conn = v17;
if ( !v17 )
{
v27 = (const char *)mysql_error(0LL);
v28 = mysql_errno(conn);
printf("Error %u re-initializing connection structure : %s\n", v28, v27);
exit(-16);
}
if ( !mysql_real_connect(v17, v39, v35, v34, v38, 0LL, 0LL, 0LL) )
{
v29 = (const char *)mysql_error(conn);
v30 = mysql_errno(conn);
printLog(6LL, "Error %u connecting to new Database: %s\n", v30, v29);
exit(-17);
}
printf(
"\n"
"Congratulations. A new database called '%s' has been created, with the table 'logged_values'.\n"
"A new user '%s' has also been created and was granted access to the new DB.\n"
"It is now strongly recommended to edit yasdi.ini to remove the\n"
"root_username/root_password information.\n"
"\n"
"Waiting 20 seconds...\n",
v38,
v35);
fflush(stdout);
sleep(0x14u);
}
strcpy(v33, "CREATE TABLE IF NOT EXISTS logged_values (logdate DATE, logtime TIME");
if ( a2 > 0 )
{
v11 = a3;
do
{
strcat(v33, ", ");
strcat(v33, v11);
*(_WORD *)&v33[strlen(v33)] = 32;
strcat(v33, a4);
v11 += 30;
a4 += 30;
}
while ( v11 != &a3[30 * (a2 - 1) + 30] );
}
*(_WORD *)&v33[strlen(v33)] = 41;
printLog(0LL, "Executing : %s\n", v33);
if ( (unsigned int)mysql_query(conn, v33) )
{
v31 = (const char *)mysql_error(conn);
v32 = mysql_errno(conn);
printLog(6LL, "Error %u creating table: %s\n", v32, v31);
exit(-18);
}
return printLog(1LL, "Connection OK.\n");
}

canonical_decompiled_code:
__int64 __fastcall func0(char *src, int a2, const char *a3, const char *a4)
{
const char *v6; // r15
const char *v7; // r14
const char *v8; // rbx
const char *v9; // rax
__int64 v10; // rdi
const char *v11; // rbx
const char *v13; // rbx
unsigned int v14; // eax
unsigned int v15; // ebx
unsigned int *v16; // r14
__int64 v17; // rdi
const char *v18; // rax
const char *v19; // rbx
unsigned int v20; // eax
const char *v21; // rbx
unsigned int v22; // eax
const char *v23; // rbx
unsigned int v24; // eax
const char *v25; // rbx
unsigned int v26; // eax
const char *v27; // rbx
unsigned int v28; // eax
const char *v29; // rbx
unsigned int v30; // eax
const char *v31; // rbx
unsigned int v32; // eax
char v33[2000]; // [rsp+0h] [rbp-8C8h] BYREF
char v34[32]; // [rsp+7D0h] [rbp-F8h] BYREF
char v35[32]; // [rsp+7F0h] [rbp-D8h] BYREF
char v36[32]; // [rsp+810h] [rbp-B8h] BYREF
char v37[32]; // [rsp+830h] [rbp-98h] BYREF
char v38[32]; // [rsp+850h] [rbp-78h] BYREF
char v39[88]; // [rsp+870h] [rbp-58h] BYREF

strcpy(iniFilename, src);
channelCount = a2;
GetPrivateProfileString_("DB", "root_username", "root", v37, 30LL, iniFilename);
GetPrivateProfileString_("DB", "root_password", &unk_1C16, v36, 30LL, iniFilename);
GetPrivateProfileString_("DB", "username", "sma", v35, 30LL, iniFilename);
GetPrivateProfileString_("DB", "password", "smysqlogger", v34, 30LL, iniFilename);
GetPrivateProfileString_("DB", "server", "localhost", v39, 30LL, iniFilename);
GetPrivateProfileString_("DB", "database", "sma", v38, 30LL, iniFilename);
v6 = (const char *)mysql_get_client_info();
v7 = (const char *)trim(v38);
v8 = (const char *)trim(v39);
v9 = (const char *)trim(v35);
printLog(1LL, "Connecting to MySQL as %s@%s:%s (client version: %s)\n", v9, v8, v7, v6);
v10 = mysql_init(0LL);
conn = v10;
if ( !v10 )
{
v13 = (const char *)((__int64 (*)(void))mysql_error)();
v14 = mysql_errno(conn);
printLog(6LL, "Error %u initializing connection structure : %s\n", v14, v13);
exit(-10);
}
if ( !mysql_real_connect(v10, v39, v35, v34, v38, 0LL, 0LL, 0LL) )
{
v15 = mysql_errno(conn);
v16 = (unsigned int *)_errno_location();
*v16 = v15;
if ( ((v15 - 1045) & 0xFFFFFFFB) != 0 )
{
v18 = (const char *)mysql_error(conn);
printLog(6LL, "Error %u connecting to Database: %s\n", *v16, v18);
exit(-11);
}
printf("Database '%s' doesn't exist. Trying to create it...", v38);
if ( !mysql_real_connect(conn, v39, v37, v36, 0LL, 0LL, 0LL, 0LL) )
{
v19 = (const char *)mysql_error(conn);
v20 = mysql_errno(conn);
printf("Error %u connecting to Database: %s\n", v20, v19);
exit(-12);
}
sprintf(v33, "create database if not exists %s", v38);
if ( (unsigned int)mysql_query(conn, v33) )
{
v21 = (const char *)mysql_error(conn);
v22 = mysql_errno(conn);
printf("Error %u creating Database: %s\n", v22, v21);
exit(-13);
}
sprintf(v33, "create user %s@localhost identified by '%s'", v35, v34);
if ( (unsigned int)mysql_query(conn, v33) )
{
v23 = (const char *)mysql_error(conn);
v24 = mysql_errno(conn);
printf("Error %u creating user: %s\n", v24, v23);
exit(-14);
}
sprintf(v33, "GRANT ALL PRIVILEGES ON %s.* TO %s@localhost;", v38, v35);
if ( (unsigned int)mysql_query(conn, v33) )
{
v25 = (const char *)mysql_error(conn);
v26 = mysql_errno(conn);
printf("Error %u granting rights: %s\n", v26, v25);
exit(-15);
}
mysql_close(conn);
printf("Done. Reconnecting to newly created database...\n");
v17 = mysql_init(0LL);
conn = v17;
if ( !v17 )
{
v27 = (const char *)mysql_error(0LL);
v28 = mysql_errno(conn);
printf("Error %u re-initializing connection structure : %s\n", v28, v27);
exit(-16);
}
if ( !mysql_real_connect(v17, v39, v35, v34, v38, 0LL, 0LL, 0LL) )
{
v29 = (const char *)mysql_error(conn);
v30 = mysql_errno(conn);
printLog(6LL, "Error %u connecting to new Database: %s\n", v30, v29);
exit(-17);
}
printf(
"\n"
"Congratulations. A new database called '%s' has been created, with the table 'logged_values'.\n"
"A new user '%s' has also been created and was granted access to the new DB.\n"
"It is now strongly recommended to edit yasdi.ini to remove the\n"
"root_username/root_password information.\n"
"\n"
"Waiting 20 seconds...\n",
v38,
v35);
fflush(stdout);
sleep(0x14u);
}
strcpy(v33, "CREATE TABLE IF NOT EXISTS logged_values (logdate DATE, logtime TIME");
if ( a2 > 0 )
{
v11 = a3;
do
{
strcat(v33, ", ");
strcat(v33, v11);
*(_WORD *)&v33[strlen(v33)] = 32;
strcat(v33, a4);
v11 += 30;
a4 += 30;
}
while ( v11 != &a3[30 * (a2 - 1) + 30] );
}
*(_WORD *)&v33[strlen(v33)] = 41;
printLog(0LL, "Executing : %s\n", v33);
if ( (unsigned int)mysql_query(conn, v33) )
{
v31 = (const char *)mysql_error(conn);
v32 = mysql_errno(conn);
printLog(6LL, "Error %u creating table: %s\n", v32, v31);
exit(-18);
}
return printLog(1LL, "Connection OK.\n");
}

original_code:
void dbInit(char * iniFilename, int channelCount, char dbColumns[][30], char dbTypes[][30])
{
 char server[30];
 char database[30];
 char rootUsername[30];
 char rootPassword[30];
 char username[30];
 char password[30];
 char query[2000];
 int (*__errno_location ());
 int channelNr;
    strcpy(_iniFilename, iniFilename);
    _channelCount = channelCount;
 GetPrivateProfileString_("DB", "root_username", "root", rootUsername, sizeof(rootUsername), _iniFilename);
 GetPrivateProfileString_("DB", "root_password", "", rootPassword, sizeof(rootPassword), _iniFilename);
 GetPrivateProfileString_("DB", "username", "sma", username, sizeof(username), _iniFilename);
 GetPrivateProfileString_("DB", "password", "smysqlogger", password, sizeof(password), _iniFilename);
 GetPrivateProfileString_("DB", "server", "localhost", server, sizeof(server), _iniFilename);
 GetPrivateProfileString_("DB", "database", "sma", database, sizeof(database), _iniFilename);
 printLog(1, "Connecting to MySQL as %s@%s:%s (client version: %s)\n", trim(username), trim(server), trim(database), mysql_get_client_info());
 _conn = mysql_init(((void *)0));
 if (_conn == ((void *)0)) {
  printLog(6, "Error %u initializing connection structure : %s\n", mysql_errno(_conn), mysql_error(_conn));
  exit(-10);
 }
 if (mysql_real_connect(_conn, server, username, password, database, 0, ((void *)0), 0) == ((void *)0)) {
  (*__errno_location ()) = mysql_errno(_conn);
  if ((*__errno_location ()) != 1049 && (*__errno_location ()) != 1045) {
   printLog(6, "Error %u connecting to Database: %s\n", (*__errno_location ()), mysql_error(_conn));
   exit(-11);
  }
  else {
   printf("Database '%s' doesn't exist. Trying to create it...", database);
   if (mysql_real_connect(_conn, server, rootUsername, rootPassword, ((void *)0), 0, ((void *)0), 0) == ((void *)0)) {
    printf("Error %u connecting to Database: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-12);
   }
   sprintf(query, "create database if not exists %s", database);
   if (mysql_query(_conn, query)) {
    printf("Error %u creating Database: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-13);
   }
   sprintf(query, "create user %s@localhost identified by '%s'", username, password);
   if (mysql_query(_conn, query)) {
    printf("Error %u creating user: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-14);
   }
   sprintf(query, "GRANT ALL PRIVILEGES ON %s.* TO %s@localhost;", database, username);
   if (mysql_query(_conn, query)) {
    printf("Error %u granting rights: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-15);
   }
   mysql_close(_conn);
   printf("Done. Reconnecting to newly created database...\n", database);
   _conn = mysql_init(((void *)0));
   if (_conn == ((void *)0)) {
    printf("Error %u re-initializing connection structure : %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-16);
   }
   if (mysql_real_connect(_conn, server, username, password, database, 0, ((void *)0), 0) == ((void *)0)) {
    printLog(6, "Error %u connecting to new Database: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-17);
   }
   printf("\nCongratulations. A new database called '%s' has been created, with the table 'logged_values'.\nA new user '%s' has also been created and was granted access to the new DB.\nIt is now strongly recommended to edit yasdi.ini to remove the\nroot_username/root_password information.\n\nWaiting 20 seconds...\n", database, username);
   fflush(stdout);
   sleep(20);
  }
 }
 sprintf(query, "CREATE TABLE IF NOT EXISTS logged_values (logdate DATE, logtime TIME");
 for (channelNr = 0; channelNr < channelCount; channelNr++) {
  strcat(query, ", ");
  strcat(query, dbColumns[channelNr]);
  strcat(query, " ");
  strcat(query, dbTypes[channelNr]);
 }
 strcat(query, ")");
 printLog(0, "Executing : %s\n", query);
 if (mysql_query(_conn, query)) {
  printLog(6, "Error %u creating table: %s\n", mysql_errno(_conn), mysql_error(_conn));
  exit(-18);
 }
 printLog(1, "Connection OK.\n");
}

canonical_original_code:
void dbInit(char *iniFilename, int channelCount, char dbColumns[][30], char dbTypes[][30])
{
 char server[30];
 char database[30];
 char rootUsername[30];
 char rootPassword[30];
 char username[30];
 char password[30];
 char query[2000];
 int *__errno_location();
 int channelNr;
    strcpy(_iniFilename, iniFilename);
    _channelCount = channelCount;
 GetPrivateProfileString_("DB", "root_username", "root", rootUsername, sizeof(rootUsername), _iniFilename);
 GetPrivateProfileString_("DB", "root_password", "", rootPassword, sizeof(rootPassword), _iniFilename);
 GetPrivateProfileString_("DB", "username", "sma", username, sizeof(username), _iniFilename);
 GetPrivateProfileString_("DB", "password", "smysqlogger", password, sizeof(password), _iniFilename);
 GetPrivateProfileString_("DB", "server", "localhost", server, sizeof(server), _iniFilename);
 GetPrivateProfileString_("DB", "database", "sma", database, sizeof(database), _iniFilename);
 printLog(1, "Connecting to MySQL as %s@%s:%s (client version: %s)\n", trim(username), trim(server), trim(database), mysql_get_client_info());
 _conn = mysql_init(((void *)0));
 if (_conn == ((void *)0)) {
  printLog(6, "Error %u initializing connection structure : %s\n", mysql_errno(_conn), mysql_error(_conn));
  exit(-10);
 }
 if (mysql_real_connect(_conn, server, username, password, database, 0, ((void *)0), 0) == ((void *)0)) {
  (*__errno_location ()) = mysql_errno(_conn);
  if ((*__errno_location ()) != 1049 && (*__errno_location ()) != 1045) {
   printLog(6, "Error %u connecting to Database: %s\n", (*__errno_location ()), mysql_error(_conn));
   exit(-11);
  }
  else {
   printf("Database '%s' doesn't exist. Trying to create it...", database);
   if (mysql_real_connect(_conn, server, rootUsername, rootPassword, ((void *)0), 0, ((void *)0), 0) == ((void *)0)) {
    printf("Error %u connecting to Database: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-12);
   }
   sprintf(query, "create database if not exists %s", database);
   if (mysql_query(_conn, query)) {
    printf("Error %u creating Database: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-13);
   }
   sprintf(query, "create user %s@localhost identified by '%s'", username, password);
   if (mysql_query(_conn, query)) {
    printf("Error %u creating user: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-14);
   }
   sprintf(query, "GRANT ALL PRIVILEGES ON %s.* TO %s@localhost;", database, username);
   if (mysql_query(_conn, query)) {
    printf("Error %u granting rights: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-15);
   }
   mysql_close(_conn);
   printf("Done. Reconnecting to newly created database...\n", database);
   _conn = mysql_init(((void *)0));
   if (_conn == ((void *)0)) {
    printf("Error %u re-initializing connection structure : %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-16);
   }
   if (mysql_real_connect(_conn, server, username, password, database, 0, ((void *)0), 0) == ((void *)0)) {
    printLog(6, "Error %u connecting to new Database: %s\n", mysql_errno(_conn), mysql_error(_conn));
    exit(-17);
   }
   printf("\nCongratulations. A new database called '%s' has been created, with the table 'logged_values'.\nA new user '%s' has also been created and was granted access to the new DB.\nIt is now strongly recommended to edit yasdi.ini to remove the\nroot_username/root_password information.\n\nWaiting 20 seconds...\n", database, username);
   fflush(stdout);
   sleep(20);
  }
 }
 sprintf(query, "CREATE TABLE IF NOT EXISTS logged_values (logdate DATE, logtime TIME");
 for (channelNr = 0; channelNr < channelCount; channelNr++) {
  strcat(query, ", ");
  strcat(query, dbColumns[channelNr]);
  strcat(query, " ");
  strcat(query, dbTypes[channelNr]);
 }
 strcat(query, ")");
 printLog(0, "Executing : %s\n", query);
 if (mysql_query(_conn, query)) {
  printLog(6, "Error %u creating table: %s\n", mysql_errno(_conn), mysql_error(_conn));
  exit(-18);
 }
 printLog(1, "Connection OK.\n");
}

variable_types:
iniFilename
char *

variable_types:
dbColumns
char [][30]

variable_types:
dbTypes
char [][30]

variable_types:
server
char [30]

variable_types:
database
char [30]

variable_types:
rootUsername
char [30]

variable_types:
rootPassword
char [30]

variable_types:
username
char [30]

variable_types:
password
char [30]

variable_types:
query
char [2000]

variable_types: channelNr int


reconnect

decompiled_code:
__int64 reconnect()
{
const char *v0; // r12
const char *v1; // rbp
const char *v2; // rbx
const char *v3; // rax
__int64 v4; // rdi
const char *v6; // rbx
unsigned int v7; // eax
unsigned int v8; // ebp
unsigned int *v9; // rbx
const char *v10; // rax
char v11[32]; // [rsp+0h] [rbp-98h] BYREF
char v12[32]; // [rsp+20h] [rbp-78h] BYREF
char v13[32]; // [rsp+40h] [rbp-58h] BYREF
char v14[56]; // [rsp+60h] [rbp-38h] BYREF

mysql_close(conn);
GetPrivateProfileString_("DB", "username", "sma", v12, 30LL, iniFilename);
GetPrivateProfileString_("DB", "password", "smysqlogger", v11, 30LL, iniFilename);
GetPrivateProfileString_("DB", "server", "localhost", v14, 30LL, iniFilename);
GetPrivateProfileString_("DB", "database", "sma", v13, 30LL, iniFilename);
v0 = (const char *)mysql_get_client_info();
v1 = (const char *)trim(v13);
v2 = (const char *)trim(v14);
v3 = (const char *)trim(v12);
printLog(1LL, "Reconnecting to MySQL as %s@%s:%s (client version: %s)\n", v3, v2, v1, v0);
v4 = mysql_init(0LL);
conn = v4;
if ( !v4 )
{
v6 = (const char *)mysql_error(0LL);
v7 = mysql_errno(conn);
printLog(6LL, "Error %u reinitializing connection structure : %s\n", v7, v6);
exit(-10);
}
if ( !mysql_real_connect(v4, v14, v12, v11, v13, 0LL, 0LL, 0LL) )
{
v8 = mysql_errno(conn);
v9 = (unsigned int *)_errno_location();
*v9 = v8;
v10 = (const char *)mysql_error(conn);
printLog(6LL, "Error %u reconnecting to Database: %s\n", *v9, v10);
exit(-11);
}
return printLog(2LL, "Reconnected\n");
}

canonical_decompiled_code:
__int64 func1()
{
const char *v0; // r12
const char *v1; // rbp
const char *v2; // rbx
const char *v3; // rax
__int64 v4; // rdi
const char *v6; // rbx
unsigned int v7; // eax
unsigned int v8; // ebp
unsigned int *v9; // rbx
const char *v10; // rax
char v11[32]; // [rsp+0h] [rbp-98h] BYREF
char v12[32]; // [rsp+20h] [rbp-78h] BYREF
char v13[32]; // [rsp+40h] [rbp-58h] BYREF
char v14[56]; // [rsp+60h] [rbp-38h] BYREF

mysql_close(conn);
GetPrivateProfileString_("DB", "username", "sma", v12, 30LL, iniFilename);
GetPrivateProfileString_("DB", "password", "smysqlogger", v11, 30LL, iniFilename);
GetPrivateProfileString_("DB", "server", "localhost", v14, 30LL, iniFilename);
GetPrivateProfileString_("DB", "database", "sma", v13, 30LL, iniFilename);
v0 = (const char *)mysql_get_client_info();
v1 = (const char *)trim(v13);
v2 = (const char *)trim(v14);
v3 = (const char *)trim(v12);
printLog(1LL, "Reconnecting to MySQL as %s@%s:%s (client version: %s)\n", v3, v2, v1, v0);
v4 = mysql_init(0LL);
conn = v4;
if ( !v4 )
{
v6 = (const char *)mysql_error(0LL);
v7 = mysql_errno(conn);
printLog(6LL, "Error %u reinitializing connection structure : %s\n", v7, v6);
exit(-10);
}
if ( !mysql_real_connect(v4, v14, v12, v11, v13, 0LL, 0LL, 0LL) )
{
v8 = mysql_errno(conn);
v9 = (unsigned int *)_errno_location();
*v9 = v8;
v10 = (const char *)mysql_error(conn);
printLog(6LL, "Error %u reconnecting to Database: %s\n", *v9, v10);
exit(-11);
}
return printLog(2LL, "Reconnected\n");
}

original_code:
void reconnect()
{
 char server[30];
 char database[30];
 char username[30];
 char password[30];
 int (*__errno_location ());
 int channelNr;
    mysql_close(_conn);
 GetPrivateProfileString_("DB", "username", "sma", username, sizeof(username), _iniFilename);
 GetPrivateProfileString_("DB", "password", "smysqlogger", password, sizeof(password), _iniFilename);
 GetPrivateProfileString_("DB", "server", "localhost", server, sizeof(server), _iniFilename);
 GetPrivateProfileString_("DB", "database", "sma", database, sizeof(database), _iniFilename);
 printLog(1, "Reconnecting to MySQL as %s@%s:%s (client version: %s)\n", trim(username), trim(server), trim(database), mysql_get_client_info());
 _conn = mysql_init(((void *)0));
 if (_conn == ((void *)0)) {
  printLog(6, "Error %u reinitializing connection structure : %s\n", mysql_errno(_conn), mysql_error(_conn));
  exit(-10);
 }
 if (mysql_real_connect(_conn, server, username, password, database, 0, ((void *)0), 0) == ((void *)0)) {
  (*__errno_location ()) = mysql_errno(_conn);
        printLog(6, "Error %u reconnecting to Database: %s\n", (*__errno_location ()), mysql_error(_conn));
  exit(-11);
 }
 printLog(2, "Reconnected\n");
}

canonical_original_code:
void reconnect()
{
 char server[30];
 char database[30];
 char username[30];
 char password[30];
 int *__errno_location();
 int channelNr;
    mysql_close(_conn);
 GetPrivateProfileString_("DB", "username", "sma", username, sizeof(username), _iniFilename);
 GetPrivateProfileString_("DB", "password", "smysqlogger", password, sizeof(password), _iniFilename);
 GetPrivateProfileString_("DB", "server", "localhost", server, sizeof(server), _iniFilename);
 GetPrivateProfileString_("DB", "database", "sma", database, sizeof(database), _iniFilename);
 printLog(1, "Reconnecting to MySQL as %s@%s:%s (client version: %s)\n", trim(username), trim(server), trim(database), mysql_get_client_info());
 _conn = mysql_init(((void *)0));
 if (_conn == ((void *)0)) {
  printLog(6, "Error %u reinitializing connection structure : %s\n", mysql_errno(_conn), mysql_error(_conn));
  exit(-10);
 }
 if (mysql_real_connect(_conn, server, username, password, database, 0, ((void *)0), 0) == ((void *)0)) {
  (*__errno_location ()) = mysql_errno(_conn);
        printLog(6, "Error %u reconnecting to Database: %s\n", (*__errno_location ()), mysql_error(_conn));
  exit(-11);
 }
 printLog(2, "Reconnected\n");
}


insertValues

decompiled_code:
__int64 __fastcall insertValues(int a1, const char *a2, const char *a3)
{
struct tm *v4; // rbx
const char *v5; // rbx
__int64 v6; // rbp
const char *v7; // rbx
__int64 v8; // rbp
int *v9; // rbx
int v10; // eax
const char *v11; // rbp
unsigned int v12; // eax
char src[9]; // [rsp+4h] [rbp-814h] BYREF
char s[11]; // [rsp+Dh] [rbp-80Bh] BYREF
time_t timer; // [rsp+18h] [rbp-800h] BYREF
char dest[2040]; // [rsp+20h] [rbp-7F8h] BYREF

timer = time(0LL);
v4 = localtime(&timer);
strftime(s, 0xBuLL, "%Y-%m-%d", v4);
strftime(src, 9uLL, "%H:%M:%S", v4);
strcpy(dest, "INSERT INTO logged_values (logdate, logtime");
if ( a1 <= 0 )
{
strcat(dest, ") VALUES ('");
strcat(dest, s);
strcat(dest, "', '");
strcat(dest, src);
*(_WORD *)&dest[strlen(dest)] = 39;
}
else
{
v5 = a2;
v6 = 30LL * (unsigned int)(a1 - 1);
do
{
strcat(dest, ", ");
strcat(dest, v5);
v5 += 30;
}
while ( v5 != &a2[v6 + 30] );
strcat(dest, ") VALUES ('");
strcat(dest, s);
strcat(dest, "', '");
strcat(dest, src);
*(_WORD *)&dest[strlen(dest)] = 39;
v7 = a3;
v8 = (__int64)&a3[v6 + 30];
do
{
*(_DWORD *)&dest[strlen(dest)] = 2564140;
strcat(dest, v7);
*(_WORD *)&dest[strlen(dest)] = 39;
v7 += 30;
}
while ( v7 != (const char *)v8 );
}
*(_WORD *)&dest[strlen(dest)] = 41;
printLog(0LL, "Executing : %s\n", dest);
v9 = _errno_location();
*v9 = -1;
while ( *v9 )
{
v10 = mysql_query(conn, dest);
*v9 = v10;
if ( v10 )
{
v11 = (const char *)mysql_error(conn);
v12 = mysql_errno(conn);
printLog(4LL, "Error %u performing insert into Database: %s.\n", v12, v11);
reconnect();
sleep(1u);
}
}
return 0LL;
}

canonical_decompiled_code:
__int64 __fastcall func2(int a1, const char *a2, const char *a3)
{
struct tm *v4; // rbx
const char *v5; // rbx
__int64 v6; // rbp
const char *v7; // rbx
__int64 v8; // rbp
int *v9; // rbx
int v10; // eax
const char *v11; // rbp
unsigned int v12; // eax
char src[9]; // [rsp+4h] [rbp-814h] BYREF
char s[11]; // [rsp+Dh] [rbp-80Bh] BYREF
time_t timer; // [rsp+18h] [rbp-800h] BYREF
char dest[2040]; // [rsp+20h] [rbp-7F8h] BYREF

timer = time(0LL);
v4 = localtime(&timer);
strftime(s, 0xBuLL, "%Y-%m-%d", v4);
strftime(src, 9uLL, "%H:%M:%S", v4);
strcpy(dest, "INSERT INTO logged_values (logdate, logtime");
if ( a1 <= 0 )
{
strcat(dest, ") VALUES ('");
strcat(dest, s);
strcat(dest, "', '");
strcat(dest, src);
*(_WORD *)&dest[strlen(dest)] = 39;
}
else
{
v5 = a2;
v6 = 30LL * (unsigned int)(a1 - 1);
do
{
strcat(dest, ", ");
strcat(dest, v5);
v5 += 30;
}
while ( v5 != &a2[v6 + 30] );
strcat(dest, ") VALUES ('");
strcat(dest, s);
strcat(dest, "', '");
strcat(dest, src);
*(_WORD *)&dest[strlen(dest)] = 39;
v7 = a3;
v8 = (__int64)&a3[v6 + 30];
do
{
*(_DWORD *)&dest[strlen(dest)] = 2564140;
strcat(dest, v7);
*(_WORD *)&dest[strlen(dest)] = 39;
v7 += 30;
}
while ( v7 != (const char *)v8 );
}
*(_WORD *)&dest[strlen(dest)] = 41;
printLog(0LL, "Executing : %s\n", dest);
v9 = _errno_location();
*v9 = -1;
while ( *v9 )
{
v10 = mysql_query(conn, dest);
*v9 = v10;
if ( v10 )
{
v11 = (const char *)mysql_error(conn);
v12 = mysql_errno(conn);
printLog(4LL, "Error %u performing insert into Database: %s.\n", v12, v11);
func1();
sleep(1u);
}
}
return 0LL;
}

original_code:
int insertValues(int channelCount, char dbColumns[][30], char channelValues[][30]) {
 char query[2000];
 int (*__errno_location ());
 int channelNr;
 time_t now;
 struct tm *tm_now;
 char logdate[11];
 char logtime[9];
 now = time(((void *)0));
 tm_now = localtime(&now);
 strftime(logdate, sizeof(logdate), "%Y-%m-%d", tm_now);
 strftime(logtime, sizeof(logtime), "%H:%M:%S", tm_now);
 sprintf(query, "INSERT INTO logged_values (logdate, logtime");
 for (channelNr = 0; channelNr < channelCount; channelNr++) {
  strcat(query, ", ");
  strcat(query, dbColumns[channelNr]);
 }
 strcat(query, ") VALUES ('");
 strcat(query, logdate);
 strcat(query, "', '");
 strcat(query, logtime);
 strcat(query, "'");
 for (channelNr = 0; channelNr < channelCount; channelNr++) {
  strcat(query, ", '");
  strcat(query, channelValues[channelNr]);
  strcat(query, "'");
 }
 strcat(query, ")");
 printLog(0, "Executing : %s\n", query);
    (*__errno_location ()) = -1;
    while ((*__errno_location ()) != 0) {
        (*__errno_location ()) = mysql_query(_conn, query);
        if ((*__errno_location ()) != 0) {
            printLog(4, "Error %u performing insert into Database: %s.\n", mysql_errno(_conn), mysql_error(_conn));
            reconnect();
            sleep(1);
        }
    }
 return (*__errno_location ());
}

canonical_original_code:
int insertValues(int channelCount, char dbColumns[][30], char channelValues[][30]) {
 char query[2000];
 int *__errno_location();
 int channelNr;
 long now;
 struct tm *tm_now;
 char logdate[11];
 char logtime[9];
 now = time(((void *)0));
 tm_now = localtime(&now);
 strftime(logdate, sizeof(logdate), "%Y-%m-%d", tm_now);
 strftime(logtime, sizeof(logtime), "%H:%M:%S", tm_now);
 sprintf(query, "INSERT INTO logged_values (logdate, logtime");
 for (channelNr = 0; channelNr < channelCount; channelNr++) {
  strcat(query, ", ");
  strcat(query, dbColumns[channelNr]);
 }
 strcat(query, ") VALUES ('");
 strcat(query, logdate);
 strcat(query, "', '");
 strcat(query, logtime);
 strcat(query, "'");
 for (channelNr = 0; channelNr < channelCount; channelNr++) {
  strcat(query, ", '");
  strcat(query, channelValues[channelNr]);
  strcat(query, "'");
 }
 strcat(query, ")");
 printLog(0, "Executing : %s\n", query);
    (*__errno_location ()) = -1;
    while ((*__errno_location ()) != 0) {
        (*__errno_location ()) = mysql_query(_conn, query);
        if ((*__errno_location ()) != 0) {
            printLog(4, "Error %u performing insert into Database: %s.\n", mysql_errno(_conn), mysql_error(_conn));
            reconnect();
            sleep(1);
        }
    }
 return (*__errno_location ());
}

variable_types: channelCount int

variable_types:
channelValues
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; } *

variable_types:
logdate
char [11]

variable_types:
logtime
char [9]


dbShutdown

decompiled_code:
__int64 dbShutdown()
{
return mysql_close(conn);
}

canonical_decompiled_code:
__int64 func3()
{
return mysql_close(conn);
}

original_code:
void dbShutdown()
{
 mysql_close(_conn);
}