// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movq %rsi, %r15
movq %rdi, %r14
movq 0xd0(%rdi), %rsi
testq %rsi, %rsi
js 0x83075
cmpq %rsi, 0x28(%r14)
jne 0x8319b
leaq 0xd0(%r14), %r13
movq 0xe0(%r14), %rax
testq %rax, %rax
je 0x83024
movq 0xd8(%r14), %rdx
movq %rdx, %rbp
subq %rsi, %rbp
jle 0x83024
movq 0xe8(%r14), %rcx
subq %rcx, %rdx
cmpq %rsi, %rdx
jbe 0x83139
movq 0xd8(%r14), %rax
testq %rax, %rax
js 0x83047
movq %rsi, %r12
subq %rax, %r12
setg %al
cmpq $0xf4240, %r12          # imm = 0xF4240
setl %cl
testb %cl, %al
jne 0x8305e
movq %r14, %rdi
callq 0x81d94
movq $-0x1, %r12
testl %eax, %eax
js 0x83189
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, (%r13)
movq $0x0, 0xe8(%r14)
jmp 0x8307c
movq $-0x1, %r12
leaq 0x50(%r14), %rbp
movq %r12, %r13
movq %r15, 0x50(%r14)
movq %rbx, 0x58(%r14)
movb 0x64(%r14), %al
movl %eax, %ecx
andb $-0x2, %cl
movb %cl, 0x64(%r14)
testb $0x4, %al
jne 0x830af
movq 0x38(%r14), %rdi
xorl %esi, %esi
callq 0x4970
testl %eax, %eax
jne 0x8316e
testb $0x5, 0x64(%r14)
jne 0x830c7
movq %r14, %rdi
callq 0x81bb2
testl %eax, %eax
jns 0x830af
jmp 0x83182
movq (%rbp), %r12
subq %r15, %r12
testq %r13, %r13
js 0x8310b
movq %rbx, (%rsp)
movq %rbp, %rbx
movq %r12, %rbp
subq %r13, %rbp
jle 0x83101
jle 0x830fc
addq %r15, %r13
movq %r15, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x4830
movq $-0x1, %r13
movq %rbp, %r12
jmp 0x83104
subq %r12, %r13
movq %rbx, %rbp
movq (%rsp), %rbx
testq %r13, %r13
js 0x8311b
testb $0x4, 0x64(%r14)
je 0x83083
pxor %xmm0, %xmm0
movdqu %xmm0, (%rbp)
testb $0x4, 0x64(%r14)
je 0x83189
movl 0x60(%r14), %esi
testl %esi, %esi
je 0x83189
movq 0x38(%r14), %rdi
jmp 0x83174
subq %rbp, %rcx
addq %rcx, %rax
cmpq %rbx, %rbp
movq %rbx, %r12
cmovbq %rbp, %r12
movq %r15, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x43c0
cmpq %rbx, %rbp
jbe 0x83162
addq %r12, (%r13)
jmp 0x83189
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, (%r13)
jmp 0x83189
movq 0x38(%r14), %rdi
movl %eax, %esi
callq 0x80a55
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 0x20a47(%rip), %rdi     # 0xa3be9
leaq 0x20a64(%rip), %rsi     # 0xa3c0d
leaq 0x20a6d(%rip), %rcx     # 0xa3c1d
movl $0x310, %edx            # imm = 0x310
callq 0x42f0