// File: /workspace/github_c_makefile_star2_O3/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 %r12
pushq %rbx
subq $0x100, %rsp            # imm = 0x100
movq %rsi, %r14
movq %rdi, %rbx
movq 0x90(%rdi), %r15
testq %r15, %r15
je 0x7f8a2
movq %rbx, %rdi
callq 0x7f40e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x7fa00
cmpl $0x0, 0xa8(%rbx)
jle 0x7f8cc
cmpq $0x0, 0xa0(%rbx)
je 0x7f8cc
movq %rbx, %rdi
callq 0x7ff04
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x7fa00
jmp 0x7f8d1
testq %r15, %r15
je 0x7f920
movl $0x70, %eax
cmpl $0x0, 0x78(%rbx)
jne 0x7f8ea
movl $0x80, %eax
cmpl $0x0, 0x88(%rbx)
je 0x7f920
movq (%rbx,%rax), %rdx
testq %rdx, %rdx
je 0x7f920
movq 0x38(%rbx), %rdi
movl $0x2727, %esi           # imm = 0x2727
xorl %eax, %eax
callq 0x4580
testl %eax, %eax
je 0x7f920
movq 0x38(%rbx), %rdi
movl %eax, %esi
callq 0x7e55c
movl %eax, %ebx
callq 0x4050
movl %ebx, (%rax)
jmp 0x7f9af
leaq 0x8(%rsp), %r12
movl $0xf8, %edx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x43c0
xorps %xmm0, %xmm0
movups %xmm0, 0x50(%r12)
movq 0x38(%rbx), %rdi
callq 0x41c0
movq %rax, 0x38(%r12)
testq %rax, %rax
je 0x7f9ab
movq %rax, %r15
movq %rax, %rdi
movl $0x75a4, %esi           # imm = 0x75A4
movq %r14, %rdx
xorl %eax, %eax
callq 0x4580
movl %eax, %ebp
leaq 0x8(%rsp), %r14
movq %r15, %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 0x7f9b6
movq 0x40(%rsp), %rdi
movl %eax, %esi
callq 0x7e55c
jmp 0x7f9dd
andb $-0x21, 0x64(%rbx)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x7fa00
movq $0x0, 0x60(%rsp)
andb $-0x6, 0x6c(%rsp)
movq 0x40(%rbx), %rdi
movq 0x40(%rsp), %rsi
callq 0x4940
testl %eax, %eax
je 0x7fa12
movl %eax, %edi
callq 0x7f626
movl %eax, %r14d
movq 0x40(%rsp), %rdi
callq 0x4620
andb $-0x21, 0x64(%rbx)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %r14d, %r14d
je 0x7fa00
callq 0x4050
movl %r14d, (%rax)
movl %ebp, %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movl 0x68(%rbx), %eax
incl %eax
movl %eax, 0x68(%rbx)
leaq 0x8(%rsp), %r14
movl %eax, 0x68(%r14)
movb 0x6c(%rsp), %al
testb $0x5, %al
jne 0x7fa41
movq %r14, %rdi
callq 0x7f676
testl %eax, %eax
jns 0x7fa23
callq 0x4050
movl (%rax), %r14d
jmp 0x7fa61
shrb $0x2, %al
movl 0x68(%rsp), %esi
testl %esi, %esi
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x7fa91
movq 0x40(%rsp), %rdi
callq 0x7e55c
movl %eax, %r14d
movq 0x40(%rsp), %rdi
callq 0x43b0
movq 0x40(%rbx), %rdi
movq 0x40(%rsp), %rsi
callq 0x4160
testl %eax, %eax
je 0x7fa89
movl %eax, %edi
callq 0x7f626
jmp 0x7f912
decl 0x68(%rbx)
jmp 0x7f9e0
movq 0x38(%rbx), %rsi
movq 0x40(%rbx), %rdi
callq 0x4160
testl %eax, %eax
je 0x7fadc
movl %eax, %ebp
movq 0x40(%rsp), %rdi
callq 0x43b0
movq 0x40(%rbx), %rdi
movq 0x40(%rsp), %rsi
callq 0x4160
testl %eax, %eax
jne 0x7facd
decl 0x68(%rbx)
movq 0x40(%rsp), %rdi
callq 0x4620
movl %ebp, %edi
callq 0x7f626
movl %eax, %r14d
jmp 0x7f9ea
decl 0x68(%rbx)
movq 0x38(%rbx), %rdi
callq 0x4620
movq 0x40(%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 0x7fb3f
movq 0x38(%rbx), %rdi
movl %eax, %esi
callq 0x7e55c
movl %eax, %ebp
movq 0x38(%rbx), %rdi
callq 0x43b0
callq 0x4050
movl %ebp, (%rax)
jmp 0x7f9af
movq $0x0, 0x58(%rbx)
movb 0x6c(%rsp), %al
movb 0x64(%rbx), %cl
andb $-0xe, %cl
andb $0xd, %al
orb %cl, %al
movb %al, 0x64(%rbx)
movl 0x68(%rsp), %eax
movl %eax, 0x60(%rbx)
jmp 0x7fa00