// File: /workspace/github_cpp_makefile_star2_O3/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, 0x4(%rsp)
movl 0xa0(%rsp), %eax
testl %eax, %eax
je 0x197671
movl %esi, %r10d
subl %edi, %r10d
movl %ecx, %edx
subl 0x4(%rsp), %edx
cvtsi2sd %r10, %xmm2
cvtsi2sd %rdx, %xmm3
movl %eax, %edx
movq 0x10(%r9), %rax
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rbx
xorl %r15d, %r15d
xorl %r10d, %r10d
movq %r9, 0x38(%rsp)
movl %r8d, 0xc(%rsp)
movl %ecx, 0x8(%rsp)
movl %esi, 0x14(%rsp)
movl %edi, 0x10(%rsp)
movsd %xmm2, 0x30(%rsp)
movsd %xmm3, 0x28(%rsp)
movq %rbx, 0x60(%rsp)
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 0x19765e
movl %r13d, %r10d
subl %r11d, %r10d
movl %edx, %ebx
subl %r12d, %ebx
subl %r11d, %edx
cvtsi2sd %r10, %xmm5
xorps %xmm0, %xmm0
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 0x1974ce
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, %r14d
subl %r12d, %r14d
testb $0x1, %r8b
movl %r14d, %r10d
cmovel %edx, %r10d
cmovnel %edx, %r14d
movl 0x10(%rax,%r15), %ebp
movq (%r9), %r11
cmpq 0x8(%r9), %r11
jne 0x19758b
leaq (%r11,%r11), %rdx
testq %r11, %r11
movl $0x2, %esi
cmoveq %rsi, %rdx
movq %rdx, 0x8(%r9)
shlq $0x3, %rdx
leaq (%rdx,%rdx,4), %rsi
movq %rax, %rdi
movsd %xmm1, 0x58(%rsp)
movq %r10, %rbx
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 %rbx, %r10
movsd 0x58(%rsp), %xmm1
movsd 0x28(%rsp), %xmm3
movsd 0x30(%rsp), %xmm2
movl 0x8(%rsp), %ecx
movl 0xc(%rsp), %r8d
movq 0x38(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %r11
movl 0x20(%rsp), %ebx
movl %ebx, %edx
movl 0x18(%rsp), %esi
subl %esi, %edx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
mulsd %xmm0, %xmm5
cvttsd2si %xmm5, %rdx
addl %esi, %edx
mulsd %xmm0, %xmm6
cvttsd2si %xmm6, %rsi
subl %esi, %ebx
movl %ebp, %esi
movl 0x1c(%rsp), %edi
subl %edi, %esi
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
mulsd %xmm0, %xmm4
cvttsd2si %xmm4, %rsi
addl %edi, %esi
mulsd %xmm0, %xmm1
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
cvttsd2si %xmm1, %rdi
subl %edi, %ebp
divsd %xmm2, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r10, %xmm1
mulsd %xmm3, %xmm0
cvttsd2si %xmm0, %rdi
movl %ecx, %r10d
subl %edi, %r10d
leaq 0x1(%r11), %rdi
movq %rdi, (%r9)
leaq (%r11,%r11,4), %rdi
movl %edx, (%rax,%rdi,8)
movl %ebx, 0x4(%rax,%rdi,8)
movl 0x24(%rsp), %edx
movl %edx, 0x8(%rax,%rdi,8)
movl %esi, 0xc(%rax,%rdi,8)
movl %ebp, 0x10(%rax,%rdi,8)
movl %r13d, 0x14(%rax,%rdi,8)
divsd %xmm2, %xmm1
mulsd %xmm3, %xmm1
movl %r12d, 0x18(%rax,%rdi,8)
cvttsd2si %xmm1, %rdx
addl 0x4(%rsp), %edx
movl %r8d, 0x1c(%rax,%rdi,8)
movl %edx, 0x20(%rax,%rdi,8)
movl %r10d, 0x24(%rax,%rdi,8)
movl $0x1, %r10d
movl 0x14(%rsp), %esi
movl 0x10(%rsp), %edi
movq 0x60(%rsp), %rbx
jmp 0x197664
cmpl $0x1, %r10d
je 0x197671
addq $0x28, %r15
cmpq %r15, %rbx
jne 0x197462
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq