// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/tovlp.cpp
// Function: refine_utg_thit_t(utg_thit_t*, kv_ca_buf_t*)
void refine_utg_thit_t(utg_thit_t *q, kv_ca_buf_t* cb)
{
    ///already know [qScur, qEcur), [qSpre, qEpre)
    uint32_t s, e, i, si, ei;
    s = q->qs; e = q->qe;///[s, e)
    for (i = 0, si = ei = cb->n; i < cb->n; i++)
    {
        if(cb->a[i].c_x_p > s && si == cb->n)
        {
            si = i;
        } 

        if(cb->a[i].c_x_p > (e-1) && ei == cb->n)
        {
            ei = i;
        }

        if(si != cb->n && ei != cb->n) break;        
    }

    if(si == 0 || ei == 0) fprintf(stderr, "ERROR-si-ei-0\n");
    if(si >= cb->n || ei >= cb->n) fprintf(stderr, "ERROR-si-ei-1\n");
    si--; ei--;

    if(s < cb->a[si].c_x_p || ((si + 1) < cb->n && s >= cb->a[si + 1].c_x_p))
    {
        fprintf(stderr, "ERROR3\n");
    }
    if(e < cb->a[ei].c_x_p || ((ei + 1) < cb->n && e > cb->a[ei + 1].c_x_p))
    {
        fprintf(stderr, "ERROR4\n");
    }
    ///si and ei must be less than (cb->n-1)    
    q->ts = cb->a[si].c_y_p + 
        get_offset_adjust(s-cb->a[si].c_x_p, cb->a[si+1].c_x_p-cb->a[si].c_x_p, cb->a[si+1].c_y_p-cb->a[si].c_y_p);
 
    q->te = cb->a[ei].c_y_p + 
        get_offset_adjust(e-cb->a[ei].c_x_p, cb->a[ei+1].c_x_p-cb->a[ei].c_x_p, cb->a[ei+1].c_y_p-cb->a[ei].c_y_p);
    
    ///might be equal
    if(q->ts > q->te) fprintf(stderr, "ERROR5\n");
    // if(q->tScur >= q->tEcur)
    // {
    //     fprintf(stderr, "\n###q->tScur: %u, s: %u, si: %u, q->tEcur: %u, e: %u, ei: %u\n", 
    //     q->tScur, s, si, q->tEcur, e, ei);

    //     fprintf(stderr, "###cb->a[si].c_x_p: %u, cb->a[si].c_y_p: %u, cb->a[si+1].c_x_p: %u, cb->a[si+1].c_y_p: %u\n", 
    //     cb->a[si].c_x_p, cb->a[si].c_y_p, cb->a[si+1].c_x_p, cb->a[si+1].c_y_p);

    //     fprintf(stderr, "###cb->a[ei].c_x_p: %u, cb->a[ei].c_y_p: %u, cb->a[ei+1].c_x_p: %u, cb->a[ei+1].c_y_p: %u\n", 
    //     cb->a[ei].c_x_p, cb->a[ei].c_y_p, cb->a[ei+1].c_x_p, cb->a[ei+1].c_y_p);

    //     fprintf(stderr, "ERROR5\n");
    // }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movl (%rdi), %r12d
movq %rdi, 0x10(%rsp)
movl 0x4(%rdi), %r11d
movq (%rsi), %rax
testq %rax, %rax
je 0x1973a3
movq 0x10(%r14), %rcx
leal -0x1(%r11), %edx
movl $0x1, %esi
xorl %edi, %edi
movl %eax, %r13d
movl %eax, %ebp
movl %ebp, %r8d
movl %r13d, %r9d
leal -0x1(%rsi), %r13d
leaq (%rdi,%rdi,8), %r10
leaq (%r10,%r10,2), %r10
addq %rdi, %r10
movl 0x10(%rcx,%r10), %edi
cmpl %r12d, %edi
cmoval %r13d, %ebp
movl %r8d, %r10d
cmpq %r10, %rax
cmovnel %r8d, %ebp
movl %r9d, %r8d
cmpq %r8, %rax
cmovnel %r9d, %r13d
cmpl %edx, %edi
cmovbel %r9d, %r13d
cmpq %rbp, %rax
sete %dil
movl %r13d, %r8d
cmpq %r8, %rax
sete %r8b
orb %dil, %r8b
cmpb $0x1, %r8b
jne 0x1971d8
movl %esi, %edi
incl %esi
cmpq %rdi, %rax
ja 0x19717a
testl %ebp, %ebp
je 0x1973a8
testl %r13d, %r13d
je 0x1973a8
movl %ebp, %ebp
movl %r13d, %r13d
cmpq %rbp, %rax
jbe 0x1973e0
cmpq %r13, %rax
jbe 0x1973e0
leal -0x1(%rbp), %ecx
leal -0x1(%r13), %r15d
movq 0x10(%r14), %rax
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rbx
addq %rcx, %rbx
cmpl 0x10(%rax,%rbx), %r12d
jb 0x19733d
cmpq %rbp, (%r14)
jbe 0x197243
leaq (,%rbp,8), %rcx
addq %rbp, %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rbp, %rcx
cmpl 0x10(%rax,%rcx), %r12d
jae 0x19733d
movl %r15d, %ecx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %r15
addq %rcx, %r15
cmpl 0x10(%rax,%r15), %r11d
jb 0x197370
cmpq %r13, (%r14)
jbe 0x19727e
leaq (,%r13,8), %rcx
addq %r13, %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r13, %rcx
cmpl 0x10(%rax,%rcx), %r11d
ja 0x197370
movl 0x10(%rax,%rbx), %ecx
movl 0x14(%rax,%rbx), %edx
subl %ecx, %r12d
leaq (,%rbp,8), %rsi
addq %rbp, %rsi
leaq (%rsi,%rsi,2), %rsi
addq %rbp, %rsi
movl 0x10(%rax,%rsi), %edi
movl 0x14(%rax,%rsi), %esi
subl %ecx, %edi
subl %edx, %esi
cvtsi2sd %r12, %xmm0
cvtsi2sd %rdi, %xmm1
divsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
addl %edx, %ecx
movq 0x10(%rsp), %r9
movl %ecx, 0x14(%r9)
movl 0x10(%rax,%r15), %edx
movl 0x14(%rax,%r15), %esi
subl %edx, %r11d
leaq (,%r13,8), %rdi
addq %r13, %rdi
leaq (%rdi,%rdi,2), %rdi
addq %r13, %rdi
movl 0x10(%rax,%rdi), %r8d
movl 0x14(%rax,%rdi), %eax
subl %edx, %r8d
subl %esi, %eax
xorps %xmm0, %xmm0
cvtsi2sd %r11, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r8, %xmm1
cvtsi2sd %rax, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rax
addl %esi, %eax
movl %eax, 0x18(%r9)
cmpl %eax, %ecx
ja 0x1973d6
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xfdc9c(%rip), %rax     # 0x294fe0
movq (%rax), %rcx
leaq 0x9f6a5(%rip), %rdi     # 0x2369f3
movl $0x7, %esi
movl $0x1, %edx
movq %r11, 0x8(%rsp)
callq 0x2410
movq 0x8(%rsp), %r11
movq 0x10(%r14), %rax
jmp 0x197243
movq 0xfdc69(%rip), %rax     # 0x294fe0
movq (%rax), %rcx
leaq 0x9f67a(%rip), %rdi     # 0x2369fb
movl $0x7, %esi
movl $0x1, %edx
movq %r11, 0x8(%rsp)
callq 0x2410
movq 0x8(%rsp), %r11
movq 0x10(%r14), %rax
jmp 0x19727e
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0xfdc31(%rip), %rax     # 0x294fe0
movq (%rax), %rcx
leaq 0x9f61c(%rip), %rdi     # 0x2369d5
movl $0xe, %esi
movl $0x1, %edx
movq %r11, %rbx
callq 0x2410
movq %rbx, %r11
movq (%r14), %rax
jmp 0x1971e9
callq 0x237d9
jmp 0x19732e
movq %r11, %rbx
callq 0x237bd
movq %rbx, %r11
jmp 0x197200