// File: qhan1028[P]JPEG-Decoder/jpeg-decoder.cpp
// Function: process_DHT(std::basic_ifstream<char, std::char_traits<char>>&, std::map<std::pair<unsigned char, unsigned short>, unsigned char, std::less<std::pair<unsigned char, unsigned short>>, std::allocator<std::pair<std::pair<unsigned char, unsigned short> const, unsigned char>>>*)
bool process_DHT(ifstream& jpegfile, htable_t* htable) {
    unsigned int pointer = 0;
    char c, c1, c2;

    /* DHT length */
    jpegfile.read(&c1, 1); pointer++;
    jpegfile.read(&c2, 1); pointer++;

    unsigned int len = to_num(c1, c2);

    if (debug)
        cout << "[DHT] len = " << len << endl;

    while (pointer < len) {
        /* H table ID */
        jpegfile.read(&c, 1); pointer++;
        c = to_uint(c);
        unsigned int tc = c >> 4;
        unsigned int th = c & 0x0F;

        unsigned int hid = tc * 2 + th;

        /* counts */
        unsigned int counts[16];
        for (unsigned int i = 0; i < 16; i++) {
            jpegfile.read(&c, 1); pointer++;
            counts[i] = to_uint(c);
        }

        /* compute target symbols and get its corresponding codes */
        uint16_t symbol = 0;
        for (unsigned int i = 0; i < 16; i++) {
            for (unsigned int j = 0; j < counts[i]; j++) {
                jpegfile.read(&c, 1); pointer++; // code
                htable[hid].insert(
                    pair<hkey_t, uint8_t>(hkey_t(i + 1, symbol), to_uint(c))
                );
                symbol++;
            }
            symbol = symbol << 1;
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x16(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x17(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movb -0x16(%rbp), %al
movsbl %al, %edi
movsbl -0x17(%rbp), %esi
callq 0x61d0
movl %eax, -0x1c(%rbp)
testb $0x1, 0xc701(%rip)      # 0x1021a
je 0x3b48
movq 0xc4ae(%rip), %rdi      # 0xffd0
leaq 0x76e1(%rip), %rsi      # 0xb20a
callq 0x21b0
movq %rax, %rdi
movl -0x1c(%rbp), %esi
callq 0x2260
movq %rax, %rdi
movq 0xc47d(%rip), %rsi      # 0xffc0
callq 0x21f0
jmp 0x3b4a
movl -0x14(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x3ce2
movq -0x8(%rbp), %rdi
leaq -0x15(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movsbl -0x15(%rbp), %edi
callq 0x6210
movb %al, -0x15(%rbp)
movsbl -0x15(%rbp), %eax
sarl $0x4, %eax
movl %eax, -0x20(%rbp)
movsbl -0x15(%rbp), %eax
andl $0xf, %eax
movl %eax, -0x24(%rbp)
movl -0x20(%rbp), %eax
shll %eax
addl -0x24(%rbp), %eax
movl %eax, -0x28(%rbp)
movl $0x0, -0x74(%rbp)
cmpl $0x10, -0x74(%rbp)
jae 0x3be1
movq -0x8(%rbp), %rdi
leaq -0x15(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movsbl -0x15(%rbp), %edi
callq 0x6210
movl %eax, %ecx
movl -0x74(%rbp), %eax
movl %ecx, -0x70(%rbp,%rax,4)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x3ba3
movw $0x0, -0x76(%rbp)
movl $0x0, -0x7c(%rbp)
cmpl $0x10, -0x7c(%rbp)
jae 0x3cdd
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
movl -0x7c(%rbp), %ecx
cmpl -0x70(%rbp,%rcx,4), %eax
jae 0x3cc5
movq -0x8(%rbp), %rdi
leaq -0x15(%rbp), %rsi
movl $0x1, %edx
callq 0x2230
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movl -0x28(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
leaq -0x8a(%rbp), %rdi
leaq -0x90(%rbp), %rsi
leaq -0x76(%rbp), %rdx
callq 0x63e0
movsbl -0x15(%rbp), %edi
callq 0x6210
movl %eax, -0x94(%rbp)
leaq -0x86(%rbp), %rdi
leaq -0x8a(%rbp), %rsi
leaq -0x94(%rbp), %rdx
callq 0x6410
movq -0xb0(%rbp), %rdi
leaq -0x86(%rbp), %rsi
callq 0x63a0
movq %rax, -0xa8(%rbp)
movb %dl, -0xa0(%rbp)
movw -0x76(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x76(%rbp)
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x3bff
movzwl -0x76(%rbp), %eax
shll %eax
movw %ax, -0x76(%rbp)
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x3bee
jmp 0x3b4a
xorl %eax, %eax
andb $0x1, %al
addq $0xb0, %rsp
popq %rbp
retq
nop