// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movq $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x4250
leaq -0x90(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x4250
movq -0x10(%rbp), %rdi
callq 0x15cf0
cmpl $0x0, %eax
je 0x2652e
movl $0x100000, -0x94(%rbp)  # imm = 0x100000
movq -0x10(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x10(%rbp), %rdi
leaq 0xc96f5(%rip), %rsi     # 0xef704
movl $0x5, %edx
callq 0x4200
cmpl $0x0, %eax
je 0x2606b
movq -0x10(%rbp), %rdi
leaq 0xc96e1(%rip), %rsi     # 0xef70a
movl $0xa, %edx
callq 0x4200
cmpl $0x0, %eax
je 0x2606b
movq -0x10(%rbp), %rdi
leaq 0xc96d2(%rip), %rsi     # 0xef715
movl $0xb, %edx
callq 0x4200
cmpl $0x0, %eax
je 0x2606b
movq -0x10(%rbp), %rdi
leaq 0xc96c4(%rip), %rsi     # 0xef721
callq 0x4090
movq %rax, -0xc8(%rbp)
jmp 0x26082
movq -0x10(%rbp), %rdi
leaq 0xcc4a7(%rip), %rsi     # 0xf251d
callq 0x4090
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rax
movq -0xc8(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rcx
xorl %eax, %eax
cmpq -0x10(%rbp), %rcx
movb %al, -0xc9(%rbp)
jbe 0x260d3
movq -0xa0(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
setne %al
movb %al, -0xc9(%rbp)
movb -0xc9(%rbp), %al
testb $0x1, %al
jne 0x260df
jmp 0x260f3
movq -0xa0(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0x260a8
movq -0xa0(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
jne 0x26114
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rax
subq %rax, %rsi
leaq -0x78(%rbp), %rdx
callq 0x23c50
movq -0x68(%rbp), %rdi
movl $0x4, %esi
callq 0x4860
cmpl $0x0, %eax
jne 0x26184
movq -0x68(%rbp), %rdi
callq 0x45a0
movq -0xa0(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0xa8(%rbp), %rax
movq -0xa0(%rbp), %rcx
subq %rcx, %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x265f6
movq -0x10(%rbp), %rdi
leaq 0xcc962(%rip), %rsi     # 0xf2af1
movb $0x0, %al
callq 0x158b0
movq %rax, -0x30(%rbp)
cmpq $0x0, %rax
jne 0x261d3
movq -0x10(%rbp), %rcx
movl $0x4, %edi
leaq 0xcab9f(%rip), %rsi     # 0xf0d4f
leaq 0xcabab(%rip), %rdx     # 0xf0d62
movb $0x0, %al
callq 0x1b030
movq -0x68(%rbp), %rdi
callq 0x45a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x265f6
movq -0x30(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0x18450
cmpl $0x0, %eax
je 0x2620c
movq -0x10(%rbp), %rcx
movl $0x1, %edi
leaq 0xcab56(%rip), %rsi     # 0xf0d4f
leaq 0xcab81(%rip), %rdx     # 0xf0d81
movb $0x0, %al
callq 0x1b030
jmp 0x2658a
cmpl $0x3, -0x60(%rbp)
jne 0x26230
cmpl $0x5, -0x5c(%rbp)
je 0x26253
cmpl $0xa, -0x5c(%rbp)
je 0x26253
cmpl $0xc, -0x5c(%rbp)
je 0x26253
cmpl $0x7, -0x5c(%rbp)
je 0x26253
cmpl $0x12, -0x5c(%rbp)
je 0x26253
movq -0x10(%rbp), %rcx
movl $0x1, %edi
leaq 0xcab0f(%rip), %rsi     # 0xf0d4f
leaq 0xcab65(%rip), %rdx     # 0xf0dac
movb $0x0, %al
callq 0x1b030
jmp 0x2658a
cmpl $0x0, -0x24(%rbp)
je 0x264c3
movq -0x68(%rbp), %rdi
leaq 0xcab6f(%rip), %rsi     # 0xf0dd7
leaq -0x90(%rbp), %rdx
callq 0x1dc90
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x262a4
movq -0xa0(%rbp), %rcx
movl $0x1, %edi
leaq 0xcaabe(%rip), %rsi     # 0xf0d4f
leaq 0xcab42(%rip), %rdx     # 0xf0dda
movb $0x0, %al
callq 0x1b030
jmp 0x2658a
movq -0x10(%rbp), %rcx
movl $0x4, %edi
leaq 0xcaa9b(%rip), %rsi     # 0xf0d4f
leaq 0xcab51(%rip), %rdx     # 0xf0e0c
movb $0x0, %al
callq 0x1b030
movl $0x100000, %edi         # imm = 0x100000
movl $0x1, %esi
callq 0x42f0
movq %rax, -0xb0(%rbp)
cmpq $0x0, -0xb0(%rbp)
jne 0x26310
callq 0x4050
movl (%rax), %edi
callq 0x4130
movq %rax, %rcx
movl $0x1, %edi
leaq 0xcaa52(%rip), %rsi     # 0xf0d4f
leaq 0xcc6f0(%rip), %rdx     # 0xf29f4
movb $0x0, %al
callq 0x1b030
jmp 0x2658a
jmp 0x26312
movq -0x30(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movl $0x100000, %edx         # imm = 0x100000
callq 0x29140
movl %eax, -0x98(%rbp)
cmpl $0x0, %eax
jle 0x263a6
movq -0x38(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movslq -0x98(%rbp), %rdx
callq 0x291f0
movslq -0x98(%rbp), %rcx
cmpq %rcx, %rax
je 0x263a1
movq -0x10(%rbp), %rax
movq %rax, -0xd8(%rbp)
callq 0x4050
movl (%rax), %edi
callq 0x4130
movq -0xd8(%rbp), %rcx
movq %rax, %r8
movl $0x1, %edi
leaq 0xca9cd(%rip), %rsi     # 0xf0d4f
leaq 0xcaaaa(%rip), %rdx     # 0xf0e33
movb $0x0, %al
callq 0x1b030
movq -0xb0(%rbp), %rdi
callq 0x45a0
jmp 0x2658a
jmp 0x26312
movq -0xb0(%rbp), %rdi
callq 0x45a0
cmpl $0x0, -0x98(%rbp)
jge 0x263de
movq -0x10(%rbp), %rcx
movl $0x1, %edi
leaq 0xca984(%rip), %rsi     # 0xf0d4f
leaq 0xcaa81(%rip), %rdx     # 0xf0e53
movb $0x0, %al
callq 0x1b030
jmp 0x2658a
movq -0x38(%rbp), %rdi
callq 0x14920
cmpl $0x0, %eax
jge 0x26434
movq -0x10(%rbp), %rax
movq %rax, -0xe0(%rbp)
callq 0x4050
movl (%rax), %edi
callq 0x4130
movq -0xe0(%rbp), %rcx
movq %rax, %r8
movl $0x1, %edi
leaq 0xca936(%rip), %rsi     # 0xf0d4f
leaq 0xcaa46(%rip), %rdx     # 0xf0e66
movb $0x0, %al
callq 0x1b030
movq $0x0, -0x38(%rbp)
jmp 0x2658a
movq $0x0, -0x38(%rbp)
movq -0x80(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x42b0
cmpl $0x0, %eax
jge 0x2648e
movq -0x80(%rbp), %rax
movq %rax, -0xe8(%rbp)
callq 0x4050
movl (%rax), %edi
callq 0x4130
movq -0xe8(%rbp), %rcx
movq %rax, %r8
movl $0x1, %edi
leaq 0xca8d4(%rip), %rsi     # 0xf0d4f
leaq 0xca9fa(%rip), %rdx     # 0xf0e7c
movb $0x0, %al
callq 0x1b030
jmp 0x2658a
leaq -0x90(%rbp), %rdi
callq 0x1dd70
movq -0xa0(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0xa8(%rbp), %rax
movq -0xa0(%rbp), %rcx
subq %rcx, %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
jmp 0x264e4
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0xa8(%rbp), %rax
movq -0x10(%rbp), %rcx
subq %rcx, %rax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x30(%rbp), %rdi
callq 0x14920
cmpl $0x0, %eax
je 0x26510
movq -0x10(%rbp), %rcx
movl $0x1, %edi
leaq 0xca84d(%rip), %rsi     # 0xf0d4f
leaq 0xca98a(%rip), %rdx     # 0xf0e93
movb $0x0, %al
callq 0x1b030
movq -0x80(%rbp), %rdi
callq 0x45a0
movq -0x68(%rbp), %rdi
callq 0x45a0
movl $0x0, -0x4(%rbp)
jmp 0x265f6
movq -0x10(%rbp), %rdi
leaq 0xcc5b8(%rip), %rsi     # 0xf2af1
movb $0x0, %al
callq 0x158b0
movq %rax, -0xb8(%rbp)
cmpq $0x0, %rax
jne 0x26559
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x265f6
movq -0xb8(%rbp), %rdi
callq 0x149d0
movq -0x10(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rdi
callq 0x41b0
movl %eax, %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x265f6
callq 0x4050
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x265a4
movq -0x30(%rbp), %rdi
callq 0x149d0
cmpq $0x0, -0x38(%rbp)
je 0x265b4
movq -0x38(%rbp), %rdi
callq 0x149d0
cmpq $0x0, -0x90(%rbp)
jbe 0x265c7
movq -0x80(%rbp), %rdi
callq 0x41f0
movq -0x80(%rbp), %rdi
callq 0x45a0
movq -0x68(%rbp), %rdi
callq 0x45a0
movl -0x3c(%rbp), %eax
movl %eax, -0xec(%rbp)
callq 0x4050
movl -0xec(%rbp), %ecx
movl %ecx, (%rax)
movl $0xfffffffe, -0x4(%rbp) # imm = 0xFFFFFFFE
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)