// File: /workspace/github_cpp_makefile_star2_O2/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
movq %rdi, %r10
movl (%rdi), %r12d
movl 0x4(%rdi), %r11d
movq (%rsi), %rax
leal -0x1(%r11), %ecx
xorl %edi, %edi
movl %eax, %esi
movl %eax, %edx
movl %edi, %edi
cmpq %rdi, %rax
jbe 0x15002b
movq 0x10(%r14), %r8
imulq $0x1c, %rdi, %r9
movl 0x10(%r8,%r9), %r8d
cmpl %r12d, %r8d
movl %esi, %r9d
movl %esi, %r15d
cmoval %edi, %r15d
cmpq %r9, %rax
cmovnel %esi, %r15d
movl %edx, %esi
cmpq %rsi, %rax
movl %edx, %ebp
cmovel %edi, %ebp
cmpl %ecx, %r8d
cmovbel %edx, %ebp
incl %edi
movl %r15d, %esi
movl %ebp, %edx
cmpq %r15, %rax
je 0x14ffdb
movl %r15d, %esi
movl %ebp, %edx
cmpq %rbp, %rax
je 0x14ffdb
jmp 0x150030
movl %esi, %r15d
movl %edx, %ebp
testl %r15d, %r15d
je 0x1501db
testl %ebp, %ebp
je 0x1501db
movl %r15d, %ebx
movl %ebp, %r13d
cmpq %rbx, %rax
jbe 0x15020b
cmpq %r13, %rax
jbe 0x15020b
decl %r15d
decl %ebp
movq 0x10(%r14), %rax
imulq $0x1c, %r15, %r15
cmpl 0x10(%rax,%r15), %r12d
jb 0x15013f
cmpq %rbx, (%r14)
jbe 0x150085
imulq $0x1c, %rbx, %rcx
cmpl 0x10(%rax,%rcx), %r12d
jae 0x15013f
movl %ebp, %ecx
imulq $0x1c, %rcx, %rbp
cmpl 0x10(%rax,%rbp), %r11d
jb 0x150178
cmpq %r13, (%r14)
jbe 0x1500aa
imulq $0x1c, %r13, %rcx
cmpl 0x10(%rax,%rcx), %r11d
ja 0x150178
movl 0x10(%rax,%r15), %ecx
movl 0x14(%rax,%r15), %edx
subl %ecx, %r12d
imulq $0x1c, %rbx, %rsi
movl 0x10(%rax,%rsi), %edi
movl 0x14(%rax,%rsi), %esi
subl %ecx, %edi
subl %edx, %esi
cvtsi2sd %r12, %xmm0
cvtsi2sd %rdi, %xmm1
cvtsi2sd %rsi, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rcx
addl %edx, %ecx
movl %ecx, 0x14(%r10)
movl 0x10(%rax,%rbp), %edx
movl 0x14(%rax,%rbp), %esi
subl %edx, %r11d
imulq $0x1c, %r13, %rdi
movl 0x10(%rax,%rdi), %r8d
movl 0x14(%rax,%rdi), %eax
subl %edx, %r8d
subl %esi, %eax
cvtsi2sd %r11, %xmm0
cvtsi2sd %r8, %xmm1
cvtsi2sd %rax, %xmm2
divsd %xmm1, %xmm0
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %rax
addl %esi, %eax
movl %eax, 0x18(%r10)
cmpl %eax, %ecx
ja 0x1501b1
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x102e9a(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x987a3(%rip), %rdi     # 0x1e88f3
pushq $0x7
popq %rsi
pushq $0x1
popq %rdx
movq %r10, 0x10(%rsp)
movq %r11, 0x8(%rsp)
callq 0x2410
movq 0x8(%rsp), %r11
movq 0x10(%rsp), %r10
movq 0x10(%r14), %rax
jmp 0x150085
movq 0x102e61(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x98772(%rip), %rdi     # 0x1e88fb
pushq $0x7
popq %rsi
pushq $0x1
popq %rdx
movq %r10, 0x10(%rsp)
movq %r11, 0x8(%rsp)
callq 0x2410
movq 0x8(%rsp), %r11
movq 0x10(%rsp), %r10
movq 0x10(%r14), %rax
jmp 0x1500aa
movq 0x102e28(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x98741(%rip), %rdi     # 0x1e8903
pushq $0x7
popq %rsi
pushq $0x1
popq %rdx
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2410
movq 0x102dfe(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x986e9(%rip), %rdi     # 0x1e88d5
pushq $0xe
popq %rsi
pushq $0x1
popq %rdx
movq %r10, %rbx
movq %r11, %r13
callq 0x2410
movq %r13, %r11
movq %rbx, %r10
movq (%r14), %rax
jmp 0x150041
movq 0x102dce(%rip), %rax    # 0x252fe0
movq (%rax), %rcx
leaq 0x986c8(%rip), %rdi     # 0x1e88e4
pushq $0xe
popq %rsi
pushq $0x1
popq %rdx
movq %r10, 0x10(%rsp)
movq %r11, 0x8(%rsp)
callq 0x2410
movq 0x8(%rsp), %r11
movq 0x10(%rsp), %r10
jmp 0x150059