// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/hfile_libcurl.c
// Function: restart_from_position
static int restart_from_position(hFILE_libcurl *fp, off_t pos) {
    hFILE_libcurl temp_fp;
    CURLcode err;
    CURLMcode errm;
    int update_headers = 0;
    int save_errno = 0;

    // TODO If we seem to be doing random access, use CURLOPT_RANGE to do
    // limited reads (e.g. about a BAM block!) so seeking can reuse the
    // existing connection more often.

    // Get new headers from the callback (if defined).  This changes the
    // headers in fp before it gets duplicated, but they should be have been
    // sent by now.

    if (fp->headers.callback) {
        if (add_callback_headers(fp) != 0)
            return -1;
        update_headers = 1;
    }
    if (fp->headers.auth_hdr_num > 0 && fp->headers.auth) {
        if (add_auth_header(fp) != 0)
            return -1;
        update_headers = 1;
    }
    if (update_headers) {
        struct curl_slist *list = get_header_list(fp);
        if (list) {
            err = curl_easy_setopt(fp->easy, CURLOPT_HTTPHEADER, list);
            if (err != CURLE_OK) {
                errno = easy_errno(fp->easy,err);
                return -1;
            }
        }
    }

    /*
      Duplicate the easy handle, and use CURLOPT_RESUME_FROM_LARGE to open
      a new request to the server, reading from the location that we want
      to seek to.  If the new request works and returns the correct data,
      the original easy handle in *fp is closed and replaced with the new
      one.  If not, we close the new handle and leave *fp unchanged.
     */

    memcpy(&temp_fp, fp, sizeof(temp_fp));
    temp_fp.buffer.len = 0;
    temp_fp.buffer.ptr.rd = NULL;
    temp_fp.easy = curl_easy_duphandle(fp->easy);
    if (!temp_fp.easy)
        goto early_error;

    err = curl_easy_setopt(temp_fp.easy, CURLOPT_RESUME_FROM_LARGE,(curl_off_t)pos);
    err |= curl_easy_setopt(temp_fp.easy, CURLOPT_PRIVATE, &temp_fp);
    err |= curl_easy_setopt(temp_fp.easy, CURLOPT_WRITEDATA, &temp_fp);
    if (err != CURLE_OK) {
        save_errno = easy_errno(temp_fp.easy, err);
        goto error;
    }

    temp_fp.buffer.len = 0;  // Ensures we only read the response headers
    temp_fp.paused = temp_fp.finished = 0;

    // fp->multi and temp_fp.multi are the same.
    errm = curl_multi_add_handle(fp->multi, temp_fp.easy);
    if (errm != CURLM_OK) {
        save_errno = multi_errno(errm);
        goto error;
    }
    temp_fp.nrunning = ++fp->nrunning;

    while (! temp_fp.paused && ! temp_fp.finished)
        if (wait_perform(&temp_fp) < 0) {
            save_errno = errno;
            goto error_remove;
        }

    if (temp_fp.finished && temp_fp.final_result != CURLE_OK) {
        save_errno = easy_errno(temp_fp.easy, temp_fp.final_result);
        goto error_remove;
    }

    // We've got a good response, close the original connection and
    // replace it with the new one.

    errm = curl_multi_remove_handle(fp->multi, fp->easy);
    if (errm != CURLM_OK) {
        // Clean up as much as possible
        curl_easy_reset(temp_fp.easy);
        if (curl_multi_remove_handle(fp->multi, temp_fp.easy) == CURLM_OK) {
            fp->nrunning--;
            curl_easy_cleanup(temp_fp.easy);
        }
        save_errno = multi_errno(errm);
        goto early_error;
    }
    fp->nrunning--;

    curl_easy_cleanup(fp->easy);
    fp->easy = temp_fp.easy;
    err = curl_easy_setopt(fp->easy, CURLOPT_WRITEDATA, fp);
    err |= curl_easy_setopt(fp->easy, CURLOPT_PRIVATE, fp);
    if (err != CURLE_OK) {
        save_errno = easy_errno(fp->easy, err);
        curl_easy_reset(fp->easy);
        errno = save_errno;
        return -1;
    }
    fp->buffer.len = 0;
    fp->paused = temp_fp.paused;
    fp->finished = temp_fp.finished;
    fp->perform_again = temp_fp.perform_again;
    fp->final_result = temp_fp.final_result;

    return 0;

 error_remove:
    curl_easy_reset(temp_fp.easy); // Ensure no pointers to on-stack temp_fp
    errm = curl_multi_remove_handle(fp->multi, temp_fp.easy);
    if (errm != CURLM_OK) {
        errno = multi_errno(errm);
        return -1;
    }
    fp->nrunning--;
 error:
    curl_easy_cleanup(temp_fp.easy);
 early_error:
    fp->can_seek = 0;  // Don't try to seek again
    if (save_errno)
        errno = save_errno;
    return -1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
cmpq $0x0, 0x90(%rdi)
je 0x81dcd
movq %rbx, %rdi
callq 0x8194a
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x81f2a
movl $0x1, %eax
jmp 0x81dcf
xorl %eax, %eax
cmpl $0x0, 0xa8(%rbx)
jle 0x81dfc
cmpq $0x0, 0xa0(%rbx)
je 0x81dfc
movq %rbx, %rdi
callq 0x82412
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x81f2a
movl $0x1, %eax
testl %eax, %eax
je 0x81e58
movl $0x70, %eax
cmpl $0x0, 0x78(%rbx)
jne 0x81e1d
movl $0x80, %eax
cmpl $0x0, 0x88(%rbx)
je 0x81f3a
movq (%rbx,%rax), %rdx
testq %rdx, %rdx
je 0x81e58
movq 0x38(%rbx), %rdi
movl $0x2727, %esi           # imm = 0x2727
xorl %eax, %eax
callq 0x4580
testl %eax, %eax
je 0x81e58
movq 0x38(%rbx), %rdi
movl %eax, %esi
callq 0x80a55
movl %eax, %ebx
callq 0x4050
movl %ebx, (%rax)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x81f2a
movq %rsp, %r15
movl $0xf8, %edx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x43c0
xorps %xmm0, %xmm0
movups %xmm0, 0x50(%r15)
movq 0x38(%rbx), %rdi
callq 0x41c0
movq %rax, 0x38(%r15)
testq %rax, %rax
je 0x81edb
movq %rax, %rdi
movl $0x75a4, %esi           # imm = 0x75A4
movq %r14, %rdx
xorl %eax, %eax
callq 0x4580
movl %eax, %ebp
movq %rsp, %r14
movq 0x38(%r14), %rdi
movl $0x2777, %esi           # imm = 0x2777
movq %r14, %rdx
xorl %eax, %eax
callq 0x4580
movl %eax, %r15d
orl %ebp, %r15d
movq 0x38(%r14), %rdi
movl $0x2711, %esi           # imm = 0x2711
movq %r14, %rdx
xorl %eax, %eax
callq 0x4580
orl %r15d, %eax
je 0x81ee0
movq 0x38(%rsp), %rdi
movl %eax, %esi
callq 0x80a55
jmp 0x81f07
xorl %r14d, %r14d
jmp 0x81f14
movq $0x0, 0x58(%rsp)
andb $-0x6, 0x64(%rsp)
movq 0x40(%rbx), %rdi
movq 0x38(%rsp), %rsi
callq 0x4940
testl %eax, %eax
je 0x81f41
movl %eax, %edi
callq 0x81b62
movl %eax, %r14d
movq 0x38(%rsp), %rdi
callq 0x4620
andb $-0x21, 0x64(%rbx)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %r14d, %r14d
je 0x81f2a
callq 0x4050
movl %r14d, (%rax)
movl %ebp, %eax
addq $0xf8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
xorl %edx, %edx
jmp 0x81e21
movl 0x68(%rbx), %eax
incl %eax
movl %eax, 0x68(%rbx)
movq %rsp, %r14
movl %eax, 0x68(%r14)
movb 0x64(%rsp), %al
testb $0x5, %al
jne 0x81f6e
movq %r14, %rdi
callq 0x81bb2
testl %eax, %eax
jns 0x81f50
callq 0x4050
movl (%rax), %r14d
jmp 0x81f8e
shrb $0x2, %al
movl 0x60(%rsp), %esi
testl %esi, %esi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x81fbe
movq 0x38(%rsp), %rdi
callq 0x80a55
movl %eax, %r14d
movq 0x38(%rsp), %rdi
callq 0x43b0
movq 0x40(%rbx), %rdi
movq 0x38(%rsp), %rsi
callq 0x4160
testl %eax, %eax
je 0x81fb6
movl %eax, %edi
callq 0x81b62
jmp 0x81e45
decl 0x68(%rbx)
jmp 0x81f0a
movq 0x38(%rbx), %rsi
movq 0x40(%rbx), %rdi
callq 0x4160
testl %eax, %eax
je 0x82009
movl %eax, %ebp
movq 0x38(%rsp), %rdi
callq 0x43b0
movq 0x40(%rbx), %rdi
movq 0x38(%rsp), %rsi
callq 0x4160
testl %eax, %eax
jne 0x81ffa
decl 0x68(%rbx)
movq 0x38(%rsp), %rdi
callq 0x4620
movl %ebp, %edi
callq 0x81b62
movl %eax, %r14d
jmp 0x81f14
decl 0x68(%rbx)
movq 0x38(%rbx), %rdi
callq 0x4620
movq 0x38(%rsp), %rdi
movq %rdi, 0x38(%rbx)
xorl %ebp, %ebp
movl $0x2711, %esi           # imm = 0x2711
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4580
movl %eax, %r14d
movq 0x38(%rbx), %rdi
movl $0x2777, %esi           # imm = 0x2777
movq %rbx, %rdx
xorl %eax, %eax
callq 0x4580
orl %r14d, %eax
je 0x8206c
movq 0x38(%rbx), %rdi
movl %eax, %esi
callq 0x80a55
movl %eax, %ebp
movq 0x38(%rbx), %rdi
callq 0x43b0
callq 0x4050
movl %ebp, (%rax)
jmp 0x81e4e
movq $0x0, 0x58(%rbx)
movb 0x64(%rsp), %al
movb 0x64(%rbx), %cl
andb $-0xe, %cl
andb $0xd, %al
orb %cl, %al
movb %al, 0x64(%rbx)
movl 0x60(%rsp), %eax
movl %eax, 0x60(%rbx)
jmp 0x81f2a