// File: /workspace/github_c_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %r15
movq %rdi, %r14
movq 0x90(%rdi), %r12
pushq $-0x1
popq %rbx
testq %r12, %r12
je 0x65804
movq %r14, %rdi
callq 0x65477
testl %eax, %eax
jne 0x65942
cmpl $0x0, 0xa8(%r14)
jle 0x6582a
cmpq $0x0, 0xa0(%r14)
je 0x6582a
movq %r14, %rdi
callq 0x65be9
testl %eax, %eax
jne 0x65942
jmp 0x6582f
testq %r12, %r12
je 0x65879
cmpl $0x0, 0x78(%r14)
je 0x6583b
pushq $0x70
popq %rax
jmp 0x6584a
movl $0x80, %eax
cmpl $0x0, 0x88(%r14)
je 0x65879
movq (%r14,%rax), %rdx
testq %rdx, %rdx
je 0x65879
movq 0x38(%r14), %rdi
movl $0x2727, %esi           # imm = 0x2727
xorl %eax, %eax
callq 0x4580
testl %eax, %eax
je 0x65879
movq 0x38(%r14), %rdi
movl %eax, %esi
callq 0x645af
movl %eax, %ebp
jmp 0x6593b
movq %rsp, %r13
movl $0xf8, %edx
movq %r13, %rdi
movq %r14, %rsi
callq 0x43c0
xorps %xmm0, %xmm0
movups %xmm0, 0x50(%r13)
movq 0x38(%r14), %rdi
callq 0x41c0
movq %rax, 0x38(%r13)
testq %rax, %rax
je 0x658fe
movq %rax, %r12
movq %rax, %rdi
movl $0x75a4, %esi           # imm = 0x75A4
movq %r15, %rdx
xorl %eax, %eax
callq 0x4580
movl %eax, %ebp
movq %rsp, %r15
movq %r12, %rdi
movl $0x2777, %esi           # imm = 0x2777
movq %r15, %rdx
xorl %eax, %eax
callq 0x4580
movl %eax, %r12d
orl %ebp, %r12d
movq 0x38(%r15), %rdi
movl $0x2711, %esi           # imm = 0x2711
movq %r15, %rdx
xorl %eax, %eax
callq 0x4580
orl %r12d, %eax
je 0x65902
movq 0x38(%rsp), %rdi
movl %eax, %esi
callq 0x645af
jmp 0x65926
xorl %ebp, %ebp
jmp 0x65932
andq $0x0, 0x58(%rsp)
andb $-0x6, 0x64(%rsp)
movq 0x40(%r14), %rdi
movq 0x38(%rsp), %rsi
callq 0x4940
testl %eax, %eax
je 0x65956
movl %eax, %edi
callq 0x655d1
movl %eax, %ebp
movq 0x38(%rsp), %rdi
callq 0x4620
andb $-0x21, 0x64(%r14)
testl %ebp, %ebp
je 0x65942
callq 0x4050
movl %ebp, (%rax)
movl %ebx, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x68(%r14), %eax
incl %eax
movl %eax, 0x68(%r14)
movq %rsp, %r15
movl %eax, 0x68(%r15)
movb 0x64(%rsp), %al
testb $0x5, %al
jne 0x65984
movq %r15, %rdi
callq 0x6561b
testl %eax, %eax
jns 0x65967
callq 0x4050
movl (%rax), %ebp
jmp 0x659a3
shrb $0x2, %al
movl 0x60(%rsp), %esi
testl %esi, %esi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x659d4
movq 0x38(%rsp), %rdi
callq 0x645af
movl %eax, %ebp
movq 0x38(%rsp), %rdi
callq 0x43b0
movq 0x40(%r14), %rdi
movq 0x38(%rsp), %rsi
callq 0x4160
testl %eax, %eax
je 0x659cb
movl %eax, %edi
callq 0x655d1
jmp 0x65872
decl 0x68(%r14)
jmp 0x65928
movq 0x38(%r14), %rsi
movq 0x40(%r14), %rdi
callq 0x4160
testl %eax, %eax
je 0x65a1f
movl %eax, %ebp
movq 0x38(%rsp), %rdi
callq 0x43b0
movq 0x40(%r14), %rdi
movq 0x38(%rsp), %rsi
callq 0x4160
testl %eax, %eax
jne 0x65a11
decl 0x68(%r14)
movq 0x38(%rsp), %rdi
callq 0x4620
movl %ebp, %edi
callq 0x655d1
movl %eax, %ebp
jmp 0x65932
decl 0x68(%r14)
movq 0x38(%r14), %rdi
callq 0x4620
movq 0x38(%rsp), %rdi
movq %rdi, 0x38(%r14)
movl $0x2711, %esi           # imm = 0x2711
movq %r14, %rdx
xorl %eax, %eax
callq 0x4580
movl %eax, %ebp
movq 0x38(%r14), %rdi
movl $0x2777, %esi           # imm = 0x2777
movq %r14, %rdx
xorl %eax, %eax
callq 0x4580
orl %ebp, %eax
je 0x65a78
movq 0x38(%r14), %rdi
movl %eax, %esi
callq 0x645af
movl %eax, %ebp
movq 0x38(%r14), %rdi
callq 0x43b0
jmp 0x6593b
andq $0x0, 0x58(%r14)
movb 0x64(%rsp), %al
movb 0x64(%r14), %cl
andb $-0xe, %cl
andb $0xd, %al
orb %cl, %al
movb %al, 0x64(%r14)
movl 0x60(%rsp), %eax
movl %eax, 0x60(%r14)
xorl %ebx, %ebx
jmp 0x65942