// File: sbme-tutorials[P]huffman-encoding/huffman.cpp
// Function: Huffman::bitPacking()
void Huffman::bitPacking()
{
    packed.clear();
    std::string str = "";
    for (auto e : encoded)
    {
        str.push_back(e);
        if (str.size() == 8)
        {
            int dec = std::bitset<8>(str).to_ulong();
            unsigned char temp = (unsigned char)dec;
            packed.push_back(temp);
            str = "";
        }
    }
    int numOfZeros = (8 - (encoded.length() % 8)); // to complete the last byte of the code
    if (numOfZeros != 8)
    {
        for (int i = 0; i < numOfZeros; ++i)
        {
            str.push_back('0');
        }
        if (str.size() == 8)
        {
            int dec = std::bitset<8>(str).to_ulong();
            unsigned char temp = (unsigned char)dec;
            packed.push_back(temp);
        }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x98(%rbp)
addq $0x110, %rdi            # imm = 0x110
callq 0x78e0
leaq -0x29(%rbp), %rdi
movq %rdi, -0x90(%rbp)
callq 0x34f0
movq -0x90(%rbp), %rdx
leaq 0x723d(%rip), %rsi      # 0xd0a1
leaq -0x28(%rbp), %rdi
callq 0x40b0
jmp 0x5e6f
leaq -0x29(%rbp), %rdi
callq 0x3310
movq -0x98(%rbp), %rax
addq $0xd0, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x3270
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
callq 0x3190
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x7900
testb $0x1, %al
jne 0x5eb9
jmp 0x5f8f
leaq -0x50(%rbp), %rdi
callq 0x7940
movb (%rax), %al
movb %al, -0x59(%rbp)
movsbl -0x59(%rbp), %esi
leaq -0x28(%rbp), %rdi
callq 0x3260
jmp 0x5ed6
leaq -0x28(%rbp), %rdi
callq 0x3210
cmpq $0x8, %rax
jne 0x5f7f
xorl %eax, %eax
movl %eax, %edx
leaq -0x68(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x7960
jmp 0x5efc
leaq -0x68(%rbp), %rdi
callq 0x79c0
movq %rax, -0xa0(%rbp)
jmp 0x5f0e
movq -0x98(%rbp), %rdi
movq -0xa0(%rbp), %rax
movl %eax, -0x60(%rbp)
movb -0x60(%rbp), %al
movb %al, -0x69(%rbp)
addq $0x110, %rdi            # imm = 0x110
leaq -0x69(%rbp), %rsi
callq 0x7850
jmp 0x5f37
leaq 0x7163(%rip), %rsi      # 0xd0a1
leaq -0x28(%rbp), %rdi
callq 0x33c0
jmp 0x5f49
jmp 0x5f7f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x29(%rbp), %rdi
callq 0x3310
jmp 0x6063
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x31d0
jmp 0x6063
jmp 0x5f81
leaq -0x50(%rbp), %rdi
callq 0x79e0
jmp 0x5ea3
movq -0x98(%rbp), %rdi
addq $0xd0, %rdi
callq 0x3430
movq %rax, %rcx
andq $0x7, %rcx
movl $0x8, %eax
subq %rcx, %rax
movl %eax, -0x70(%rbp)
cmpl $0x8, -0x70(%rbp)
je 0x6051
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x70(%rbp), %eax
jge 0x5fea
leaq -0x28(%rbp), %rdi
movl $0x30, %esi
callq 0x3260
jmp 0x5fdd
jmp 0x5fdf
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x5fc5
leaq -0x28(%rbp), %rdi
callq 0x3210
cmpq $0x8, %rax
jne 0x604f
xorl %eax, %eax
movl %eax, %edx
leaq -0x80(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x7960
jmp 0x600c
leaq -0x80(%rbp), %rdi
callq 0x79c0
movq %rax, -0xa8(%rbp)
jmp 0x601e
movq -0x98(%rbp), %rdi
movq -0xa8(%rbp), %rax
movl %eax, -0x78(%rbp)
movb -0x78(%rbp), %al
movb %al, -0x81(%rbp)
addq $0x110, %rdi            # imm = 0x110
leaq -0x81(%rbp), %rsi
callq 0x7850
jmp 0x604d
jmp 0x604f
jmp 0x6051
leaq -0x28(%rbp), %rdi
callq 0x31d0
addq $0xb0, %rsp
popq %rbp
retq
movq -0x38(%rbp), %rdi
callq 0x34e0
nopl (%rax)