// File: /workspace/github_c_makefile_star2_O1/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, %r13
movq %rsi, %r15
movq %rdi, %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rsp)
movq $0x0, 0x40(%rsp)
callq 0x103cd
testl %eax, %eax
je 0x1bc5b
leaq 0x7c97b(%rip), %rsi     # 0x984f4
movl $0x5, %edx
movq %r14, %rdi
callq 0x4210
testl %eax, %eax
movl %ebx, 0x8(%rsp)
je 0x1bbc7
leaq 0x7c965(%rip), %rsi     # 0x984fa
movl $0xa, %edx
movq %r14, %rdi
callq 0x4210
testl %eax, %eax
je 0x1bbc7
leaq 0x7c958(%rip), %rsi     # 0x98505
movl $0xb, %edx
movq %r14, %rdi
callq 0x4210
testl %eax, %eax
je 0x1bbc7
leaq 0x7c94c(%rip), %rsi     # 0x98511
jmp 0x1bbce
leaq 0x7f41e(%rip), %rsi     # 0x9afec
movq %r14, %rdi
callq 0x4090
movq %rax, %rbx
leaq (%r14,%rax), %rcx
movq %rcx, %rax
testq %rbx, %rbx
jle 0x1bbf5
movq %rcx, %rax
cmpb $0x2f, (%rax)
je 0x1bbf5
decq %rax
cmpq %r14, %rax
ja 0x1bbe8
movq %r15, 0x18(%rsp)
xorl %ebp, %ebp
cmpb $0x2f, (%rax)
sete %bpl
addq %rax, %rbp
subq %rbp, %rcx
movq %rcx, %rax
movq %rcx, 0x28(%rsp)
addq $0x2, %rax
je 0x1bc93
movq %rax, %rcx
shrq $0x3e, %rcx
movq %rax, %rdi
shrq %rdi
xorl %r15d, %r15d
testq %rcx, %rcx
cmovneq %r15, %rdi
addq %rax, %rdi
callq 0x4680
testq %rax, %rax
je 0x1bca1
movq %rax, %r12
movq %rax, %rdi
movq %rbp, %rsi
movq 0x28(%rsp), %r15
movq %r15, %rdx
callq 0x43c0
movb $0x0, (%r12,%r15)
movq %r12, %r15
jmp 0x1bca1
leaq 0x7f962(%rip), %rsi     # 0x9b5c4
xorl %ebx, %ebx
movq %r14, %rdi
xorl %eax, %eax
callq 0x10014
testq %rax, %rax
je 0x1bd62
movq %rax, %rdi
callq 0xf730
movq %r14, (%r15)
movq %r14, %rdi
callq 0x41d0
movl %eax, (%r13)
jmp 0x1bfd2
callq 0x4050
movl $0x4b, (%rax)
xorl %r15d, %r15d
movq %r15, %rdi
movl $0x4, %esi
callq 0x4880
testl %eax, %eax
movq %r15, 0x20(%rsp)
je 0x1bcfb
leaq 0x7f906(%rip), %rsi     # 0x9b5c4
movq %r14, %rdi
xorl %eax, %eax
callq 0x10014
testq %rax, %rax
je 0x1bd27
movq %rax, %r12
leaq 0x58(%rsp), %rdx
movq %rax, %rdi
movq %r14, %rsi
callq 0x11915
testl %eax, %eax
je 0x1bd6c
leaq 0x7dd88(%rip), %rsi     # 0x99a77
leaq 0x7ddb3(%rip), %rdx     # 0x99aa9
jmp 0x1bf60
movq %r15, %rdi
callq 0x45c0
movq 0x18(%rsp), %rax
movq %rbp, (%rax)
movq 0x28(%rsp), %rax
movl %eax, (%r13)
xorl %r13d, %r13d
movl $0x1, %r15d
xorl %r12d, %r12d
xorl %ebx, %ebx
jmp 0x1bf78
leaq 0x7dd49(%rip), %rsi     # 0x99a77
leaq 0x7dd55(%rip), %rdx     # 0x99a8a
xorl %r13d, %r13d
movl $0x4, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x14378
movq %r15, %rdi
callq 0x45c0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl $0x1, %r15d
xorl %r12d, %r12d
jmp 0x1bf78
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x1bfd2
cmpl $0x3, 0x58(%rsp)
jne 0x1bf52
movl 0x5c(%rsp), %eax
cmpl $0x12, %eax
ja 0x1bf52
movl $0x414a0, %ecx          # imm = 0x414A0
btl %eax, %ecx
jae 0x1bf52
cmpl $0x0, 0x8(%rsp)
je 0x1bfe3
leaq 0x7dd5b(%rip), %rsi     # 0x99aff
leaq 0x30(%rsp), %rdx
movq %r15, %rdi
callq 0x15b35
movq %rax, 0x10(%rsp)
leaq 0x7dcba(%rip), %rsi     # 0x99a77
testq %rax, %rax
je 0x1bff7
leaq 0x7dd67(%rip), %rdx     # 0x99b34
movl $0x4, %r15d
movl $0x4, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x14378
movl $0x100000, %edi         # imm = 0x100000
movl $0x1, %esi
callq 0x4300
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x1c00e
movq %r12, 0x50(%rsp)
movq %r13, 0x48(%rsp)
movq 0x8(%r12), %rsi
movq 0x10(%r12), %rbx
subq %rsi, %rbx
movl $0x100000, %eax         # imm = 0x100000
cmpq %rax, %rbx
movl $0x100000, %r15d        # imm = 0x100000
cmovbq %rbx, %r15
movq 0x8(%rsp), %rdi
movq %r15, %rdx
callq 0x43c0
addq %r15, 0x8(%r12)
cmpq $0xfffff, %rbx          # imm = 0xFFFFF
ja 0x1be63
testb $0x2, 0x30(%r12)
je 0x1be63
movl $0x100000, %edx         # imm = 0x100000
movq %r12, %rdi
movq 0x8(%rsp), %rsi
movq %r15, %rcx
callq 0xf2cd
movq %rax, %r15
testl %r15d, %r15d
jle 0x1c03e
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq 0x10(%rsp), %r13
testb $0x2, 0x30(%r13)
jne 0x1bea6
movq 0x18(%r13), %rsi
movq %rsi, %rax
subq 0x8(%r13), %rax
cmpq %r15, %rax
jae 0x1bea6
addq %r15, %rsi
subq (%r13), %rsi
movq %r13, %rdi
callq 0xefdd
movq 0x18(%r13), %rax
movq %rax, 0x10(%r13)
movq 0x8(%r13), %rdi
movq 0x18(%r13), %r12
subq %rdi, %r12
cmpq %r15, %r12
ja 0x1bebc
cmpq (%r13), %rdi
je 0x1bee8
cmpq %r15, %r12
movq %r15, %rbx
cmovbq %r12, %rbx
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
callq 0x43c0
addq %rbx, 0x8(%r13)
cmpq %r15, %r12
movq 0x50(%rsp), %r12
movq 0x48(%rsp), %r13
jb 0x1bef4
jmp 0x1bf0c
xorl %ebx, %ebx
movq 0x50(%rsp), %r12
movq 0x48(%rsp), %r13
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0xf4c6
movq %rax, %rbx
cmpq %r15, %rbx
je 0x1be09
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x7db4f(%rip), %rsi     # 0x99a77
leaq 0x7dc2c(%rip), %rdx     # 0x99b5b
xorl %ebx, %ebx
movl $0x1, %edi
movq %r14, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x14378
movq 0x8(%rsp), %rdi
callq 0x45c0
jmp 0x1c0b9
leaq 0x7db1e(%rip), %rsi     # 0x99a77
leaq 0x7db74(%rip), %rdx     # 0x99ad4
xorl %r13d, %r13d
movl $0x1, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x14378
movl $0x4, %r15d
cmpl $0x4, %r15d
jne 0x1bfd2
callq 0x4050
movq %rax, %rbx
movl (%rax), %ebp
testq %r12, %r12
je 0x1bf95
movq %r12, %rdi
callq 0xf730
testq %r13, %r13
movq 0x20(%rsp), %r14
je 0x1bfa7
movq %r13, %rdi
callq 0xf730
cmpq $0x0, 0x30(%rsp)
je 0x1bfb9
movq 0x40(%rsp), %rdi
callq 0x4200
movq 0x40(%rsp), %rdi
callq 0x45c0
movq %r14, %rdi
callq 0x45c0
movl %ebp, (%rbx)
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 0x18(%rsp), %rax
movq %r14, (%rax)
movl %ebx, (%r13)
xorl %r13d, %r13d
jmp 0x1c0cc
leaq 0x7db04(%rip), %rdx     # 0x99b02
xorl %r13d, %r13d
movl $0x1, %edi
movq %rbp, %rcx
jmp 0x1bf6b
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x7da56(%rip), %rsi     # 0x99a77
leaq 0x7f49f(%rip), %rdx     # 0x9b4c7
xorl %ebx, %ebx
movl $0x1, %edi
movq %rax, %rcx
xorl %eax, %eax
callq 0x14378
jmp 0x1c0bf
movq 0x8(%rsp), %rdi
callq 0x45c0
testl %r15d, %r15d
js 0x1c09a
movq 0x10(%rsp), %rdi
callq 0xf6d0
testl %eax, %eax
js 0x1c116
movq 0x40(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x42c0
testl %eax, %eax
js 0x1c13d
movq $0x0, 0x30(%rsp)
movq 0x18(%rsp), %rax
movq %rbp, (%rax)
movq 0x28(%rsp), %rax
movl %eax, (%r13)
xorl %r13d, %r13d
movb $0x1, %bl
xorl %r15d, %r15d
jmp 0x1c0c4
leaq 0x7d9d6(%rip), %rsi     # 0x99a77
leaq 0x7dad3(%rip), %rdx     # 0x99b7b
xorl %ebx, %ebx
movl $0x1, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x14378
movl $0x4, %r15d
movq 0x10(%rsp), %r13
testb %bl, %bl
je 0x1bf78
movq %r12, %rdi
callq 0xf6d0
testl %eax, %eax
je 0x1c0f5
leaq 0x7d998(%rip), %rsi     # 0x99a77
leaq 0x7dad5(%rip), %rdx     # 0x99bbb
movl $0x1, %edi
movq %r14, %rcx
xorl %eax, %eax
callq 0x14378
movq 0x40(%rsp), %rdi
callq 0x45c0
movq 0x20(%rsp), %rdi
callq 0x45c0
xorl %ebx, %ebx
movl $0x1, %r15d
jmp 0x1bf78
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x7d94e(%rip), %rsi     # 0x99a77
leaq 0x7da5e(%rip), %rdx     # 0x99b8e
xorl %r13d, %r13d
movl $0x1, %edi
movq %r14, %rcx
jmp 0x1c167
movq 0x40(%rsp), %rbx
callq 0x4050
movl (%rax), %edi
callq 0x4150
leaq 0x7d922(%rip), %rsi     # 0x99a77
leaq 0x7da48(%rip), %rdx     # 0x99ba4
xorl %r13d, %r13d
movl $0x1, %edi
movq %rbx, %rcx
movq %rax, %r8
xorl %eax, %eax
callq 0x14378
movl $0x4, %r15d
xorl %ebx, %ebx
jmp 0x1c0c4