// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/hts.c
// Function: idx_read
static hts_idx_t *idx_read(const char *fn)
{
    uint8_t magic[4];
    int i, is_be;
    hts_idx_t *idx = NULL;
    uint8_t *meta = NULL;
    BGZF *fp = bgzf_open(fn, "r");
    if (fp == NULL) return NULL;
    is_be = ed_is_big();
    if (bgzf_read(fp, magic, 4) != 4) goto fail;

    if (memcmp(magic, "CSI\1", 4) == 0) {
        uint32_t x[3], n;
        if (bgzf_read(fp, x, 12) != 12) goto fail;
        if (is_be) for (i = 0; i < 3; ++i) ed_swap_4p(&x[i]);
        if (x[2]) {
            if (SIZE_MAX - x[2] < 1) goto fail; // Prevent possible overflow
            if ((meta = (uint8_t*)malloc((size_t) x[2] + 1)) == NULL) goto fail;
            if (bgzf_read(fp, meta, x[2]) != x[2]) goto fail;
            // Prevent possible strlen past the end in tbx_index_load2
            meta[x[2]] = '\0';
        }
        if (bgzf_read(fp, &n, 4) != 4) goto fail;
        if (is_be) ed_swap_4p(&n);
        if (n > INT32_MAX) goto fail;
        if ((idx = hts_idx_init(n, HTS_FMT_CSI, 0, x[0], x[1])) == NULL) goto fail;
        idx->l_meta = x[2];
        idx->meta = meta;
        meta = NULL;
        if (idx_read_core(idx, fp, HTS_FMT_CSI) < 0) goto fail;
    }
    else if (memcmp(magic, "TBI\1", 4) == 0) {
        uint8_t x[8 * 4];
        uint32_t n;
        // Read file header
        if (bgzf_read(fp, x, sizeof(x)) != sizeof(x)) goto fail;
        n = le_to_u32(&x[0]); // location of n_ref
        if (n > INT32_MAX) goto fail;
        if ((idx = hts_idx_init(n, HTS_FMT_TBI, 0, 14, 5)) == NULL) goto fail;
        n = le_to_u32(&x[7*4]); // location of l_nm
        if (n > UINT32_MAX - 29) goto fail; // Prevent possible overflow
        idx->l_meta = 28 + n;
        if ((idx->meta = (uint8_t*)malloc(idx->l_meta + 1)) == NULL) goto fail;
        // copy format, col_seq, col_beg, col_end, meta, skip, l_nm
        // N.B. left in little-endian byte order.
        memcpy(idx->meta, &x[1*4], 28);
        // Read in sequence names.
        if (bgzf_read(fp, idx->meta + 28, n) != n) goto fail;
        // Prevent possible strlen past the end in tbx_index_load2
        idx->meta[idx->l_meta] = '\0';
        if (idx_read_core(idx, fp, HTS_FMT_TBI) < 0) goto fail;
    }
    else if (memcmp(magic, "BAI\1", 4) == 0) {
        uint32_t n;
        if (bgzf_read(fp, &n, 4) != 4) goto fail;
        if (is_be) ed_swap_4p(&n);
        if (n > INT32_MAX) goto fail;
        if ((idx = hts_idx_init(n, HTS_FMT_BAI, 0, 14, 5)) == NULL) goto fail;
        if (idx_read_core(idx, fp, HTS_FMT_BAI) < 0) goto fail;
    }
    else { errno = EINVAL; goto fail; }

    bgzf_close(fp);
    return idx;

fail:
    bgzf_close(fp);
    hts_idx_destroy(idx);
    free(meta);
    return NULL;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x38, %rsp
leaq 0x7c9ed(%rip), %rsi     # 0x985cb
callq 0xa550
testq %rax, %rax
je 0x1bcb8
movq %rax, %rbx
leaq 0x8(%rsp), %rsi
movl $0x4, %edx
movq %rax, %rdi
callq 0xbefc
cmpq $0x4, %rax
jne 0x1bc9a
cmpl $0x1495343, 0x8(%rsp)   # imm = 0x1495343
je 0x1bc3e
cmpl $0x1494254, 0x8(%rsp)   # imm = 0x1494254
je 0x1bcca
cmpl $0x1494142, 0x8(%rsp)   # imm = 0x1494142
je 0x1bd8a
callq 0x4050
movl $0x16, (%rax)
jmp 0x1bc9a
leaq 0x10(%rsp), %rsi
movl $0xc, %edx
movq %rbx, %rdi
callq 0xbefc
cmpq $0xc, %rax
jne 0x1bc9a
movl 0x18(%rsp), %r14d
testq %r14, %r14
je 0x1bdf3
leaq 0x1(%r14), %rdi
callq 0x4680
testq %rax, %rax
je 0x1bc9a
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0xbefc
movl 0x18(%rsp), %ecx
cmpq %rcx, %rax
jne 0x1be61
movb $0x0, (%r15,%rax)
jmp 0x1bdf6
xorl %r14d, %r14d
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0xd3cd
movq %r14, %rdi
callq 0x152c3
movq %r15, %rdi
callq 0x45c0
xorl %r14d, %r14d
movq %r14, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
leaq 0x10(%rsp), %rsi
movl $0x20, %edx
movq %rbx, %rdi
callq 0xbefc
cmpq $0x20, %rax
jne 0x1bde5
movl 0x10(%rsp), %edi
testl %edi, %edi
js 0x1bde5
xorl %r12d, %r12d
movl $0x2, %esi
xorl %edx, %edx
movl $0xe, %ecx
movl $0x5, %r8d
callq 0x15fba
testq %rax, %rax
je 0x1bde8
movq %rax, %r14
movl 0x2c(%rsp), %r15d
movl $0xffffffe2, %eax       # imm = 0xFFFFFFE2
cmpq %rax, %r15
ja 0x1bde0
leal 0x1c(%r15), %eax
movl %eax, 0x10(%r14)
leaq 0x1d(%r15), %rdi
callq 0x4680
movq %rax, 0x38(%r14)
testq %rax, %rax
je 0x1bde0
movups 0x14(%rsp), %xmm0
movups 0x20(%rsp), %xmm1
movups %xmm1, 0xc(%rax)
movups %xmm0, (%rax)
addq $0x1c, %rax
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0xbefc
cmpq %r15, %rax
jne 0x1bde0
movq 0x38(%r14), %rax
movl 0x10(%r14), %ecx
movb $0x0, (%rax,%rcx)
movq %r14, %rdi
movq %rbx, %rsi
movl $0x2, %edx
jmp 0x1bdd7
leaq 0x10(%rsp), %rsi
movl $0x4, %edx
movq %rbx, %rdi
callq 0xbefc
cmpq $0x4, %rax
jne 0x1bde5
movl 0x10(%rsp), %edi
testl %edi, %edi
js 0x1bde5
xorl %r12d, %r12d
movl $0x1, %esi
xorl %edx, %edx
movl $0xe, %ecx
movl $0x5, %r8d
callq 0x15fba
testq %rax, %rax
je 0x1bde8
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x1c80a
testl %eax, %eax
jns 0x1be54
movq %r14, %r12
jmp 0x1bde8
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r12, %r14
jmp 0x1bca0
xorl %r15d, %r15d
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
movq %rbx, %rdi
callq 0xbefc
cmpq $0x4, %rax
jne 0x1be61
movl 0xc(%rsp), %edi
testl %edi, %edi
js 0x1be61
movl 0x10(%rsp), %ecx
movl 0x14(%rsp), %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x15fba
testq %rax, %rax
je 0x1be61
movq %rax, %r14
movl 0x18(%rsp), %eax
movl %eax, 0x10(%r14)
movq %r15, 0x38(%r14)
xorl %r15d, %r15d
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x1c80a
testl %eax, %eax
js 0x1bca0
movq %rbx, %rdi
callq 0xd3cd
jmp 0x1bcbb
xorl %r14d, %r14d
jmp 0x1bca0