// File: 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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x14(%rbp)
movq -0x8(%rbp), %rax
movl 0x4(%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 0x26ed6e
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 0x26ed13
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x26ed13
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 0x26ed44
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jne 0x26ed44
movl -0x1c(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
je 0x26ed5e
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
je 0x26ed5e
jmp 0x26ed6e
jmp 0x26ed60
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x26ecd7
cmpl $0x0, -0x20(%rbp)
je 0x26ed7a
cmpl $0x0, -0x24(%rbp)
jne 0x26ed92
movq 0x15525f(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x118678(%rip), %rsi    # 0x387403
movb $0x0, %al
callq 0x22a0
movl -0x20(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x26edaa
movl -0x24(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jb 0x26edc2
movq 0x15522f(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x118657(%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 0x26ee1e
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x26ee36
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 0x26ee36
movq 0x1551bb(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x1185f2(%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 0x26ee80
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x26ee98
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 0x26ee98
movq 0x155159(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x118598(%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, 0x14(%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, 0x18(%rax)
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x8(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jbe 0x26effe
movq 0x154ff3(%rip), %rax    # 0x3c3fe0
movq (%rax), %rdi
leaq 0x11843a(%rip), %rsi    # 0x387431
movb $0x0, %al
callq 0x22a0
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)