// File: Matheus-Aguilar[P]Huffman/main.cpp
// Function: descompressFile(char*, char*)
void descompressFile(char *in, char *out){
	
	ifstream fin; //Input file
	fin.open(in, ios::binary); 

	ofstream fout; //Output file
	fout.open(out, ofstream::out);

	vector<bool> bits; //Bits of the input file
	vector<char> content; //Decoded content

	int freq[256] = {0}; //Frequency array

	//Getting the frequencies of the huffman encoding
	fin.read(reinterpret_cast<char *>(freq), sizeof(freq));

	//Get the encoding size (to ignore extra bits)
	int HuffmanSize;
	fin.read(reinterpret_cast<char *>(&HuffmanSize), sizeof(int));

	//Reading bytes and conveting to bits
	unsigned char byte;
	while(fin.peek() != -1){
		fin.read(reinterpret_cast<char *>(&byte), sizeof(unsigned char));
		splitByte(byte, bits); //Split byte and append to the end of bits
	}

	//Remove extra bits
	while(bits.size() > HuffmanSize) bits.pop_back(); 

	HuffmanTree tree(freq); //Build the tree
	tree.descompress(content, bits); //Decodes the bits into a string

	for(int i = 0; i < content.size(); ++i)
		fout.write(reinterpret_cast<char *>(&content[i]), sizeof(char)); //Save the decoded content into file

	//Close files
	fin.close();
	fout.close();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x34b0, %rsp           # imm = 0x34B0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x218(%rbp), %rdi
movq %rdi, -0x3490(%rbp)
callq 0x2140
movq -0x3490(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl $0x4, %edx
callq 0x20e0
jmp 0x2a5d
leaq -0x428(%rbp), %rdi
callq 0x21c0
jmp 0x2a6b
movq -0x10(%rbp), %rsi
leaq -0x428(%rbp), %rdi
movl $0x10, %edx
callq 0x2030
jmp 0x2a82
leaq -0x450(%rbp), %rdi
callq 0x2dd0
leaq -0x468(%rbp), %rdi
callq 0x2db0
leaq -0x870(%rbp), %rdi
movq %rdi, -0x34a0(%rbp)
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
movq %rdx, -0x3498(%rbp)
callq 0x20d0
movq -0x34a0(%rbp), %rsi
movq -0x3498(%rbp), %rdx
leaq -0x218(%rbp), %rdi
callq 0x2150
jmp 0x2ad7
leaq -0x218(%rbp), %rdi
leaq -0x874(%rbp), %rsi
movl $0x4, %edx
callq 0x2150
jmp 0x2af1
jmp 0x2af3
leaq -0x218(%rbp), %rdi
callq 0x2090
movl %eax, -0x34a4(%rbp)
jmp 0x2b07
movl -0x34a4(%rbp), %eax
cmpl $-0x1, %eax
je 0x2b88
leaq -0x218(%rbp), %rdi
leaq -0x875(%rbp), %rsi
movl $0x1, %edx
callq 0x2150
jmp 0x2b2c
movzbl -0x875(%rbp), %edi
leaq -0x450(%rbp), %rsi
callq 0x29c0
jmp 0x2b41
jmp 0x2af3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x2cf7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x2ceb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
jmp 0x2cd3
jmp 0x2b8a
leaq -0x450(%rbp), %rdi
callq 0x2e20
movslq -0x874(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x2bb2
leaq -0x450(%rbp), %rdi
callq 0x3080
jmp 0x2bb0
jmp 0x2b8a
leaq -0x3480(%rbp), %rdi
leaq -0x870(%rbp), %rsi
callq 0x58b0
jmp 0x2bc7
leaq -0x3480(%rbp), %rdi
leaq -0x468(%rbp), %rsi
leaq -0x450(%rbp), %rdx
callq 0x6180
jmp 0x2be3
movl $0x0, -0x3484(%rbp)
movslq -0x3484(%rbp), %rax
movq %rax, -0x34b0(%rbp)
leaq -0x468(%rbp), %rdi
callq 0x30a0
movq %rax, %rcx
movq -0x34b0(%rbp), %rax
cmpq %rcx, %rax
jae 0x2c72
movslq -0x3484(%rbp), %rsi
leaq -0x468(%rbp), %rdi
callq 0x30c0
movq %rax, %rsi
leaq -0x428(%rbp), %rdi
movl $0x1, %edx
callq 0x2160
jmp 0x2c3f
jmp 0x2c41
movl -0x3484(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3484(%rbp)
jmp 0x2bed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x220(%rbp)
movl %eax, -0x224(%rbp)
leaq -0x3480(%rbp), %rdi
callq 0x6010
jmp 0x2cd3
leaq -0x218(%rbp), %rdi
callq 0x2050
jmp 0x2c80
leaq -0x428(%rbp), %rdi
callq 0x2170
jmp 0x2c8e
leaq -0x3480(%rbp), %rdi
callq 0x6010
leaq -0x468(%rbp), %rdi
callq 0x3020
leaq -0x450(%rbp), %rdi
callq 0x3000
leaq -0x428(%rbp), %rdi
callq 0x2180
leaq -0x218(%rbp), %rdi
callq 0x2060
addq $0x34b0, %rsp           # imm = 0x34B0
popq %rbp
retq
leaq -0x468(%rbp), %rdi
callq 0x3020
leaq -0x450(%rbp), %rdi
callq 0x3000
leaq -0x428(%rbp), %rdi
callq 0x2180
leaq -0x218(%rbp), %rdi
callq 0x2060
movq -0x220(%rbp), %rdi
callq 0x21e0
nop