// File: /workspace/github_cpp_makefile_star2_O2/sbme-tutorials[P]huffman-encoding/huffman.cpp
// Function: Huffman::decode(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void Huffman::decode(std::string inputFile, std::string outputFile) // calls outputDecoded()
{
    std::fstream inFile;
    std::string encodedFileName = "./encoded/" + inputFile + ".txt";
    inFile.open(encodedFileName, std::ios::in);
    int sizeOfTable = 0;
    // get important data from the file
    std::string directory = "./decoded/" + outputFile + ".pgm";

    inFile >> intensity >> width >> height >> maxIntensity >> sizeOfTable >> sizeOfEncoded;
    // fill the codeWordTable as reversedCodeTable symbol codeWordAsInt
    std::map<std::string, uint8_t> reversedCodeTable;
    for (int i = 0; i < sizeOfTable; i++)
    {
        std::string codeWord;
        int symbol;
        inFile >> symbol >> codeWord;
        reversedCodeTable[codeWord] = (uint8_t)symbol;
    }
    to_decode = "";
    int numOfBitSets = ceil(sizeOfEncoded / 8.0);
    while (!inFile.eof())
    {
        char x;
        inFile.get(x);
        std::bitset<8> chr = x;
        to_decode += chr.to_string(); //0100101
    }

    inFile.close();

    std::string str = "";
    for (int i = 0; i < sizeOfEncoded; i++)
    {
        str += to_decode[i];
        if (reversedCodeTable.find(str) != reversedCodeTable.end())
        {
            decoded.push_back(reversedCodeTable[str]);
            str = "";
        }
    }
    std::ofstream myFile;
    myFile.open(directory);
    myFile << intensity << std::endl
           << width << " " << height << std::endl
           << maxIntensity;
    int i = 0;
    for (auto d : decoded)
    {
        if (i++ % width == 0)
            myFile << std::endl;
        myFile << (int)d << " ";
    }
    myFile.close();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4d8, %rsp            # imm = 0x4D8
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq -0x500(%rbp), %rdi
callq 0x2230
leaq 0x1b3c(%rip), %rsi      # 0x5070
leaq -0x2f0(%rbp), %rdi
movq %r15, %rdx
callq 0x3a94
leaq 0x1b31(%rip), %rdx      # 0x507b
leaq -0xf0(%rbp), %rdi
leaq -0x2f0(%rbp), %rsi
callq 0x3a6b
leaq -0x2f0(%rbp), %rdi
callq 0x20e0
leaq -0x500(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
pushq $0x8
popq %rdx
callq 0x2290
andl $0x0, -0x30(%rbp)
leaq 0x1afa(%rip), %rsi      # 0x5084
leaq -0x2f0(%rbp), %rdi
movq %r14, %rdx
callq 0x3a94
leaq 0x1aef(%rip), %rdx      # 0x508f
leaq -0xd0(%rbp), %rdi
leaq -0x2f0(%rbp), %rsi
callq 0x3a6b
leaq -0x2f0(%rbp), %rdi
callq 0x20e0
leaq 0x30(%rbx), %rsi
leaq -0x500(%rbp), %rdi
movq %rsi, -0x50(%rbp)
callq 0x2260
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
movq %rsi, -0x38(%rbp)
callq 0x2140
leaq 0x24(%rbx), %rsi
movq %rax, %rdi
movq %rsi, -0x48(%rbp)
callq 0x2140
leaq 0x28(%rbx), %rsi
movq %rax, %rdi
movq %rsi, -0x40(%rbp)
callq 0x2140
leaq -0x30(%rbp), %rsi
movq %rax, %rdi
callq 0x2140
leaq 0xf0(%rbx), %rsi
movq %rax, %rdi
movq %rsi, -0x60(%rbp)
callq 0x2250
movq %rbx, -0x58(%rbp)
leaq -0xa8(%rbp), %rax
andl $0x0, (%rax)
andq $0x0, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
andq $0x0, 0x20(%rax)
xorl %ebx, %ebx
leaq -0x80(%rbp), %r12
leaq -0x2f0(%rbp), %r13
leaq -0xb0(%rbp), %r15
cmpl -0x30(%rbp), %ebx
jge 0x36b0
leaq -0x2e0(%rbp), %rax
movq %rax, -0x2f0(%rbp)
andq $0x0, -0x2e8(%rbp)
movb $0x0, -0x2e0(%rbp)
leaq -0x500(%rbp), %rdi
movq %r12, %rsi
callq 0x2140
movq %rax, %rdi
movq %r13, %rsi
callq 0x2260
movl -0x80(%rbp), %r14d
movq %r15, %rdi
movq %r13, %rsi
callq 0x3b20
movb %r14b, (%rax)
movq %r13, %rdi
callq 0x20e0
incl %ebx
jmp 0x3656
leaq 0x19ca(%rip), %rsi      # 0x5081
movq -0x58(%rbp), %rbx
movq %rbx, %rdi
callq 0x23a0
leaq -0x500(%rbp), %r14
leaq -0x29(%rbp), %r15
leaq -0x2f0(%rbp), %r12
leaq -0x80(%rbp), %r13
movq -0x500(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x2, -0x4e0(%rbp,%rax)
jne 0x3721
movq %r14, %rdi
movq %r15, %rsi
callq 0x22b0
movzbl -0x29(%rbp), %eax
movq %rax, -0x80(%rbp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x4aa8
movq %rbx, %rdi
movq %r12, %rsi
callq 0x23b0
movq %r12, %rdi
callq 0x20e0
jmp 0x36d9
leaq -0x500(%rbp), %rdi
callq 0x2040
leaq 0x194d(%rip), %rsi      # 0x5081
leaq -0x80(%rbp), %rdi
leaq -0x2f0(%rbp), %rdx
callq 0x2946
leaq 0xf8(%rbx), %r12
xorl %r15d, %r15d
leaq -0x80(%rbp), %r13
leaq -0xb0(%rbp), %r14
movq -0x60(%rbp), %rax
cmpq %r15, (%rax)
jbe 0x37b3
movq (%rbx), %rax
movsbl (%rax,%r15), %esi
movq %r13, %rdi
callq 0x21b0
movq %r14, %rdi
movq %r13, %rsi
callq 0x4b38
leaq -0xa8(%rbp), %rcx
cmpq %rcx, %rax
je 0x37ae
movq %r14, %rdi
movq %r13, %rsi
callq 0x3b20
movq %r12, %rdi
movq %rax, %rsi
callq 0x3b8a
movq %r13, %rdi
leaq 0x18d8(%rip), %rsi      # 0x5081
callq 0x23a0
incq %r15
jmp 0x3759
leaq -0x2f0(%rbp), %rdi
callq 0x2340
leaq -0x2f0(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
pushq $0x10
popq %rdx
callq 0x20f0
leaq -0x2f0(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x2100
movq -0x38(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x2350
leaq 0x1880(%rip), %rsi      # 0x5082
movq %rax, %rdi
callq 0x21e0
movq -0x48(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x2350
movq %rax, %rdi
callq 0x2100
movq -0x40(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x2350
movq 0xf8(%rbx), %r13
movq 0x100(%rbx), %r15
xorl %ebx, %ebx
leaq -0x2f0(%rbp), %r14
leaq (%rbx,%r13), %rax
cmpq %r15, %rax
je 0x3886
movzbl (%rax), %r12d
movl %ebx, %eax
cltd
movq -0x38(%rbp), %rcx
idivl (%rcx)
testl %edx, %edx
jne 0x3867
movq %r14, %rdi
callq 0x2100
movq %r14, %rdi
movl %r12d, %esi
callq 0x2350
movq %rax, %rdi
leaq 0x1806(%rip), %rsi      # 0x5082
callq 0x21e0
incq %rbx
jmp 0x3845
leaq -0x2f0(%rbp), %rdi
callq 0x22c0
leaq -0x2f0(%rbp), %rdi
callq 0x22e0
leaq -0x80(%rbp), %rdi
callq 0x20e0
leaq -0xb0(%rbp), %rdi
callq 0x3c2a
leaq -0xd0(%rbp), %rdi
callq 0x20e0
leaq -0xf0(%rbp), %rdi
callq 0x20e0
leaq -0x500(%rbp), %rdi
callq 0x2080
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3929
jmp 0x393a
jmp 0x3924
jmp 0x3924
jmp 0x3924
movq %rax, %rbx
leaq -0x2f0(%rbp), %rdi
jmp 0x3965
jmp 0x3901
movq %rax, %rbx
jmp 0x396a
movq %rax, %rbx
leaq -0x2f0(%rbp), %rdi
jmp 0x3971
movq %rax, %rbx
jmp 0x3976
movq %rax, %rbx
jmp 0x395e
jmp 0x3929
jmp 0x393a
jmp 0x3943
jmp 0x3924
movq %rax, %rbx
jmp 0x3952
movq %rax, %rbx
leaq -0x2f0(%rbp), %rdi
callq 0x22e0
jmp 0x393d
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
jmp 0x394d
movq %rax, %rbx
leaq -0x2f0(%rbp), %rdi
callq 0x20e0
leaq -0xb0(%rbp), %rdi
callq 0x3c2a
leaq -0xd0(%rbp), %rdi
callq 0x20e0
leaq -0xf0(%rbp), %rdi
callq 0x20e0
leaq -0x500(%rbp), %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x2370