// File: /workspace/github_c_makefile_star2_O2/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 $0x98, %rsp
movl %ecx, %ebp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
andq $0x0, 0x70(%rsp)
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
callq 0xe4f2
testl %eax, %eax
je 0x17830
leaq 0x5fdc8(%rip), %rsi     # 0x774e4
pushq $0x5
popq %rdx
movq %rbx, %rdi
callq 0x4210
testl %eax, %eax
movl %ebp, 0x8(%rsp)
je 0x17764
leaq 0x5fdb4(%rip), %rsi     # 0x774ea
pushq $0xa
popq %rdx
movq %rbx, %rdi
callq 0x4210
testl %eax, %eax
je 0x17764
leaq 0x5fda9(%rip), %rsi     # 0x774f5
pushq $0xb
popq %rdx
movq %rbx, %rdi
callq 0x4210
testl %eax, %eax
je 0x17764
leaq 0x5fd9f(%rip), %rsi     # 0x77501
jmp 0x1776b
leaq 0x627ef(%rip), %rsi     # 0x79f5a
movq %r15, 0x48(%rsp)
movq %r14, 0x50(%rsp)
movq %rbx, %rdi
callq 0x4090
movq %rax, %r12
addq %rbx, %rax
pushq $0x1
popq %rbp
movq %rax, %rdx
movb (%rax), %cl
decq %rax
decq %rbp
cmpq %rbx, %rdx
jbe 0x1779b
cmpb $0x2f, %cl
jne 0x17786
xorl %r13d, %r13d
cmpb $0x2f, %cl
sete %r13b
leaq (%rax,%r13), %r14
incq %r14
movq %r13, %rsi
addq %rbp, %rsi
negq %rsi
leaq 0x60(%rsp), %r15
movq %r14, %rdi
movq %rsi, 0x58(%rsp)
movq %r15, %rdx
callq 0x163d5
movq 0x10(%r15), %rdi
pushq $0x4
popq %rsi
movq %rdi, 0x18(%rsp)
callq 0x4880
testl %eax, %eax
je 0x17863
movq %r14, 0x40(%rsp)
leaq 0x62d37(%rip), %rsi     # 0x7a526
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe19b
testq %rax, %rax
je 0x17888
movq %rax, %r15
leaq 0x78(%rsp), %rdx
movq %rax, %rdi
movq %rbx, %rsi
callq 0xf79d
testl %eax, %eax
je 0x178b4
leaq 0x612e4(%rip), %rsi     # 0x78b08
leaq 0x6130f(%rip), %rdx     # 0x78b3a
jmp 0x17a9f
leaq 0x62cef(%rip), %rsi     # 0x7a526
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe19b
testq %rax, %rax
je 0x178ad
movq %rax, %rdi
callq 0xd94c
movq %rbx, (%r15)
movq %rbx, %rdi
callq 0x41d0
movl %eax, (%r14)
jmp 0x17af9
movq 0x18(%rsp), %rdi
callq 0x45c0
movq 0x48(%rsp), %rax
movq %r14, (%rax)
addl %r13d, %ebp
negl %ebp
movq 0x50(%rsp), %rax
movl %ebp, (%rax)
xorl %ebp, %ebp
jmp 0x17af9
leaq 0x61279(%rip), %rsi     # 0x78b08
leaq 0x61285(%rip), %rdx     # 0x78b1b
pushq $0x4
popq %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x110c6
movq 0x18(%rsp), %rdi
callq 0x45c0
pushq $-0x1
jmp 0x17af8
cmpl $0x3, 0x78(%rsp)
jne 0x17a91
movl 0x7c(%rsp), %eax
cmpl $0x12, %eax
ja 0x17a91
movl $0x414a0, %ecx          # imm = 0x414A0
btl %eax, %ecx
jae 0x17a91
movq %rbx, %rax
cmpl $0x0, 0x8(%rsp)
je 0x17b9f
leaq 0x612a1(%rip), %rsi     # 0x78b90
leaq 0x20(%rsp), %rdx
movq 0x18(%rsp), %rdi
callq 0x127bd
testq %rax, %rax
je 0x17b0d
movq %rax, %r13
leaq 0x611f7(%rip), %rsi     # 0x78b08
leaq 0x612ad(%rip), %rdx     # 0x78bc5
pushq $0x4
popq %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x110c6
pushq $0x1
popq %rsi
movl $0x100000, %edi         # imm = 0x100000
callq 0x4300
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x17b28
movq %r15, 0x10(%rsp)
movq 0x8(%r15), %r12
movq %r13, %r14
movq %r15, %r13
movq 0x10(%r15), %r15
subq %r12, %r15
movl $0x100000, %eax         # imm = 0x100000
cmpq %rax, %r15
movl $0x100000, %ebp         # imm = 0x100000
cmovbq %r15, %rbp
movq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x43c0
addq %rbp, %r12
movq %r12, 0x8(%r13)
movq %r14, %r13
movl $0x100000, %r12d        # imm = 0x100000
cmpq $0xfffff, %r15          # imm = 0xFFFFF
ja 0x179c1
movq 0x10(%rsp), %rax
testb $0x2, 0x30(%rax)
je 0x179b5
movl $0x100000, %edx         # imm = 0x100000
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rbp, %rcx
callq 0xd55e
movq %rax, %r15
movq %r15, %r12
testl %r15d, %r15d
jle 0x17b4d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
testb $0x2, 0x30(%r13)
movq 0x8(%r13), %rdi
movq 0x18(%r13), %r15
jne 0x17a00
movq %r15, %rax
subq %rdi, %rax
cmpq %r12, %rax
jae 0x17a00
addq %r12, %r15
subq (%r13), %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0xd26c
movq 0x8(%r13), %rdi
movq 0x18(%r13), %r15
movq %r15, 0x10(%r13)
subq %rdi, %r15
cmpq %r12, %r15
ja 0x17a0e
cmpq (%r13), %rdi
je 0x17a35
cmpq %r12, %r15
movq %r12, %rbp
cmovbq %r15, %rbp
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
callq 0x43c0
addq %rbp, 0x8(%r13)
cmpq %r12, %r15
movq 0x10(%rsp), %r15
jb 0x17a3c
jmp 0x17a52
xorl %ebp, %ebp
movq 0x10(%rsp), %r15
movq %r13, %rdi
movq 0x8(%rsp), %rsi
movq %r12, %rdx
movq %rbp, %rcx
callq 0xd717
movq %rax, %rbp
cmpq %r12, %rbp
je 0x17945
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x6109a(%rip), %rsi     # 0x78b08
leaq 0x61177(%rip), %rdx     # 0x78bec
pushq $0x1
popq %rdi
movq %rbx, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x110c6
movq 0x8(%rsp), %rdi
callq 0x45c0
jmp 0x17aaf
leaq 0x61070(%rip), %rsi     # 0x78b08
leaq 0x610c6(%rip), %rdx     # 0x78b65
xorl %r13d, %r13d
pushq $0x1
popq %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x110c6
callq 0x4050
movq %rax, %rbx
movl (%rax), %ebp
movq %r15, %rdi
callq 0xd94c
testq %r13, %r13
je 0x17ace
movq %r13, %rdi
callq 0xd94c
cmpq $0x0, 0x20(%rsp)
je 0x17ae0
movq 0x30(%rsp), %rdi
callq 0x4200
movq 0x30(%rsp), %rdi
callq 0x45c0
movq 0x70(%rsp), %rdi
callq 0x45c0
movl %ebp, (%rbx)
pushq $-0x2
popq %rbp
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x60ff4(%rip), %rsi     # 0x78b08
leaq 0x61078(%rip), %rdx     # 0x78b93
xorl %r13d, %r13d
pushq $0x1
popq %rdi
movq 0x40(%rsp), %rcx
jmp 0x17aa8
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x60fcd(%rip), %rsi     # 0x78b08
leaq 0x628e7(%rip), %rdx     # 0x7a429
pushq $0x1
popq %rdi
movq %rax, %rcx
jmp 0x17aa8
movq 0x8(%rsp), %rdi
callq 0x45c0
testl %r15d, %r15d
movq 0x40(%rsp), %r14
js 0x17bef
movq %r13, %rdi
callq 0xd8ee
testl %eax, %eax
movq 0x10(%rsp), %r15
js 0x17c14
movq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x42c0
testl %eax, %eax
js 0x17c30
andq $0x0, 0x20(%rsp)
movq %r14, %rax
movq 0x58(%rsp), %r12
movq 0x48(%rsp), %rcx
movq %rax, (%rcx)
movq 0x50(%rsp), %rax
movl %r12d, (%rax)
movq %r15, %rdi
callq 0xd8ee
testl %eax, %eax
je 0x17bd6
leaq 0x60f46(%rip), %rsi     # 0x78b08
leaq 0x61083(%rip), %rdx     # 0x78c4c
pushq $0x1
popq %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x110c6
movq 0x30(%rsp), %rdi
callq 0x45c0
movq 0x18(%rsp), %rdi
callq 0x45c0
jmp 0x17881
leaq 0x60f12(%rip), %rsi     # 0x78b08
leaq 0x6100f(%rip), %rdx     # 0x78c0c
pushq $0x1
popq %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x110c6
movq 0x10(%rsp), %r15
jmp 0x17aaf
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x60ee1(%rip), %rsi     # 0x78b08
leaq 0x60ff1(%rip), %rdx     # 0x78c1f
jmp 0x17c4f
movq 0x30(%rsp), %rbx
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x60ec0(%rip), %rsi     # 0x78b08
leaq 0x60fe6(%rip), %rdx     # 0x78c35
xorl %r13d, %r13d
pushq $0x1
popq %rdi
movq %rbx, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x110c6
jmp 0x17aaf