// File: irakr[P]Minimal-File-Compression-Tool/src/huffman-encoder.cpp
// Function: HuffmanEncoder::buildHuffmanTree()
void HuffmanEncoder :: buildHuffmanTree() {
	Log(this, "Started...");

	MinHeapNode<byte, double> *left, *right, *top;

	// Create a min heap & inserts all characters of data[]
	//@@@ MinHeap<byte, double> minHeap;
	for(auto& x : *m_frequencyTable)
		m_huffmanTree->push(new MinHeapNode<byte, double>(x.first, x.second));
	//printMinheap(*m_huffmanTree);

	// Iterate while size of heap doesn't become 1
	while (m_huffmanTree->size() != 1)
	{
		// Extract the two minimum freq items from min heap
		left = m_huffmanTree->top();
		m_huffmanTree->pop();

		right = m_huffmanTree->top();
		m_huffmanTree->pop();

		// Create a new internal node with frequency equal to the
		// sum of the two nodes frequencies. Make the two extracted
		// node as left and right children of this new node. Add
		// this node to the min heap.
		// 0x00 is a special value for internal nodes.
		top = new MinHeapNode<byte, double>(0x00, left->freq + right->freq, true);
		top->left = left;
		top->right = right;
		m_huffmanTree->push(top);
	}
	//printMinheap(*m_huffmanTree);
	//traversePreorder(m_huffmanTree->top(), "");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq 0x30(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4440
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
callq 0x4470
movq %rax, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x44a0
testb $0x1, %al
jne 0x3b8f
jmp 0x3c13
leaq -0x30(%rbp), %rdi
callq 0x44d0
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq %rcx, -0x40(%rbp)
movq 0x38(%rax), %rax
movq %rax, -0x70(%rbp)
movl $0x28, %edi
callq 0x3210
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x68(%rbp)
movq -0x40(%rbp), %rax
movzbl (%rax), %esi
movsd 0x8(%rax), %xmm0
xorl %edx, %edx
callq 0x4580
jmp 0x3bd4
movq -0x70(%rbp), %rdi
movq -0x68(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq -0x48(%rbp), %rsi
callq 0x4530
leaq -0x30(%rbp), %rdi
callq 0x4500
jmp 0x3b79
movq -0x68(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
movl $0x28, %esi
callq 0x3220
jmp 0x3d07
jmp 0x3c15
movq -0x60(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x45d0
cmpq $0x1, %rax
je 0x3cfe
movq -0x60(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x45f0
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq (%rcx), %rcx
movq %rcx, -0x10(%rbp)
movq 0x38(%rax), %rdi
callq 0x4610
movq -0x60(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x45f0
movq %rax, %rcx
movq -0x60(%rbp), %rax
movq (%rcx), %rcx
movq %rcx, -0x18(%rbp)
movq 0x38(%rax), %rdi
callq 0x4610
movl $0x28, %edi
callq 0x3210
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movsd 0x8(%rax), %xmm0
movq -0x18(%rbp), %rax
movsd 0x8(%rax), %xmm1
addsd %xmm1, %xmm0
xorl %esi, %esi
movl $0x1, %edx
callq 0x4580
jmp 0x3cac
movq -0x60(%rbp), %rax
movq -0x78(%rbp), %rcx
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq %rdx, 0x10(%rcx)
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq %rdx, 0x18(%rcx)
movq 0x38(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x4660
jmp 0x3c15
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
movl $0x28, %esi
callq 0x3220
jmp 0x3d07
addq $0x80, %rsp
popq %rbp
retq
movq -0x50(%rbp), %rdi
callq 0x33e0