// File: 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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x24(%rbp)
movl %eax, -0x20(%rbp)
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xf815f
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x1c(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
cmpl -0x14(%rbp), %eax
jbe 0xf8104
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jne 0xf8104
movl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x1c(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl -0x18(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jbe 0xf8135
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jne 0xf8135
movl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
je 0xf814f
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
je 0xf814f
jmp 0xf815f
jmp 0xf8151
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0xf80c8
cmpl $0x0, -0x20(%rbp)
je 0xf816b
cmpl $0x0, -0x24(%rbp)
jne 0xf8183
movq 0x2cbe6e(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x28f287(%rip), %rsi    # 0x387403
movb $0x0, %al
callq 0x22a0
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xf819b
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jb 0xf81b3
movq 0x2cbe3e(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x28f266(%rip), %rsi    # 0x387412
movb $0x0, %al
callq 0x22a0
movl -0x20(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x20(%rbp)
movl -0x24(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x20(%rbp), %edx
imulq $0x1c, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x10(%rcx), %eax
jb 0xf820f
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xf8227
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x20(%rbp), %edx
addl $0x1, %edx
movl %edx, %edx
imulq $0x1c, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x10(%rcx), %eax
jb 0xf8227
movq 0x2cbdca(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x28f201(%rip), %rsi    # 0x387421
movb $0x0, %al
callq 0x22a0
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x24(%rbp), %edx
imulq $0x1c, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x10(%rcx), %eax
jb 0xf8271
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0xf8289
movl -0x18(%rbp), %eax
movq -0x10(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x24(%rbp), %edx
addl $0x1, %edx
movl %edx, %edx
imulq $0x1c, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x10(%rcx), %eax
jbe 0xf8289
movq 0x2cbd68(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x28f1a7(%rip), %rsi    # 0x387429
movb $0x0, %al
callq 0x22a0
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x20(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movl %eax, -0x2c(%rbp)
movl -0x14(%rbp), %edi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x20(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
subl 0x10(%rax), %edi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %esi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x20(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
subl 0x10(%rax), %esi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x20(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %edx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x20(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
subl 0x14(%rax), %edx
callq 0xc14d0
movl -0x2c(%rbp), %ecx
addl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x1c(%rax)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x24(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movl %eax, -0x28(%rbp)
movl -0x18(%rbp), %edi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x24(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
subl 0x10(%rax), %edi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %esi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x24(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
subl 0x10(%rax), %esi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %edx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x24(%rbp), %ecx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
subl 0x14(%rax), %edx
callq 0xc14d0
movl -0x28(%rbp), %ecx
addl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x20(%rax)
movq -0x8(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jbe 0xf83ef
movq 0x2cbc02(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x28f049(%rip), %rsi    # 0x387431
movb $0x0, %al
callq 0x22a0
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)