// File: /workspace/github_cpp_makefile_star2_O3/dannye[P]jed/src/decoder.cpp
// Function: readHuffmanTable(BitReader&, JPGImage*)
void readHuffmanTable(BitReader& bitReader, JPGImage* const image) {
    std::cout << "Reading DHT Marker\n";
    int length = bitReader.readWord();
    length -= 2;

    while (length > 0) {
        byte tableInfo = bitReader.readByte();
        byte tableID = tableInfo & 0x0F;
        bool acTable = tableInfo >> 4;

        if (tableID > 3) {
            std::cout << "Error - Invalid Huffman table ID: " << (uint)tableID << '\n';
            image->valid = false;
            return;
        }

        HuffmanTable& hTable = (acTable) ?
            (image->huffmanACTables[tableID]) :
            (image->huffmanDCTables[tableID]);
        hTable.set = true;

        hTable.offsets[0] = 0;
        uint allSymbols = 0;
        for (uint i = 1; i <= 16; ++i) {
            allSymbols += bitReader.readByte();
            hTable.offsets[i] = allSymbols;
        }
        if (allSymbols > 176) {
            std::cout << "Error - Too many symbols in Huffman table: " << allSymbols << '\n';
            image->valid = false;
            return;
        }

        for (uint i = 0; i < allSymbols; ++i) {
            hTable.symbols[i] = bitReader.readByte();
        }

        generateCodes(hTable);

        length -= 17 + allSymbols;
    }

    if (length != 0) {
        std::cout << "Error - DHT invalid\n";
        image->valid = false;
        return;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbp
movq %rdi, %r14
movq 0x7640(%rip), %rdi      # 0x9fd8
leaq 0x4940(%rip), %rsi      # 0x72df
movl $0x13, %edx
callq 0x2130
movb $0x0, 0x1(%r14)
leaq 0x8(%r14), %r15
movq %r15, %rdi
callq 0x2140
movl %eax, %r12d
shll $0x8, %r12d
movq %r15, %rdi
callq 0x2140
leal (%rax,%r12), %ebx
addl $-0x2, %ebx
testl %ebx, %ebx
jle 0x2ae2
leaq 0x1230(%rbp), %rax
movq %rax, 0x20(%rsp)
leaq 0x410(%rbp), %rax
movq %rax, 0x18(%rsp)
movq %rbp, 0x10(%rsp)
movb $0x0, 0x1(%r14)
movq %r15, %rdi
callq 0x2140
movl %eax, %r12d
andb $0xf, %al
cmpb $0x4, %al
jae 0x2aff
movl %ebx, 0xc(%rsp)
movzbl %al, %eax
imulq $0x388, %rax, %rax      # imm = 0x388
cmpb $0x10, %r12b
movq 0x20(%rsp), %rcx
cmovbq 0x18(%rsp), %rcx
leaq (%rcx,%rax), %rbx
movb $0x1, 0x384(%rcx,%rax)
movb $0x0, (%rcx,%rax)
xorl %r12d, %r12d
movl $0x1, %r13d
movb $0x0, 0x1(%r14)
movq %r15, %rdi
callq 0x2140
movzbl %al, %eax
addl %eax, %r12d
movb %r12b, (%rbx,%r13)
incq %r13
cmpq $0x11, %r13
jne 0x2a45
cmpl $0xb0, %r12d
ja 0x2b30
testl %r12d, %r12d
je 0x2a95
movl %r12d, %r13d
xorl %ebp, %ebp
movb $0x0, 0x1(%r14)
movq %r15, %rdi
callq 0x2140
movb %al, 0x11(%rbx,%rbp)
incq %rbp
cmpq %rbp, %r13
jne 0x2a7c
movb (%rbx), %sil
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x10(%rsp), %rbp
movb 0x1(%rbx,%rax), %dl
incq %rax
cmpb %dl, %sil
jae 0x2ac6
movzbl %sil, %esi
movl %ecx, 0xc4(%rbx,%rsi,4)
incl %ecx
incq %rsi
movzbl (%rbx,%rax), %edx
cmpq %rdx, %rsi
jb 0x2ab1
addl %ecx, %ecx
movl %edx, %esi
cmpq $0x10, %rax
jne 0x2aa1
movl 0xc(%rsp), %ebx
subl %r12d, %ebx
addl $-0x11, %ebx
testl %ebx, %ebx
jg 0x29f5
testl %ebx, %ebx
je 0x2b72
movq 0x74e7(%rip), %rdi      # 0x9fd8
leaq 0x484a(%rip), %rsi      # 0x7342
movl $0x14, %edx
jmp 0x2b66
andl $0xf, %r12d
movq 0x74ce(%rip), %r14      # 0x9fd8
leaq 0x47e2(%rip), %rsi      # 0x72f3
movl $0x22, %edx
movq %r14, %rdi
callq 0x2130
movq %r14, %rdi
movq %r12, %rsi
callq 0x20a0
leaq 0xa(%rsp), %rsi
jmp 0x2b5b
movq 0x74a1(%rip), %r14      # 0x9fd8
leaq 0x47d8(%rip), %rsi      # 0x7316
movl $0x2b, %edx
movq %r14, %rdi
callq 0x2130
movl %r12d, %esi
movq %r14, %rdi
callq 0x20a0
leaq 0xb(%rsp), %rsi
movb $0xa, (%rsi)
movl $0x1, %edx
movq %rax, %rdi
callq 0x2130
movb $0x0, 0x2088(%rbp)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq