// File: /workspace/github_cpp_makefile_star2_O2/karel-brinda[P]ococo/ext/htslib/hts.c
// Function: test_and_fetch
static char *test_and_fetch(const char *fn)
{
    FILE *fp;
    if (hisremote(fn)) {
        const int buf_size = 1 * 1024 * 1024;
        hFILE *fp_remote;
        uint8_t *buf;
        int l;
        const char *p;
        for (p = fn + strlen(fn) - 1; p >= fn; --p)
            if (*p == '/') break;
        ++p; // p now points to the local file name
        // Attempt to open local file first
        if ((fp = fopen((char*)p, "rb")) != 0)
        {
            fclose(fp);
            return (char*)p;
        }
        // Attempt to open remote file. Stay quiet on failure, it is OK to fail when trying first .csi then .tbi index.
        if ((fp_remote = hopen(fn, "r")) == 0) return 0;
        if ((fp = fopen(p, "w")) == 0) {
            if (hts_verbose >= 1) fprintf(stderr, "[E::%s] fail to create file '%s' in the working directory\n", __func__, p);
            hclose_abruptly(fp_remote);
            return 0;
        }
        if (hts_verbose >= 3) fprintf(stderr, "[M::%s] downloading file '%s' to local directory\n", __func__, fn);
        buf = (uint8_t*)calloc(buf_size, 1);
        while ((l = hread(fp_remote, buf, buf_size)) > 0) fwrite(buf, 1, l, fp);
        free(buf);
        fclose(fp);
        if (hclose(fp_remote) != 0) fprintf(stderr, "[E::%s] fail to close remote file '%s'\n", __func__, fn);
        return (char*)p;
    } else {
        if ((fp = fopen(fn, "rb")) == 0) return 0;
        fclose(fp);
        return (char*)fn;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r14
callq 0x3aa79
testl %eax, %eax
je 0xff63
movq %r14, %rdi
callq 0x4210
addq %r14, %rax
decq %rax
movq %rax, %rbx
cmpq %r14, %rax
jb 0xff3f
leaq -0x1(%rbx), %rax
cmpb $0x2f, (%rbx)
jne 0xff2e
incq %rbx
leaq 0x34121(%rip), %rsi     # 0x4406a
movq %rbx, %rdi
callq 0x45f0
testq %rax, %rax
je 0xff8b
movq %rax, %rdi
callq 0x4530
jmp 0x100aa
leaq 0x34100(%rip), %rsi     # 0x4406a
movq %r14, %rdi
callq 0x45f0
testq %rax, %rax
je 0x100a8
movq %rax, %rdi
callq 0x4530
movq %r14, %rbx
jmp 0x100aa
leaq 0x34923(%rip), %rsi     # 0x448b5
movq %r14, %rdi
callq 0x3a708
testq %rax, %rax
je 0x100a8
movq %rax, %r15
leaq 0x34009(%rip), %rsi     # 0x43fb6
movq %rbx, %rdi
callq 0x45f0
movl 0x4758d(%rip), %ecx     # 0x57548
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x1009c
cmpl $0x3, %ecx
jge 0x100bc
movq %r14, 0x10(%rsp)
pushq $0x1
popq %rsi
movl $0x100000, %edi         # imm = 0x100000
callq 0x4370
movq %rax, %rbp
movq 0x8(%r15), %r12
movq 0x10(%r15), %r14
subq %r12, %r14
movl $0x100000, %eax         # imm = 0x100000
cmpq %rax, %r14
movl $0x100000, %r13d        # imm = 0x100000
cmovbq %r14, %r13
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x4420
addq %r13, %r12
movq %r12, 0x8(%r15)
movl $0x100000, %eax         # imm = 0x100000
cmpq $0xfffff, %r14          # imm = 0xFFFFF
ja 0x1003e
movl $0x100000, %edx         # imm = 0x100000
movq %r15, %rdi
movq %rbp, %rsi
movq %r13, %rcx
callq 0x3a0c7
testl %eax, %eax
jle 0x10058
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rbp, %rdi
pushq $0x1
popq %rsi
movq %rax, %rdx
movq 0x8(%rsp), %rcx
callq 0x4a20
jmp 0xffe7
movq %rbp, %rdi
callq 0x4600
movq 0x8(%rsp), %rdi
callq 0x4530
movq %r15, %rdi
callq 0x3a3a2
testl %eax, %eax
je 0x100aa
movq 0x46f53(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0x34062(%rip), %rsi     # 0x440e9
leaq 0x3401a(%rip), %rdx     # 0x440a8
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x4770
jmp 0x100aa
testl %ecx, %ecx
jg 0x100e3
movq %r15, %rdi
callq 0x3a3fa
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x46f0d(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0x33fea(%rip), %rsi     # 0x440b7
leaq 0x33fd4(%rip), %rdx     # 0x440a8
movq %r14, %rcx
xorl %eax, %eax
callq 0x4770
jmp 0xffd2
movq 0x46ee6(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0x33f79(%rip), %rsi     # 0x4406d
leaq 0x33fad(%rip), %rdx     # 0x440a8
movq %rbx, %rcx
xorl %eax, %eax
callq 0x4770
jmp 0x100a0