// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/cram/cram_codecs.c
// Function: cram_huffman_decode_init
cram_codec *cram_huffman_decode_init(cram_block_compression_hdr *hdr,
                                     char *data, int size,
                                     enum cram_encoding codec,
                                     enum cram_external_type option,
                                     int version, varint_vec *vv) {
    int32_t ncodes = 0, i, j;
    char *cp = data, *data_end = &data[size];
    cram_codec *h;
    cram_huffman_code *codes = NULL;
    int32_t val, last_len, max_len = 0;
    uint32_t max_val; // needs one more bit than val
    const int max_code_bits = sizeof(val) * 8 - 1;
    int err = 0;

    if (option == E_BYTE_ARRAY_BLOCK) {
        hts_log_error("BYTE_ARRAYs not supported by this codec");
        return NULL;
    }

    ncodes = vv->varint_get32(&cp, data_end, &err);
    if (ncodes < 0) {
        hts_log_error("Invalid number of symbols in huffman stream");
        return NULL;
    }
    if (ncodes >= SIZE_MAX / sizeof(*codes)) {
        errno = ENOMEM;
        return NULL;
    }
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
    if (ncodes > FUZZ_ALLOC_LIMIT / sizeof(*codes)) {
        errno = ENOMEM;
        return NULL;
    }
#endif
    h = calloc(1, sizeof(*h));
    if (!h)
        return NULL;

    h->codec  = E_HUFFMAN;
    h->free   = cram_huffman_decode_free;

    h->u.huffman.ncodes = ncodes;
    h->u.huffman.option = option;
    if (ncodes) {
        codes = h->u.huffman.codes = malloc(ncodes * sizeof(*codes));
        if (!codes) {
            free(h);
            return NULL;
        }
    } else {
        codes = h->u.huffman.codes = NULL;
    }

    /* Read symbols and bit-lengths */
    if (option == E_LONG) {
        for (i = 0; i < ncodes; i++)
            codes[i].symbol = vv->varint_get64(&cp, data_end, &err);
    } else if (option == E_INT || option == E_BYTE) {
        for (i = 0; i < ncodes; i++)
            codes[i].symbol = vv->varint_get32(&cp, data_end, &err);
    } else {
        goto malformed;
    }

    if (err)
        goto malformed;

    i = vv->varint_get32(&cp, data_end, &err);
    if (i != ncodes)
        goto malformed;

    if (ncodes == 0) {
        /* NULL huffman stream.  Ensure it returns an error if
           anything tries to use it. */
        h->decode = cram_huffman_decode_null;
        return h;
    }

    for (i = 0; i < ncodes; i++) {
        codes[i].len = vv->varint_get32(&cp, data_end, &err);
        if (err)
            break;
        if (codes[i].len < 0) {
            hts_log_error("Huffman code length (%d) is negative", codes[i].len);
            goto malformed;
        }
        if (max_len < codes[i].len)
            max_len = codes[i].len;
    }
    if (err || cp - data != size || max_len >= ncodes)
        goto malformed;

    /* 31 is max. bits available in val */
    if (max_len > max_code_bits) {
        hts_log_error("Huffman code length (%d) is greater "
                      "than maximum supported (%d)", max_len, max_code_bits);
        goto malformed;
    }

    /* Sort by bit length and then by symbol value */
    qsort(codes, ncodes, sizeof(*codes), code_sort);

    /* Assign canonical codes */
    val = -1, last_len = 0, max_val = 0;
    for (i = 0; i < ncodes; i++) {
        val++;
        if (val > max_val)
            goto malformed;

        if (codes[i].len > last_len) {
            val <<= (codes[i].len - last_len);
            last_len = codes[i].len;
            max_val = (1U << codes[i].len) - 1;
        }
        codes[i].code = val;
    }

    /*
     * Compute the next starting point, offset by the i'th value.
     * For example if codes 10, 11, 12, 13 are 30, 31, 32, 33 then
     * codes[10..13].p = 30 - 10.
     */
    last_len = 0;
    for (i = j = 0; i < ncodes; i++) {
        if (codes[i].len > last_len) {
            j = codes[i].code - i;
            last_len = codes[i].len;
        }
        codes[i].p = j;
    }

    // puts("==HUFF LEN==");
    // for (i = 0; i <= last_len+1; i++) {
    //     printf("len %d=%d prefix %d\n", i, h->u.huffman.lengths[i], h->u.huffman.prefix[i]);
    // }
    // puts("===HUFFMAN CODES===");
    // for (i = 0; i < ncodes; i++) {
    //     int j;
    //     printf("%d: %d %d %d ", i, codes[i].symbol, codes[i].len, codes[i].code);
    //     j = codes[i].len;
    //     while (j) {
    //         putchar(codes[i].code & (1 << --j) ? '1' : '0');
    //     }
    //     printf(" %d\n", codes[i].code);
    // }

    if (option == E_BYTE || option == E_BYTE_ARRAY) {
        if (h->u.huffman.codes[0].len == 0)
            h->decode = cram_huffman_decode_char0;
        else
            h->decode = cram_huffman_decode_char;
    } else if (option == E_LONG || option == E_SLONG) {
        if (h->u.huffman.codes[0].len == 0)
            h->decode = cram_huffman_decode_long0;
        else
            h->decode = cram_huffman_decode_long;
    } else if (option == E_INT || option == E_SINT || option == E_BYTE) {
        if (h->u.huffman.codes[0].len == 0)
            h->decode = cram_huffman_decode_int0;
        else
            h->decode = cram_huffman_decode_int;
    } else {
        return NULL;
    }
    h->describe = cram_huffman_describe;

    return (cram_codec *)h;

 malformed:
    hts_log_error("Malformed huffman header stream");
    free(codes);
    free(h);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, 0x10(%rsp)
movl $0x0, 0x4(%rsp)
cmpl $0x5, %r8d
jne 0x90989
leaq 0x1226d(%rip), %rsi     # 0xa2bea
leaq 0x12062(%rip), %rdx     # 0xa29e6
jmp 0x90a9b
movl %r8d, %r14d
movq %rsi, %r13
movq 0x80(%rsp), %rax
movslq %edx, %rbx
leaq (%rsi,%rbx), %rbp
leaq 0x10(%rsp), %rdi
leaq 0x4(%rsp), %rdx
movq %rbp, %rsi
callq *0x18(%rax)
movq %rax, %r15
testl %r15d, %r15d
js 0x90a8d
movl $0x1, %edi
movl $0x880, %esi            # imm = 0x880
callq 0x4300
testq %rax, %rax
je 0x90ad8
movq %rax, %r12
movq %rbx, 0x40(%rsp)
movl %r15d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl $0x3, (%rax)
leaq -0x73a(%rip), %rax      # 0x902b6
movq %rax, 0x20(%r12)
movl %r15d, 0x60(%r12)
movl %r14d, 0x70(%r12)
testl %r15d, %r15d
movq %rcx, 0x38(%rsp)
je 0x90abc
leaq (,%rcx,8), %rax
leaq (%rax,%rax,2), %rdi
callq 0x4680
movq %rax, 0x68(%r12)
testq %rax, %rax
je 0x90ad0
movq %rax, %rbx
cmpl $0x2, %r14d
jne 0x90add
movq %r13, 0x30(%rsp)
movq %r14, 0x20(%rsp)
movq %r12, 0x18(%rsp)
movq %r15, 0x28(%rsp)
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
leaq 0x10(%rsp), %r14
movq %rbx, %r12
leaq 0x4(%rsp), %rbx
movq %r12, 0x8(%rsp)
movq 0x80(%rsp), %r13
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq *0x28(%r13)
movq %rax, (%r12)
addq $0x18, %r12
decq %r15
jne 0x90a6e
jmp 0x90b3b
leaq 0x12156(%rip), %rsi     # 0xa2bea
leaq 0x12168(%rip), %rdx     # 0xa2c03
xorl %r14d, %r14d
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leal -0x1(%r14), %eax
xorl %ebx, %ebx
cmpl $0x3, %eax
jb 0x90b54
jmp 0x90c66
movq %r12, %rdi
callq 0x45c0
xorl %r14d, %r14d
jmp 0x90aaa
movl %r14d, %eax
andl $-0x3, %eax
cmpl $0x1, %eax
jne 0x90c66
movq %r13, 0x30(%rsp)
movq %r14, 0x20(%rsp)
movq %r12, 0x18(%rsp)
movq %r15, 0x28(%rsp)
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
leaq 0x10(%rsp), %r14
movq %rbx, %r12
leaq 0x4(%rsp), %rbx
movq %r12, 0x8(%rsp)
movq 0x80(%rsp), %r13
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq *0x18(%r13)
movq %rax, (%r12)
addq $0x18, %r12
decq %r15
jne 0x90b21
movq 0x18(%rsp), %r12
movq 0x8(%rsp), %rbx
movq 0x20(%rsp), %r14
movq 0x28(%rsp), %r15
movq 0x30(%rsp), %r13
cmpl $0x0, 0x4(%rsp)
jne 0x90c66
leaq 0x10(%rsp), %rdi
leaq 0x4(%rsp), %rdx
movq %rbp, %rsi
movq 0x80(%rsp), %rax
callq *0x18(%rax)
cmpl %r15d, %eax
jne 0x90c66
testl %r15d, %r15d
je 0x90c31
movq %r14, 0x20(%rsp)
movq %r15, 0x28(%rsp)
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq %rbx, 0x8(%rsp)
addq $0x10, %rbx
xorl %r14d, %r14d
movq %r15, 0x18(%rsp)
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
leaq 0x4(%rsp), %rdx
movq 0x80(%rsp), %rax
callq *0x18(%rax)
movl %eax, (%rbx)
cmpl $0x0, 0x4(%rsp)
jne 0x90c61
testl %eax, %eax
js 0x90c45
cmpl %eax, %r14d
cmovlel %eax, %r14d
addq $0x18, %rbx
decq %r15
jne 0x90bab
movq 0x10(%rsp), %rax
subq %r13, %rax
cmpq 0x40(%rsp), %rax
jne 0x90c61
movq 0x28(%rsp), %r15
cmpl %r15d, %r14d
movq 0x8(%rsp), %rbx
jge 0x90c66
cmpl $0x20, %r14d
jb 0x90c98
leaq 0x11fd7(%rip), %rsi     # 0xa2bea
leaq 0x1203a(%rip), %rdx     # 0xa2c54
movl $0x1, %edi
movl %r14d, %ecx
movl $0x1f, %r8d
xorl %eax, %eax
callq 0x13de5
jmp 0x90c66
leaq -0x961(%rip), %rax      # 0x902d7
movq %rax, 0x28(%r12)
movq %r12, %r14
jmp 0x90aaa
leaq 0x11f9e(%rip), %rsi     # 0xa2bea
leaq 0x11fdc(%rip), %rdx     # 0xa2c2f
movl $0x1, %edi
movl %eax, %ecx
xorl %eax, %eax
callq 0x13de5
movq 0x8(%rsp), %rbx
leaq 0x11f7d(%rip), %rsi     # 0xa2bea
leaq 0x12020(%rip), %rdx     # 0xa2c94
xorl %r14d, %r14d
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
movq %rbx, %rdi
callq 0x45c0
movq %r12, %rdi
callq 0x45c0
jmp 0x90aaa
leaq 0x117(%rip), %rcx       # 0x90db6
movl $0x18, %edx
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
callq 0x4990
leaq 0x10(%rbx), %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
movq 0x20(%rsp), %r11
movq 0x18(%rsp), %r14
incl %esi
cmpl %r8d, %esi
ja 0x90c66
movl (%rdx), %ecx
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
shll %cl, %r10d
movl %ecx, %eax
subl %r9d, %eax
notl %r10d
cmovlel %edi, %eax
cmovgl %ecx, %r9d
cmovgl %r10d, %r8d
movl %eax, %ecx
shll %cl, %esi
movl %esi, -0x4(%rdx)
addq $0x18, %rdx
decq %r14
jne 0x90ccc
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
leaq 0x10(%rbx), %rax
negq %r15
xorl %ecx, %ecx
xorl %esi, %esi
xorl %edx, %edx
movl (%rax), %edi
cmpl %esi, %edi
jle 0x90d22
movl -0x4(%rax), %edx
addl %ecx, %edx
movl %edi, %esi
movl %edx, -0x8(%rax)
decq %rcx
addq $0x18, %rax
cmpq %rcx, %r15
jne 0x90d15
leal -0x3(%r11), %eax
cmpl $0x1, %eax
ja 0x90d49
cmpl $0x0, 0x10(%rbx)
je 0x90d87
leaq -0xa45(%rip), %rax      # 0x90302
jmp 0x90da0
xorl %r14d, %r14d
decl %r11d
cmpl $0x6, %r11d
ja 0x90aaa
leaq 0x11ad0(%rip), %rax     # 0xa2830
movslq (%rax,%r11,4), %rcx
addq %rax, %rcx
jmpq *%rcx
cmpl $0x0, 0x10(%rbx)
je 0x90d90
leaq -0x839(%rip), %rax      # 0x9053d
jmp 0x90da0
cmpl $0x0, 0x10(%rbx)
je 0x90d99
leaq -0x66e(%rip), %rax      # 0x90717
jmp 0x90da0
leaq -0xab1(%rip), %rax      # 0x902dd
jmp 0x90da0
leaq -0x95b(%rip), %rax      # 0x9043c
jmp 0x90da0
leaq -0x72e(%rip), %rax      # 0x90672
movq %rax, 0x28(%r12)
leaq -0x55c(%rip), %rax      # 0x90850
movq %rax, 0x58(%r12)
jmp 0x90c3d