// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/hts.c
// Function: idx_test_and_fetch
static int idx_test_and_fetch(const char *fn, const char **local_fn, int *local_len, int download)
{
    hFILE *remote_hfp = NULL;
    hFILE *local_fp = NULL;
    int save_errno;
    htsFormat fmt;
    kstring_t s = KS_INITIALIZE;
    kstring_t tmps = KS_INITIALIZE;

    if (hisremote(fn)) {
        const int buf_size = 1 * 1024 * 1024;
        int l;
        const char *p, *e;
        // Ignore ?# params: eg any file.fmt?param=val, except for S3 URLs
        e = fn + ((strncmp(fn, "s3://", 5) && strncmp(fn, "s3+http://", 10) && strncmp(fn, "s3+https://", 11)) ? strcspn(fn, "?#") : strcspn(fn, "?"));
        // Find the previous slash from there.
        p = e;
        while (p > fn && *p != '/') p--;
        if (*p == '/') p++;

        // Attempt to open local file first
        kputsn(p, e-p, &s);
        if (access(s.s, R_OK) == 0)
        {
            free(s.s);
            *local_fn = p;
            *local_len = e-p;
            return 0;
        }

        // Attempt to open remote file. Stay quiet on failure, it is OK to fail when trying first .csi then .bai or .tbi index.
        if ((remote_hfp = hopen(fn, "r")) == 0) {
            hts_log_info("Failed to open index file '%s'", fn);
            free(s.s);
            return -1;
        }
        if (hts_detect_format2(remote_hfp, fn, &fmt)) {
            hts_log_error("Failed to detect format of index file '%s'", fn);
            goto fail;
        }
        if (fmt.category != index_file || (fmt.format != bai &&  fmt.format != csi && fmt.format != tbi
                && fmt.format != crai && fmt.format != fai_format)) {
            hts_log_error("Format of index file '%s' is not supported", fn);
            goto fail;
        }

        if (download) {
            if ((local_fp = hts_open_tmpfile(s.s, "wx", &tmps)) == NULL) {
                hts_log_error("Failed to create file %s in the working directory", p);
                goto fail;
            }
            hts_log_info("Downloading file %s to local directory", fn);
            uint8_t *buf = (uint8_t*)calloc(buf_size, 1);
            if (!buf) {
                hts_log_error("%s", strerror(errno));
                goto fail;
            }
            while ((l = hread(remote_hfp, buf, buf_size)) > 0) {
                if (hwrite(local_fp, buf, l) != l) {
                    hts_log_error("Failed to write data to %s : %s",
                            fn, strerror(errno));
                    free(buf);
                    goto fail;
                }
            }
            free(buf);
            if (l < 0) {
                hts_log_error("Error reading \"%s\"", fn);
                goto fail;
            }
            if (hclose(local_fp) < 0) {
                hts_log_error("Error closing %s : %s", fn, strerror(errno));
                local_fp = NULL;
                goto fail;
            }
            local_fp = NULL;
            if (rename(tmps.s, s.s) < 0) {
                hts_log_error("Error renaming %s : %s", tmps.s, strerror(errno));
                goto fail;
            }
            ks_clear(&tmps);

            *local_fn = p;
            *local_len = e-p;
        } else {
            *local_fn = fn;
            *local_len = e-fn;
        }

        if (hclose(remote_hfp) != 0) {
            hts_log_error("Failed to close remote file %s", fn);
        }

        free(tmps.s);
        free(s.s);
        return 0;
    } else {
        hFILE *local_hfp;
        if ((local_hfp = hopen(fn, "r")) == 0) return -1;
        hclose_abruptly(local_hfp);
        *local_fn = fn;
        *local_len = strlen(fn);
        return 0;
    }

 fail:
    save_errno = errno;
    if (remote_hfp) hclose_abruptly(remote_hfp);
    if (local_fp) hclose_abruptly(local_fp);
    if (tmps.l > 0) unlink(tmps.s);
    free(tmps.s);
    free(s.s);
    errno = save_errno;
    return -2;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %ecx, %ebx
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
callq 0x101ec
testl %eax, %eax
je 0x1b6e4
leaq 0x79f2e(%rip), %rsi     # 0x95534
movl $0x5, %edx
movq %r14, %rdi
callq 0x4210
testl %eax, %eax
movq %r15, 0x50(%rsp)
je 0x1b655
leaq 0x79f17(%rip), %rsi     # 0x9553a
movl $0xa, %edx
movq %r14, %rdi
callq 0x4210
testl %eax, %eax
je 0x1b655
leaq 0x79f0a(%rip), %rsi     # 0x95545
movl $0xb, %edx
movq %r14, %rdi
callq 0x4210
testl %eax, %eax
je 0x1b655
leaq 0x79efe(%rip), %rsi     # 0x95551
jmp 0x1b65c
leaq 0x7c9a3(%rip), %rsi     # 0x97fff
movq %r14, %rdi
callq 0x4090
movq %rax, %rbp
leaq (%r14,%rax), %r15
movq %r15, %rax
testq %rbp, %rbp
jle 0x1b683
movq %r15, %rax
cmpb $0x2f, (%rax)
je 0x1b683
decq %rax
cmpq %r14, %rax
ja 0x1b676
movl %ebx, 0x8(%rsp)
movq %r12, 0x48(%rsp)
xorl %r12d, %r12d
cmpb $0x2f, (%rax)
sete %r12b
addq %rax, %r12
subq %r12, %r15
movq %r15, %rax
addq $0x2, %rax
je 0x1b71c
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %rdi
shrq %rdi
xorl %ebx, %ebx
testq %rcx, %rcx
cmovneq %rbx, %rdi
addq %rax, %rdi
callq 0x4680
testq %rax, %rax
je 0x1b729
movq %rax, %r13
movq %rax, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x43c0
movb $0x0, (%r13,%r15)
movq %r13, %rbx
jmp 0x1b729
leaq 0x7cee0(%rip), %rsi     # 0x985cb
xorl %ebx, %ebx
movq %r14, %rdi
xorl %eax, %eax
callq 0xfe44
testq %rax, %rax
je 0x1b7c3
movq %rax, %rdi
callq 0xf53f
movq %r14, (%r12)
movq %r14, %rdi
callq 0x41d0
movl %eax, (%r15)
jmp 0x1b9e4
callq 0x4050
movl $0x4b, (%rax)
xorl %ebx, %ebx
movq %rbx, %rdi
movl $0x4, %esi
callq 0x4880
testl %eax, %eax
je 0x1b77f
leaq 0x7ce8a(%rip), %rsi     # 0x985cb
movq %r14, %rdi
xorl %eax, %eax
callq 0xfe44
testq %rax, %rax
je 0x1b79e
movq %rax, %r13
leaq 0x58(%rsp), %rdx
movq %rax, %rdi
movq %r14, %rsi
callq 0x116ab
testl %eax, %eax
movq %rbx, 0x40(%rsp)
je 0x1b7cd
leaq 0x7b3a4(%rip), %rsi     # 0x96b17
leaq 0x7b3cf(%rip), %rdx     # 0x96b49
jmp 0x1b994
movq %rbx, %rdi
callq 0x45c0
movq 0x48(%rsp), %rax
movq %r12, (%rax)
movq 0x50(%rsp), %rax
movl %r15d, (%rax)
xorl %ebx, %ebx
jmp 0x1b9e4
leaq 0x7b372(%rip), %rsi     # 0x96b17
leaq 0x7b37e(%rip), %rdx     # 0x96b2a
movl $0x4, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x13de5
movq %rbx, %rdi
callq 0x45c0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x1b9e4
cmpl $0x3, 0x58(%rsp)
jne 0x1b986
movl 0x5c(%rsp), %eax
cmpl $0x12, %eax
ja 0x1b986
movl $0x414a0, %ecx          # imm = 0x414A0
btl %eax, %ecx
jae 0x1b986
cmpl $0x0, 0x8(%rsp)
je 0x1b9f5
leaq 0x7b39a(%rip), %rsi     # 0x96b9f
leaq 0x20(%rsp), %rdx
movq %rbx, %rdi
callq 0x155a0
movq %rax, 0x8(%rsp)
leaq 0x7b2f9(%rip), %rsi     # 0x96b17
testq %rax, %rax
je 0x1ba4b
leaq 0x7b3a6(%rip), %rdx     # 0x96bd4
movl $0x4, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x13de5
movl $0x100000, %edi         # imm = 0x100000
movl $0x1, %esi
callq 0x4300
testq %rax, %rax
je 0x1bab0
movq %r13, 0x10(%rsp)
movq %r13, %rdi
movq %rax, 0x18(%rsp)
movq %rax, %rsi
callq 0x1c792
movq %rax, %rbx
testl %ebx, %ebx
jle 0x1ba5f
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %rax
testb $0x2, 0x30(%rax)
movq 0x8(%rax), %rdi
movq 0x18(%rax), %r13
jne 0x1b8bc
movq %r13, %rax
subq %rdi, %rax
cmpq %rbx, %rax
jae 0x1b8bc
addq %rbx, %r13
movq 0x8(%rsp), %rbp
subq (%rbp), %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0xee24
movq 0x8(%rbp), %rdi
movq 0x18(%rbp), %r13
movq %r13, 0x10(%rbp)
subq %rdi, %r13
cmpq %rbx, %r13
ja 0x1b8ce
movq 0x8(%rsp), %rax
cmpq (%rax), %rdi
je 0x1b8fa
cmpq %rbx, %r13
movq %rbx, %rbp
cmovbq %r13, %rbp
movq 0x18(%rsp), %rsi
movq %rbp, %rdx
callq 0x43c0
movq 0x8(%rsp), %rax
addq %rbp, 0x8(%rax)
cmpq %rbx, %r13
movq 0x10(%rsp), %r13
jb 0x1b901
jmp 0x1b919
xorl %ebp, %ebp
movq 0x10(%rsp), %r13
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rbx, %rdx
movq %rbp, %rcx
callq 0xf300
movq %rax, %rbp
cmpq %rbx, %rbp
jne 0x1bade
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rbp
subq %rsi, %rbp
movl $0x100000, %ebx         # imm = 0x100000
cmpq %rbx, %rbp
movl $0x100000, %r13d        # imm = 0x100000
cmovbq %rbp, %r13
movq 0x18(%rsp), %rdi
movq %r13, %rdx
callq 0x43c0
movq 0x10(%rsp), %rdi
addq %r13, 0x8(%rdi)
cmpq $0xfffff, %rbp          # imm = 0xFFFFF
ja 0x1b86d
testb $0x2, 0x30(%rdi)
movq %rbp, %rbx
je 0x1b86d
movl $0x100000, %edx         # imm = 0x100000
movq 0x18(%rsp), %rsi
movq %r13, %rcx
callq 0xf129
jmp 0x1b86a
leaq 0x7b18a(%rip), %rsi     # 0x96b17
leaq 0x7b1e0(%rip), %rdx     # 0x96b74
movl $0x1, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x13de5
callq 0x4050
movq %rax, %r12
movl (%rax), %ebx
movq %r13, %rdi
callq 0xf53f
cmpq $0x0, 0x20(%rsp)
je 0x1b9c7
movq 0x30(%rsp), %rdi
callq 0x4200
movq 0x30(%rsp), %rdi
callq 0x45c0
movq 0x40(%rsp), %rdi
callq 0x45c0
movl %ebx, (%r12)
movl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %r12
movq %rbp, %r15
movq 0x48(%rsp), %rax
movq %r12, (%rax)
movq 0x50(%rsp), %rax
movl %r15d, (%rax)
movq %r13, %rdi
callq 0xf4df
testl %eax, %eax
je 0x1ba34
leaq 0x7b0f9(%rip), %rsi     # 0x96b17
leaq 0x7b236(%rip), %rdx     # 0x96c5b
movl $0x1, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x13de5
movq 0x30(%rsp), %rdi
callq 0x45c0
movq %rbx, %rdi
callq 0x45c0
jmp 0x1b797
leaq 0x7b150(%rip), %rdx     # 0x96ba2
movl $0x1, %edi
movq %r12, %rcx
jmp 0x1b99c
movq 0x18(%rsp), %rdi
callq 0x45c0
testl %ebx, %ebx
js 0x1bb19
movq 0x8(%rsp), %rdi
callq 0xf4df
testl %eax, %eax
movq 0x40(%rsp), %rbx
js 0x1bb59
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x42c0
testl %eax, %eax
movq 0x10(%rsp), %r13
js 0x1bb8f
movq $0x0, 0x20(%rsp)
jmp 0x1b9fb
callq 0x4050
movq %rax, %r12
movl (%rax), %edi
callq 0x4150
leaq 0x7b051(%rip), %rsi     # 0x96b17
leaq 0x7ca01(%rip), %rdx     # 0x984ce
movl $0x1, %edi
movq %rax, %rcx
xorl %eax, %eax
callq 0x13de5
jmp 0x1bb43
callq 0x4050
movq %rax, %r12
movl (%rax), %edi
callq 0x4150
leaq 0x7b023(%rip), %rsi     # 0x96b17
leaq 0x7b100(%rip), %rdx     # 0x96bfb
movl $0x1, %edi
movq %r14, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x13de5
movq 0x18(%rsp), %rdi
callq 0x45c0
jmp 0x1bb43
leaq 0x7aff7(%rip), %rsi     # 0x96b17
leaq 0x7b0f4(%rip), %rdx     # 0x96c1b
movl $0x1, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x13de5
callq 0x4050
movq %rax, %r12
movq 0x10(%rsp), %r13
movl (%r12), %ebx
movq %r13, %rdi
callq 0xf53f
movq 0x8(%rsp), %r13
jmp 0x1b9ad
callq 0x4050
movq %rax, %r12
movl (%rax), %edi
callq 0x4150
leaq 0x7afa8(%rip), %rsi     # 0x96b17
leaq 0x7b0b8(%rip), %rdx     # 0x96c2e
movl $0x1, %edi
movq %r14, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x13de5
movq 0x10(%rsp), %r13
jmp 0x1bbc3
movq 0x30(%rsp), %rbx
callq 0x4050
movq %rax, %r12
movl (%rax), %edi
callq 0x4150
leaq 0x7af6d(%rip), %rsi     # 0x96b17
leaq 0x7b093(%rip), %rdx     # 0x96c44
movl $0x1, %edi
movq %rbx, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x13de5
movl (%r12), %ebx
jmp 0x1b9ad