// File: 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
subq $0x670, %rsp            # imm = 0x670
movq %rsi, -0x5b0(%rbp)
movq %rdx, -0x5c0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x5b8(%rbp)
leaq -0x228(%rbp), %rdi
callq 0x3350
movq -0x5b0(%rbp), %rdx
leaq 0x6fd6(%rip), %rsi      # 0xd090
leaq -0x268(%rbp), %rdi
callq 0x7450
jmp 0x60c8
leaq 0x6fcc(%rip), %rdx      # 0xd09b
leaq -0x248(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x7400
jmp 0x60e4
leaq -0x268(%rbp), %rdi
callq 0x31d0
leaq -0x228(%rbp), %rdi
leaq -0x248(%rbp), %rsi
movl $0x8, %edx
callq 0x33d0
jmp 0x610a
movq -0x5c0(%rbp), %rdx
movl $0x0, -0x278(%rbp)
leaq 0x6f82(%rip), %rsi      # 0xd0a4
leaq -0x2b8(%rbp), %rdi
callq 0x7450
jmp 0x6130
leaq 0x6f78(%rip), %rdx      # 0xd0af
leaq -0x298(%rbp), %rdi
leaq -0x2b8(%rbp), %rsi
callq 0x7400
jmp 0x614c
leaq -0x2b8(%rbp), %rdi
callq 0x31d0
movq -0x5b8(%rbp), %rsi
addq $0x30, %rsi
leaq -0x228(%rbp), %rdi
callq 0x3380
movq %rax, -0x5c8(%rbp)
jmp 0x6178
movq -0x5c8(%rbp), %rdi
movq -0x5b8(%rbp), %rsi
addq $0x20, %rsi
callq 0x31c0
movq %rax, -0x5d0(%rbp)
jmp 0x6198
movq -0x5d0(%rbp), %rdi
movq -0x5b8(%rbp), %rsi
addq $0x24, %rsi
callq 0x31c0
movq %rax, -0x5d8(%rbp)
jmp 0x61b8
movq -0x5d8(%rbp), %rdi
movq -0x5b8(%rbp), %rsi
addq $0x28, %rsi
callq 0x31c0
movq %rax, -0x5e0(%rbp)
jmp 0x61d8
movq -0x5e0(%rbp), %rdi
leaq -0x278(%rbp), %rsi
callq 0x31c0
movq %rax, -0x5e8(%rbp)
jmp 0x61f4
movq -0x5e8(%rbp), %rdi
movq -0x5b8(%rbp), %rsi
addq $0xf0, %rsi
callq 0x3340
jmp 0x6210
leaq -0x2e8(%rbp), %rdi
callq 0x7640
movl $0x0, -0x2ec(%rbp)
movl -0x2ec(%rbp), %eax
cmpl -0x278(%rbp), %eax
jge 0x637a
leaq -0x310(%rbp), %rdi
callq 0x33f0
leaq -0x228(%rbp), %rdi
leaq -0x314(%rbp), %rsi
callq 0x31c0
movq %rax, -0x5f0(%rbp)
jmp 0x6260
movq -0x5f0(%rbp), %rdi
leaq -0x310(%rbp), %rsi
callq 0x3380
jmp 0x6275
movb -0x314(%rbp), %al
movb %al, -0x5f9(%rbp)
leaq -0x2e8(%rbp), %rdi
leaq -0x310(%rbp), %rsi
callq 0x7660
movq %rax, -0x5f8(%rbp)
jmp 0x629d
movq -0x5f8(%rbp), %rax
movb -0x5f9(%rbp), %cl
movb %cl, (%rax)
leaq -0x310(%rbp), %rdi
callq 0x31d0
movl -0x2ec(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2ec(%rbp)
jmp 0x6226
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
jmp 0x68ec
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x31d0
jmp 0x68ec
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
jmp 0x68e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
leaq -0x2b8(%rbp), %rdi
callq 0x31d0
jmp 0x68e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
jmp 0x68d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
leaq -0x310(%rbp), %rdi
callq 0x31d0
jmp 0x68c8
movq -0x5b8(%rbp), %rdi
leaq 0x6d19(%rip), %rsi      # 0xd0a1
callq 0x33c0
jmp 0x638f
movq -0x5b8(%rbp), %rax
movq 0xf0(%rax), %xmm0
movaps 0x6cbb(%rip), %xmm1     # 0xd060
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x6cbf(%rip), %xmm1     # 0xd070
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x6c88(%rip), %xmm1     # 0xd050
divsd %xmm1, %xmm0
callq 0x3080
cvttsd2si %xmm0, %eax
movl %eax, -0x318(%rbp)
movq -0x228(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x228(%rbp,%rax), %rdi
callq 0x3510
movb %al, -0x5fa(%rbp)
jmp 0x63fb
movb -0x5fa(%rbp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x640c
jmp 0x64b1
leaq -0x228(%rbp), %rdi
leaq -0x319(%rbp), %rsi
callq 0x33e0
jmp 0x6421
movsbq -0x319(%rbp), %rsi
leaq -0x328(%rbp), %rdi
movq %rdi, -0x608(%rbp)
callq 0x7740
movq -0x608(%rbp), %rsi
leaq -0x348(%rbp), %rdi
callq 0x7790
jmp 0x6451
movq -0x5b8(%rbp), %rdi
leaq -0x348(%rbp), %rsi
callq 0x3390
jmp 0x6466
leaq -0x348(%rbp), %rdi
callq 0x31d0
jmp 0x63db
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
jmp 0x68c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
leaq -0x348(%rbp), %rdi
callq 0x31d0
jmp 0x68c8
leaq -0x228(%rbp), %rdi
callq 0x3050
jmp 0x64bf
leaq -0x369(%rbp), %rdi
movq %rdi, -0x610(%rbp)
callq 0x34f0
movq -0x610(%rbp), %rdx
leaq 0x6bc1(%rip), %rsi      # 0xd0a1
leaq -0x368(%rbp), %rdi
callq 0x40b0
jmp 0x64ee
leaq -0x369(%rbp), %rdi
callq 0x3310
movl $0x0, -0x370(%rbp)
movq -0x5b8(%rbp), %rcx
movslq -0x370(%rbp), %rax
cmpq 0xf0(%rcx), %rax
jae 0x6658
movq -0x5b8(%rbp), %rdi
movslq -0x370(%rbp), %rsi
callq 0x3550
movq %rax, -0x618(%rbp)
jmp 0x653b
movq -0x618(%rbp), %rax
movsbl (%rax), %esi
leaq -0x368(%rbp), %rdi
callq 0x31e0
jmp 0x6553
leaq -0x2e8(%rbp), %rdi
leaq -0x368(%rbp), %rsi
callq 0x77f0
movq %rax, -0x620(%rbp)
jmp 0x656f
movq -0x620(%rbp), %rax
movq %rax, -0x378(%rbp)
leaq -0x2e8(%rbp), %rdi
callq 0x7820
movq %rax, -0x380(%rbp)
leaq -0x378(%rbp), %rdi
leaq -0x380(%rbp), %rsi
callq 0x77c0
testb $0x1, %al
jne 0x65ac
jmp 0x6642
movq -0x5b8(%rbp), %rax
addq $0xf8, %rax
movq %rax, -0x630(%rbp)
leaq -0x2e8(%rbp), %rdi
leaq -0x368(%rbp), %rsi
callq 0x7660
movq %rax, -0x628(%rbp)
jmp 0x65dc
movq -0x628(%rbp), %rsi
movq -0x630(%rbp), %rdi
callq 0x7850
jmp 0x65f1
leaq 0x6aa9(%rip), %rsi      # 0xd0a1
leaq -0x368(%rbp), %rdi
callq 0x33c0
jmp 0x6606
jmp 0x6642
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
leaq -0x369(%rbp), %rdi
callq 0x3310
jmp 0x68c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
jmp 0x68bc
jmp 0x6644
movl -0x370(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x370(%rbp)
jmp 0x6504
leaq -0x580(%rbp), %rdi
callq 0x34a0
jmp 0x6666
leaq -0x580(%rbp), %rdi
leaq -0x298(%rbp), %rsi
movl $0x10, %edx
callq 0x3150
jmp 0x6680
movq -0x5b8(%rbp), %rsi
addq $0x30, %rsi
leaq -0x580(%rbp), %rdi
callq 0x3290
movq %rax, -0x638(%rbp)
jmp 0x66a0
movq -0x638(%rbp), %rdi
movq 0xc91a(%rip), %rsi      # 0x12fc8
callq 0x32f0
movq %rax, -0x640(%rbp)
jmp 0x66bc
movq -0x640(%rbp), %rdi
movq -0x5b8(%rbp), %rax
movl 0x20(%rax), %esi
callq 0x34c0
movq %rax, -0x648(%rbp)
jmp 0x66db
movq -0x648(%rbp), %rdi
leaq 0x69b9(%rip), %rsi      # 0xd0a2
callq 0x32b0
movq %rax, -0x650(%rbp)
jmp 0x66f7
movq -0x650(%rbp), %rdi
movq -0x5b8(%rbp), %rax
movl 0x24(%rax), %esi
callq 0x34c0
movq %rax, -0x658(%rbp)
jmp 0x6716
movq -0x658(%rbp), %rdi
movq 0xc8a4(%rip), %rsi      # 0x12fc8
callq 0x32f0
movq %rax, -0x660(%rbp)
jmp 0x6732
movq -0x660(%rbp), %rdi
movq -0x5b8(%rbp), %rax
movl 0x28(%rax), %esi
callq 0x34c0
jmp 0x674a
movq -0x5b8(%rbp), %rax
movl $0x0, -0x584(%rbp)
addq $0xf8, %rax
movq %rax, -0x590(%rbp)
movq -0x590(%rbp), %rdi
callq 0x6eb0
movq %rax, -0x598(%rbp)
movq -0x590(%rbp), %rdi
callq 0x6ee0
movq %rax, -0x5a0(%rbp)
leaq -0x598(%rbp), %rdi
leaq -0x5a0(%rbp), %rsi
callq 0x6f10
testb $0x1, %al
jne 0x67aa
jmp 0x685d
leaq -0x598(%rbp), %rdi
callq 0x6f50
movq -0x5b8(%rbp), %rcx
movb (%rax), %al
movb %al, -0x5a1(%rbp)
movl -0x584(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x584(%rbp)
cltd
idivl 0x20(%rcx)
cmpl $0x0, %edx
jne 0x6819
movq 0xc7e2(%rip), %rsi      # 0x12fc8
leaq -0x580(%rbp), %rdi
callq 0x32f0
jmp 0x67f4
jmp 0x6819
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x270(%rbp)
movl %eax, -0x274(%rbp)
leaq -0x580(%rbp), %rdi
callq 0x3420
jmp 0x68bc
movzbl -0x5a1(%rbp), %esi
leaq -0x580(%rbp), %rdi
callq 0x34c0
movq %rax, -0x668(%rbp)
jmp 0x6835
movq -0x668(%rbp), %rdi
leaq 0x685f(%rip), %rsi      # 0xd0a2
callq 0x32b0
jmp 0x684a
jmp 0x684c
leaq -0x598(%rbp), %rdi
callq 0x7050
jmp 0x678e
leaq -0x580(%rbp), %rdi
callq 0x3400
jmp 0x686b
leaq -0x580(%rbp), %rdi
callq 0x3420
leaq -0x368(%rbp), %rdi
callq 0x31d0
leaq -0x2e8(%rbp), %rdi
callq 0x78c0
leaq -0x298(%rbp), %rdi
callq 0x31d0
leaq -0x248(%rbp), %rdi
callq 0x31d0
leaq -0x228(%rbp), %rdi
callq 0x30b0
addq $0x670, %rsp            # imm = 0x670
popq %rbp
retq
leaq -0x368(%rbp), %rdi
callq 0x31d0
leaq -0x2e8(%rbp), %rdi
callq 0x78c0
leaq -0x298(%rbp), %rdi
callq 0x31d0
leaq -0x248(%rbp), %rdi
callq 0x31d0
leaq -0x228(%rbp), %rdi
callq 0x30b0
movq -0x270(%rbp), %rdi
callq 0x34e0
nopw %cs:(%rax,%rax)