// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x6b720
cmpl $0x0, %eax
je 0x1939d
movl $0x100000, -0x1c(%rbp)  # imm = 0x100000
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rdi
callq 0x4230
movq %rax, %rcx
movq -0x48(%rbp), %rax
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x10(%rbp), %rax
jb 0x19220
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
jne 0x19210
jmp 0x19220
jmp 0x19212
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x191f8
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rdi
leaq 0x61f7a(%rip), %rsi     # 0x7b1b1
callq 0x4690
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
je 0x1925c
movq -0x18(%rbp), %rdi
callq 0x4590
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x193d2
movq -0x10(%rbp), %rdi
leaq 0x62788(%rip), %rsi     # 0x7b9ef
callq 0x6b3f0
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0x19283
movq $0x0, -0x8(%rbp)
jmp 0x193d2
movq -0x40(%rbp), %rdi
leaq 0x61e6f(%rip), %rsi     # 0x7b0fd
callq 0x4690
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0x192df
cmpl $0x1, 0x7133c(%rip)     # 0x8a5e0
jl 0x192c9
movq 0x70d23(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x40(%rbp), %rcx
leaq 0x61ef9(%rip), %rsi     # 0x7b1b4
leaq 0x61f2d(%rip), %rdx     # 0x7b1ef
movb $0x0, %al
callq 0x4830
movq -0x28(%rbp), %rdi
callq 0x6ae00
movq $0x0, -0x8(%rbp)
jmp 0x193d2
cmpl $0x3, 0x712fa(%rip)     # 0x8a5e0
jl 0x1930b
movq 0x70ce1(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x10(%rbp), %rcx
leaq 0x61f01(%rip), %rsi     # 0x7b1fe
leaq 0x61eeb(%rip), %rdx     # 0x7b1ef
movb $0x0, %al
callq 0x4830
movl $0x100000, %edi         # imm = 0x100000
movl $0x1, %esi
callq 0x4370
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl $0x100000, %edx         # imm = 0x100000
callq 0x1a4b0
movl %eax, -0x34(%rbp)
cmpl $0x0, %eax
jle 0x19350
movq -0x30(%rbp), %rdi
movslq -0x34(%rbp), %rdx
movq -0x18(%rbp), %rcx
movl $0x1, %esi
callq 0x4b20
jmp 0x1931e
movq -0x30(%rbp), %rdi
callq 0x46a0
movq -0x18(%rbp), %rdi
callq 0x4590
movq -0x28(%rbp), %rdi
callq 0x6ad60
cmpl $0x0, %eax
je 0x19393
movq 0x70c59(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x10(%rbp), %rcx
leaq 0x61eab(%rip), %rsi     # 0x7b230
leaq 0x61e63(%rip), %rdx     # 0x7b1ef
movb $0x0, %al
callq 0x4830
movq -0x40(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x193d2
movq -0x10(%rbp), %rdi
leaq 0x61e09(%rip), %rsi     # 0x7b1b1
callq 0x4690
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
jne 0x193c1
movq $0x0, -0x8(%rbp)
jmp 0x193d2
movq -0x18(%rbp), %rdi
callq 0x4590
movq -0x10(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)