// File: /workspace/github_c_makefile_star2_O2/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, 0x10(%rsp)
andl $0x0, 0xc(%rsp)
cmpl $0x5, %r8d
jne 0x72dbc
leaq 0x1172a(%rip), %rsi     # 0x844da
leaq 0x1151f(%rip), %rdx     # 0x842d6
jmp 0x72e5f
movl %r8d, %ebx
movq %rsi, %r14
movq 0x90(%rsp), %rax
movslq %edx, %r15
leaq (%rsi,%r15), %rbp
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movq %rbp, %rsi
callq *0x18(%rax)
movq %rax, %r12
testl %r12d, %r12d
js 0x72e51
pushq $0x1
popq %rdi
movl $0x880, %esi            # imm = 0x880
callq 0x4300
testq %rax, %rax
je 0x72e4c
movq %rax, %r13
movq %r15, 0x50(%rsp)
movl %r12d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl $0x3, (%rax)
leaq -0x548(%rip), %rax      # 0x728d1
movq %rax, 0x20(%r13)
movl %r12d, 0x60(%r13)
movl %ebx, 0x70(%r13)
testl %r12d, %r12d
movq %rcx, 0x48(%rsp)
je 0x72e7e
imulq $0x18, %rcx, %rdi
callq 0x4680
movq %rax, %r15
movq %rax, 0x68(%r13)
testq %rax, %rax
jne 0x72e81
movq %r13, %rdi
callq 0x45c0
xorl %r12d, %r12d
jmp 0x72e6c
leaq 0x11682(%rip), %rsi     # 0x844da
leaq 0x11694(%rip), %rdx     # 0x844f3
xorl %r12d, %r12d
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
movq %r12, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r15d, %r15d
movq %r14, 0x40(%rsp)
cmpl $0x2, %ebx
movq %rbx, 0x38(%rsp)
jne 0x72ed6
movq %r13, 0x28(%rsp)
movq %r12, 0x20(%rsp)
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
imulq $0x18, %rax, %r13
xorl %r12d, %r12d
leaq 0x10(%rsp), %rbx
leaq 0xc(%rsp), %r14
cmpq %r12, %r13
je 0x72f2a
movq %rbx, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq 0x90(%rsp), %rax
callq *0x28(%rax)
movq %rax, (%r15,%r12)
addq $0x18, %r12
jmp 0x72eb3
movl %ebx, %eax
andl $-0x3, %eax
cmpl $0x1, %eax
jne 0x7302f
movq %r13, 0x28(%rsp)
movq %r12, 0x20(%rsp)
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
imulq $0x18, %rax, %r13
xorl %r12d, %r12d
leaq 0x10(%rsp), %rbx
leaq 0xc(%rsp), %r14
cmpq %r12, %r13
je 0x72f2a
movq %rbx, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq 0x90(%rsp), %rax
callq *0x18(%rax)
movq %rax, (%r15,%r12)
addq $0x18, %r12
jmp 0x72f07
cmpl $0x0, 0xc(%rsp)
movq 0x28(%rsp), %r13
movq 0x20(%rsp), %r12
jne 0x7302f
leaq 0x10(%rsp), %rdi
leaq 0xc(%rsp), %rdx
movq %rbp, %rsi
movq 0x90(%rsp), %rax
callq *0x18(%rax)
cmpl %r12d, %eax
jne 0x7302f
testl %r12d, %r12d
je 0x72fc0
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x30(%rsp)
imulq $0x18, %rax, %rax
movq %rax, 0x18(%rsp)
xorl %r14d, %r14d
xorl %ebx, %ebx
cmpq %r14, 0x18(%rsp)
je 0x72fd3
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
leaq 0xc(%rsp), %rdx
movq 0x90(%rsp), %rax
callq *0x18(%rax)
movl %eax, 0x10(%r15,%r14)
cmpl $0x0, 0xc(%rsp)
jne 0x7302f
testl %eax, %eax
js 0x73015
cmpl %eax, %ebx
cmovgl %ebx, %eax
addq $0x18, %r14
movl %eax, %ebx
jmp 0x72f80
leaq -0x6da(%rip), %rax      # 0x728ed
movq %rax, 0x28(%r13)
movq %r13, %r12
jmp 0x72e6c
cmpl $0x0, 0xc(%rsp)
jne 0x7302f
movq 0x10(%rsp), %rax
subq 0x40(%rsp), %rax
cmpq 0x50(%rsp), %rax
jne 0x7302f
cmpl %r12d, %ebx
jge 0x7302f
cmpl $0x20, %ebx
jb 0x7305f
leaq 0x114de(%rip), %rsi     # 0x844da
leaq 0x11541(%rip), %rdx     # 0x84544
pushq $0x1
popq %rdi
pushq $0x1f
popq %r8
movl %ebx, %ecx
xorl %eax, %eax
callq 0x110c6
jmp 0x7302f
leaq 0x114be(%rip), %rsi     # 0x844da
leaq 0x114fc(%rip), %rdx     # 0x8451f
pushq $0x1
popq %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x110c6
leaq 0x114a4(%rip), %rsi     # 0x844da
leaq 0x11547(%rip), %rdx     # 0x84584
xorl %r12d, %r12d
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
movq %r15, %rdi
callq 0x45c0
movq %r13, %rdi
callq 0x45c0
jmp 0x72e6c
leaq 0x118(%rip), %rcx       # 0x7317e
pushq $0x18
popq %rdx
movq %r15, %rdi
movq 0x48(%rsp), %rsi
callq 0x4990
xorl %edx, %edx
pushq $-0x1
popq %rsi
xorl %edi, %edi
movl %esi, %r8d
xorl %r9d, %r9d
xorl %r10d, %r10d
cmpq %rdi, 0x18(%rsp)
je 0x730c3
incl %r8d
cmpl %r10d, %r8d
ja 0x7302f
movl 0x10(%r15,%rdi), %ecx
movl %esi, %r11d
shll %cl, %r11d
movl %ecx, %eax
subl %r9d, %eax
notl %r11d
cmovlel %edx, %eax
cmovgl %ecx, %r9d
cmovgl %r11d, %r10d
movl %eax, %ecx
shll %cl, %r8d
movl %r8d, 0xc(%r15,%rdi)
addq $0x18, %rdi
jmp 0x73086
movq 0x30(%rsp), %r9
negq %r9
leaq 0x10(%r15), %rax
xorl %ecx, %ecx
xorl %edx, %edx
xorl %esi, %esi
movq 0x38(%rsp), %r8
cmpq %rcx, %r9
je 0x730f8
movl (%rax), %edi
cmpl %esi, %edi
jle 0x730ec
movl -0x4(%rax), %edx
addl %ecx, %edx
movl %edi, %esi
movl %edx, -0x8(%rax)
decq %rcx
addq $0x18, %rax
jmp 0x730da
leal -0x3(%r8), %eax
cmpl $0x1, %eax
ja 0x73111
cmpl $0x0, 0x10(%r15)
je 0x73151
leaq -0x7f7(%rip), %rax      # 0x72918
jmp 0x7316a
xorl %r12d, %r12d
decl %r8d
cmpl $0x6, %r8d
ja 0x72e6c
leaq 0x10ff8(%rip), %rax     # 0x84120
movslq (%rax,%r8,4), %rcx
addq %rax, %rcx
jmpq *%rcx
cmpl $0x0, 0x10(%r15)
je 0x7315a
leaq -0x6f3(%rip), %rax      # 0x72a4c
jmp 0x7316a
cmpl $0x0, 0x10(%r15)
je 0x73163
leaq -0x5cf(%rip), %rax      # 0x72b80
jmp 0x7316a
leaq -0x867(%rip), %rax      # 0x728f1
jmp 0x7316a
leaq -0x735(%rip), %rax      # 0x72a2c
jmp 0x7316a
leaq -0x60c(%rip), %rax      # 0x72b5e
movq %rax, 0x28(%r13)
leaq -0x4e1(%rip), %rax      # 0x72c94
movq %rax, 0x58(%r13)
jmp 0x72fcb