// File: rapirent[P]JPEG/decoder.c
// Function: read_ht(_IO_FILE*)
void read_ht(FILE* fp)
{
    byte huffman_length[16];
    int len = read_word_to_bigendian(fp);
    len = len - 2;
    int ht_node_num;
    byte ht_length;
    byte class_id;
    byte ht_value;
    while (len > 0) {
        fread(&class_id,1,1,fp);
        len--;
        ht_node_num = 0;
        for(int i = 0; i<16; i++) {
            fread(&(ht_length),1,1,fp);
            huffman_length[i] = ht_length;
            len--;
            ht_node_num += huffman_length[i];
        }

        huffman_tables[huffman_table_index(class_id)].ht_node_num = ht_node_num;
        huffman_leaf* ht_leaf = (huffman_leaf*)malloc((ht_node_num)*sizeof(
                                    huffman_leaf));
        word codeword = 0;
        for(int height=0, leaf_index = 0; height<16; height++) {
            for(int j = 0; j<huffman_length[height]; j++) {

                fread(&(ht_value),1,1,fp);
                len--;
                ht_leaf[leaf_index].value = ht_value;
                ht_leaf[leaf_index].codeword = (codeword++);
                ht_leaf[leaf_index].codeword_len = height+1;
                leaf_index++;
            }
            codeword = codeword<<1;
        }
        huffman_tables[huffman_table_index(class_id)].start = ht_leaf;
    }
}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq 0x50(%rsp), %rdi
callq 0x5470
movzwl %ax, %eax
movl %eax, 0x3c(%rsp)
movl 0x3c(%rsp), %eax
subl $0x2, %eax
movl %eax, 0x3c(%rsp)
cmpl $0x0, 0x3c(%rsp)
jle 0x5b77
movq 0x50(%rsp), %rcx
leaq 0x36(%rsp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x5290
movl 0x3c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x3c(%rsp)
movl $0x0, 0x38(%rsp)
movl $0x0, 0x30(%rsp)
cmpl $0x10, 0x30(%rsp)
jge 0x5a17
movq 0x50(%rsp), %rcx
leaq 0x37(%rsp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x5290
movb 0x37(%rsp), %cl
movslq 0x30(%rsp), %rax
movb %cl, 0x40(%rsp,%rax)
movl 0x3c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x3c(%rsp)
movslq 0x30(%rsp), %rax
movzbl 0x40(%rsp,%rax), %eax
addl 0x38(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x59c2
movl 0x38(%rsp), %eax
movl %eax, 0x14(%rsp)
movzbl 0x36(%rsp), %edi
callq 0x5550
movl 0x14(%rsp), %ecx
movslq %eax, %rdx
leaq 0x92c9(%rip), %rax      # 0xed00
shlq $0x4, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movslq 0x38(%rsp), %rax
imulq $0xc, %rax, %rdi
callq 0x5250
movq %rax, 0x28(%rsp)
movw $0x0, 0x26(%rsp)
movl $0x0, 0x20(%rsp)
movl $0x0, 0x1c(%rsp)
cmpl $0x10, 0x20(%rsp)
jge 0x5b45
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %eax
movslq 0x20(%rsp), %rcx
movzbl 0x40(%rsp,%rcx), %ecx
cmpl %ecx, %eax
jge 0x5b29
movq 0x50(%rsp), %rcx
leaq 0x35(%rsp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x5290
movl 0x3c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x3c(%rsp)
movb 0x35(%rsp), %cl
movq 0x28(%rsp), %rax
movslq 0x1c(%rsp), %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movb %cl, 0x8(%rax)
movw 0x26(%rsp), %cx
movw %cx, %ax
addw $0x1, %ax
movw %ax, 0x26(%rsp)
movq 0x28(%rsp), %rax
movslq 0x1c(%rsp), %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movw %cx, (%rax)
movl 0x20(%rsp), %ecx
addl $0x1, %ecx
movq 0x28(%rsp), %rax
movslq 0x1c(%rsp), %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x4(%rax)
movl 0x1c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1c(%rsp)
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x5a7e
movzwl 0x26(%rsp), %eax
shll %eax
movw %ax, 0x26(%rsp)
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x5a6b
movq 0x28(%rsp), %rax
movq %rax, 0x8(%rsp)
movzbl 0x36(%rsp), %edi
callq 0x5550
movq 0x8(%rsp), %rcx
movslq %eax, %rdx
leaq 0x9198(%rip), %rax      # 0xed00
shlq $0x4, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
jmp 0x5985
addq $0x58, %rsp
retq
nopl (%rax)