// File: karel-brinda[P]ococo/ext/htslib/faidx.c
// Function: download_and_open
static FILE *download_and_open(const char *fn)
{
    const int buf_size = 1 * 1024 * 1024;
    uint8_t *buf;
    FILE *fp;
    hFILE *fp_remote;
    const char *url = fn;
    const char *p;
    int l = strlen(fn);
    for (p = fn + l - 1; p >= fn; --p)
        if (*p == '/') break;
    fn = p + 1;

    // First try to open a local copy
    fp = fopen(fn, "r");
    if (fp)
        return fp;

    // If failed, download from remote and open
    fp_remote = hopen(url, "rb");
    if (fp_remote == 0) {
        fprintf(stderr, "[download_from_remote] fail to open remote file %s\n",url);
        return NULL;
    }
    if ((fp = fopen(fn, "wb")) == 0) {
        fprintf(stderr, "[download_from_remote] fail to create file in the working directory %s\n",fn);
        hclose_abruptly(fp_remote);
        return NULL;
    }
    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, "[download_from_remote] fail to close remote file %s\n", url);

    return fopen(fn, "r");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x100000, -0x14(%rbp)  # imm = 0x100000
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rdi
callq 0x4230
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movslq -0x44(%rbp), %rcx
addq %rcx, %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x10(%rbp), %rax
jb 0x68d02
movq -0x40(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2f, %eax
jne 0x68cf2
jmp 0x68d02
jmp 0x68cf4
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x68cda
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x12cd6(%rip), %rsi     # 0x7b9ef
callq 0x4690
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x68d36
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x68e5b
movq -0x38(%rbp), %rdi
leaq 0x12470(%rip), %rsi     # 0x7b1b1
callq 0x6b3f0
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x68d7a
movq 0x21278(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x38(%rbp), %rdx
leaq 0x15abb(%rip), %rsi     # 0x7e821
movb $0x0, %al
callq 0x4830
movq $0x0, -0x8(%rbp)
jmp 0x68e5b
movq -0x10(%rbp), %rdi
leaq 0x15363(%rip), %rsi     # 0x7e0e8
callq 0x4690
movq %rax, -0x28(%rbp)
cmpq $0x0, %rax
jne 0x68dc6
movq 0x21235(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x10(%rbp), %rdx
leaq 0x15aac(%rip), %rsi     # 0x7e855
movb $0x0, %al
callq 0x4830
movq -0x30(%rbp), %rdi
callq 0x6ae00
movq $0x0, -0x8(%rbp)
jmp 0x68e5b
movl $0x100000, %edi         # imm = 0x100000
movl $0x1, %esi
callq 0x4370
movq %rax, -0x20(%rbp)
movq -0x30(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x100000, %edx         # imm = 0x100000
callq 0x6a270
movl %eax, -0x44(%rbp)
cmpl $0x0, %eax
jle 0x68e0b
movq -0x20(%rbp), %rdi
movslq -0x44(%rbp), %rdx
movq -0x28(%rbp), %rcx
movl $0x1, %esi
callq 0x4b20
jmp 0x68dd9
movq -0x20(%rbp), %rdi
callq 0x46a0
movq -0x28(%rbp), %rdi
callq 0x4590
movq -0x30(%rbp), %rdi
callq 0x6ad60
cmpl $0x0, %eax
je 0x68e47
movq 0x2119e(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
movq -0x38(%rbp), %rdx
leaq 0x15a5d(%rip), %rsi     # 0x7e89d
movb $0x0, %al
callq 0x4830
movq -0x10(%rbp), %rdi
leaq 0x12b9d(%rip), %rsi     # 0x7b9ef
callq 0x4690
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)