// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/hts.c
// Function: hts_idx_check_local
int hts_idx_check_local(const char *fn, int fmt, char **fnidx) {
    int i, l_fn, l_ext;
    const char *fn_tmp = NULL;
    char *fnidx_tmp;
    const char *csi_ext = ".csi";
    const char *bai_ext = ".bai";
    const char *tbi_ext = ".tbi";
    const char *crai_ext = ".crai";
    const char *fai_ext = ".fai";
    const char *gzi_ext = ".gzi";

    if (!fn)
        return 0;

    if (hisremote(fn)) {
        for (i = strlen(fn) - 1; i >= 0; --i)
            if (fn[i] == '/') {
                fn_tmp = (char *)&fn[i+1];
                break;
            }
    } else {
        // Borrowed from hopen_fd_fileuri()
        if (strncmp(fn, "file://localhost/", 17) == 0) fn_tmp = fn + 16;
        else if (strncmp(fn, "file:///", 8) == 0) fn_tmp = fn + 7;
        else fn_tmp = fn;
#if defined(_WIN32) || defined(__MSYS__)
        // For cases like C:/foo
        if (fn_tmp[0] == '/' && fn_tmp[1] && fn_tmp[2] == ':' && fn_tmp[3] == '/')
            fn_tmp++;
#endif
    }

    if (!fn_tmp) return 0;
    hts_log_info("Using alignment file '%s'", fn_tmp);
    l_fn = strlen(fn_tmp); l_ext = 5;
    fnidx_tmp = (char*)calloc(l_fn + l_ext + 1, 1);
    if (!fnidx_tmp) return 0;

    struct stat sbuf;

    // Try alignment.bam.csi first
    strcpy(fnidx_tmp, fn_tmp); strcpy(fnidx_tmp + l_fn, csi_ext);
    if(stat(fnidx_tmp, &sbuf) == 0) {
        *fnidx = fnidx_tmp;
        return 1;
    } else { // Then try alignment.csi
        for (i = l_fn - 1; i > 0; --i)
            if (fnidx_tmp[i] == '.') {
                strcpy(fnidx_tmp + i, csi_ext);
                if(stat(fnidx_tmp, &sbuf) == 0) {
                    *fnidx = fnidx_tmp;
                    return 1;
                }
                break;
            }
    }
    if (fmt == HTS_FMT_BAI) {
        // Next, try alignment.bam.bai
        strcpy(fnidx_tmp, fn_tmp); strcpy(fnidx_tmp + l_fn, bai_ext);
        if(stat(fnidx_tmp, &sbuf) == 0) {
            *fnidx = fnidx_tmp;
            return 1;
        } else { // And finally, try alignment.bai
            for (i = l_fn - 1; i > 0; --i)
                if (fnidx_tmp[i] == '.') {
                    strcpy(fnidx_tmp + i, bai_ext);
                    if(stat(fnidx_tmp, &sbuf) == 0) {
                        *fnidx = fnidx_tmp;
                        return 1;
                    }
                    break;
                }
        }
    } else if (fmt == HTS_FMT_TBI) { // Or .tbi
        strcpy(fnidx_tmp, fn_tmp); strcpy(fnidx_tmp + l_fn, tbi_ext);
        if(stat(fnidx_tmp, &sbuf) == 0) {
            *fnidx = fnidx_tmp;
            return 1;
        } else {
            for (i = l_fn - 1; i > 0; --i)
                if (fnidx_tmp[i] == '.') {
                    strcpy(fnidx_tmp + i, tbi_ext);
                    if(stat(fnidx_tmp, &sbuf) == 0) {
                        *fnidx = fnidx_tmp;
                        return 1;
                    }
                    break;
                }
        }
    } else if (fmt == HTS_FMT_CRAI) { // Or .crai
        strcpy(fnidx_tmp, fn_tmp); strcpy(fnidx_tmp + l_fn, crai_ext);
        if(stat(fnidx_tmp, &sbuf) == 0) {
            *fnidx = fnidx_tmp;
            return 1;
        } else {
            for (i = l_fn - 1; i > 0; --i)
                if (fnidx_tmp[i] == '.') {
                    strcpy(fnidx_tmp + i, crai_ext);
                    if(stat(fnidx_tmp, &sbuf) == 0) {
                        *fnidx = fnidx_tmp;
                        return 1;
                    }
                    break;
                }
        }
    } else if (fmt == HTS_FMT_FAI) { // Or .fai
        // Check .gzi if we have a .gz file
        strcpy(fnidx_tmp, fn_tmp);
        int gzi_ok = 1;
        if ((l_fn > 3 && strcmp(fn_tmp+l_fn-3, ".gz") == 0) ||
            (l_fn > 5 && strcmp(fn_tmp+l_fn-5, ".bgzf") == 0)) {
            strcpy(fnidx_tmp + l_fn, gzi_ext);
            gzi_ok = stat(fnidx_tmp, &sbuf)==0;
        }

        // Now check for .fai.  Occurs second as we're returning this
        // in *fnidx irrespective of whether we did gzi check.
        strcpy(fnidx_tmp + l_fn, fai_ext);
        *fnidx = fnidx_tmp;
        if (stat(fnidx_tmp, &sbuf) == 0)
            return gzi_ok;
        else
            return 0;
    }

    free(fnidx_tmp);
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
xorl %ebp, %ebp
testq %rdi, %rdi
je 0x172a6
movq %rdx, %rbx
movl %esi, %r12d
movq %rdi, %r15
callq 0xe4f2
testl %eax, %eax
je 0x16fe1
movq %r15, %rdi
callq 0x41d0
movl %eax, %eax
incq %rax
leaq -0x2(%rax), %rcx
testl %ecx, %ecx
js 0x172a6
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
decq %rax
cmpb $0x2f, (%r15,%rcx)
jne 0x16fbc
movq %rbx, 0x8(%rsp)
movl %eax, %eax
jmp 0x1701e
leaq 0x6063b(%rip), %rsi     # 0x77623
pushq $0x11
popq %rdx
movq %r15, %rdi
callq 0x4210
testl %eax, %eax
movq %rbx, 0x8(%rsp)
je 0x1701b
leaq 0x60632(%rip), %rsi     # 0x77635
pushq $0x8
popq %rdx
movq %r15, %rdi
callq 0x4210
xorl %ecx, %ecx
testl %eax, %eax
pushq $0x7
popq %rax
cmovneq %rcx, %rax
jmp 0x1701e
pushq $0x10
popq %rax
addq %rax, %r15
leaq 0x6191c(%rip), %rsi     # 0x78944
leaq 0x61929(%rip), %rdx     # 0x78958
xorl %ebp, %ebp
pushq $0x4
popq %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0x110c6
movq %r15, %rdi
callq 0x41d0
movq %rax, %rbx
movq %rax, %r13
shlq $0x20, %r13
movabsq $0x600000000, %rdi      # imm = 0x600000000
addq %r13, %rdi
sarq $0x20, %rdi
pushq $0x1
popq %rsi
callq 0x4300
testq %rax, %rax
je 0x172a6
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
callq 0x4460
sarq $0x20, %r13
movl $0x6973632e, (%r14,%r13) # imm = 0x6973632E
movb $0x0, 0x4(%r14,%r13)
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testl %eax, %eax
je 0x1729b
movq %r14, %rax
addq %r13, %rax
movq %rax, 0x10(%rsp)
leal -0x1(%rbx), %ebp
movl %ebp, %eax
testl %eax, %eax
jle 0x170e9
movl %eax, %ecx
decl %eax
cmpb $0x2e, (%r14,%rcx)
jne 0x170b7
movb $0x0, 0x4(%r14,%rcx)
movl $0x6973632e, (%r14,%rcx) # imm = 0x6973632E
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testl %eax, %eax
je 0x1729b
decl %r12d
cmpl $0x3, %r12d
ja 0x1728f
leaq 0x607fb(%rip), %rax     # 0x778f8
movslq (%rax,%r12,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq %r14, %rdi
movq %r15, %rsi
callq 0x4460
movq 0x10(%rsp), %rax
movl $0x6961622e, (%rax)     # imm = 0x6961622E
movb $0x0, 0x4(%rax)
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testl %eax, %eax
je 0x1729b
testl %ebp, %ebp
jle 0x1728f
movl %ebp, %eax
decl %ebp
cmpb $0x2e, (%r14,%rax)
jne 0x17135
movb $0x0, 0x4(%r14,%rax)
movl $0x6961622e, (%r14,%rax) # imm = 0x6961622E
jmp 0x1727e
movq %r14, %rdi
movq %r15, %rsi
callq 0x4460
movq 0x10(%rsp), %rax
movl $0x6172632e, (%rax)     # imm = 0x6172632E
movw $0x69, 0x4(%rax)
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testl %eax, %eax
je 0x1729b
testl %ebp, %ebp
jle 0x1728f
movl %ebp, %eax
decl %ebp
cmpb $0x2e, (%r14,%rax)
jne 0x1718c
movw $0x69, 0x4(%r14,%rax)
movl $0x6172632e, (%r14,%rax) # imm = 0x6172632E
jmp 0x1727e
movq %r14, %rdi
movq %r15, %rsi
callq 0x4460
pushq $0x1
popq %r12
cmpl $0x4, %ebx
jl 0x172ba
leaq (%r15,%r13), %rdi
addq $-0x3, %rdi
leaq 0x6108b(%rip), %rsi     # 0x78266
callq 0x46c0
testl %eax, %eax
movq 0x10(%rsp), %rbp
je 0x17210
cmpl $0x6, %ebx
jb 0x172bf
addq %r13, %r15
addq $-0x5, %r15
leaq 0x61772(%rip), %rsi     # 0x78972
movq %r15, %rdi
callq 0x46c0
testl %eax, %eax
jne 0x172bf
movb $0x0, 0x4(%rbp)
movl $0x697a672e, (%rbp)     # imm = 0x697A672E
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
xorl %r12d, %r12d
testl %eax, %eax
sete %r12b
jmp 0x172bf
movq %r14, %rdi
movq %r15, %rsi
callq 0x4460
movq 0x10(%rsp), %rax
movl $0x6962742e, (%rax)     # imm = 0x6962742E
movb $0x0, 0x4(%rax)
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testl %eax, %eax
je 0x1729b
testl %ebp, %ebp
jle 0x1728f
movl %ebp, %eax
decl %ebp
cmpb $0x2e, (%r14,%rax)
jne 0x17261
movb $0x0, 0x4(%r14,%rax)
movl $0x6962742e, (%r14,%rax) # imm = 0x6962742E
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testl %eax, %eax
je 0x1729b
movq %r14, %rdi
callq 0x45c0
xorl %ebp, %ebp
jmp 0x172a6
movq 0x8(%rsp), %rax
movq %r14, (%rax)
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rbp
movb $0x0, 0x4(%rbp)
movl $0x6961662e, (%rbp)     # imm = 0x6961662E
movq 0x8(%rsp), %rax
movq %r14, (%rax)
leaq 0x18(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
xorl %ebp, %ebp
testl %eax, %eax
cmovel %r12d, %ebp
jmp 0x172a6