// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
leaq -0x40(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x4250
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x4250
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movl $0x0, -0x70(%rbp)
cmpl $0x1, -0x28(%rbp)
jne 0xd3e98
leaq 0x1c180(%rip), %rax     # 0xf0012
movq %rax, -0x78(%rbp)
jmp 0xd3ea3
leaq 0x1c179(%rip), %rax     # 0xf0018
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0xd3eb7
movq $0x0, -0x8(%rbp)
jmp 0xd43cc
cmpq $0x0, -0x18(%rbp)
jne 0xd3ee6
movq -0x10(%rbp), %rdx
leaq -0x40(%rbp), %rdi
leaq 0x21ec1(%rip), %rsi     # 0xf5d8e
movb $0x0, %al
callq 0xb040
cmpl $0x0, %eax
jge 0xd3ede
jmp 0xd4392
movq -0x30(%rbp), %rax
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0xd3f15
movq -0x10(%rbp), %rdx
leaq -0x58(%rbp), %rdi
leaq 0x23a46(%rip), %rsi     # 0xf7942
movb $0x0, %al
callq 0xb040
cmpl $0x0, %eax
jge 0xd3f0d
jmp 0xd4392
movq -0x48(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
leaq 0x1b168(%rip), %rsi     # 0xef088
movb $0x0, %al
callq 0x158b0
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
je 0xd410f
movq $0x0, -0x80(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x1b13f(%rip), %rsi     # 0xef088
callq 0xc090
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x88(%rbp)
jne 0xd3f86
movq -0x78(%rbp), %rcx
movq -0x10(%rbp), %r8
movl $0x1, %edi
leaq 0x23dc8(%rip), %rsi     # 0xf7d3b
leaq 0x23dd0(%rip), %rdx     # 0xf7d4a
movb $0x0, %al
callq 0x1b030
jmp 0xd4392
movq -0x88(%rbp), %rdi
callq 0x10de0
cmpl $0x2, %eax
jne 0xd4103
movq -0x20(%rbp), %rdi
leaq 0x1b0e2(%rip), %rsi     # 0xef088
movb $0x0, %al
callq 0x158b0
movq %rax, -0x80(%rbp)
cmpq $0x0, %rax
jne 0xd409b
movl -0x24(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xd3fd0
callq 0x4050
cmpl $0x2, (%rax)
je 0xd402e
movq -0x78(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x90(%rbp)
callq 0x4050
movl (%rax), %edi
callq 0x4130
movq -0x98(%rbp), %rcx
movq -0x90(%rbp), %r8
movq %rax, %r9
movl $0x1, %edi
leaq 0x23d2c(%rip), %rsi     # 0xf7d3b
leaq 0x23d4e(%rip), %rdx     # 0xf7d64
movb $0x0, %al
callq 0x1b030
movq -0x88(%rbp), %rdi
callq 0x103d0
jmp 0xd4392
movl $0x1, -0x70(%rbp)
movq -0x60(%rbp), %rdi
callq 0x14920
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
jge 0xd4099
movq -0x78(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xa0(%rbp)
callq 0x4050
movl (%rax), %edi
callq 0x4130
movq -0xa8(%rbp), %rcx
movq -0xa0(%rbp), %r8
movq %rax, %r9
movl $0x1, %edi
leaq 0x23cb5(%rip), %rsi     # 0xf7d3b
leaq 0x23933(%rip), %rdx     # 0xf79c0
movb $0x0, %al
callq 0x1b030
jmp 0xd4392
jmp 0xd4101
movq -0x80(%rbp), %rdi
callq 0x14920
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
jge 0xd40ff
movq -0x78(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xb0(%rbp)
callq 0x4050
movl (%rax), %edi
callq 0x4130
movq -0xb8(%rbp), %rcx
movq -0xb0(%rbp), %r8
movq %rax, %r9
movl $0x1, %edi
leaq 0x23c4f(%rip), %rsi     # 0xf7d3b
leaq 0x238cd(%rip), %rdx     # 0xf79c0
movb $0x0, %al
callq 0x1b030
jmp 0xd4392
jmp 0xd4101
jmp 0xd4103
movq -0x88(%rbp), %rdi
callq 0x103d0
cmpq $0x0, -0x60(%rbp)
je 0xd4120
cmpl $0x0, -0x70(%rbp)
je 0xd4231
movl -0x24(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0xd4135
callq 0x4050
cmpl $0x2, (%rax)
je 0xd4187
movq -0x78(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xc0(%rbp)
callq 0x4050
movl (%rax), %edi
callq 0x4130
movq -0xc8(%rbp), %rcx
movq -0xc0(%rbp), %r8
movq %rax, %r9
movl $0x1, %edi
leaq 0x23bc7(%rip), %rsi     # 0xf7d3b
leaq 0x23be9(%rip), %rdx     # 0xf7d64
movb $0x0, %al
callq 0x1b030
jmp 0xd4392
movq -0x78(%rbp), %rcx
movl $0x4, %edi
leaq 0x23ba4(%rip), %rsi     # 0xf7d3b
leaq 0x23be5(%rip), %rdx     # 0xf7d83
movb $0x0, %al
callq 0x1b030
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0xd39b0
cmpl $0x0, %eax
jge 0xd41c0
jmp 0xd4392
movq -0x18(%rbp), %rdi
leaq 0x1aebd(%rip), %rsi     # 0xef088
movb $0x0, %al
callq 0x158b0
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0xd422f
movq -0x78(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xd0(%rbp)
callq 0x4050
movl (%rax), %edi
callq 0x4130
movq -0xd8(%rbp), %rcx
movq -0xd0(%rbp), %r8
movq %rax, %r9
movl $0x1, %edi
leaq 0x23b1f(%rip), %rsi     # 0xf7d3b
leaq 0x23b41(%rip), %rdx     # 0xf7d64
movb $0x0, %al
callq 0x1b030
jmp 0xd4392
jmp 0xd4231
movq -0x60(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x28(%rbp), %edx
callq 0xd6d60
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0xd4273
movq -0x78(%rbp), %rcx
movq -0x18(%rbp), %r8
movl $0x1, %edi
leaq 0x23adb(%rip), %rsi     # 0xf7d3b
leaq 0x23b2b(%rip), %rdx     # 0xf7d92
movb $0x0, %al
callq 0x1b030
jmp 0xd4392
movq -0x60(%rbp), %rdi
callq 0x14920
movl %eax, -0x6c(%rbp)
movq $0x0, -0x60(%rbp)
cmpl $0x0, -0x6c(%rbp)
jge 0xd42df
movq -0x78(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xe0(%rbp)
callq 0x4050
movl (%rax), %edi
callq 0x4130
movq -0xe8(%rbp), %rcx
movq -0xe0(%rbp), %r8
movq %rax, %r9
movl $0x1, %edi
leaq 0x23a6f(%rip), %rsi     # 0xf7d3b
leaq 0x236ed(%rip), %rdx     # 0xf79c0
movb $0x0, %al
callq 0x1b030
jmp 0xd4392
movq -0x10(%rbp), %rdi
leaq 0x1ad9e(%rip), %rsi     # 0xef088
callq 0xc090
movq %rax, %rcx
movq -0x68(%rbp), %rax
movq %rcx, (%rax)
movq -0x68(%rbp), %rax
cmpq $0x0, (%rax)
jne 0xd4327
movq -0x78(%rbp), %rcx
movq -0x10(%rbp), %r8
movl $0x1, %edi
leaq 0x23a24(%rip), %rsi     # 0xf7d3b
leaq 0x23a2c(%rip), %rdx     # 0xf7d4a
movb $0x0, %al
callq 0x1b030
jmp 0xd4392
movq -0x68(%rbp), %rax
movq (%rax), %rax
movl (%rax), %eax
shrl $0x1e, %eax
andl $0x1, %eax
cmpl $0x1, %eax
jne 0xd4376
movq -0x68(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x118d0
cmpl $0x0, %eax
jge 0xd4374
movq -0x20(%rbp), %rcx
movl $0x1, %edi
leaq 0x239d7(%rip), %rsi     # 0xf7d3b
leaq 0x23a42(%rip), %rdx     # 0xf7dad
movb $0x0, %al
callq 0x1b030
jmp 0xd4392
jmp 0xd4376
movq -0x30(%rbp), %rdi
callq 0x45a0
movq -0x48(%rbp), %rdi
callq 0x45a0
movq -0x68(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xd43cc
cmpq $0x0, -0x68(%rbp)
je 0xd43a2
movq -0x68(%rbp), %rdi
callq 0xd38a0
cmpq $0x0, -0x60(%rbp)
je 0xd43b2
movq -0x60(%rbp), %rdi
callq 0x149d0
movq -0x30(%rbp), %rdi
callq 0x45a0
movq -0x48(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax)