// File: /workspace/github_c_makefile_star2_O3/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 0xf3df
testb $0x2, 0x30(%rbx)
je 0xf3df
movq %rbx, %rdi
callq 0xf270
testl %eax, %eax
js 0xf472
movq (%rbx), %rax
movq 0x28(%rbx), %rcx
cmpl $0x1, %ebp
jne 0xf400
leaq (%rcx,%r14), %rsi
subq %rax, %rsi
addq 0x8(%rbx), %rsi
js 0xf44f
movb 0x30(%rbx), %dl
movq %rsi, %r14
jmp 0xf47a
movb 0x30(%rbx), %dl
movl %edx, %esi
andb $0x2, %sil
shrb %sil
cmpl $0x2, %ebp
setne %dil
orb %sil, %dil
jne 0xf449
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 0xf477
callq 0x4050
movl $0x16, %ecx
movl %ecx, (%rax)
movl %ecx, 0x34(%rbx)
jmp 0xf469
testl %ebp, %ebp
jne 0xf498
jmp 0xf47a
testq %r14, %r14
movl $0x16, %eax
movl $0x4b, %ebp
cmovsl %eax, %ebp
callq 0x4050
movl %ebp, (%rax)
movl %ebp, 0x34(%rbx)
movq $-0x1, %r14
jmp 0xf4d7
movslq %eax, %r14
jmp 0xf4d7
addq %rsi, %r14
andb $0x6, %dl
xorl %ebp, %ebp
cmpb $0x2, %dl
je 0xf498
movq %r14, %rdx
subq %rcx, %rdx
jl 0xf498
movq 0x10(%rbx), %rcx
subq %rax, %rcx
cmpq %rcx, %rdx
jle 0xf4d0
movq 0x20(%rbx), %rax
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq *0x10(%rax)
movq %rax, %r14
testq %rax, %rax
js 0xf4c4
movq (%rbx), %rax
movq %rax, 0x10(%rbx)
movq %rax, 0x8(%rbx)
andb $-0x2, 0x30(%rbx)
movq %r14, 0x28(%rbx)
jmp 0xf4d7
callq 0x4050
movl (%rax), %eax
movl %eax, 0x34(%rbx)
jmp 0xf4d7
addq %rdx, %rax
movq %rax, 0x8(%rbx)
movq %r14, %rax
popq %rbx
popq %r14
popq %rbp
retq