// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/tovlp.cpp
// Function: extract_sub_overlaps_utg_thit(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, kv_utg_thit_t_t*, unsigned int)
void extract_sub_overlaps_utg_thit(uint32_t i_ts, uint32_t i_te, uint32_t i_tus, uint32_t i_tue,
uint32_t tn, kv_utg_thit_t_t* ktb, uint32_t bn)
{
    uint32_t i, ovlp, found, beg, end, offS, offE;
    utg_thit_t *q = NULL, x;
    for (i = found = 0; i < bn; i++)
    {
        q = &(ktb->a[i]);///for q, already know [qs, qe), [qus, que), [ts, te)

        ovlp = ((MIN(i_te, q->te) > MAX(i_ts, q->ts))?
                                        MIN(i_te, q->te) - MAX(i_ts, q->ts):0);
        if(found == 1 && ovlp == 0) break;
        if(ovlp > 0) found = 1;
        if(ovlp == 0) continue;

        
        beg = MAX(i_ts, q->ts); end = MIN(i_te, q->te);
        offS = beg - q->ts; offE = q->te - end;
        x.ts = q->ts + offS; 
        x.te = q->te - offE;
        //x.qs = q->qs + offS; 
        x.qs = q->qs + get_offset_adjust(offS, q->te-q->ts, q->qe-q->qs);        
        ///x.qe = q->qe - offE;
        x.qe = q->qe - get_offset_adjust(offE, q->te-q->ts, q->qe-q->qs);  

        x.qn = q->qn;
        offS = beg - q->ts; offE = q->te - end;
        if((x.qn&1) == 0)
        {
            // x.qus = q->qus + offS; 
            x.qus = q->qus + get_offset_adjust(offS, q->te-q->ts, q->que-q->qus);  
            // x.que = q->que - offE;
            x.que = q->que - get_offset_adjust(offE, q->te-q->ts, q->que-q->qus);
        }
        else
        {
            // x.qus = q->qus + offE; 
            x.qus = q->qus + get_offset_adjust(offE, q->te-q->ts, q->que-q->qus);
            // x.que = q->que - offS;
            x.que = q->que - get_offset_adjust(offS, q->te-q->ts, q->que-q->qus);
        }

        x.tn = tn; 
        offS = beg - i_ts; offE = i_te - end;
        if((x.tn&1) == 0)
        {
            // x.tus = i_tus + offS; 
            x.tus = i_tus +  get_offset_adjust(offS, i_te-i_ts, i_tue-i_tus); 
            // x.tue = i_tue - offE;
            x.tue = i_tue - get_offset_adjust(offE, i_te-i_ts, i_tue-i_tus); 
        }
        else
        {
            // x.tus = i_tus + offE; 
            x.tus = i_tus + get_offset_adjust(offE, i_te-i_ts, i_tue-i_tus); 
            // x.tue = i_tue - offS;
            x.tue = i_tue - get_offset_adjust(offS, i_te-i_ts, i_tue-i_tus);
        }
        
        kv_push(utg_thit_t, *ktb, x);

        // if(x.tus >= x.tue || x.qus >= x.que)
        // {
        //     fprintf(stderr, "\n*********x.qn: %u, x.tn: %u\n", x.qn, x.tn);
        //     fprintf(stderr, "x.qus: %u, x.que: %u, x.tus: %u, x.tue: %u\n", 
        //     x.qus, x.que, x.tus, x.tue);
        //     fprintf(stderr, "q->qs: %u, q->qe: %u, q->qus: %u, q->que: %u\n", 
        //     q->qs, q->qe, q->qus, q->que);
        //     fprintf(stderr, "q->ts: %u, q->te: %u, q->tus: %u, q->tue: %u\n", 
        //     q->ts, q->te, q->tus, q->tue);
        //     fprintf(stderr, "i_ts: %u, i_te: %u, i_tus: %u, i_tue: %u\n", 
        //     i_ts, i_te, i_tus, i_tue);
        // }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl 0xa0(%rsp), %eax
movl %esi, %r10d
subl %edi, %r10d
movl %ecx, %r11d
cvtsi2sd %r10, %xmm2
movl %edx, 0x14(%rsp)
subl %edx, %r11d
cvtsi2sd %r11, %xmm3
imulq $0x28, %rax, %rbx
xorl %r15d, %r15d
xorl %r10d, %r10d
movq %r9, 0x38(%rsp)
movl %r8d, 0x8(%rsp)
movl %ecx, 0x4(%rsp)
movl %esi, 0x10(%rsp)
movl %edi, 0xc(%rsp)
movsd %xmm2, 0x30(%rsp)
movsd %xmm3, 0x28(%rsp)
movq %rbx, 0x60(%rsp)
cmpq %r15, %rbx
je 0x1504a2
movq 0x10(%r9), %rax
movl 0x14(%rax,%r15), %r11d
movl 0x18(%rax,%r15), %edx
cmpl %esi, %edx
movl %esi, %r12d
cmovbl %edx, %r12d
cmpl %edi, %r11d
movl %edi, %r13d
cmoval %r11d, %r13d
cmpl %r13d, %r12d
jbe 0x150493
movl %r13d, %r10d
subl %r11d, %r10d
movl %edx, %ebx
subl %r12d, %ebx
subl %r11d, %edx
cvtsi2sd %r10, %xmm5
cvtsi2sd %rdx, %xmm0
divsd %xmm0, %xmm5
cvtsi2sd %rbx, %xmm6
divsd %xmm0, %xmm6
movl 0x8(%rax,%r15), %edx
movapd %xmm5, %xmm4
movapd %xmm6, %xmm1
movl %edx, 0x24(%rsp)
testb $0x1, %dl
je 0x150316
movapd %xmm6, %xmm4
movapd %xmm5, %xmm1
movl (%rax,%r15), %edx
movl %edx, 0x18(%rsp)
movl 0x4(%rax,%r15), %edx
movl %edx, 0x20(%rsp)
movl 0xc(%rax,%r15), %edx
movl %edx, 0x1c(%rsp)
movl %r13d, %edx
subl %edi, %edx
movl %esi, %ebx
subl %r12d, %ebx
testb $0x1, %r8b
movl %ebx, %r10d
cmovel %edx, %r10d
cmovnel %edx, %ebx
movl 0x10(%rax,%r15), %r14d
movq (%r9), %r11
cmpq 0x8(%r9), %r11
jne 0x1503cb
leaq (%r11,%r11), %rdx
testq %r11, %r11
pushq $0x2
popq %rsi
cmoveq %rsi, %rdx
movq %rdx, 0x8(%r9)
imulq $0x28, %rdx, %rsi
movq %rax, %rdi
movsd %xmm1, 0x58(%rsp)
movq %r10, %rbp
movsd %xmm4, 0x50(%rsp)
movsd %xmm5, 0x48(%rsp)
movsd %xmm6, 0x40(%rsp)
callq 0x2370
movsd 0x40(%rsp), %xmm6
movsd 0x48(%rsp), %xmm5
movsd 0x50(%rsp), %xmm4
movq %rbp, %r10
movsd 0x58(%rsp), %xmm1
movsd 0x28(%rsp), %xmm3
movsd 0x30(%rsp), %xmm2
movl 0x4(%rsp), %ecx
movl 0x8(%rsp), %r8d
movq 0x38(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %r11
movl 0x20(%rsp), %ebp
movl %ebp, %edx
movl 0x18(%rsp), %esi
subl %esi, %edx
cvtsi2sd %rdx, %xmm0
mulsd %xmm0, %xmm5
cvttsd2si %xmm5, %rdx
addl %esi, %edx
mulsd %xmm0, %xmm6
cvttsd2si %xmm6, %rsi
subl %esi, %ebp
movl %r14d, %esi
movl 0x1c(%rsp), %edi
subl %edi, %esi
cvtsi2sd %rsi, %xmm0
mulsd %xmm0, %xmm4
cvttsd2si %xmm4, %rsi
addl %edi, %esi
mulsd %xmm0, %xmm1
cvtsi2sd %rbx, %xmm0
cvttsd2si %xmm1, %rdi
subl %edi, %r14d
divsd %xmm2, %xmm0
cvtsi2sd %r10, %xmm1
mulsd %xmm3, %xmm0
cvttsd2si %xmm0, %rdi
movl %ecx, %r10d
subl %edi, %r10d
leaq 0x1(%r11), %rdi
movq %rdi, (%r9)
imulq $0x28, %r11, %rdi
movl %edx, (%rax,%rdi)
movl %ebp, 0x4(%rax,%rdi)
movl 0x24(%rsp), %edx
movl %edx, 0x8(%rax,%rdi)
movl %esi, 0xc(%rax,%rdi)
movl %r14d, 0x10(%rax,%rdi)
movl %r13d, 0x14(%rax,%rdi)
divsd %xmm2, %xmm1
mulsd %xmm3, %xmm1
movl %r12d, 0x18(%rax,%rdi)
cvttsd2si %xmm1, %rdx
addl 0x14(%rsp), %edx
movl %r8d, 0x1c(%rax,%rdi)
movl %edx, 0x20(%rax,%rdi)
movl %r10d, 0x24(%rax,%rdi)
pushq $0x1
popq %r10
movl 0x10(%rsp), %esi
movl 0xc(%rsp), %edi
movq 0x60(%rsp), %rbx
jmp 0x150499
cmpl $0x1, %r10d
je 0x1504a2
addq $0x28, %r15
jmp 0x1502a0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq