// File: /workspace/github_cpp_makefile_star2_O1/Matheus-Aguilar[P]Huffman/Huffman.cpp
// Function: HuffmanTree::build(int const*)
void HuffmanTree::build(const int freq[256]){

    //Copying the frequencies
    for(int i = 0; i < 256; ++i)
        this->freq[i] = freq[i];

    //Priority queue of frequencies and roots to mantain the order of the trees
    priority_queue<pair<int, Node *>> pq;
    
    //Adding characters that appear at least once in the string
    for(int i = 0; i < 256; ++i){
        if(this->freq[i] > 0){
            //Priority and root the tree
            pq.push(make_pair(-freq[i], new Node(i)));
        }
    }

    //If the string is empty
    if(pq.size() == 0){
        root = NULL;
        return;
    }

    //Keeps merging trees until there is just one
    while(pq.size() > 1){
        
        //Remove the two ones without the highest priority
        pair<int, Node *> topTree1 = pq.top();
        pq.pop();
        pair<int, Node *> topTree2 = pq.top();
        pq.pop();

        /*Merge both nodes into a bigger tree*/

        //Add the frequencies of the smaller trees roots into the merged one
        pair<int, Node *> mergedTree = make_pair(topTree1.first + topTree2.first, new Node(0));
        //Highest priority tree becomes left child
        mergedTree.second->left = topTree1.second; 
        //Second highest becomes right child
        mergedTree.second->right = topTree2.second; 
        //Merged tree becomes the parent of the smaller ones
        topTree1.second->parent = topTree2.second->parent = mergedTree.second;

        //Add the merged node into the priority queue
        pq.push(mergedTree);
    }

    //Define the root of the Huffman Tree as the merge of all character trees
    root = pq.top().second;

    /*Build character codes*/

    /*File with only one type of character. This is a special case
    because the decompresion algorithm assumes that the root has at
    least one child*/
    if(root->left == NULL && root->right == NULL){
        code[root->character].push_back(0); //The code is 0 by default
        Node *aux = root; //Save the merged result
        root = new Node(0); //Set a new root of the tree
        root->left = aux; //Update the left child of the root
        return;
    }
    
    /*More than one type of character cases*/
    vector<bool> codeString;
    buildCodes(root, codeString); //Start from the root to build codes
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %r14
movq %rdi, %rbx
xorl %eax, %eax
movl (%r14,%rax,4), %ecx
movl %ecx, 0x8(%rbx,%rax,4)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x303c
xorps %xmm0, %xmm0
leaq 0x30(%rsp), %r15
movaps %xmm0, (%r15)
movq $0x0, 0x10(%r15)
xorl %r13d, %r13d
leaq 0x8(%rsp), %r12
cmpl $0x0, 0x8(%rbx,%r13,4)
jle 0x30a9
movl (%r14,%r13,4), %ebp
movl $0x20, %edi
callq 0x2120
negl %ebp
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movb %r13b, 0x18(%rax)
movl %ebp, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x359e
incq %r13
cmpq $0x100, %r13            # imm = 0x100
jne 0x306b
movq 0x38(%rsp), %rax
cmpq 0x30(%rsp), %rax
je 0x31b6
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
subq %rax, %rcx
cmpq $0x11, %rcx
jb 0x3150
leaq 0x30(%rsp), %r14
movl (%rax), %ebp
movq 0x8(%rax), %r12
movq %r14, %rdi
callq 0x35f2
movq 0x30(%rsp), %rax
movl (%rax), %r15d
movq 0x8(%rax), %r13
movq %r14, %rdi
callq 0x35f2
movl $0x20, %edi
callq 0x2120
addl %ebp, %r15d
xorps %xmm0, %xmm0
movups %xmm0, 0x9(%rax)
movups %xmm0, (%rax)
movl %r15d, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movq %r12, (%rax)
movq %r13, 0x8(%rax)
movq %rax, 0x10(%r13)
movq %rax, 0x10(%r12)
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
callq 0x3636
movq 0x30(%rsp), %rax
movq 0x38(%rsp), %rcx
subq %rax, %rcx
cmpq $0x10, %rcx
ja 0x30dd
movq 0x8(%rax), %rsi
movq %rsi, (%rbx)
cmpq $0x0, (%rsi)
jne 0x3164
cmpq $0x0, 0x8(%rsi)
je 0x31e3
xorl %eax, %eax
leaq 0x8(%rsp), %rdx
movq %rax, (%rdx)
movl %eax, 0x8(%rdx)
movq %rax, 0x10(%rdx)
movl %eax, 0x18(%rdx)
movq %rax, 0x20(%rdx)
movq %rbx, %rdi
callq 0x334c
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x31bd
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x2130
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movl %eax, 0x20(%rsp)
movq %rax, 0x28(%rsp)
jmp 0x31bd
movq $0x0, (%rbx)
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x31d4
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2130
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movzbl 0x18(%rsi), %eax
leaq (%rax,%rax,4), %rax
leaq (%rbx,%rax,8), %rdi
addq $0x408, %rdi            # imm = 0x408
xorl %esi, %esi
callq 0x2a92
movq (%rbx), %r14
movl $0x20, %edi
callq 0x2120
xorps %xmm0, %xmm0
movups %xmm0, 0x9(%rax)
movups %xmm0, (%rax)
movq %rax, (%rbx)
movq %r14, (%rax)
jmp 0x31bd
jmp 0x325d
jmp 0x325d
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x3260
movq 0x28(%rsp), %rsi
subq %rdi, %rsi
callq 0x2130
xorl %eax, %eax
movq %rax, 0x8(%rsp)
movl %eax, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movl %eax, 0x20(%rsp)
movq %rax, 0x28(%rsp)
jmp 0x3260
jmp 0x325d
jmp 0x325d
jmp 0x325d
jmp 0x325d
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x3277
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2130
movq %rbx, %rdi
callq 0x21d0
nop