// File: irakr[P]Minimal-File-Compression-Tool/src/huffman-encoder.cpp
// Function: HuffmanEncoder::buildFrequencyTable(unsigned char*)
void HuffmanEncoder :: buildFrequencyTable(byte *buffer) {
	//@std::cout << "In HuffmanEncoder::buildFrequencyTable()" << std::endl;
	Log(this, "Started...");

	// Compute the frequencies of each byte in buffer and generate the
	// symbol-frequency map
	int i, j;	//, freq_len = 0;
	double frequency[UCHAR_MAX + 1] = {0}; // 2^8-1 = 255 = UCHAR_MAX different symbols
	memset(frequency, 0, UCHAR_MAX);
	for(i = 0; buffer[i] != '\0'; i++) {
		j = buffer[i]; // Just to typecast 'byte' to 'int'
		++(frequency[j]);
	}

	/****** Test *******
	for(i = 0; i <= UCHAR_MAX; i++) {
		std::cout << "frequency[" << i << "] = " << frequency[i] << std::endl;
	}
	****/

	// Fill the original symbol-frequency map by filtering out symbols
	// that does not exist(or frequency[symbol(i)] = 0)
	for(i = 0; i <= UCHAR_MAX; i++) {
		if(frequency[i] > 0) {
			byte b = i;
			(*m_frequencyTable)[b] = frequency[i];
		}
	}


	for(auto& x : *m_frequencyTable)
		x.second = (x.second / m_inBuffSize) * 100;

	/****** Test *******
	for(auto& x : *m_frequencyTable)
		std::cout << x.first << ":" << x.second << std::endl;
	//std::cout << (*m_frequencyTable)[(byte)'0'] << std::endl;
	****/

	//asm("nop");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x860, %rsp            # imm = 0x860
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x850(%rbp)
leaq -0x820(%rbp), %rdi
xorl %esi, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x3130
leaq -0x820(%rbp), %rdi
xorl %esi, %esi
movl $0xff, %edx
callq 0x3130
movl $0x0, -0x14(%rbp)
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x3a14
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
movzbl (%rax,%rcx), %eax
movl %eax, -0x18(%rbp)
movslq -0x18(%rbp), %rax
movsd 0x9619(%rip), %xmm0     # 0xd010
addsd -0x820(%rbp,%rax,8), %xmm0
movsd %xmm0, -0x820(%rbp,%rax,8)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x39cb
movl $0x0, -0x14(%rbp)
cmpl $0xff, -0x14(%rbp)
jg 0x3a88
movslq -0x14(%rbp), %rax
movsd -0x820(%rbp,%rax,8), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x3a7b
movq -0x850(%rbp), %rax
movl -0x14(%rbp), %ecx
movb %cl, -0x821(%rbp)
movslq -0x14(%rbp), %rcx
movsd -0x820(%rbp,%rcx,8), %xmm0
movsd %xmm0, -0x858(%rbp)
movq 0x30(%rax), %rdi
leaq -0x821(%rbp), %rsi
callq 0x4410
movsd -0x858(%rbp), %xmm0
movsd %xmm0, (%rax)
jmp 0x3a7d
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x3a1b
movq -0x850(%rbp), %rax
movq 0x30(%rax), %rax
movq %rax, -0x830(%rbp)
movq -0x830(%rbp), %rdi
callq 0x4440
movq %rax, -0x838(%rbp)
movq -0x830(%rbp), %rdi
callq 0x4470
movq %rax, -0x840(%rbp)
leaq -0x838(%rbp), %rdi
leaq -0x840(%rbp), %rsi
callq 0x44a0
testb $0x1, %al
jne 0x3ad9
jmp 0x3b32
leaq -0x838(%rbp), %rdi
callq 0x44d0
movq %rax, %rcx
movq -0x850(%rbp), %rax
movq %rcx, -0x848(%rbp)
movq -0x848(%rbp), %rcx
movsd 0x8(%rcx), %xmm0
cvtsi2sdq 0x28(%rax), %xmm1
divsd %xmm1, %xmm0
movsd 0x94f4(%rip), %xmm1     # 0xd008
mulsd %xmm1, %xmm0
movq -0x848(%rbp), %rax
movsd %xmm0, 0x8(%rax)
leaq -0x838(%rbp), %rdi
callq 0x4500
jmp 0x3ac0
addq $0x860, %rsp            # imm = 0x860
popq %rbp
retq
nopl (%rax,%rax)