// File: /workspace/github_c_makefile_star2_O1/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 %r12d, %r12d
movq %r12, 0x80(%rsp)
movq %r12, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movl %r8d, 0x34(%rsp)
cmpl $0x1, %r8d
leaq 0x14532(%rip), %rax     # 0x98d42
leaq 0x14531(%rip), %rbx     # 0x98d48
cmoveq %rax, %rbx
testq %rdi, %rdi
je 0x84e72
movq %rdx, %r15
movq %rsi, %rbp
movq %rdi, %r13
movl %ecx, %r12d
testq %rsi, %rsi
jne 0x8485e
leaq 0x19b87(%rip), %rsi     # 0x9e3c3
xorl %r14d, %r14d
leaq 0x70(%rsp), %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x9aab
testl %eax, %eax
js 0x84a08
movq 0x80(%rsp), %rbp
testq %r15, %r15
jne 0x84889
leaq 0x1f66c(%rip), %rsi     # 0xa3ed6
xorl %r14d, %r14d
leaq 0x50(%rsp), %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0x9aab
testl %eax, %eax
js 0x84a08
movq 0x60(%rsp), %r15
movq %r13, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %rbx, 0x8(%rsp)
leaq 0x135e4(%rip), %rsi     # 0x97e83
xorl %r13d, %r13d
movq %rbp, %rdi
xorl %eax, %eax
callq 0x10014
movq %rax, %r14
testq %rax, %rax
je 0x84951
leaq 0x135c4(%rip), %rsi     # 0x97e83
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0xa4a5
testq %rax, %rax
je 0x84920
movq %rax, %r15
movq %rax, %rdi
callq 0xdb9b
xorl %r13d, %r13d
cmpl $0x2, %eax
jne 0x84913
leaq 0x13598(%rip), %rsi     # 0x97e83
xorl %r13d, %r13d
movq 0x18(%rsp), %rdi
xorl %eax, %eax
callq 0x10014
testq %rax, %rax
je 0x84c13
movq %rax, %rdi
callq 0xf6d0
testl %eax, %eax
js 0x84cd2
movq %r15, %rdi
callq 0xd4a4
movb $0x1, %r15b
jmp 0x84948
leaq 0x1f9a8(%rip), %rsi     # 0xa42cf
leaq 0x1f9b0(%rip), %rdx     # 0xa42de
xorl %r13d, %r13d
movl $0x1, %edi
movq 0x8(%rsp), %rcx
movq %rbx, %r8
xorl %eax, %eax
callq 0x14378
xorl %r15d, %r15d
testb %r15b, %r15b
je 0x84a08
testq %r14, %r14
setne %al
testl %r13d, %r13d
sete %cl
testb %cl, %al
jne 0x84a10
testb $0x1, %r12b
je 0x84bdf
callq 0x4050
cmpl $0x2, (%rax)
jne 0x84bdf
movq %rax, %r15
leaq 0x1f948(%rip), %rsi     # 0xa42cf
leaq 0x1f989(%rip), %rdx     # 0xa4317
xorl %r12d, %r12d
movl $0x4, %edi
movq 0x8(%rsp), %rcx
xorl %eax, %eax
callq 0x14378
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %rdx
callq 0x839ee
testl %eax, %eax
js 0x84e3e
leaq 0x134bd(%rip), %rsi     # 0x97e83
movq %rbp, %rdi
xorl %eax, %eax
callq 0x10014
movq %rax, %r14
testq %rax, %rax
jne 0x84a15
movl (%r15), %edi
callq 0x4150
leaq 0x1f8e8(%rip), %rsi     # 0xa42cf
leaq 0x1f90a(%rip), %rdx     # 0xa42f8
xorl %r14d, %r14d
movl $0x1, %edi
movq 0x8(%rsp), %rcx
movq %rbp, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x14378
xorl %r12d, %r12d
jmp 0x84e3e
movq 0x10(%rsp), %r13
movl $0x1, %edi
movl $0x28, %esi
callq 0x4300
testq %rax, %rax
je 0x84d4e
movq %rax, %r12
movl $0x1, %edi
movl $0x28, %esi
callq 0x4300
movq %rax, 0x18(%r12)
testq %rax, %rax
je 0x84c0b
movl $0x1, %eax
movq %rax, 0x28(%rsp)
movl $0x10000, %edi          # imm = 0x10000
movl $0x1, %esi
callq 0x4300
testq %rax, %rax
je 0x84c0b
movq %rax, %r15
movl $0x10000, %esi          # imm = 0x10000
movq %r15, %rdi
movl $0xa, %edx
movq %r14, %rcx
callq 0xf107
testq %rax, %rax
jle 0x84cb9
movq %rax, %r13
movq %rbp, %rbx
movq $0x0, 0x38(%rsp)
movb (%r15), %bpl
movq %r15, %rdi
testb %bpl, %bpl
je 0x84acc
callq 0x4510
movq (%rax), %rax
movq %r15, %rdi
movzbl %bpl, %ecx
testb $0x20, 0x1(%rax,%rcx,2)
jne 0x84acc
movb 0x1(%rdi), %bpl
incq %rdi
testb %bpl, %bpl
jne 0x84ab5
movq %rdi, %rax
subq %r15, %rax
cmpq %r13, %rax
jge 0x84add
movb $0x0, (%rdi)
incq %rdi
movq %rbx, %rbp
cmpl $0x1, 0x34(%rsp)
jne 0x84b29
leaq 0x1f871(%rip), %rsi     # 0xa435f
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 0x84b76
xorl %r13d, %r13d
movl $0x1, %edi
leaq 0x1f84d(%rip), %rsi     # 0xa436a
leaq 0x1f84f(%rip), %rdx     # 0xa4373
jmp 0x84bad
leaq 0x38(%rsp), %rax
movq %rax, (%rsp)
leaq 0x1f867(%rip), %rsi     # 0xa43a0
leaq 0x48(%rsp), %rdx
leaq 0x40(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x24(%rsp), %r9
xorl %eax, %eax
callq 0x44a0
cmpl $0x5, %eax
je 0x84b76
xorl %r13d, %r13d
movl $0x1, %edi
leaq 0x1f802(%rip), %rsi     # 0xa436a
cmpl $0x4, %eax
jne 0x84ba6
leaq 0x1f83a(%rip), %rdx     # 0xa43ae
jmp 0x84bad
movq 0x48(%rsp), %rdx
movl 0x24(%rsp), %ecx
movl 0x20(%rsp), %r8d
movq 0x40(%rsp), %r9
movq 0x38(%rsp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
movq %r15, %rsi
callq 0x85d32
testl %eax, %eax
je 0x84bbe
xorl %r13d, %r13d
jmp 0x84bd1
leaq 0x1f84a(%rip), %rdx     # 0xa43f7
movq %rbp, %rcx
movq 0x28(%rsp), %r8
xorl %eax, %eax
callq 0x14378
jmp 0x84bd1
xorl %eax, %eax
cmpb $0xa, -0x1(%r15,%r13)
sete %al
addq %rax, 0x28(%rsp)
movb $0x1, %r13b
testb %r13b, %r13b
jne 0x84a72
jmp 0x84d39
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x1f6dd(%rip), %rsi     # 0xa42cf
leaq 0x1f6ff(%rip), %rdx     # 0xa42f8
xorl %r12d, %r12d
movl $0x1, %edi
movq 0x8(%rsp), %rcx
jmp 0x84e0c
xorl %r15d, %r15d
jmp 0x84d3e
testb $0x1, %r12b
je 0x84c76
callq 0x4050
cmpl $0x2, (%rax)
jne 0x84c76
movq %rax, %rbx
movq %r14, %rdi
callq 0xf6d0
movl $0x1, %r13d
testl %eax, %eax
jns 0x84913
movl (%rbx), %edi
callq 0x4150
leaq 0x1f685(%rip), %rsi     # 0xa42cf
leaq 0x1f303(%rip), %rdx     # 0xa3f54
xorl %r15d, %r15d
movl $0x1, %r13d
movl $0x1, %edi
movq 0x8(%rsp), %rcx
movq %rbp, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x14378
jmp 0x84948
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x1f646(%rip), %rsi     # 0xa42cf
leaq 0x1f668(%rip), %rdx     # 0xa42f8
xorl %r13d, %r13d
movl $0x1, %edi
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x14378
movq %r15, %rdi
callq 0xd4a4
jmp 0x84945
js 0x84d0d
movq %r15, %rdi
callq 0x45c0
movq 0x8(%rsp), %rbx
movq 0x10(%rsp), %r13
jmp 0x84d56
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x1f5ea(%rip), %rsi     # 0xa42cf
leaq 0x1f268(%rip), %rdx     # 0xa3f54
xorl %r13d, %r13d
movl $0x1, %edi
movq 0x8(%rsp), %rcx
movq 0x18(%rsp), %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x14378
jmp 0x84945
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x1f64a(%rip), %rsi     # 0xa436a
leaq 0x1f6fd(%rip), %rdx     # 0xa4424
movl $0x1, %edi
movq %rbp, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x14378
movq 0x10(%rsp), %r13
movq %r15, %rdi
callq 0x45c0
movq %r12, %rdi
callq 0x8396c
xorl %r12d, %r12d
movq 0x8(%rsp), %rbx
testq %r12, %r12
movq 0x18(%rsp), %r15
je 0x84dc9
movq %r14, %rdi
callq 0xf6d0
testl %eax, %eax
js 0x84de7
leaq 0x13110(%rip), %rsi     # 0x97e83
movq %r13, %rdi
callq 0xa4a5
movq %rax, (%r12)
testq %rax, %rax
je 0x84e1b
testb $0x40, 0x3(%rax)
je 0x84e5b
movq %rax, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xe51c
testl %eax, %eax
jns 0x84e5b
leaq 0x1f521(%rip), %rsi     # 0xa42cf
leaq 0x1f58c(%rip), %rdx     # 0xa4341
xorl %r14d, %r14d
movl $0x1, %edi
movq %r15, %rcx
xorl %eax, %eax
callq 0x14378
jmp 0x84e3e
leaq 0x1f4ff(%rip), %rsi     # 0xa42cf
leaq 0x1f54f(%rip), %rdx     # 0xa4326
xorl %r12d, %r12d
movl $0x1, %edi
movq %rbx, %rcx
movq %rbp, %r8
jmp 0x84e37
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x1f4d5(%rip), %rsi     # 0xa42cf
leaq 0x1f153(%rip), %rdx     # 0xa3f54
xorl %r14d, %r14d
movl $0x1, %edi
movq %rbx, %rcx
movq %rbp, %r8
movq %rax, %r9
xorl %eax, %eax
callq 0x14378
jmp 0x84e3e
leaq 0x1f4ad(%rip), %rsi     # 0xa42cf
leaq 0x1f4b5(%rip), %rdx     # 0xa42de
xorl %r14d, %r14d
movl $0x1, %edi
movq %rbx, %rcx
movq %r13, %r8
xorl %eax, %eax
callq 0x14378
testq %r12, %r12
je 0x84e4b
movq %r12, %rdi
callq 0x8396c
testq %r14, %r14
je 0x84e58
movq %r14, %rdi
callq 0xf730
xorl %r12d, %r12d
movq 0x80(%rsp), %rdi
callq 0x45c0
movq 0x60(%rsp), %rdi
callq 0x45c0
movq %r12, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq