// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: extract_sub_overlaps(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, kv_u_trans_hit_t*, unsigned int)
void extract_sub_overlaps(uint32_t i_tScur, uint32_t i_tEcur, uint32_t i_tSpre, uint32_t i_tEpre,
uint32_t tn, kv_u_trans_hit_t* ktb, uint32_t bn)
{
    uint32_t i, ovlp, found, beg, end, offS, offE;
    u_trans_hit_t *q = NULL, x;
    for (i = found = 0; i < bn; i++)
    {
        q = &(ktb->a[i]);///for q, already know [qScur, qEcur), [qSpre, qEpre), [tScur, tEcur)

        ovlp = ((MIN(i_tEcur, q->tEcur) > MAX(i_tScur, q->tScur))?
                                        MIN(i_tEcur, q->tEcur) - MAX(i_tScur, q->tScur):0);
        if(found == 1 && ovlp == 0) break;
        if(ovlp > 0) found = 1;
        if(ovlp == 0) continue;

        
        beg = MAX(i_tScur, q->tScur); end = MIN(i_tEcur, q->tEcur);
        offS = beg - q->tScur; offE = q->tEcur - end;
        x.tScur = q->tScur + offS; 
        x.tEcur = q->tEcur - offE;
        //x.qScur = q->qScur + offS; 
        x.qScur = q->qScur + get_offset_adjust(offS, q->tEcur-q->tScur, q->qEcur-q->qScur);        
        ///x.qEcur = q->qEcur - offE;
        x.qScur = q->qEcur - get_offset_adjust(offE, q->tEcur-q->tScur, q->qEcur-q->qScur);  

        x.qn = q->qn;
        offS = beg - q->tScur; offE = q->tEcur - end;
        if((x.qn&1) == 0)
        {
            // x.qSpre = q->qSpre + offS; 
            x.qSpre = q->qSpre + get_offset_adjust(offS, q->tEcur-q->tScur, q->qEpre-q->qSpre);  
            // x.qEpre = q->qEpre - offE;
            x.qEpre = q->qEpre - get_offset_adjust(offE, q->tEcur-q->tScur, q->qEpre-q->qSpre);
        }
        else
        {
            // x.qSpre = q->qSpre + offE; 
            x.qSpre = q->qSpre + get_offset_adjust(offE, q->tEcur-q->tScur, q->qEpre-q->qSpre);
            // x.qEpre = q->qEpre - offS;
            x.qEpre = q->qEpre - get_offset_adjust(offS, q->tEcur-q->tScur, q->qEpre-q->qSpre);
        }

        x.tn = tn; 
        offS = beg - i_tScur; offE = i_tEcur - end;
        if((x.tn&1) == 0)
        {
            // x.tSpre = i_tSpre + offS; 
            x.tSpre = i_tSpre +  get_offset_adjust(offS, i_tEcur-i_tScur, i_tEpre-i_tSpre); 
            // x.tEpre = i_tEpre - offE;
            x.tEpre = i_tEpre - get_offset_adjust(offE, i_tEcur-i_tScur, i_tEpre-i_tSpre); 
        }
        else
        {
            // x.tSpre = i_tSpre + offE; 
            x.tSpre = i_tSpre + get_offset_adjust(offE, i_tEcur-i_tScur, i_tEpre-i_tSpre); 
            // x.tEpre = i_tEpre - offS;
            x.tEpre = i_tEpre - get_offset_adjust(offS, i_tEcur-i_tScur, i_tEpre-i_tSpre);
        }
        
        kv_push(u_trans_hit_t, *ktb, x);

        // if(x.tSpre >= x.tEpre || x.qSpre >= x.qEpre)
        // {
        //     fprintf(stderr, "\n*********x.qn: %u, x.tn: %u\n", x.qn, x.tn);
        //     fprintf(stderr, "x.qSpre: %u, x.qEpre: %u, x.tSpre: %u, x.tEpre: %u\n", 
        //     x.qSpre, x.qEpre, x.tSpre, x.tEpre);
        //     fprintf(stderr, "q->qScur: %u, q->qEcur: %u, q->qSpre: %u, q->qEpre: %u\n", 
        //     q->qScur, q->qEcur, q->qSpre, q->qEpre);
        //     fprintf(stderr, "q->tScur: %u, q->tEcur: %u, q->tSpre: %u, q->tEpre: %u\n", 
        //     q->tScur, q->tEcur, q->tSpre, q->tEpre);
        //     fprintf(stderr, "i_tScur: %u, i_tEcur: %u, i_tSpre: %u, i_tEpre: %u\n", 
        //     i_tScur, i_tEcur, i_tSpre, i_tEpre);
        // }
    }
}

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