// File: /workspace/github_cpp_makefile_star2_O1/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 %edx, (%rsp)
movl 0xa0(%rsp), %r14d
testl %r14d, %r14d
je 0x18e8ac
movl %edi, %ebx
movl %ecx, %eax
subl (%rsp), %eax
cvtsi2sd %rax, %xmm2
movl %esi, %eax
subl %edi, %eax
cvtsi2sd %rax, %xmm3
xorl %edx, %edx
movl $0x18, %ebp
movl $0x1, %r11d
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 0x10(%r9), %rdi
movl -0x4(%rdi,%rbp), %r10d
movl (%rdi,%rbp), %eax
cmpl %esi, %eax
movl %esi, %r12d
cmovbl %eax, %r12d
cmpl %ebx, %r10d
movl %ebx, %r13d
cmoval %r10d, %r13d
cmpl $0x1, %edx
jne 0x18e6a2
cmpl %r13d, %r12d
jbe 0x18e8ac
cmpl %r13d, %r12d
cmoval %r11d, %edx
jbe 0x18e89f
movl %r14d, 0x18(%rsp)
movl %edx, 0x24(%rsp)
movl %r13d, %edx
subl %r10d, %edx
movl %eax, %r11d
subl %r12d, %r11d
subl %r10d, %eax
cvtsi2sd %rdx, %xmm6
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm6
cvtsi2sd %r11, %xmm4
divsd %xmm0, %xmm4
movl -0x10(%rdi,%rbp), %eax
movapd %xmm6, %xmm5
movapd %xmm4, %xmm1
movl %eax, 0x20(%rsp)
testb $0x1, %al
je 0x18e6fd
movapd %xmm4, %xmm5
movapd %xmm6, %xmm1
movl -0x18(%rdi,%rbp), %r11d
movl -0x14(%rdi,%rbp), %eax
movl %eax, 0x1c(%rsp)
movl -0xc(%rdi,%rbp), %r10d
movl %r13d, %eax
subl %ebx, %eax
movl %esi, %r15d
subl %r12d, %r15d
testb $0x1, %r8b
movl %r15d, %esi
cmovel %eax, %esi
cmovnel %eax, %r15d
movl -0x8(%rdi,%rbp), %eax
movl %eax, 0x14(%rsp)
movq 0x8(%r9), %rax
cmpq %rax, (%r9)
jne 0x18e7c2
leaq (%rax,%rax), %rdx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rdx
movq %rdx, 0x8(%r9)
shlq $0x3, %rdx
movq %rsi, 0x40(%rsp)
leaq (%rdx,%rdx,4), %rsi
movsd %xmm1, 0x60(%rsp)
movsd %xmm4, 0x58(%rsp)
movsd %xmm5, 0x50(%rsp)
movl %r10d, %ebx
movsd %xmm6, 0x48(%rsp)
movl %r11d, %r14d
callq 0x2370
movl %r14d, %r11d
movq 0x40(%rsp), %rsi
movsd 0x48(%rsp), %xmm6
movl %ebx, %r10d
movsd 0x50(%rsp), %xmm5
movsd 0x58(%rsp), %xmm4
movsd 0x60(%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)
movl 0x1c(%rsp), %ebx
movl %ebx, %eax
subl %r11d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd %xmm0, %xmm6
cvttsd2si %xmm6, %rax
mulsd %xmm0, %xmm4
movl 0x14(%rsp), %r14d
movl %r14d, %edx
subl %r10d, %edx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
cvttsd2si %xmm4, %rdx
addl %r11d, %eax
mulsd %xmm0, %xmm5
cvttsd2si %xmm5, %r11
subl %edx, %ebx
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rdx
addl %r10d, %r11d
subl %edx, %r14d
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r15, %xmm1
divsd %xmm3, %xmm1
mulsd %xmm2, %xmm1
cvttsd2si %xmm1, %rdx
movl %ecx, %esi
subl %edx, %esi
movq (%r9), %rdx
movq 0x10(%r9), %rdi
leaq 0x1(%rdx), %r10
movq %r10, (%r9)
leaq (%rdx,%rdx,4), %rdx
movl %eax, (%rdi,%rdx,8)
movl %ebx, 0x4(%rdi,%rdx,8)
movl 0x20(%rsp), %eax
movl %eax, 0x8(%rdi,%rdx,8)
movl %r11d, 0xc(%rdi,%rdx,8)
movl %r14d, 0x10(%rdi,%rdx,8)
movl %r13d, 0x14(%rdi,%rdx,8)
divsd %xmm3, %xmm0
mulsd %xmm2, %xmm0
movl %r12d, 0x18(%rdi,%rdx,8)
cvttsd2si %xmm0, %rax
addl (%rsp), %eax
movl %r8d, 0x1c(%rdi,%rdx,8)
movl %eax, 0x20(%rdi,%rdx,8)
movl %esi, 0x24(%rdi,%rdx,8)
movl 0x10(%rsp), %esi
movl 0xc(%rsp), %ebx
movl 0x24(%rsp), %edx
movl $0x1, %r11d
movl 0x18(%rsp), %r14d
addq $0x28, %rbp
decl %r14d
jne 0x18e675
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq