// File: /workspace/github_c_makefile_star2_O1/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 %ebp, %ebp
testq %rdi, %rdi
je 0x1b6d8
movq %rdx, %r14
movq %rdi, %rbx
movq %rsi, (%rsp)
callq 0x103cd
testl %eax, %eax
je 0x1b414
movq %rbx, %rdi
callq 0x41d0
movl %eax, %eax
incq %rax
xorl %r13d, %r13d
leaq -0x2(%rax), %rcx
testl %ecx, %ecx
js 0x1b453
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
decq %rax
cmpb $0x2f, (%rbx,%rcx)
jne 0x1b3f6
movl %eax, %eax
addq %rax, %rbx
jmp 0x1b450
leaq 0x7d218(%rip), %rsi     # 0x98633
movl $0x11, %edx
movq %rbx, %rdi
callq 0x4210
testl %eax, %eax
je 0x1b44c
leaq 0x7d212(%rip), %rsi     # 0x98645
movl $0x8, %edx
movq %rbx, %rdi
callq 0x4210
leaq 0x7(%rbx), %r13
testl %eax, %eax
cmovneq %rbx, %r13
jmp 0x1b453
addq $0x10, %rbx
movq %rbx, %r13
testq %r13, %r13
je 0x1b6d8
leaq 0x7e450(%rip), %rsi     # 0x998b3
leaq 0x7e45d(%rip), %rdx     # 0x998c7
xorl %ebp, %ebp
movl $0x4, %edi
movq %r13, %rcx
xorl %eax, %eax
callq 0x14378
movq %r13, %rdi
callq 0x41d0
movq %rax, %r12
movq %rax, %r15
shlq $0x20, %r15
movabsq $0x600000000, %rdi      # imm = 0x600000000
addq %r15, %rdi
sarq $0x20, %rdi
movl $0x1, %esi
callq 0x4300
testq %rax, %rax
je 0x1b6d8
movq %rax, %rbx
movq %r14, %rbp
movq %rax, %r14
movq %rbx, %rdi
movq %r13, %rsi
callq 0x4460
sarq $0x20, %r15
movl $0x6973632e, (%rbx,%r15) # imm = 0x6973632E
movb $0x0, 0x4(%rbx,%r15)
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x4420
testl %eax, %eax
je 0x1b6cf
cmpl $0x2, %r12d
movq (%rsp), %rcx
jl 0x1b530
movl %r12d, %eax
decl %eax
cmpb $0x2e, (%r14,%rax)
je 0x1b509
cmpl $0x2, %eax
jge 0x1b4f9
jmp 0x1b530
movb $0x0, 0x4(%r14,%rax)
movl $0x6973632e, (%r14,%rax) # imm = 0x6973632E
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
movq (%rsp), %rcx
testl %eax, %eax
je 0x1b6cf
decl %ecx
cmpl $0x3, %ecx
ja 0x1b6c3
addq %r15, %rbx
leaq 0x7d353(%rip), %rax     # 0x98898
movslq (%rax,%rcx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movq %r14, %rdi
movq %r13, %rsi
callq 0x4460
movl $0x6961622e, (%rbx)     # imm = 0x6961622E
movb $0x0, 0x4(%rbx)
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testl %eax, %eax
je 0x1b6cf
cmpl $0x2, %r12d
jl 0x1b6c3
decl %r12d
cmpb $0x2e, (%r14,%r12)
jne 0x1b578
movb $0x0, 0x4(%r14,%r12)
movl $0x6961622e, (%r14,%r12) # imm = 0x6961622E
jmp 0x1b6b2
movq %r14, %rdi
movq %r13, %rsi
callq 0x4460
movl $0x6172632e, (%rbx)     # imm = 0x6172632E
movw $0x69, 0x4(%rbx)
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testl %eax, %eax
je 0x1b6cf
cmpl $0x2, %r12d
jl 0x1b6c3
decl %r12d
cmpb $0x2e, (%r14,%r12)
jne 0x1b5cb
movw $0x69, 0x4(%r14,%r12)
movl $0x6172632e, (%r14,%r12) # imm = 0x6172632E
jmp 0x1b6b2
movq %r14, %rdi
movq %r13, %rsi
callq 0x4460
cmpl $0x4, %r12d
jl 0x1b61d
leaq (%r15,%r13), %rdi
addq $-0x3, %rdi
leaq 0x7dbc1(%rip), %rsi     # 0x991d5
callq 0x46c0
testl %eax, %eax
je 0x1b649
cmpl $0x6, %r12d
jl 0x1b6ec
leaq (%r15,%r13), %rdi
addq $-0x5, %rdi
leaq 0x7e2ab(%rip), %rsi     # 0x998e1
callq 0x46c0
testl %eax, %eax
movl $0x1, %r15d
jne 0x1b6f2
movb $0x0, 0x4(%rbx)
movl $0x697a672e, (%rbx)     # imm = 0x697A672E
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
xorl %r15d, %r15d
testl %eax, %eax
sete %r15b
jmp 0x1b6f2
movq %r14, %rdi
movq %r13, %rsi
callq 0x4460
movl $0x6962742e, (%rbx)     # imm = 0x6962742E
movb $0x0, 0x4(%rbx)
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testl %eax, %eax
je 0x1b6cf
cmpl $0x2, %r12d
jl 0x1b6c3
decl %r12d
cmpb $0x2e, (%r14,%r12)
jne 0x1b694
movb $0x0, 0x4(%r14,%r12)
movl $0x6962742e, (%r14,%r12) # imm = 0x6962742E
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
testl %eax, %eax
je 0x1b6cf
movq %r14, %rdi
callq 0x45c0
xorl %ebp, %ebp
jmp 0x1b6d8
movq %r14, (%rbp)
movl $0x1, %ebp
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %r15d
movb $0x0, 0x4(%rbx)
movl $0x6961662e, (%rbx)     # imm = 0x6961662E
movq %r14, (%rbp)
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x4420
xorl %ebp, %ebp
testl %eax, %eax
cmovel %r15d, %ebp
jmp 0x1b6d8