// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/bgzf.c
// Function: bgzf_useek
int bgzf_useek(BGZF *fp, off_t uoffset, int where)
{
    if (fp->is_write || where != SEEK_SET || fp->is_gzip) {
        fp->errcode |= BGZF_ERR_MISUSE;
        return -1;
    }
    if (uoffset >= fp->uncompressed_address - fp->block_offset &&
        uoffset < fp->uncompressed_address + fp->block_length - fp->block_offset) {
        // Can seek into existing data
        fp->block_offset += uoffset - fp->uncompressed_address;
        fp->uncompressed_address = uoffset;
        return 0;
    }
    if ( !fp->is_compressed )
    {
        if (hseek(fp->fp, uoffset, SEEK_SET) < 0)
        {
            fp->errcode |= BGZF_ERR_IO;
            return -1;
        }
        fp->block_length = 0;  // indicates current block has not been loaded
        fp->block_address = uoffset;
        fp->block_offset = 0;
        if (bgzf_read_block(fp) < 0) {
            fp->errcode |= BGZF_ERR_IO;
            return -1;
        }
        fp->uncompressed_address = uoffset;
        return 0;
    }

    if ( !fp->idx )
    {
        fp->errcode |= BGZF_ERR_IO;
        return -1;
    }

    // binary search
    int ilo = 0, ihi = fp->idx->noffs - 1;
    while ( ilo<=ihi )
    {
        int i = (ilo+ihi)*0.5;
        if ( uoffset < fp->idx->offs[i].uaddr ) ihi = i - 1;
        else if ( uoffset >= fp->idx->offs[i].uaddr ) ilo = i + 1;
        else break;
    }
    int i = ilo-1;
    off_t offset = 0;
    if (bgzf_seek_common(fp, fp->idx->offs[i].caddr, 0) < 0)
        return -1;

    if ( bgzf_read_block(fp) < 0 ) {
        fp->errcode |= BGZF_ERR_IO;
        return -1;
    }
    offset = uoffset - fp->idx->offs[i].uaddr;
    if ( offset > 0 )
    {
        if (offset > fp->block_length) {
            fp->errcode |= BGZF_ERR_IO;
            return -1;                                      //offset outside the available data
        }
        fp->block_offset = offset;
        assert( fp->block_offset <= fp->block_length );     // todo: skipped, unindexed, blocks
    }
    fp->uncompressed_address = uoffset;
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl (%rdi), %ecx
testl %ecx, %ecx
js 0xe650
movl %ecx, %eax
andl $0x20000, %eax          # imm = 0x20000
orl %edx, %eax
jne 0xe650
movq %rsi, %r14
movq 0x20(%rbx), %rdx
movslq 0x10(%rbx), %rax
movq %rdx, %rsi
subq %rax, %rsi
cmpq %r14, %rsi
jg 0xe65c
movslq 0x8(%rbx), %rdi
addq %rdi, %rsi
cmpq %r14, %rsi
jle 0xe65c
movl %r14d, %ecx
subl %edx, %ecx
addl %eax, %ecx
movl %ecx, 0x10(%rbx)
jmp 0xe693
orl $0x8, %ecx
movl %ecx, (%rbx)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0xe699
cmpl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
ja 0xe6a6
movq 0x40(%rbx), %rdi
xorl %ebp, %ebp
movq %r14, %rsi
xorl %edx, %edx
callq 0xf583
testq %rax, %rax
js 0xe703
movl %ebp, 0x8(%rbx)
movq %r14, 0x18(%rbx)
movl %ebp, 0x10(%rbx)
movq %rbx, %rdi
callq 0xae60
testl %eax, %eax
js 0xe703
movq %r14, 0x20(%rbx)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x50(%rbx), %rax
testq %rax, %rax
je 0xe70b
movl (%rax), %ecx
testl %ecx, %ecx
jle 0xe713
decl %ecx
movq 0x8(%rax), %rdx
xorl %esi, %esi
movsd 0x8795b(%rip), %xmm0    # 0x96020
movl %esi, %edi
leal (%rdi,%rcx), %esi
xorps %xmm1, %xmm1
cvtsi2sd %esi, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %esi
movslq %esi, %rsi
movq %rsi, %r8
shlq $0x4, %r8
leal -0x1(%rsi), %r9d
incl %esi
cmpq %r14, (%rdx,%r8)
cmovbel %ecx, %r9d
cmoval %edi, %esi
movl %r9d, %ecx
cmpl %r9d, %esi
jle 0xe6c5
decl %esi
movslq %esi, %r15
jmp 0xe71a
orb $0x4, (%rbx)
jmp 0xe655
orl $0x4, %ecx
jmp 0xe653
movq $-0x1, %r15
movq 0x8(%rax), %rax
shlq $0x4, %r15
movq 0x8(%rax,%r15), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xd9db
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testq %rax, %rax
js 0xe699
movq %rbx, %rdi
callq 0xae60
testl %eax, %eax
js 0xe76c
movq 0x50(%rbx), %rax
movq 0x8(%rax), %rcx
movq %r14, %rax
subq (%rcx,%r15), %rax
testq %rax, %rax
jle 0xe693
movslq 0x8(%rbx), %rcx
cmpq %rcx, %rax
jle 0xe774
orb $0x4, (%rbx)
jmp 0xe699
movl %eax, 0x10(%rbx)
cmpl %eax, %ecx
jge 0xe693
leaq 0x8971e(%rip), %rdi     # 0x97ea4
leaq 0x89453(%rip), %rsi     # 0x97be0
leaq 0x89735(%rip), %rcx     # 0x97ec9
movl $0xa26, %edx            # imm = 0xA26
callq 0x42f0