// File: /workspace/github_c_makefile_star2_O2/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 0x628ad(%rip), %rsi     # 0x7a526
callq 0x9596
testq %rax, %rax
je 0x17d4f
movq %rax, %rbx
leaq 0x8(%rsp), %rsi
pushq $0x4
popq %rdx
movq %rax, %rdi
callq 0xab95
cmpq $0x4, %rax
jne 0x17d31
cmpl $0x1495343, 0x8(%rsp)   # imm = 0x1495343
je 0x17cd7
cmpl $0x1494254, 0x8(%rsp)   # imm = 0x1494254
je 0x17d61
cmpl $0x1494142, 0x8(%rsp)   # imm = 0x1494142
je 0x17e10
callq 0x4050
movl $0x16, (%rax)
jmp 0x17d31
leaq 0x10(%rsp), %rsi
pushq $0xc
popq %rdx
movq %rbx, %rdi
callq 0xab95
cmpq $0xc, %rax
jne 0x17d31
movl 0x18(%rsp), %r14d
testq %r14, %r14
je 0x17e6f
leaq 0x1(%r14), %rdi
callq 0x4680
testq %rax, %rax
je 0x17d31
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0xab95
movl 0x18(%rsp), %ecx
cmpq %rcx, %rax
jne 0x17edb
movb $0x0, (%r15,%rax)
jmp 0x17e72
xorl %r14d, %r14d
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0xbc96
movq %r14, %rdi
callq 0x12502
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
pushq $0x20
popq %rdx
movq %rbx, %rdi
callq 0xab95
cmpq $0x20, %rax
jne 0x17e61
movl 0x10(%rsp), %edi
testl %edi, %edi
js 0x17e61
xorl %r12d, %r12d
pushq $0x2
popq %rsi
pushq $0xe
popq %rcx
pushq $0x5
popq %r8
xorl %edx, %edx
callq 0x13187
testq %rax, %rax
je 0x17e64
movq %rax, %r14
movl 0x2c(%rsp), %r15d
movl $0xffffffe2, %eax       # imm = 0xFFFFFFE2
cmpq %rax, %r15
ja 0x17e5c
leal 0x1c(%r15), %eax
movl %eax, 0x10(%r14)
leaq 0x1d(%r15), %rdi
callq 0x4680
movq %rax, 0x38(%r14)
testq %rax, %rax
je 0x17e5c
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 0xab95
cmpq %r15, %rax
jne 0x17e5c
movq 0x38(%r14), %rax
movl 0x10(%r14), %ecx
movb $0x0, (%rax,%rcx)
pushq $0x2
jmp 0x17e4c
leaq 0x10(%rsp), %rsi
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0xab95
cmpq $0x4, %rax
jne 0x17e61
movl 0x10(%rsp), %edi
testl %edi, %edi
js 0x17e61
xorl %r12d, %r12d
pushq $0x1
popq %rsi
pushq $0xe
popq %rcx
pushq $0x5
popq %r8
xorl %edx, %edx
callq 0x13187
testq %rax, %rax
je 0x17e64
movq %rax, %r14
pushq $0x1
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x189e2
testl %eax, %eax
jns 0x17ece
movq %r14, %r12
jmp 0x17e64
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r12, %r14
jmp 0x17d37
xorl %r15d, %r15d
leaq 0xc(%rsp), %rsi
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0xab95
cmpq $0x4, %rax
jne 0x17edb
movl 0xc(%rsp), %edi
testl %edi, %edi
js 0x17edb
movl 0x10(%rsp), %ecx
movl 0x14(%rsp), %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x13187
testq %rax, %rax
je 0x17edb
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 0x189e2
testl %eax, %eax
js 0x17d37
movq %rbx, %rdi
callq 0xbc96
jmp 0x17d52
xorl %r14d, %r14d
jmp 0x17d37