// File: /workspace/github_c_makefile_star2_O2/samtools[P]htslib/hfile.c
// Function: hseek
off_t hseek(hFILE *fp, off_t offset, int whence)
{
    off_t curpos, pos;

    if (writebuffer_is_nonempty(fp) && fp->mobile) {
        int ret = flush_buffer(fp);
        if (ret < 0) return ret;
    }

    curpos = htell(fp);

    // Relative offsets are given relative to the hFILE's stream position,
    // which may differ from the backend's physical position due to buffering
    // read-ahead.  Correct for this by converting to an absolute position.
    if (whence == SEEK_CUR) {
        if (curpos + offset < 0) {
            // Either a negative offset resulted in a position before the
            // start of the file, or we overflowed when given a positive offset
            fp->has_errno = errno = (offset < 0)? EINVAL : EOVERFLOW;
            return -1;
        }

        whence = SEEK_SET;
        offset = curpos + offset;
    }
    // For fixed immobile buffers, convert everything else to SEEK_SET too
    // so that seeking can be avoided for all (within range) requests.
    else if (! fp->mobile && whence == SEEK_END) {
        size_t length = fp->end - fp->buffer;
        if (offset > 0 || -offset > length) {
            fp->has_errno = errno = EINVAL;
            return -1;
        }

        whence = SEEK_SET;
        offset = length + offset;
    }

    // Avoid seeking if the desired position is within our read buffer.
    // (But not when the next operation may be a write on a mobile buffer.)
    if (whence == SEEK_SET && (! fp->mobile || fp->readonly) &&
        offset >= fp->offset && offset - fp->offset <= fp->end - fp->buffer) {
        fp->begin = &fp->buffer[offset - fp->offset];
        return offset;
    }

    pos = fp->backend->seek(fp, offset, whence);
    if (pos < 0) { fp->has_errno = errno; return pos; }

    // Seeking succeeded, so discard any non-empty read buffer
    fp->begin = fp->end = fp->buffer;
    fp->at_eof = 0;

    fp->offset = pos;
    return pos;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x8(%rdi), %rax
cmpq 0x10(%rdi), %rax
jbe 0xd7f7
testb $0x2, 0x30(%rbx)
je 0xd7f7
movq %rbx, %rdi
callq 0xd690
testl %eax, %eax
js 0xd881
movq (%rbx), %rax
movq 0x28(%rbx), %rcx
cmpl $0x1, %ebp
jne 0xd818
leaq (%rcx,%r14), %rsi
subq %rax, %rsi
addq 0x8(%rbx), %rsi
js 0xd865
movb 0x30(%rbx), %dl
movq %rsi, %r14
jmp 0xd889
movb 0x30(%rbx), %dl
movl %edx, %esi
andb $0x2, %sil
shrb %sil
cmpl $0x2, %ebp
setne %dil
orb %sil, %dil
jne 0xd85f
movq 0x10(%rbx), %rsi
subq %rax, %rsi
testq %r14, %r14
setg %dil
movq %r14, %r8
negq %r8
cmpq %r8, %rsi
setb %r8b
orb %dil, %r8b
je 0xd886
callq 0x4050
pushq $0x16
popq %rcx
movl %ecx, (%rax)
movl %ecx, 0x34(%rbx)
jmp 0xd87b
testl %ebp, %ebp
jne 0xd8a7
jmp 0xd889
testq %r14, %r14
pushq $0x16
popq %rax
pushq $0x4b
popq %rbp
cmovsl %eax, %ebp
callq 0x4050
movl %ebp, (%rax)
movl %ebp, 0x34(%rbx)
pushq $-0x1
popq %r14
jmp 0xd8e6
movslq %eax, %r14
jmp 0xd8e6
addq %rsi, %r14
andb $0x6, %dl
xorl %ebp, %ebp
cmpb $0x2, %dl
je 0xd8a7
movq %r14, %rdx
subq %rcx, %rdx
jl 0xd8a7
movq 0x10(%rbx), %rcx
subq %rax, %rcx
cmpq %rcx, %rdx
jle 0xd8df
movq 0x20(%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq *0x10(%rax)
movq %rax, %r14
testq %rax, %rax
js 0xd8d3
movq (%rbx), %rax
movq %rax, 0x10(%rbx)
movq %rax, 0x8(%rbx)
andb $-0x2, 0x30(%rbx)
movq %r14, 0x28(%rbx)
jmp 0xd8e6
callq 0x4050
movl (%rax), %eax
movl %eax, 0x34(%rbx)
jmp 0xd8e6
addq %rdx, %rax
movq %rax, 0x8(%rbx)
movq %r14, %rax
popq %rbx
popq %r14
popq %rbp
retq