// File: sbme-tutorials[P]huffman-encoding/huffman.cpp
// Function: Huffman::buildTree()
void Huffman::buildTree()
{
    struct Compare
    {
        bool operator()(Node *a, Node *b)
        {
            return (a->p > b->p);
        }
    };
    // Create a min heap & inserts all characters of input data[]
    std::priority_queue<Node *, std::vector<Node *>, Compare> minHeap;
    //build heap
    for (auto pixel : probability)
    {
        Node *n = new Node{pixel.first, pixel.second, nullptr, nullptr};
        minHeap.push(n);
    }
    Node *parent;
    while (minHeap.size() != 1)
    {
        Node *left = minHeap.top();
        minHeap.pop();
        Node *right = minHeap.top();
        minHeap.pop();
        parent = new Node;
        parent->p = left->p + right->p;
        parent->left = left;
        parent->right = right;
        minHeap.push(parent);
    }
    tree = parent;

    char arr[255];
    getCodeTable(tree, arr, 0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1b0, %rsp            # imm = 0x1B0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x188(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x54a0
movq -0x188(%rbp), %rax
addq $0x70, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0x7070
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0x70a0
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x70d0
testb $0x1, %al
jne 0x52e2
jmp 0x5363
leaq -0x38(%rbp), %rdi
callq 0x7100
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movl $0x18, %edi
callq 0x32c0
movq %rax, -0x190(%rbp)
jmp 0x5305
movq -0x190(%rbp), %rax
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movq $0x0, 0x10(%rax)
movb -0x48(%rbp), %cl
movb %cl, (%rax)
movss -0x44(%rbp), %xmm0
movss %xmm0, 0x4(%rax)
movq %rax, -0x50(%rbp)
leaq -0x28(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x54d0
jmp 0x533c
jmp 0x533e
leaq -0x38(%rbp), %rdi
callq 0x7140
jmp 0x52cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x5710
jmp 0x5488
jmp 0x5365
leaq -0x28(%rbp), %rdi
callq 0x5520
movq %rax, -0x198(%rbp)
jmp 0x5377
movq -0x198(%rbp), %rax
cmpq $0x1, %rax
je 0x5453
leaq -0x28(%rbp), %rdi
callq 0x5540
movq %rax, -0x1a0(%rbp)
jmp 0x539a
movq -0x1a0(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x70(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x5560
jmp 0x53b3
leaq -0x28(%rbp), %rdi
callq 0x5540
movq %rax, -0x1a8(%rbp)
jmp 0x53c5
movq -0x1a8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x78(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x5560
jmp 0x53de
movl $0x18, %edi
callq 0x32c0
movq %rax, -0x1b0(%rbp)
jmp 0x53f1
movq -0x1b0(%rbp), %rdi
callq 0x7170
movq -0x1b0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x70(%rbp), %rax
movss 0x4(%rax), %xmm0
movq -0x78(%rbp), %rax
movss 0x4(%rax), %xmm1
addss %xmm1, %xmm0
movq -0x68(%rbp), %rax
movss %xmm0, 0x4(%rax)
movq -0x70(%rbp), %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x78(%rbp), %rcx
movq -0x68(%rbp), %rax
movq %rcx, 0x10(%rax)
leaq -0x28(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x54d0
jmp 0x544e
jmp 0x5365
movq -0x188(%rbp), %rdi
movq -0x68(%rbp), %rax
movq %rax, 0x50(%rdi)
movq 0x50(%rdi), %rsi
leaq -0x180(%rbp), %rdx
xorl %ecx, %ecx
callq 0x55b0
jmp 0x5476
leaq -0x28(%rbp), %rdi
callq 0x5710
addq $0x1b0, %rsp            # imm = 0x1B0
popq %rbp
retq
movq -0x58(%rbp), %rdi
callq 0x34e0
nopw %cs:(%rax,%rax)