// File: /workspace/github_c_makefile_star2_O1/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 0x7f434(%rip), %rsi     # 0x9b5c4
callq 0xa4a5
testq %rax, %rax
je 0x1c204
movq %rax, %rbx
leaq 0x8(%rsp), %rsi
movl $0x4, %edx
movq %rax, %rdi
callq 0xbf5d
cmpq $0x4, %rax
jne 0x1c1e6
cmpl $0x1495343, 0x8(%rsp)   # imm = 0x1495343
je 0x1c216
cmpl $0x1494254, 0x8(%rsp)   # imm = 0x1494254
je 0x1c27a
cmpl $0x1494142, 0x8(%rsp)   # imm = 0x1494142
je 0x1c349
callq 0x4050
movl $0x16, (%rax)
xorl %r14d, %r14d
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0xd4a4
movq %r14, %rdi
callq 0x1585e
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 $0xc, %edx
movq %rbx, %rdi
callq 0xbf5d
cmpq $0xc, %rax
jne 0x1c33e
movl 0x18(%rsp), %r14d
testq %r14, %r14
je 0x1c3b4
leaq 0x1(%r14), %rdi
callq 0x4680
testq %rax, %rax
je 0x1c33e
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0xbf5d
movl 0x18(%rsp), %ecx
cmpq %rcx, %rax
jne 0x1c41c
movb $0x0, (%r15,%rcx)
jmp 0x1c3b7
leaq 0x10(%rsp), %rsi
movl $0x20, %edx
movq %rbx, %rdi
callq 0xbf5d
cmpq $0x20, %rax
jne 0x1c3a5
movl 0x10(%rsp), %edi
testl %edi, %edi
js 0x1c3a5
xorl %r14d, %r14d
movl $0x2, %esi
xorl %edx, %edx
movl $0xe, %ecx
movl $0x5, %r8d
callq 0x16550
testq %rax, %rax
je 0x1c3a8
movq %rax, %r15
movl 0x2c(%rsp), %r14d
movl $0xffffffe2, %eax       # imm = 0xFFFFFFE2
cmpq %rax, %r14
ja 0x1c436
leal 0x1c(%r14), %eax
movl %eax, 0x10(%r15)
leaq 0x1d(%r14), %rdi
callq 0x4680
movq %rax, 0x38(%r15)
testq %rax, %rax
je 0x1c436
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 %r14, %rdx
callq 0xbf5d
cmpq %r14, %rax
jne 0x1c436
movq 0x38(%r15), %rax
movl 0x10(%r15), %ecx
movb $0x0, (%rax,%rcx)
movq %r15, %rdi
movq %rbx, %rsi
movl $0x2, %edx
jmp 0x1c396
xorl %r14d, %r14d
xorl %r15d, %r15d
jmp 0x1c41f
leaq 0x10(%rsp), %rsi
movl $0x4, %edx
movq %rbx, %rdi
callq 0xbf5d
cmpq $0x4, %rax
jne 0x1c3a5
movl 0x10(%rsp), %edi
testl %edi, %edi
js 0x1c3a5
xorl %r14d, %r14d
movl $0x1, %esi
xorl %edx, %edx
movl $0xe, %ecx
movl $0x5, %r8d
callq 0x16550
testq %rax, %rax
je 0x1c3a8
movq %rax, %r15
movq %rax, %rdi
movq %rbx, %rsi
movl $0x1, %edx
callq 0x1cd8c
testl %eax, %eax
setns %al
jmp 0x1c438
xorl %r14d, %r14d
xorl %eax, %eax
testb %al, %al
je 0x1c1e9
jmp 0x1c429
xorl %r15d, %r15d
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
movq %rbx, %rdi
callq 0xbf5d
cmpq $0x4, %rax
jne 0x1c41c
movl 0xc(%rsp), %edi
testl %edi, %edi
js 0x1c41c
movl 0x10(%rsp), %ecx
movl 0x14(%rsp), %r8d
xorl %r14d, %r14d
xorl %esi, %esi
xorl %edx, %edx
callq 0x16550
testq %rax, %rax
je 0x1c41f
movq %rax, %r12
movl 0x18(%rsp), %eax
movl %eax, 0x10(%r12)
movq %r15, 0x38(%r12)
xorl %r15d, %r15d
movq %r12, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x1cd8c
testl %eax, %eax
setns %al
movq %r12, %r14
jmp 0x1c421
xorl %r14d, %r14d
xorl %eax, %eax
testb %al, %al
je 0x1c1ec
movq %rbx, %rdi
callq 0xd4a4
jmp 0x1c207
xorl %eax, %eax
movq %r15, %r14
jmp 0x1c3aa