// File: /workspace/github_c_makefile_star2_O3/samtools[P]htslib/hfile_libcurl.c
// Function: libcurl_read
static ssize_t libcurl_read(hFILE *fpv, void *bufferv, size_t nbytes)
{
    hFILE_libcurl *fp = (hFILE_libcurl *) fpv;
    char *buffer = (char *) bufferv;
    off_t to_skip = -1;
    ssize_t got = 0;
    CURLcode err;

    if (fp->delayed_seek >= 0) {
        assert(fp->base.offset == fp->delayed_seek);

        if (fp->preserved
            && fp->last_offset > fp->delayed_seek
            && fp->last_offset - fp->preserved_bytes <= fp->delayed_seek) {
            // Can use buffer contents copied when seeking started, to
            // avoid having to re-read data discarded by hseek().
            // Note fp->last_offset is the offset of the *end* of the
            // preserved buffer.
            size_t n = fp->last_offset - fp->delayed_seek;
            char *start = fp->preserved + (fp->preserved_bytes - n);
            size_t bytes = n <= nbytes ? n : nbytes;
            memcpy(buffer, start, bytes);
            if (bytes < n) { // Part of the preserved buffer still left
                fp->delayed_seek += bytes;
            } else {
                fp->last_offset = fp->delayed_seek = -1;
            }
            return bytes;
        }

        if (fp->last_offset >= 0
            && fp->delayed_seek > fp->last_offset
            && fp->delayed_seek - fp->last_offset < MIN_SEEK_FORWARD) {
            // If not seeking far, just read the data and throw it away.  This
            // is likely to be quicker than opening a new stream
            to_skip = fp->delayed_seek - fp->last_offset;
        } else {
            if (restart_from_position(fp, fp->delayed_seek) < 0) {
                return -1;
            }
        }
        fp->delayed_seek = -1;
        fp->last_offset = -1;
        fp->preserved_bytes = 0;
    }

    do {
        fp->buffer.ptr.rd = buffer;
        fp->buffer.len = nbytes;
        fp->paused = 0;
        if (!fp->finished) {
            err = curl_easy_pause(fp->easy, CURLPAUSE_CONT);
            if (err != CURLE_OK) {
                errno = easy_errno(fp->easy, err);
                return -1;
            }
        }

        while (! fp->paused && ! fp->finished) {
            if (wait_perform(fp) < 0) return -1;
        }

        got = fp->buffer.ptr.rd - buffer;

        if (to_skip >= 0) { // Skipping over a small seek
            if (got <= to_skip) { // Need to skip more data
                to_skip -= got;
            } else {
                got -= to_skip;
                if (got > 0) {  // If enough was skipped, return the rest
                    memmove(buffer, buffer + to_skip, got);
                    to_skip = -1;
                }
            }
        }
    } while (to_skip >= 0 && ! fp->finished);
    fp->buffer.ptr.rd = NULL;
    fp->buffer.len = 0;

    if (fp->finished && fp->final_result != CURLE_OK) {
        errno = easy_errno(fp->easy, fp->final_result);
        return -1;
    }

    return got;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq 0xd0(%rdi), %rsi
testq %rsi, %rsi
js 0x80b50
cmpq %rsi, 0x28(%rbx)
jne 0x80c8c
movq 0xd8(%rbx), %rcx
movq 0xe0(%rbx), %rax
testq %rax, %rax
setne %dl
movq %rcx, %r13
subq %rsi, %r13
setg %dil
andb %dl, %dil
cmpb $0x1, %dil
jne 0x80b59
movq 0xe8(%rbx), %rdx
movq %rcx, %rdi
subq %rdx, %rdi
cmpq %rsi, %rdi
ja 0x80b5e
addq $0xd0, %rbx
subq %r13, %rdx
addq %rdx, %rax
cmpq %r14, %r13
movq %r14, %r12
cmovbq %r13, %r12
movq %r15, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x43c0
cmpq %r14, %r13
jbe 0x80c23
addq %r12, (%rbx)
jmp 0x80c7a
movq $-0x1, %r12
jmp 0x80bac
testq %rcx, %rcx
js 0x80b75
movq %rsi, %r12
subq %rcx, %r12
seta %al
cmpq $0xf4240, %r12          # imm = 0xF4240
setl %cl
testb %cl, %al
jne 0x80b8c
movq %rbx, %rdi
callq 0x7f86c
movq $-0x1, %r12
testl %eax, %eax
js 0x80c7a
movq $-0x1, %rax
movq %rax, 0xd0(%rbx)
movq %rax, 0xd8(%rbx)
movq $0x0, 0xe8(%rbx)
leaq 0x50(%rbx), %rbp
movb 0x64(%rbx), %al
movq %r12, %r13
movq %r15, 0x50(%rbx)
movq %r14, 0x58(%rbx)
movl %eax, %ecx
andb $-0x2, %cl
movb %cl, 0x64(%rbx)
testb $0x4, %al
jne 0x80bd9
movq 0x38(%rbx), %rdi
xorl %esi, %esi
callq 0x4970
testl %eax, %eax
jne 0x80c2d
movb 0x64(%rbx), %al
testb $0x5, %al
jne 0x80bf1
movq %rbx, %rdi
callq 0x7f676
testl %eax, %eax
jns 0x80bd9
jmp 0x80c73
movq (%rbp), %r12
subq %r15, %r12
testq %r13, %r13
js 0x80c4f
movq %r14, %rcx
movq %rbp, %r14
movq %r12, %rbp
subq %r13, %rbp
jle 0x80c12
jg 0x80c35
movq %rbp, %r12
jmp 0x80c17
subq %r12, %r13
js 0x80c4c
testb $0x4, %al
movq %r14, %rbp
movq %rcx, %r14
je 0x80bb6
jmp 0x80c4f
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, (%rbx)
jmp 0x80c7a
movq 0x38(%rbx), %rdi
movl %eax, %esi
jmp 0x80c65
addq %r15, %r13
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x4830
movb 0x64(%rbx), %al
movq %rbp, %r12
movq %r14, %rbp
xorps %xmm0, %xmm0
movups %xmm0, (%rbp)
testb $0x4, %al
je 0x80c7a
movl 0x60(%rbx), %esi
testl %esi, %esi
je 0x80c7a
movq 0x38(%rbx), %rdi
callq 0x7e55c
movl %eax, %ebx
callq 0x4050
movl %ebx, (%rax)
movq $-0x1, %r12
movq %r12, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x206e6(%rip), %rdi     # 0xa1379
leaq 0x20703(%rip), %rsi     # 0xa139d
leaq 0x2070c(%rip), %rcx     # 0xa13ad
movl $0x310, %edx            # imm = 0x310
callq 0x42f0