// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
movslq -0x1c(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq $0x0, -0x58(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x1f, -0x6c(%rbp)
movl $0x0, -0x70(%rbp)
cmpl $0x5, -0x24(%rbp)
jne 0xe956a
movl $0x1, %edi
leaq 0xfaf3(%rip), %rsi      # 0xf9042
leaq 0xf8e8(%rip), %rdx      # 0xf8e3e
movb $0x0, %al
callq 0x1b030
movq $0x0, -0x8(%rbp)
jmp 0xe9b15
movq 0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rsi
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq *%rax
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0xe95b0
movl $0x1, %edi
leaq 0xfaad(%rip), %rsi      # 0xf9042
leaq 0xfabf(%rip), %rdx      # 0xf905b
movb $0x0, %al
callq 0x1b030
movq $0x0, -0x8(%rbp)
jmp 0xe9b15
movslq -0x2c(%rbp), %rax
movabsq $0xaaaaaaaaaaaaaaa, %rcx # imm = 0xAAAAAAAAAAAAAAA
cmpq %rcx, %rax
jb 0xe95db
callq 0x4050
movl $0xc, (%rax)
movq $0x0, -0x8(%rbp)
jmp 0xe9b15
movl $0x1, %edi
movl $0x880, %esi            # imm = 0x880
callq 0x42f0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xe9602
movq $0x0, -0x8(%rbp)
jmp 0xe9b15
movq -0x50(%rbp), %rax
movl $0x3, (%rax)
movq -0x50(%rbp), %rax
leaq -0x9b7(%rip), %rcx      # 0xe8c60
movq %rcx, 0x20(%rax)
movl -0x2c(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x60(%rax)
movl -0x24(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x70(%rax)
cmpl $0x0, -0x2c(%rbp)
je 0xe966d
movslq -0x2c(%rbp), %rax
imulq $0x18, %rax, %rdi
callq 0x4650
movq -0x50(%rbp), %rcx
movq %rax, 0x68(%rcx)
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0xe966b
movq -0x50(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
jmp 0xe9b15
jmp 0xe9681
movq -0x50(%rbp), %rax
movq $0x0, 0x68(%rax)
movq $0x0, -0x58(%rbp)
cmpl $0x2, -0x24(%rbp)
jne 0xe96ce
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0xe96cc
movq 0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x48(%rbp), %rsi
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq *%rax
movq %rax, %rcx
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xe968e
jmp 0xe9728
cmpl $0x1, -0x24(%rbp)
je 0xe96da
cmpl $0x3, -0x24(%rbp)
jne 0xe9721
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0xe971f
movq 0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rsi
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq *%rax
movq %rax, %rcx
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movq %rcx, (%rax)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xe96e1
jmp 0xe9726
jmp 0xe9ae1
jmp 0xe9728
cmpl $0x0, -0x70(%rbp)
je 0xe9733
jmp 0xe9ae1
movq 0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rsi
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq *%rax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
je 0xe9759
jmp 0xe9ae1
cmpl $0x0, -0x2c(%rbp)
jne 0xe977b
movq -0x50(%rbp), %rax
leaq -0xaca(%rip), %rcx      # 0xe8ca0
movq %rcx, 0x28(%rax)
movq -0x50(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xe9b15
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0xe9845
movq 0x10(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x48(%rbp), %rsi
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rdx
callq *%rax
movl %eax, %ecx
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x10(%rax)
cmpl $0x0, -0x70(%rbp)
je 0xe97c3
jmp 0xe9845
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x10(%rax)
jge 0xe9809
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %ecx
movl $0x1, %edi
leaq 0xf84c(%rip), %rsi      # 0xf9042
leaq 0xf88a(%rip), %rdx      # 0xf9087
movb $0x0, %al
callq 0x1b030
jmp 0xe9ae1
movl -0x64(%rbp), %eax
movq -0x58(%rbp), %rcx
movslq -0x30(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x10(%rcx), %eax
jge 0xe9835
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, -0x64(%rbp)
jmp 0xe9837
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xe9782
cmpl $0x0, -0x70(%rbp)
jne 0xe9867
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
subq %rcx, %rax
movslq -0x1c(%rbp), %rcx
cmpq %rcx, %rax
jne 0xe9867
movl -0x64(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jl 0xe986c
jmp 0xe9ae1
cmpl $0x1f, -0x64(%rbp)
jle 0xe989a
movl -0x64(%rbp), %ecx
movl $0x1, %edi
leaq 0xf7c1(%rip), %rsi      # 0xf9042
leaq 0xf824(%rip), %rdx      # 0xf90ac
movl $0x1f, %r8d
movb $0x0, %al
callq 0x1b030
jmp 0xe9ae1
movq -0x58(%rbp), %rdi
movslq -0x2c(%rbp), %rsi
movl $0x18, %edx
leaq 0x272(%rip), %rcx       # 0xe9b20
callq 0x4990
movl $0xffffffff, -0x5c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x60(%rbp)
movl $0x0, -0x68(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0xe997c
movl -0x5c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
cmpl -0x68(%rbp), %eax
jbe 0xe98f1
jmp 0xe9ae1
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
cmpl -0x60(%rbp), %eax
jle 0xe9959
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %ecx
subl -0x60(%rbp), %ecx
movl -0x5c(%rbp), %eax
shll %cl, %eax
movl %eax, -0x5c(%rbp)
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, -0x60(%rbp)
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
subl $0x1, %eax
movl %eax, -0x68(%rbp)
movl -0x5c(%rbp), %ecx
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xe98cf
movl $0x0, -0x60(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0xe99fd
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
cmpl -0x60(%rbp), %eax
jle 0xe99dd
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
subl -0x30(%rbp), %eax
movl %eax, -0x34(%rbp)
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, -0x60(%rbp)
movl -0x34(%rbp), %ecx
movq -0x58(%rbp), %rax
movslq -0x30(%rbp), %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0xe9991
cmpl $0x3, -0x24(%rbp)
je 0xe9a09
cmpl $0x4, -0x24(%rbp)
jne 0xe9a3c
movq -0x50(%rbp), %rax
movq 0x68(%rax), %rax
cmpl $0x0, 0x10(%rax)
jne 0xe9a28
movq -0x50(%rbp), %rax
leaq -0xd62(%rip), %rcx      # 0xe8cc0
movq %rcx, 0x28(%rax)
jmp 0xe9a37
movq -0x50(%rbp), %rax
leaq -0xd03(%rip), %rcx      # 0xe8d30
movq %rcx, 0x28(%rax)
jmp 0xe9ac8
cmpl $0x2, -0x24(%rbp)
je 0xe9a48
cmpl $0x7, -0x24(%rbp)
jne 0xe9a78
movq -0x50(%rbp), %rax
movq 0x68(%rax), %rax
cmpl $0x0, 0x10(%rax)
jne 0xe9a67
movq -0x50(%rbp), %rax
leaq -0x921(%rip), %rcx      # 0xe9140
movq %rcx, 0x28(%rax)
jmp 0xe9a76
movq -0x50(%rbp), %rax
leaq -0x8c2(%rip), %rcx      # 0xe91b0
movq %rcx, 0x28(%rax)
jmp 0xe9ac6
cmpl $0x1, -0x24(%rbp)
je 0xe9a8a
cmpl $0x6, -0x24(%rbp)
je 0xe9a8a
cmpl $0x3, -0x24(%rbp)
jne 0xe9aba
movq -0x50(%rbp), %rax
movq 0x68(%rax), %rax
cmpl $0x0, 0x10(%rax)
jne 0xe9aa9
movq -0x50(%rbp), %rax
leaq -0xba3(%rip), %rcx      # 0xe8f00
movq %rcx, 0x28(%rax)
jmp 0xe9ab8
movq -0x50(%rbp), %rax
leaq -0xb44(%rip), %rcx      # 0xe8f70
movq %rcx, 0x28(%rax)
jmp 0xe9ac4
movq $0x0, -0x8(%rbp)
jmp 0xe9b15
jmp 0xe9ac6
jmp 0xe9ac8
movq -0x50(%rbp), %rax
leaq -0x753(%rip), %rcx      # 0xe9380
movq %rcx, 0x58(%rax)
movq -0x50(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xe9b15
movl $0x1, %edi
leaq 0xf555(%rip), %rsi      # 0xf9042
leaq 0xf5f8(%rip), %rdx      # 0xf90ec
movb $0x0, %al
callq 0x1b030
movq -0x58(%rbp), %rdi
callq 0x45a0
movq -0x50(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nop