// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: refine_u_trans_t(u_trans_hit_t*, kv_ca_buf_t*)
void refine_u_trans_t(u_trans_hit_t *q, kv_ca_buf_t* cb)
{
    ///already know [qScur, qEcur), [qSpre, qEpre)
    uint32_t s, e, i, si, ei;
    s = q->qScur; e = q->qEcur;///[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->tScur = cb->a[si].c_y_p + ((cb->a[si+1].c_y_p - cb->a[si].c_y_p)
    //             *(double)((double)(s - cb->a[si].c_x_p)/(double)(cb->a[si+1].c_x_p - cb->a[si].c_x_p)));
    q->tScur = 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->tEcur = cb->a[ei].c_y_p + ((cb->a[ei+1].c_y_p - cb->a[ei].c_y_p)
    //             *(double)((double)(e - cb->a[ei].c_x_p)/(double)(cb->a[ei+1].c_x_p - cb->a[ei].c_x_p)));
    q->tEcur = 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->tScur > q->tEcur) 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
movl 0x8(%rdi), %r12d
movl 0xc(%rdi), %edx
movq (%rsi), %rax
testq %rax, %rax
movq %rdx, 0x10(%rsp)
je 0xbbf28
movq %rsi, %r13
movq 0x10(%rsi), %rcx
decl %edx
movl $0x1, %esi
xorl %r11d, %r11d
movl %eax, %ebp
movl %eax, %ebx
movl %ebx, %r8d
leal -0x1(%rsi), %r9d
leaq (%r11,%r11,8), %r10
leaq (%r10,%r10,2), %r10
addq %r11, %r10
movl 0x10(%rcx,%r10), %r11d
cmpl %r12d, %r11d
cmoval %r9d, %ebx
movl %r8d, %r10d
cmpq %r10, %rax
cmovnel %r8d, %ebx
movl %ebp, %r8d
cmpq %r8, %rax
cmovnel %ebp, %r9d
cmpl %edx, %r11d
cmoval %r9d, %ebp
cmpq %rbx, %rax
sete %r9b
movl %ebp, %r8d
cmpq %r8, %rax
sete %r8b
orb %r9b, %r8b
cmpb $0x1, %r8b
jne 0xbbf2f
movl %esi, %r11d
incl %esi
cmpq %r11, %rax
ja 0xbbec9
jmp 0xbbf2f
movq %rsi, %r13
movl %eax, %ebx
movl %eax, %ebp
testl %ebx, %ebx
je 0xbc0c6
testl %ebp, %ebp
je 0xbc0c6
movl %ebx, %r15d
movq %r13, %rsi
movq (%r13), %rax
movl %ebp, %r13d
cmpq %r15, %rax
jbe 0xbc0d6
cmpq %r13, %rax
jbe 0xbc0d6
decl %ebx
decl %ebp
movq 0x10(%rsi), %rax
leaq (%rbx,%rbx,8), %rcx
leaq (%rcx,%rcx,2), %r14
addq %rbx, %r14
cmpl 0x10(%rax,%r14), %r12d
jb 0xbc08b
cmpq %r15, (%rsi)
jbe 0xbbf97
leaq (%r15,%r15,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r15, %rcx
cmpl 0x10(%rax,%rcx), %r12d
jae 0xbc08b
movq 0x10(%rsi), %rax
movl %ebp, %ecx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rbx
addq %rcx, %rbx
movq 0x10(%rsp), %rbp
cmpl 0x10(%rax,%rbx), %ebp
jb 0xbc0a3
cmpq %r13, (%rsi)
jbe 0xbbfd8
leaq (,%r13,8), %rcx
addq %r13, %rcx
leaq (%rcx,%rcx,2), %rcx
addq %r13, %rcx
cmpl 0x10(%rax,%rcx), %ebp
ja 0xbc0a3
movq 0x10(%rsi), %rax
movl 0x10(%rax,%r14), %ecx
movl 0x14(%rax,%r14), %edx
subl %ecx, %r12d
leaq (%r15,%r15,8), %rsi
leaq (%rsi,%rsi,2), %rsi
addq %r15, %rsi
movl 0x10(%rax,%rsi), %r8d
movl 0x14(%rax,%rsi), %esi
subl %ecx, %r8d
subl %edx, %esi
cvtsi2sd %r12, %xmm0
cvtsi2sd %r8, %xmm1
divsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rcx
addl %edx, %ecx
movl %ecx, 0x1c(%rdi)
movl 0x10(%rax,%rbx), %edx
movl 0x14(%rax,%rbx), %esi
subl %edx, %ebp
leaq (,%r13,8), %r8
addq %r13, %r8
leaq (%r8,%r8,2), %r9
addq %r13, %r9
movl 0x10(%rax,%r9), %r8d
movl 0x14(%rax,%r9), %eax
subl %edx, %r8d
subl %esi, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rbp, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r8, %xmm1
cvtsi2sd %rax, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rax
addl %esi, %eax
movl %eax, 0x20(%rdi)
cmpl %eax, %ecx
ja 0xbc0bf
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rdi, (%rsp)
movq %rsi, %rbx
callq 0x1068a
movq %rbx, %rsi
movq (%rsp), %rdi
jmp 0xbbf97
movq %rdi, (%rsp)
movq %rsi, 0x8(%rsp)
callq 0x106a6
movq 0x8(%rsp), %rsi
movq (%rsp), %rdi
jmp 0xbbfd8
callq 0x106c2
jmp 0xbc07c
movq %rdi, %r14
callq 0x10652
movq %r14, %rdi
jmp 0xbbf3f
movq %rdi, (%rsp)
movq %rsi, %r14
callq 0x1066e
movq %r14, %rsi
movq (%rsp), %rdi
jmp 0xbbf5e