// File: /workspace/github_cpp_makefile_star2_O3/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 -0x4f8(%rbp), %rdi
callq 0x2240
leaq 0x25e4(%rip), %rsi      # 0x6070
leaq -0x2e8(%rbp), %rdi
movq %r15, %rdx
callq 0x42a6
leaq 0x25d9(%rip), %rsi      # 0x607b
leaq -0x2e8(%rbp), %rdi
callq 0x2390
leaq -0x60(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x3ad2
movq %rdx, -0x70(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x60(%rbp)
jmp 0x3ad9
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
movq %rdx, -0x68(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq -0x2d8(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3b0f
movq -0x2d8(%rbp), %rsi
incq %rsi
callq 0x2210
leaq -0x4f8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movl $0x8, %edx
callq 0x2290
movl $0x0, -0x30(%rbp)
leaq 0x2552(%rip), %rsi      # 0x6084
leaq -0x2e8(%rbp), %rdi
movq %r14, %rdx
callq 0x42a6
leaq 0x2547(%rip), %rsi      # 0x608f
leaq -0x2e8(%rbp), %rdi
callq 0x2390
leaq -0x40(%rbp), %rsi
movq %rsi, -0x10(%rsi)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x3b78
movq %rdx, -0x50(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x40(%rbp)
jmp 0x3b7e
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, -0x48(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq -0x2e8(%rbp), %rdi
leaq -0x2d8(%rbp), %rax
cmpq %rax, %rdi
je 0x3bb7
movq -0x2d8(%rbp), %rsi
incq %rsi
callq 0x2210
leaq 0x30(%rbx), %rsi
leaq -0x4f8(%rbp), %rdi
callq 0x2270
leaq 0x20(%rbx), %rsi
movq %rax, %rdi
movq %rsi, -0x98(%rbp)
callq 0x2140
leaq 0x24(%rbx), %rsi
movq %rax, %rdi
movq %rsi, -0xb0(%rbp)
callq 0x2140
leaq 0x28(%rbx), %rsi
movq %rax, %rdi
movq %rsi, -0xa8(%rbp)
callq 0x2140
leaq -0x30(%rbp), %rsi
movq %rax, %rdi
callq 0x2140
leaq 0xf0(%rbx), %rsi
movq %rax, %rdi
movq %rsi, -0xa0(%rbp)
callq 0x2260
leaq -0xe0(%rbp), %rax
movl $0x0, (%rax)
xorl %r14d, %r14d
movq %r14, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %r14, 0x20(%rax)
cmpl $0x0, -0x30(%rbp)
jle 0x3cd8
leaq -0x2e8(%rbp), %r12
leaq -0xe8(%rbp), %r13
leaq -0x2d8(%rbp), %rax
movq %rax, -0x2e8(%rbp)
movq $0x0, -0x2e0(%rbp)
movb $0x0, -0x2d8(%rbp)
leaq -0x4f8(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x2140
movq %rax, %rdi
movq %r12, %rsi
callq 0x2270
movl -0x90(%rbp), %r15d
movq %r13, %rdi
movq %r12, %rsi
callq 0x4338
movb %r15b, (%rax)
movq -0x2e8(%rbp), %rdi
leaq -0x2d8(%rbp), %rax
cmpq %rax, %rdi
je 0x3ccf
movq -0x2d8(%rbp), %rsi
incq %rsi
callq 0x2210
incl %r14d
cmpl -0x30(%rbp), %r14d
jl 0x3c5a
movq 0x8(%rbx), %rdx
leaq 0x239e(%rip), %rcx      # 0x6081
movq %rbx, %rdi
xorl %esi, %esi
xorl %r8d, %r8d
callq 0x2300
movq -0x4f8(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x2, -0x4d8(%rbp,%rax)
jne 0x3d90
leaq -0x4f8(%rbp), %r14
leaq -0x29(%rbp), %r15
leaq -0x2e8(%rbp), %r12
leaq -0x90(%rbp), %r13
movq %r14, %rdi
movq %r15, %rsi
callq 0x22a0
movzbl -0x29(%rbp), %eax
movq %rax, -0x90(%rbp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x4f78
movq -0x2e8(%rbp), %rsi
movq -0x2e0(%rbp), %rdx
movq %rbx, %rdi
callq 0x20b0
movq -0x2e8(%rbp), %rdi
leaq -0x2d8(%rbp), %rax
cmpq %rax, %rdi
je 0x3d7b
movq -0x2d8(%rbp), %rsi
incq %rsi
callq 0x2210
movq -0x4f8(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x2, -0x4d8(%rbp,%rax)
je 0x3d22
leaq -0x4f8(%rbp), %rdi
callq 0x2050
leaq -0x80(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x22d6(%rip), %rdx      # 0x6081
leaq -0x90(%rbp), %rdi
movq %rdx, %rsi
callq 0x2c6c
movq -0xa0(%rbp), %rax
cmpq $0x0, (%rax)
je 0x3e79
leaq 0xf8(%rbx), %rax
movq %rax, -0xb8(%rbp)
xorl %r13d, %r13d
leaq -0x90(%rbp), %r12
leaq -0xe8(%rbp), %r14
leaq 0x2290(%rip), %r15      # 0x6081
movq (%rbx), %rax
movsbl (%rax,%r13), %esi
movq %r12, %rdi
callq 0x21d0
movq %r14, %rdi
movq %r12, %rsi
callq 0x502e
leaq -0xe0(%rbp), %rcx
cmpq %rcx, %rax
je 0x3e66
movq %r14, %rdi
movq %r12, %rsi
callq 0x4338
movq 0x100(%rbx), %rsi
cmpq 0x108(%rbx), %rsi
je 0x3e40
movb (%rax), %al
movb %al, (%rsi)
incq 0x100(%rbx)
jmp 0x3e4f
movq -0xb8(%rbp), %rdi
movq %rax, %rdx
callq 0x507e
movq -0x88(%rbp), %rdx
movq %r12, %rdi
xorl %esi, %esi
movq %r15, %rcx
xorl %r8d, %r8d
callq 0x2300
incq %r13
movq -0xa0(%rbp), %rax
cmpq %r13, (%rax)
ja 0x3df1
leaq -0x2e8(%rbp), %rdi
callq 0x2340
leaq -0x2e8(%rbp), %rdi
leaq -0x50(%rbp), %rsi
movl $0x10, %edx
callq 0x2100
movq 0x30(%rbx), %rsi
movq 0x38(%rbx), %rdx
leaq -0x2e8(%rbp), %rdi
callq 0x2230
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21f0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2170
movq -0x98(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x2350
movq %rax, %r14
leaq 0x218f(%rip), %rsi      # 0x6082
movl $0x1, %edx
movq %rax, %rdi
callq 0x2230
movq -0xb0(%rbp), %rax
movl (%rax), %esi
movq %r14, %rdi
callq 0x2350
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21f0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2170
movq -0xa8(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x2350
movq 0xf8(%rbx), %r12
movq 0x100(%rbx), %r13
cmpq %r13, %r12
je 0x3fe5
xorl %ebx, %ebx
leaq 0x2116(%rip), %r14      # 0x6082
movzbl (%r12,%rbx), %r15d
movl %ebx, %eax
cltd
movq -0x98(%rbp), %rcx
idivl (%rcx)
testl %edx, %edx
jne 0x3fb7
movq -0x2e8(%rbp), %rax
movq -0x18(%rax), %rdi
leaq -0x2e8(%rbp), %rax
addq %rax, %rdi
movl $0xa, %esi
callq 0x21f0
movsbl %al, %esi
leaq -0x2e8(%rbp), %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2170
leaq -0x2e8(%rbp), %rdi
movl %r15d, %esi
callq 0x2350
movl $0x1, %edx
movq %rax, %rdi
movq %r14, %rsi
callq 0x2230
leaq (%r12,%rbx), %rax
incq %rax
incq %rbx
cmpq %r13, %rax
jne 0x3f6c
leaq -0x2e8(%rbp), %rdi
callq 0x22b0
movq 0x4fd8(%rip), %rax      # 0x8fd0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x2e0(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x2e8(%rbp,%rcx)
callq 0x21c0
leaq -0x1f0(%rbp), %rdi
callq 0x20c0
movq -0x90(%rbp), %rdi
leaq -0x80(%rbp), %rax
cmpq %rax, %rdi
leaq -0x60(%rbp), %rbx
leaq -0x40(%rbp), %r14
je 0x404b
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x2210
leaq -0xe8(%rbp), %rdi
callq 0x4406
movq -0x50(%rbp), %rdi
cmpq %r14, %rdi
je 0x406c
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2210
movq -0x70(%rbp), %rdi
cmpq %rbx, %rdi
je 0x4081
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2210
leaq -0x4f8(%rbp), %rdi
callq 0x2080
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4158
jmp 0x4124
jmp 0x411b
jmp 0x411b
jmp 0x411b
movq %rax, %rbx
movq -0x2e8(%rbp), %rdi
leaq -0x2d8(%rbp), %rax
cmpq %rax, %rdi
je 0x41ac
movq -0x2d8(%rbp), %rsi
jmp 0x41a4
jmp 0x40d4
movq %rax, %rbx
jmp 0x41ac
movq %rax, %rbx
leaq -0x2d8(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x41c1
movq -0x2d8(%rbp), %rsi
jmp 0x41b9
movq %rax, %rbx
jmp 0x41c1
movq %rax, %rbx
leaq -0x60(%rbp), %r15
leaq -0x40(%rbp), %r14
jmp 0x4197
jmp 0x4158
jmp 0x411b
movq %rax, %rbx
jmp 0x414e
jmp 0x4129
jmp 0x4124
movq %rax, %rbx
jmp 0x4167
movq %rax, %rbx
movq -0x2e8(%rbp), %rdi
leaq -0x2d8(%rbp), %rax
cmpq %rax, %rdi
je 0x414e
movq -0x2d8(%rbp), %rsi
incq %rsi
callq 0x2210
leaq -0x60(%rbp), %r15
leaq -0x40(%rbp), %r14
jmp 0x418b
movq %rax, %rbx
leaq -0x2e8(%rbp), %rdi
callq 0x22d0
leaq -0x60(%rbp), %r15
leaq -0x40(%rbp), %r14
leaq -0x80(%rbp), %rax
movq -0x90(%rbp), %rdi
cmpq %rax, %rdi
je 0x418b
movq -0x80(%rbp), %rsi
incq %rsi
callq 0x2210
leaq -0xe8(%rbp), %rdi
callq 0x4406
movq -0x50(%rbp), %rdi
cmpq %r14, %rdi
je 0x41ac
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2210
movq -0x70(%rbp), %rdi
cmpq %r15, %rdi
je 0x41c1
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2210
leaq -0x4f8(%rbp), %rdi
callq 0x2080
movq %rbx, %rdi
callq 0x2360
nop