// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x13fd0
cmpl $0x0, %eax
je 0x146f5
movq -0x10(%rbp), %rax
movb 0x30(%rax), %al
shrb %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x146f5
movq -0x10(%rbp), %rdi
callq 0x14430
movl %eax, -0x34(%rbp)
cmpl $0x0, -0x34(%rbp)
jge 0x146f3
movslq -0x34(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x148dc
jmp 0x146f5
movq -0x10(%rbp), %rdi
callq 0x148f0
movq %rax, -0x28(%rbp)
cmpl $0x1, -0x1c(%rbp)
jne 0x14761
movq -0x28(%rbp), %rax
addq -0x18(%rbp), %rax
cmpq $0x0, %rax
jge 0x1474c
movq -0x18(%rbp), %rdx
movl $0x4b, %eax
movl $0x16, %ecx
cmpq $0x0, %rdx
cmovll %ecx, %eax
movl %eax, -0x44(%rbp)
callq 0x4050
movl -0x44(%rbp), %ecx
movl %ecx, (%rax)
movq -0x10(%rbp), %rax
movl %ecx, 0x34(%rax)
movq $-0x1, -0x8(%rbp)
jmp 0x148dc
movl $0x0, -0x1c(%rbp)
movq -0x28(%rbp), %rax
addq -0x18(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x147db
movq -0x10(%rbp), %rax
movb 0x30(%rax), %al
shrb %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x147d9
cmpl $0x2, -0x1c(%rbp)
jne 0x147d9
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movq (%rcx), %rcx
subq %rcx, %rax
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x18(%rbp)
jg 0x147a3
xorl %eax, %eax
subq -0x18(%rbp), %rax
cmpq -0x40(%rbp), %rax
jbe 0x147c6
callq 0x4050
movl $0x16, (%rax)
movq -0x10(%rbp), %rax
movl $0x16, 0x34(%rax)
movq $-0x1, -0x8(%rbp)
jmp 0x148dc
movl $0x0, -0x1c(%rbp)
movq -0x40(%rbp), %rax
addq -0x18(%rbp), %rax
movq %rax, -0x18(%rbp)
jmp 0x147db
cmpl $0x0, -0x1c(%rbp)
jne 0x14865
movq -0x10(%rbp), %rax
movb 0x30(%rax), %al
shrb %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x1480c
movq -0x10(%rbp), %rax
movb 0x30(%rax), %al
shrb $0x2, %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x14865
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jl 0x14865
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
subq 0x28(%rcx), %rax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movq -0x10(%rbp), %rdx
movq (%rdx), %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jg 0x14865
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rdx
subq 0x28(%rdx), %rax
addq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x148dc
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x1c(%rbp), %edx
callq *%rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jge 0x148a1
callq 0x4050
movl (%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x34(%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x148dc
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movb 0x30(%rax), %cl
andb $-0x2, %cl
orb $0x0, %cl
movb %cl, 0x30(%rax)
movq -0x30(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)