// File: /workspace/github_c_makefile_star2_O3/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 $0x98, %rsp
xorl %r14d, %r14d
testq %rdi, %rdi
je 0x1b1b5
movq %rdx, %rbx
movl %esi, %r13d
movq %rdi, %r15
callq 0x101ec
testl %eax, %eax
je 0x1aedb
movq %r15, %rdi
callq 0x41d0
movl %eax, %eax
incq %rax
leaq -0x2(%rax), %rcx
testl %ecx, %ecx
js 0x1b1b5
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
decq %rax
cmpb $0x2f, (%r15,%rcx)
jne 0x1aeb7
movq %rbx, (%rsp)
movl %eax, %eax
jmp 0x1af1f
leaq 0x7a791(%rip), %rsi     # 0x95673
movl $0x11, %edx
movq %r15, %rdi
callq 0x4210
testl %eax, %eax
movq %rbx, (%rsp)
je 0x1af1a
leaq 0x7a787(%rip), %rsi     # 0x95685
movl $0x8, %edx
movq %r15, %rdi
callq 0x4210
xorl %ecx, %ecx
testl %eax, %eax
movl $0x7, %eax
cmovneq %rcx, %rax
jmp 0x1af1f
movl $0x10, %eax
addq %rax, %r15
leaq 0x7ba2a(%rip), %rsi     # 0x96953
leaq 0x7ba37(%rip), %rdx     # 0x96967
xorl %r14d, %r14d
movl $0x4, %edi
movq %r15, %rcx
xorl %eax, %eax
callq 0x13de5
movq %r15, %rdi
callq 0x41d0
movq %rax, %rbx
movq %rax, %r12
shlq $0x20, %r12
movabsq $0x600000000, %rdi      # imm = 0x600000000
addq %r12, %rdi
sarq $0x20, %rdi
movl $0x1, %esi
callq 0x4300
testq %rax, %rax
je 0x1b1b5
movq %rax, %rbp
movq %rax, %r14
movq %rbp, %rdi
movq %r15, %rsi
callq 0x4460
sarq $0x20, %r12
movl $0x6973632e, (%rbp,%r12) # imm = 0x6973632E
movb $0x0, 0x4(%rbp,%r12)
leaq 0x8(%rsp), %rsi
movq %rbp, %rdi
callq 0x4420
testl %eax, %eax
je 0x1b1a8
cmpl $0x2, %ebx
jl 0x1afeb
movl %ebx, %eax
decl %eax
cmpb $0x2e, (%r14,%rax)
je 0x1afc8
cmpl $0x2, %eax
jge 0x1afb8
jmp 0x1afeb
movb $0x0, 0x4(%r14,%rax)
movl $0x6973632e, (%r14,%rax) # imm = 0x6973632E
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testl %eax, %eax
je 0x1b1a8
decl %r13d
cmpl $0x3, %r13d
ja 0x1b19b
addq %r12, %rbp
leaq 0x7a946(%rip), %rax     # 0x95948
movslq (%rax,%r13,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq %r14, %rdi
movq %r15, %rsi
callq 0x4460
movl $0x6961622e, (%rbp)     # imm = 0x6961622E
movb $0x0, 0x4(%rbp)
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testl %eax, %eax
je 0x1b1a8
cmpl $0x2, %ebx
jl 0x1b19b
decl %ebx
cmpb $0x2e, (%r14,%rbx)
jne 0x1b036
movb $0x0, 0x4(%r14,%rbx)
movl $0x6961622e, (%r14,%rbx) # imm = 0x6961622E
jmp 0x1b18a
movq %r14, %rdi
movq %r15, %rsi
callq 0x4460
movl $0x6172632e, (%rbp)     # imm = 0x6172632E
movw $0x69, 0x4(%rbp)
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testl %eax, %eax
je 0x1b1a8
cmpl $0x2, %ebx
jl 0x1b19b
decl %ebx
cmpb $0x2e, (%r14,%rbx)
jne 0x1b088
movw $0x69, 0x4(%r14,%rbx)
movl $0x6172632e, (%r14,%rbx) # imm = 0x6172632E
jmp 0x1b18a
movq %r14, %rdi
movq %r15, %rsi
callq 0x4460
movl $0x1, %r13d
cmpl $0x4, %ebx
jl 0x1b11d
leaq (%r15,%r12), %rdi
addq $-0x3, %rdi
leaq 0x7b1a1(%rip), %rsi     # 0x96275
callq 0x46c0
testl %eax, %eax
je 0x1b0fc
cmpl $0x6, %ebx
jb 0x1b11d
addq %r12, %r15
addq $-0x5, %r15
leaq 0x7b891(%rip), %rsi     # 0x96981
movq %r15, %rdi
callq 0x46c0
testl %eax, %eax
jne 0x1b11d
movb $0x0, 0x4(%rbp)
movl $0x697a672e, (%rbp)     # imm = 0x697A672E
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
xorl %r13d, %r13d
testl %eax, %eax
sete %r13b
movb $0x0, 0x4(%rbp)
movl $0x6961662e, (%rbp)     # imm = 0x6961662E
movq (%rsp), %rax
movq %r14, (%rax)
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
xorl %r14d, %r14d
testl %eax, %eax
cmovel %r13d, %r14d
jmp 0x1b1b5
movq %r14, %rdi
movq %r15, %rsi
callq 0x4460
movl $0x6962742e, (%rbp)     # imm = 0x6962742E
movb $0x0, 0x4(%rbp)
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testl %eax, %eax
je 0x1b1a8
cmpl $0x2, %ebx
jl 0x1b19b
decl %ebx
cmpb $0x2e, (%r14,%rbx)
jne 0x1b16e
movb $0x0, 0x4(%r14,%rbx)
movl $0x6962742e, (%r14,%rbx) # imm = 0x6962742E
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testl %eax, %eax
je 0x1b1a8
movq %r14, %rdi
callq 0x45c0
xorl %r14d, %r14d
jmp 0x1b1b5
movq (%rsp), %rax
movq %r14, (%rax)
movl $0x1, %r14d
movl %r14d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq