// File: /workspace/github_c_makefile_star2_O1/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x8(%rdi), %rax
cmpq 0x10(%rdi), %rax
jbe 0xf5b9
testb $0x2, 0x30(%rbx)
je 0xf5b9
movq %rbx, %rdi
callq 0xf42c
movslq %eax, %r15
testl %eax, %eax
js 0xf6be
movq (%rbx), %r12
movq 0x28(%rbx), %rdi
cmpl $0x1, %ebp
jne 0xf5fe
leaq (%rdi,%r14), %rsi
subq %r12, %rsi
xorl %ebp, %ebp
addq 0x8(%rbx), %rsi
jns 0xf65e
testq %r14, %r14
movl $0x16, %eax
movl $0x4b, %ebp
cmovsl %eax, %ebp
callq 0x4050
movl %ebp, (%rax)
movl %ebp, 0x34(%rbx)
movq $-0x1, %r15
jmp 0xf6be
cmpl $0x2, %ebp
jne 0xf630
movb 0x30(%rbx), %al
andb $0x2, %al
jne 0xf630
movq 0x10(%rbx), %rax
subq %r12, %rax
testq %r14, %r14
setle %cl
movq %r14, %rdx
negq %rdx
cmpq %rdx, %rax
setae %r13b
andb %cl, %r13b
je 0xf635
addq %rax, %r14
xorl %ebp, %ebp
jmp 0xf656
movq %r14, %rsi
jmp 0xf65e
movq %rdi, %r15
callq 0x4050
movq %r15, %rdi
movl $0x16, %ecx
movl %ecx, (%rax)
movl %ecx, 0x34(%rbx)
movq $-0x1, %r15
movl $0x2, %ebp
movq %r14, %rsi
testb %r13b, %r13b
je 0xf6be
testl %ebp, %ebp
je 0xf68b
movq 0x20(%rbx), %rax
movq %rbx, %rdi
movl %ebp, %edx
callq *0x10(%rax)
movq %rax, %r15
testq %rax, %rax
js 0xf6b4
movq (%rbx), %rax
movq %rax, 0x10(%rbx)
movq %rax, 0x8(%rbx)
andb $-0x2, 0x30(%rbx)
movq %r15, 0x28(%rbx)
jmp 0xf6be
movb 0x30(%rbx), %al
andb $0x6, %al
cmpb $0x2, %al
je 0xf662
movq %rsi, %rax
subq %rdi, %rax
jl 0xf662
movq 0x10(%rbx), %rcx
subq %r12, %rcx
cmpq %rcx, %rax
jg 0xf662
addq %rax, %r12
movq %r12, 0x8(%rbx)
movq %rsi, %r15
jmp 0xf6be
callq 0x4050
movl (%rax), %eax
movl %eax, 0x34(%rbx)
movq %r15, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq