// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/faidx.c
// Function: fai_load3_core
static faidx_t *fai_load3_core(const char *fn, const char *fnfai, const char *fngzi,
                   int flags, int format)
{
    kstring_t fai_kstr = { 0, 0, NULL };
    kstring_t gzi_kstr = { 0, 0, NULL };
    hFILE *fp = NULL;
    faidx_t *fai = NULL;
    int res, gzi_index_needed = 0;
    char *file_type;

    if (format == FAI_FASTA) {
        file_type   = "FASTA";
    } else {
        file_type   = "FASTQ";
    }

    if (fn == NULL)
        return NULL;

    if (fnfai == NULL) {
        if (ksprintf(&fai_kstr, "%s.fai", fn) < 0) goto fail;
        fnfai = fai_kstr.s;
    }
    if (fngzi == NULL) {
        if (ksprintf(&gzi_kstr, "%s.gzi", fn) < 0) goto fail;
        fngzi = gzi_kstr.s;
    }

    fp = hopen(fnfai, "rb");

    if (fp) {
        // index file present, check if a compressed index is needed
        hFILE *gz = NULL;
        BGZF *bgzf = bgzf_open(fn, "rb");

        if (bgzf == 0) {
            hts_log_error("Failed to open %s file %s", file_type, fn);
            goto fail;
        }

        if (bgzf_compression(bgzf) == 2) { // BGZF compression
            if ((gz = hopen(fngzi, "rb")) == 0) {

                if (!(flags & FAI_CREATE) || errno != ENOENT) {
                    hts_log_error("Failed to open %s index %s: %s", file_type, fngzi, strerror(errno));
                    bgzf_close(bgzf);
                    goto fail;
                }

                gzi_index_needed = 1;
                res = hclose(fp); // closed as going to be re-indexed

                if (res < 0) {
                    hts_log_error("Failed on closing %s index %s : %s", file_type, fnfai, strerror(errno));
                    goto fail;
                }
            } else {
                res = hclose(gz);

                if (res < 0) {
                    hts_log_error("Failed on closing %s index %s : %s", file_type, fngzi, strerror(errno));
                    goto fail;
                }
            }
        }

        bgzf_close(bgzf);
    }

    if (fp == 0 || gzi_index_needed) {
        if (!(flags & FAI_CREATE) || errno != ENOENT) {
            hts_log_error("Failed to open %s index %s: %s", file_type, fnfai, strerror(errno));
            goto fail;
        }

        hts_log_info("Build %s index", file_type);

        if (fai_build3_core(fn, fnfai, fngzi) < 0) {
            goto fail;
        }

        fp = hopen(fnfai, "rb");
        if (fp == 0) {
            hts_log_error("Failed to open %s index %s: %s", file_type, fnfai, strerror(errno));
            goto fail;
        }
    }

    fai = fai_read(fp, fnfai, format);
    if (fai == NULL) {
        hts_log_error("Failed to read %s index %s", file_type, fnfai);
        goto fail;
    }

    res = hclose(fp);
    fp = NULL;
    if (res < 0) {
        hts_log_error("Failed on closing %s index %s : %s", file_type, fnfai, strerror(errno));
        goto fail;
    }

    fai->bgzf = bgzf_open(fn, "rb");
    if (fai->bgzf == 0) {
        hts_log_error("Failed to open %s file %s", file_type, fn);
        goto fail;
    }

    if ( fai->bgzf->is_compressed==1 ) {
        if ( bgzf_index_load(fai->bgzf, fngzi, NULL) < 0 ) {
            hts_log_error("Failed to load .gzi index: %s", fngzi);
            goto fail;
        }
    }
    free(fai_kstr.s);
    free(gzi_kstr.s);
    return fai;

 fail:
    if (fai) fai_destroy(fai);
    if (fp) hclose_abruptly(fp);
    free(fai_kstr.s);
    free(gzi_kstr.s);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
xorl %ebp, %ebp
movq %rbp, 0x80(%rsp)
movq %rbp, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
cmpl $0x1, %r8d
leaq 0x13b00(%rip), %rax     # 0x95de2
leaq 0x13aff(%rip), %rbx     # 0x95de8
cmoveq %rax, %rbx
testq %rdi, %rdi
je 0x828c5
movl %ecx, %r14d
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r13
movl %r8d, 0x1c(%rsp)
testq %rsi, %rsi
jne 0x82334
leaq 0x192a8(%rip), %rsi     # 0x9b5bb
xorl %ebp, %ebp
leaq 0x70(%rsp), %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x9ab7
testl %eax, %eax
js 0x828ae
movq 0x80(%rsp), %r12
testq %r15, %r15
jne 0x8235e
leaq 0x1f326(%rip), %rsi     # 0xa1666
xorl %ebp, %ebp
leaq 0x50(%rsp), %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x9ab7
testl %eax, %eax
js 0x828ae
movq 0x60(%rsp), %r15
leaq 0x12b5e(%rip), %rsi     # 0x94ec3
movq %r12, %rdi
xorl %eax, %eax
callq 0xfe44
movq %rax, %rbp
testq %rax, %rax
je 0x82610
movq %r15, 0x10(%rsp)
leaq 0x12b3c(%rip), %rsi     # 0x94ec3
movq %r13, %rdi
callq 0xa550
testq %rax, %rax
je 0x82623
movq %rax, %r15
movq %rax, %rdi
callq 0xdad8
cmpl $0x2, %eax
jne 0x823d4
leaq 0x12b14(%rip), %rsi     # 0x94ec3
movq 0x10(%rsp), %rdi
xorl %eax, %eax
callq 0xfe44
testq %rax, %rax
je 0x82650
movq %rax, %rdi
callq 0xf4df
testl %eax, %eax
js 0x82792
movq %r15, %rdi
callq 0xd3cd
movq 0x10(%rsp), %r15
movq %rbp, 0x28(%rsp)
movl $0x1, %edi
movl $0x28, %esi
callq 0x4300
testq %rax, %rax
je 0x8287a
movq %rax, %rbp
movq %r12, 0x8(%rsp)
movq %rbx, 0x30(%rsp)
movl $0x1, %edi
movl $0x28, %esi
callq 0x4300
movq %rax, 0x18(%rbp)
testq %rax, %rax
je 0x82648
movl $0x10000, %edi          # imm = 0x10000
movl $0x1, %esi
callq 0x4300
testq %rax, %rax
je 0x82648
movq %rax, %r12
movq %r15, 0x10(%rsp)
movl $0x10000, %esi          # imm = 0x10000
movq %rax, %rdi
movl $0xa, %edx
movq 0x28(%rsp), %rcx
callq 0xef50
movq %rax, %r15
testq %rax, %rax
jle 0x82586
movl $0x1, %r14d
movq $0x0, 0x38(%rsp)
movb (%r12), %bl
testb %bl, %bl
je 0x824a9
callq 0x4510
movq (%rax), %rcx
movq %r12, %rax
movl 0x1c(%rsp), %esi
movzbl %bl, %edx
testb $0x20, 0x1(%rcx,%rdx,2)
jne 0x824a4
movb 0x1(%rax), %bl
incq %rax
testb %bl, %bl
jne 0x82490
movq %rax, %rdi
jmp 0x824b3
movq %r12, %rax
movq %r12, %rdi
movl 0x1c(%rsp), %esi
subq %r12, %rax
cmpq %r15, %rax
jge 0x824c1
movb $0x0, (%rdi)
incq %rdi
cmpl $0x1, %esi
jne 0x824f2
leaq 0x1f622(%rip), %rsi     # 0xa1aef
leaq 0x48(%rsp), %rdx
leaq 0x40(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x24(%rsp), %r9
xorl %eax, %eax
callq 0x44a0
cmpl $0x4, %eax
je 0x82526
jmp 0x8283e
leaq 0x38(%rsp), %rax
movq %rax, (%rsp)
leaq 0x1f62e(%rip), %rsi     # 0xa1b30
leaq 0x48(%rsp), %rdx
leaq 0x40(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x24(%rsp), %r9
xorl %eax, %eax
callq 0x44a0
cmpl $0x5, %eax
jne 0x827f1
movq 0x48(%rsp), %rdx
movl 0x24(%rsp), %ecx
movl 0x20(%rsp), %r8d
movq 0x40(%rsp), %r9
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
movq %rbp, %rdi
movq %r12, %rsi
callq 0x837f9
testl %eax, %eax
jne 0x82860
xorl %eax, %eax
cmpb $0xa, -0x1(%r12,%r15)
sete %al
addq %rax, %r14
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdi
movl $0xa, %edx
movq 0x28(%rsp), %rcx
callq 0xef50
movq %rax, %r15
testq %rax, %rax
jg 0x82470
testq %r15, %r15
js 0x827c8
movq %r12, %rdi
callq 0x45c0
movq 0x28(%rsp), %rdi
callq 0xf4df
testl %eax, %eax
js 0x82806
leaq 0x12913(%rip), %rsi     # 0x94ec3
movq %r13, %rdi
callq 0xa550
movq %rax, (%rbp)
testq %rax, %rax
movq 0x30(%rsp), %rcx
je 0x82903
testb $0x40, 0x3(%rax)
je 0x828ae
movq %rax, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
callq 0xe3ef
testl %eax, %eax
jns 0x828ae
leaq 0x1f46a(%rip), %rsi     # 0xa1a5f
leaq 0x1f4d5(%rip), %rdx     # 0xa1ad1
movl $0x1, %edi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x13de5
jmp 0x82920
testb $0x1, %r14b
jne 0x82694
callq 0x4050
movq %rax, %r14
jmp 0x82727
leaq 0x1f435(%rip), %rsi     # 0xa1a5f
leaq 0x1f43d(%rip), %rdx     # 0xa1a6e
movl $0x1, %edi
movq %rbx, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x13de5
jmp 0x828a4
xorl %r12d, %r12d
jmp 0x82860
movq %r12, 0x8(%rsp)
callq 0x4050
movq %rax, %r12
testb $0x1, %r14b
je 0x82757
cmpl $0x2, (%r12)
jne 0x82757
movq %rbp, %rdi
callq 0xf4df
testl %eax, %eax
js 0x828da
movq %r15, %rdi
callq 0xd3cd
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %r15
callq 0x4050
movq %rax, %r14
cmpl $0x2, (%rax)
jne 0x82727
leaq 0x1f3b3(%rip), %rsi     # 0xa1a5f
leaq 0x1f3f4(%rip), %rdx     # 0xa1aa7
movl $0x4, %edi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x13de5
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x814e2
testl %eax, %eax
js 0x8289f
leaq 0x127e4(%rip), %rsi     # 0x94ec3
movq %r12, %rdi
xorl %eax, %eax
callq 0xfe44
movq %rax, %rbp
testq %rax, %rax
jne 0x823e1
movl (%r14), %edi
callq 0x4150
leaq 0x1f35b(%rip), %rsi     # 0xa1a5f
leaq 0x1f37d(%rip), %rdx     # 0xa1a88
xorl %ebp, %ebp
movl $0x1, %edi
movq %rbx, %rcx
movq %r12, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x13de5
jmp 0x828ae
movl (%r14), %edi
callq 0x4150
leaq 0x1f329(%rip), %rsi     # 0xa1a5f
leaq 0x1f34b(%rip), %rdx     # 0xa1a88
movl $0x1, %edi
movq %rbx, %rcx
movq %r12, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x13de5
jmp 0x8289f
movl (%r12), %edi
callq 0x4150
leaq 0x1f2f8(%rip), %rsi     # 0xa1a5f
leaq 0x1f31a(%rip), %rdx     # 0xa1a88
movl $0x1, %edi
movq %rbx, %rcx
movq 0x10(%rsp), %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x13de5
movq %r15, %rdi
callq 0xd3cd
jmp 0x828a4
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x1f2ba(%rip), %rsi     # 0xa1a5f
leaq 0x1ef38(%rip), %rdx     # 0xa16e4
movl $0x1, %edi
movq %rbx, %rcx
movq 0x10(%rsp), %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x13de5
jmp 0x828a4
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x1f31f(%rip), %rsi     # 0xa1afa
leaq 0x1f3d2(%rip), %rdx     # 0xa1bb4
movl $0x1, %edi
movq 0x8(%rsp), %rcx
movq %rax, %r8
jmp 0x82859
cmpl $0x4, %eax
leaq 0x1f38c(%rip), %rdx     # 0xa1b87
jne 0x82845
leaq 0x1f33a(%rip), %rdx     # 0xa1b3e
jmp 0x82845
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x1f246(%rip), %rsi     # 0xa1a5f
leaq 0x1eec4(%rip), %rdx     # 0xa16e4
movl $0x1, %edi
movq 0x30(%rsp), %rcx
movq 0x8(%rsp), %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x13de5
jmp 0x82920
leaq 0x1f2be(%rip), %rdx     # 0xa1b03
leaq 0x1f2ae(%rip), %rsi     # 0xa1afa
movl $0x1, %edi
movq 0x8(%rsp), %rcx
movq %r14, %r8
xorl %eax, %eax
callq 0x13de5
movq %r12, %rdi
callq 0x45c0
movq %rbp, %rdi
callq 0x81460
movq 0x30(%rsp), %rbx
movq 0x8(%rsp), %r12
leaq 0x1f1de(%rip), %rsi     # 0xa1a5f
leaq 0x1f22e(%rip), %rdx     # 0xa1ab6
movl $0x1, %edi
movq %rbx, %rcx
movq %r12, %r8
xorl %eax, %eax
callq 0x13de5
movq 0x28(%rsp), %rbp
testq %rbp, %rbp
je 0x828ac
movq %rbp, %rdi
callq 0xf53f
xorl %ebp, %ebp
movq 0x80(%rsp), %rdi
callq 0x45c0
movq 0x60(%rsp), %rdi
callq 0x45c0
movq %rbp, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl (%r12), %edi
callq 0x4150
leaq 0x1f175(%rip), %rsi     # 0xa1a5f
leaq 0x1edf3(%rip), %rdx     # 0xa16e4
movl $0x1, %edi
movq %rbx, %rcx
movq 0x8(%rsp), %r8
jmp 0x827b9
leaq 0x1f155(%rip), %rsi     # 0xa1a5f
leaq 0x1f15d(%rip), %rdx     # 0xa1a6e
movl $0x1, %edi
movq %r13, %r8
xorl %eax, %eax
callq 0x13de5
movq %rbp, %rdi
callq 0x81460
jmp 0x828ac