// File: /workspace/github_cpp_makefile_star2_O1/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 0x7619(%rip), %rdi      # 0x9fd8
leaq 0x4919(%rip), %rsi      # 0x72df
movl $0x13, %edx
callq 0x2130
movb $0x0, 0x1(%r14)
leaq 0x8(%r14), %r12
movq %r12, %rdi
callq 0x2140
movl %eax, %r13d
shll $0x8, %r13d
movq %r12, %rdi
callq 0x2140
leal (%rax,%r13), %r15d
addl $-0x2, %r15d
leaq 0x1230(%rbp), %rax
movq %rax, 0x20(%rsp)
leaq 0x410(%rbp), %rax
movq %rax, 0x18(%rsp)
movq %rbp, 0x10(%rsp)
testl %r15d, %r15d
jle 0x2bac
movb $0x0, 0x1(%r14)
movq %r12, %rdi
callq 0x2140
movl %eax, %r13d
andb $0xf, %al
cmpb $0x4, %al
jb 0x2a83
andl $0xf, %r13d
movl $0x22, %edx
movq 0x7594(%rip), %rbx      # 0x9fd8
movq %rbx, %rdi
leaq 0x48a5(%rip), %rsi      # 0x72f3
callq 0x2130
movq %rbx, %rdi
movq %r13, %rsi
callq 0x20a0
movb $0xa, 0x6(%rsp)
movl $0x1, %edx
movq %rax, %rdi
leaq 0x6(%rsp), %rsi
callq 0x2130
movb $0x0, 0x2088(%rbp)
xorl %eax, %eax
jmp 0x2ba2
movl %r15d, 0x8(%rsp)
movzbl %al, %eax
imulq $0x388, %rax, %rax      # imm = 0x388
cmpb $0x10, %r13b
movq 0x20(%rsp), %rcx
cmovbq 0x18(%rsp), %rcx
leaq (%rcx,%rax), %r15
movb $0x1, 0x384(%rcx,%rax)
movb $0x0, (%rcx,%rax)
xorl %ebx, %ebx
xorl %r13d, %r13d
movb $0x0, 0x1(%r14)
movq %r12, %rdi
callq 0x2140
movzbl %al, %eax
addl %eax, %r13d
movb %r13b, 0x1(%r15,%rbx)
incq %rbx
cmpq $0x10, %rbx
jne 0x2ab6
cmpl $0xb1, %r13d
setb %al
jae 0x2b57
movl %eax, 0xc(%rsp)
testl %r13d, %r13d
je 0x2b0b
movl %r13d, %ebp
xorl %ebx, %ebx
movb $0x0, 0x1(%r14)
movq %r12, %rdi
callq 0x2140
movb %al, 0x11(%r15,%rbx)
incq %rbx
cmpq %rbx, %rbp
jne 0x2af1
xorl %ecx, %ecx
xorl %eax, %eax
movq 0x10(%rsp), %rbp
movq %rcx, %rsi
movzbl (%r15,%rcx), %edx
incq %rcx
cmpb 0x1(%r15,%rsi), %dl
jae 0x2b3d
movl %eax, 0xc4(%r15,%rdx,4)
incl %eax
incq %rdx
movzbl (%r15,%rcx), %esi
cmpq %rsi, %rdx
jb 0x2b26
addl %eax, %eax
cmpq $0x10, %rcx
jne 0x2b14
movl 0x8(%rsp), %r15d
subl %r13d, %r15d
addl $-0x11, %r15d
movl 0xc(%rsp), %eax
jmp 0x2ba2
movl $0x2b, %edx
movq 0x7475(%rip), %rbx      # 0x9fd8
movq %rbx, %rdi
leaq 0x47a9(%rip), %rsi      # 0x7316
callq 0x2130
movl %r13d, %esi
movq %rbx, %rdi
callq 0x20a0
movb $0xa, 0x7(%rsp)
movl $0x1, %edx
movq %rax, %rdi
leaq 0x7(%rsp), %rsi
callq 0x2130
movb $0x0, 0x2088(%rbp)
xorl %eax, %eax
movl 0x8(%rsp), %r15d
testb %al, %al
jne 0x2a15
jmp 0x2bcd
je 0x2bcd
movq 0x7423(%rip), %rdi      # 0x9fd8
leaq 0x4786(%rip), %rsi      # 0x7342
movl $0x14, %edx
callq 0x2130
movb $0x0, 0x2088(%rbp)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq