// File: /workspace/github_c_makefile_star2_O2/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 $0x98, %rsp
andq $0x0, 0x90(%rsp)
xorps %xmm0, %xmm0
andq $0x0, 0x70(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x60(%rsp)
cmpl $0x1, %r8d
leaq 0x103ca(%rip), %rax     # 0x77dcb
leaq 0x103c9(%rip), %rbx     # 0x77dd1
cmoveq %rax, %rbx
testq %rdi, %rdi
je 0x67c82
movl %ecx, %r12d
movq %rdx, %rbp
movq %rsi, %r13
movq %rdi, %r14
movl %r8d, 0x3c(%rsp)
testq %rsi, %rsi
jne 0x67a57
leaq 0x15984(%rip), %rsi     # 0x7d3b6
xorl %r15d, %r15d
leaq 0x80(%rsp), %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x8c78
testl %eax, %eax
js 0x67f66
movq 0x90(%rsp), %r13
testq %rbp, %rbp
jne 0x67a82
leaq 0x1b4f3(%rip), %rsi     # 0x82f56
xorl %r15d, %r15d
leaq 0x60(%rsp), %rdi
movq %r14, %rdx
xorl %eax, %eax
callq 0x8c78
testl %eax, %eax
js 0x67f66
movq 0x70(%rsp), %rbp
leaq 0xf3ea(%rip), %rsi      # 0x76e73
movq %r13, %rdi
xorl %eax, %eax
callq 0xe19b
movq %rax, %r15
testq %rax, %rax
je 0x67cfc
leaq 0xf3cd(%rip), %rsi      # 0x76e73
movq %r14, %rdi
callq 0x9596
movq %r14, %r8
testq %rax, %rax
je 0x67c8a
movq %rax, %r14
movq %r8, 0x18(%rsp)
movq %rax, %rdi
callq 0xc218
cmpl $0x2, %eax
jne 0x67af9
leaq 0xf39d(%rip), %rsi      # 0x76e73
movq %rbp, %rdi
xorl %eax, %eax
callq 0xe19b
testq %rax, %rax
je 0x67cb2
movq %rax, %rdi
callq 0xd8ee
testl %eax, %eax
js 0x67e06
movq %r14, %rdi
callq 0xbc96
pushq $0x1
popq %rdi
pushq $0x28
popq %rsi
callq 0x4300
testq %rax, %rax
je 0x67f38
movq %rax, %r14
movq %rbx, 0x28(%rsp)
callq 0x689eb
movq %r14, 0x20(%rsp)
movq %rax, 0x18(%r14)
testq %rax, %rax
je 0x67caa
pushq $0x1
popq %rbx
movl $0x10000, %edi          # imm = 0x10000
movq %rbx, %rsi
callq 0x4300
testq %rax, %rax
je 0x67caa
movq %rax, %r12
movq %rbp, 0x58(%rsp)
movq %rax, 0x30(%rsp)
movl $0x10000, %esi          # imm = 0x10000
movq %r12, %rdi
pushq $0xa
popq %rdx
movq %r15, %rcx
callq 0xd394
testq %rax, %rax
jle 0x67e38
movq %rax, %r14
movq %rbx, 0x8(%rsp)
movq %r15, %rbp
movq %r13, %rbx
andq $0x0, 0x40(%rsp)
xorl %r15d, %r15d
movzbl (%r12), %r13d
testq %r13, %r13
je 0x67baf
callq 0x4510
movq (%rax), %rax
testb $0x20, 0x1(%rax,%r13,2)
jne 0x67baf
incq %r12
incq %r15
jmp 0x67b8d
cmpq %r14, %r15
jge 0x67bbc
movb $0x0, (%r12)
incq %r12
movq %rbx, %r13
movq %rbp, %r15
cmpl $0x1, 0x3c(%rsp)
movq 0x58(%rsp), %rbp
movq 0x8(%rsp), %rbx
jne 0x67c02
movq %r12, %rdi
leaq 0x1b802(%rip), %rsi     # 0x833df
leaq 0x50(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x14(%rsp), %r9
xorl %eax, %eax
callq 0x44a0
cmpl $0x4, %eax
je 0x67c39
jmp 0x67ed9
leaq 0x40(%rsp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
leaq 0x1b80b(%rip), %rsi     # 0x83420
leaq 0x50(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x14(%rsp), %r9
xorl %eax, %eax
callq 0x44a0
cmpl $0x5, %eax
jne 0x67ebf
movq 0x50(%rsp), %rdx
movl 0x14(%rsp), %ecx
movl 0x10(%rsp), %r8d
movq 0x48(%rsp), %r9
movq 0x40(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %r12
movq %r12, %rsi
callq 0x68a16
testl %eax, %eax
jne 0x67f21
xorl %eax, %eax
cmpb $0xa, -0x1(%r12,%r14)
sete %al
addq %rax, %rbx
jmp 0x67b5a
xorl %r15d, %r15d
jmp 0x67f7d
leaq 0x1b6be(%rip), %rsi     # 0x8334f
leaq 0x1b6c6(%rip), %rdx     # 0x8335e
pushq $0x1
popq %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x110c6
jmp 0x67f5b
xorl %r12d, %r12d
jmp 0x67f21
movq %r15, 0x8(%rsp)
movq %rbx, %r15
callq 0x4050
movq %rax, %rbx
testb $0x1, %r12b
je 0x67dcc
cmpl $0x2, (%rbx)
jne 0x67dcc
movq 0x8(%rsp), %rdi
callq 0xd8ee
testl %eax, %eax
js 0x67f92
movq %r14, %rdi
callq 0xbc96
movq %r15, %rbx
movq 0x8(%rsp), %r15
movq 0x18(%rsp), %r14
movq %r14, 0x18(%rsp)
callq 0x4050
movq %rax, %r14
testb $0x1, %r12b
je 0x67d9e
cmpl $0x2, (%r14)
jne 0x67d9e
leaq 0x1b62b(%rip), %rsi     # 0x8334f
leaq 0x1b66c(%rip), %rdx     # 0x83397
pushq $0x4
popq %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x110c6
movq 0x18(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x66f36
testl %eax, %eax
js 0x67f56
leaq 0xf11c(%rip), %rsi      # 0x76e73
movq %r13, %rdi
xorl %eax, %eax
callq 0xe19b
movq %rax, %r15
testq %rax, %rax
jne 0x67b01
movl (%r14), %edi
callq 0x4150
leaq 0x1b5d3(%rip), %rsi     # 0x8334f
leaq 0x1b5f5(%rip), %rdx     # 0x83378
xorl %r15d, %r15d
pushq $0x1
popq %rdi
movq %rbx, %rcx
movq %r13, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x110c6
jmp 0x67f66
movl (%r14), %edi
callq 0x4150
leaq 0x1b5a2(%rip), %rsi     # 0x8334f
leaq 0x1b5c4(%rip), %rdx     # 0x83378
pushq $0x1
popq %rdi
movq %rbx, %rcx
movq %r13, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x110c6
jmp 0x67f56
movl (%rbx), %edi
callq 0x4150
leaq 0x1b575(%rip), %rsi     # 0x8334f
leaq 0x1b597(%rip), %rdx     # 0x83378
pushq $0x1
popq %rdi
movq %r15, %rcx
movq %rbp, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x110c6
movq %r14, %rdi
callq 0xbc96
movq 0x8(%rsp), %r15
jmp 0x67f5b
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x1b536(%rip), %rsi     # 0x8334f
leaq 0x1b1b4(%rip), %rdx     # 0x82fd4
pushq $0x1
popq %rdi
movq %rbx, %rcx
movq %rbp, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x110c6
jmp 0x67f5b
js 0x67ef7
movq %r12, %rdi
callq 0x45c0
movq %r15, %rdi
callq 0xd8ee
testl %eax, %eax
js 0x67fbf
leaq 0xf016(%rip), %rsi      # 0x76e73
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x9596
movq 0x20(%rsp), %r15
movq %rax, (%r15)
testq %rax, %rax
movq 0x28(%rsp), %rcx
je 0x67ff5
testb $0x40, 0x3(%rax)
je 0x67f66
movq %rax, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0xc898
testl %eax, %eax
jns 0x67f66
leaq 0x1b4a9(%rip), %rsi     # 0x8334f
leaq 0x1b514(%rip), %rdx     # 0x833c1
pushq $0x1
popq %rdi
movq %rbp, %rcx
xorl %eax, %eax
callq 0x110c6
jmp 0x68010
cmpl $0x4, %eax
movq 0x30(%rsp), %r12
leaq 0x1b5a9(%rip), %rdx     # 0x83477
jne 0x67ee5
leaq 0x1b557(%rip), %rdx     # 0x8342e
jmp 0x67ee5
leaq 0x1b513(%rip), %rdx     # 0x833f3
movq 0x30(%rsp), %r12
leaq 0x1b4fe(%rip), %rsi     # 0x833ea
pushq $0x1
popq %rdi
movq %r13, %rcx
movq %rbx, %r8
jmp 0x67f1a
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x1b4e0(%rip), %rsi     # 0x833ea
leaq 0x1b593(%rip), %rdx     # 0x834a4
pushq $0x1
popq %rdi
movq %r13, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x110c6
movq %r12, %rdi
callq 0x45c0
movq 0x20(%rsp), %rdi
callq 0x66ebc
movq 0x28(%rsp), %rbx
leaq 0x1b410(%rip), %rsi     # 0x8334f
leaq 0x1b460(%rip), %rdx     # 0x833a6
pushq $0x1
popq %rdi
movq %rbx, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x110c6
testq %r15, %r15
je 0x67f63
movq %r15, %rdi
callq 0xd94c
xorl %r15d, %r15d
movq 0x90(%rsp), %rdi
callq 0x45c0
movq 0x70(%rsp), %rdi
callq 0x45c0
movq %r15, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl (%rbx), %edi
callq 0x4150
leaq 0x1b3af(%rip), %rsi     # 0x8334f
leaq 0x1b02d(%rip), %rdx     # 0x82fd4
pushq $0x1
popq %rdi
movq %r15, %rcx
movq %r13, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x110c6
jmp 0x67dfc
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x1b37d(%rip), %rsi     # 0x8334f
leaq 0x1affb(%rip), %rdx     # 0x82fd4
pushq $0x1
popq %rdi
movq 0x28(%rsp), %rcx
movq %r13, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x110c6
movq 0x20(%rsp), %r15
jmp 0x68010
leaq 0x1b353(%rip), %rsi     # 0x8334f
leaq 0x1b35b(%rip), %rdx     # 0x8335e
pushq $0x1
popq %rdi
movq %rbx, %r8
xorl %eax, %eax
callq 0x110c6
movq %r15, %rdi
callq 0x66ebc
jmp 0x67f63