// File: /workspace/github_cpp_makefile_star2_O3/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 $0x48, %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 0x3050
xorps %xmm0, %xmm0
movq %rsp, %r15
movaps %xmm0, (%r15)
movq $0x0, 0x10(%r15)
xorl %r13d, %r13d
leaq 0x20(%rsp), %r12
cmpl $0x0, 0x8(%rbx,%r13,4)
jle 0x30bb
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, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x357a
incq %r13
cmpq $0x100, %r13            # imm = 0x100
jne 0x307d
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
subq %rdi, %rax
je 0x31a9
cmpq $0x11, %rax
jb 0x3155
movq %rsp, %r14
movl (%rdi), %ebp
movq 0x8(%rdi), %r12
movq %r14, %rdi
callq 0x35d6
movq (%rsp), %rax
movl (%rax), %r15d
movq 0x8(%rax), %r13
movq %r14, %rdi
callq 0x35d6
movl $0x20, %edi
callq 0x2120
addl %ebp, %r15d
movb $0x0, 0x18(%rax)
movq $0x0, 0x10(%rax)
movl %r15d, 0x20(%rsp)
movq %rax, 0x28(%rsp)
movq %r12, (%rax)
movq %r13, 0x8(%rax)
movq %rax, 0x10(%r13)
movq %rax, 0x10(%r12)
movq %r14, %rdi
leaq 0x20(%rsp), %rsi
callq 0x3622
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
subq %rdi, %rax
cmpq $0x10, %rax
ja 0x30e2
movq 0x8(%rdi), %rsi
movq %rsi, (%rbx)
cmpq $0x0, (%rsi)
jne 0x3169
cmpq $0x0, 0x8(%rsi)
je 0x31b2
xorl %eax, %eax
leaq 0x40(%rsp), %r14
movq %rax, -0x20(%r14)
movl %eax, -0x18(%r14)
movq %rax, -0x10(%r14)
movl %eax, -0x8(%r14)
movq %rax, (%r14)
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
callq 0x3320
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x31ea
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x2130
jmp 0x31ea
movq $0x0, (%rbx)
jmp 0x31ee
movzbl 0x18(%rsi), %eax
leaq (%rax,%rax,4), %rax
leaq (%rbx,%rax,8), %rdi
addq $0x408, %rdi            # imm = 0x408
xorl %esi, %esi
callq 0x2a9c
movq (%rbx), %r14
movl $0x20, %edi
callq 0x2120
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rax)
movb $0x0, 0x18(%rax)
movq %rax, (%rbx)
movq %r14, (%rax)
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x3200
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2130
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3232
jmp 0x3232
movq %rax, %rbx
movq 0x20(%rsp), %rsi
testq %rsi, %rsi
je 0x3235
movq %r14, %rdi
callq 0x2201
jmp 0x3235
jmp 0x3232
jmp 0x3232
jmp 0x3232
jmp 0x3232
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x324b
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2130
movq %rbx, %rdi
callq 0x21d0
nop