// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq %rsi, 0x18(%rsp)
movl $0x0, 0xc(%rsp)
cmpl $0x5, %r8d
jne 0x9327f
leaq 0x121f7(%rip), %rsi     # 0xa546a
leaq 0x11fec(%rip), %rdx     # 0xa5266
jmp 0x93324
movl %r8d, %ebx
movq %rsi, %r12
movq 0x90(%rsp), %rax
movslq %edx, %r13
leaq (%rsi,%r13), %rbp
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movq %rbp, %rsi
callq *0x18(%rax)
movq %rax, %r14
testl %r14d, %r14d
js 0x93316
movl $0x1, %edi
movl $0x880, %esi            # imm = 0x880
callq 0x4300
testq %rax, %rax
je 0x93311
movq %rax, %r15
movl %r14d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl $0x3, (%rax)
leaq -0x647(%rip), %rax      # 0x92c92
movq %rax, 0x20(%r15)
movl %r14d, 0x60(%r15)
movl %ebx, 0x70(%r15)
testl %r14d, %r14d
movq %rcx, 0x50(%rsp)
je 0x93345
leaq (,%rcx,8), %rax
leaq (%rax,%rax,2), %rdi
callq 0x4680
movq %rax, 0x68(%r15)
testq %rax, %rax
jne 0x9334f
movq %r15, %rdi
callq 0x45c0
xorl %r12d, %r12d
jmp 0x93333
leaq 0x1214d(%rip), %rsi     # 0xa546a
leaq 0x1215f(%rip), %rdx     # 0xa5483
xorl %r12d, %r12d
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
movq %r12, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x0, 0x68(%r15)
xorl %eax, %eax
movq %r13, 0x40(%rsp)
movq %r12, 0x48(%rsp)
movq %rax, 0x10(%rsp)
cmpl $0x2, %ebx
movq %rbx, 0x38(%rsp)
jne 0x933d3
movq %r15, 0x30(%rsp)
movq %r14, 0x20(%rsp)
testl %r14d, %r14d
movq 0x10(%rsp), %r13
movq 0x90(%rsp), %r8
je 0x93439
movq 0x20(%rsp), %rcx
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r12
xorl %r15d, %r15d
leaq 0xc(%rsp), %r14
leaq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %rbp, %rbx
movq %r8, %rbp
callq *0x28(%r8)
movq %rbp, %r8
movq %rbx, %rbp
movq %rax, (%r13,%r15)
addq $0x18, %r15
cmpq %r15, %r12
jne 0x933a8
jmp 0x93439
movl %ebx, %eax
andl $-0x3, %eax
cmpl $0x1, %eax
jne 0x9354b
movq %r15, 0x30(%rsp)
movq %r14, 0x20(%rsp)
testl %r14d, %r14d
movq 0x10(%rsp), %r13
movq 0x90(%rsp), %rbx
je 0x93439
movq 0x20(%rsp), %rcx
cmpl $0x2, %ecx
movl $0x1, %eax
cmovgel %ecx, %eax
shlq $0x3, %rax
leaq (%rax,%rax,2), %r12
xorl %r15d, %r15d
leaq 0xc(%rsp), %r14
leaq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq *0x18(%rbx)
movq %rax, (%r13,%r15)
addq $0x18, %r15
cmpq %r15, %r12
jne 0x9341d
cmpl $0x0, 0xc(%rsp)
movq 0x30(%rsp), %r15
movq 0x20(%rsp), %r13
jne 0x9354b
leaq 0x18(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movq %rbp, %rsi
movq 0x90(%rsp), %rax
callq *0x18(%rax)
cmpl %r13d, %eax
jne 0x9354b
testl %r13d, %r13d
je 0x934d4
cmpl $0x2, %r13d
movl $0x1, %eax
cmovgel %r13d, %eax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x28(%rsp)
xorl %r14d, %r14d
xorl %ebx, %ebx
movq 0x10(%rsp), %r12
leaq 0x18(%rsp), %rdi
movq %rbp, %rsi
leaq 0xc(%rsp), %rdx
movq 0x90(%rsp), %rax
callq *0x18(%rax)
movl %eax, 0x10(%r12,%r14)
cmpl $0x0, 0xc(%rsp)
jne 0x934e7
testl %eax, %eax
js 0x9352f
cmpl %eax, %ebx
cmovgl %ebx, %eax
addq $0x18, %r14
movl %eax, %ebx
cmpq %r14, 0x28(%rsp)
jne 0x93498
jmp 0x934e9
leaq -0x828(%rip), %rax      # 0x92cb3
movq %rax, 0x28(%r15)
movq %r15, %r12
jmp 0x93333
movl %ebx, %eax
cmpl $0x0, 0xc(%rsp)
jne 0x9354b
movq 0x18(%rsp), %rcx
subq 0x48(%rsp), %rcx
cmpq 0x40(%rsp), %rcx
jne 0x9354b
cmpl %r13d, %eax
jge 0x9354b
cmpl $0x20, %eax
jl 0x9357f
leaq 0x11f58(%rip), %rsi     # 0xa546a
leaq 0x11fbb(%rip), %rdx     # 0xa54d4
movl $0x1, %edi
movl %eax, %ecx
movl $0x1f, %r8d
xorl %eax, %eax
callq 0x14378
jmp 0x9354b
leaq 0x11f34(%rip), %rsi     # 0xa546a
leaq 0x11f72(%rip), %rdx     # 0xa54af
movl $0x1, %edi
movl %eax, %ecx
xorl %eax, %eax
callq 0x14378
leaq 0x11f18(%rip), %rsi     # 0xa546a
leaq 0x11fbb(%rip), %rdx     # 0xa5514
xorl %r12d, %r12d
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
movq 0x10(%rsp), %rdi
callq 0x45c0
movq %r15, %rdi
callq 0x45c0
jmp 0x93333
leaq 0x131(%rip), %rcx       # 0x936b7
movl $0x18, %edx
movq 0x10(%rsp), %rdi
movq 0x50(%rsp), %rsi
callq 0x4990
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %esi, %esi
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
movq 0x10(%rsp), %r11
incl %edx
cmpl %r8d, %edx
ja 0x9354b
movl 0x10(%r11,%rdi), %ecx
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
shll %cl, %r10d
movl %ecx, %eax
subl %r9d, %eax
notl %r10d
cmovlel %esi, %eax
cmovgl %ecx, %r9d
cmovgl %r10d, %r8d
movl %eax, %ecx
shll %cl, %edx
movl %edx, 0xc(%r11,%rdi)
addq $0x18, %rdi
cmpq %rdi, 0x28(%rsp)
jne 0x935ae
cmpl $0x2, %r13d
movl $0x1, %eax
cmovgel %r13d, %eax
addq $0x10, %r11
negq %rax
xorl %ecx, %ecx
xorl %esi, %esi
xorl %edx, %edx
movq 0x38(%rsp), %r8
movl (%r11), %edi
cmpl %esi, %edi
jle 0x93618
movl -0x4(%r11), %edx
addl %ecx, %edx
movl %edi, %esi
movl %edx, -0x8(%r11)
decq %rcx
addq $0x18, %r11
cmpq %rcx, %rax
jne 0x93609
leal -0x3(%r8), %eax
cmpl $0x1, %eax
ja 0x93644
movq 0x68(%r15), %rax
cmpl $0x0, 0x10(%rax)
je 0x9368a
leaq -0x964(%rip), %rax      # 0x92cde
jmp 0x936a3
xorl %r12d, %r12d
decl %r8d
cmpl $0x6, %r8d
ja 0x93333
leaq 0x11a55(%rip), %rax     # 0xa50b0
movslq (%rax,%r8,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq 0x68(%r15), %rax
cmpl $0x0, 0x10(%rax)
je 0x93693
leaq -0x812(%rip), %rax      # 0x92e63
jmp 0x936a3
movq 0x68(%r15), %rax
cmpl $0x0, 0x10(%rax)
je 0x9369c
leaq -0x6a4(%rip), %rax      # 0x92fe4
jmp 0x936a3
leaq -0x9d8(%rip), %rax      # 0x92cb9
jmp 0x936a3
leaq -0x855(%rip), %rax      # 0x92e45
jmp 0x936a3
leaq -0x6df(%rip), %rax      # 0x92fc4
movq %rax, 0x28(%r15)
leaq -0x568(%rip), %rax      # 0x93146
movq %rax, 0x58(%r15)
jmp 0x934df