// File: /workspace/github_cpp_makefile_star2_O2/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 0xa0(%rsp), %eax
movl %esi, %r10d
subl %edi, %r10d
movl %ecx, 0x18(%rsp)
movl %ecx, %r11d
cvtsi2sd %r10, %xmm2
subl %edx, %r11d
cvtsi2sd %r11, %xmm3
imulq $0x28, %rax, %r14
xorl %r15d, %r15d
xorl %r10d, %r10d
movq %r9, 0x38(%rsp)
movl %r8d, 0xc(%rsp)
movl %edx, 0x8(%rsp)
movl %esi, 0x14(%rsp)
movl %edi, 0x10(%rsp)
movsd %xmm2, 0x30(%rsp)
movsd %xmm3, 0x28(%rsp)
movq %r14, 0x60(%rsp)
cmpq %r15, %r14
je 0x81cae
movq 0x10(%r9), %rax
movl 0x1c(%rax,%r15), %ebx
movl 0x20(%rax,%r15), %r11d
cmpl %esi, %r11d
movl %esi, %r12d
cmovbl %r11d, %r12d
cmpl %edi, %ebx
movl %edi, %r13d
cmoval %ebx, %r13d
cmpl %r13d, %r12d
jbe 0x81b2a
movl %r13d, %r10d
subl %ebx, %r10d
movl %r11d, %r14d
subl %r12d, %r14d
subl %ebx, %r11d
cvtsi2sd %r10, %xmm1
cvtsi2sd %r11, %xmm0
cvtsi2sd %r14, %xmm4
divsd %xmm0, %xmm1
divsd %xmm0, %xmm4
movl 0x10(%rax,%r15), %r10d
movl %r10d, 0x24(%rsp)
testb $0x1, %r10b
je 0x81b39
movapd %xmm4, %xmm5
jmp 0x81b41
cmpl $0x1, %r10d
jne 0x81ca5
jmp 0x81cae
movapd %xmm1, %xmm5
movapd %xmm4, %xmm1
movl 0xc(%rax,%r15), %r10d
movl %r10d, 0x20(%rsp)
movl %r10d, %ebx
subl 0x8(%rax,%r15), %ebx
movl (%rax,%r15), %r10d
movl %r10d, 0x1c(%rsp)
movl %r13d, %r10d
subl %edi, %r10d
movl %esi, %ebp
subl %r12d, %ebp
testb $0x1, %r8b
movl %r10d, %edi
cmovel %ebp, %edi
cmovel %r10d, %ebp
movl 0x4(%rax,%r15), %r14d
movq (%r9), %r10
cmpq 0x8(%r9), %r10
jne 0x81bf0
leaq (%r10,%r10), %rsi
testq %r10, %r10
movq %rdi, 0x40(%rsp)
pushq $0x2
popq %rdi
cmoveq %rdi, %rsi
movq %rsi, 0x8(%r9)
imulq $0x28, %rsi, %rsi
movq %rax, %rdi
movsd %xmm1, 0x58(%rsp)
movsd %xmm4, 0x50(%rsp)
movsd %xmm5, 0x48(%rsp)
callq 0x2370
movq 0x40(%rsp), %rdi
movsd 0x48(%rsp), %xmm5
movsd 0x50(%rsp), %xmm4
movsd 0x58(%rsp), %xmm1
movsd 0x28(%rsp), %xmm3
movsd 0x30(%rsp), %xmm2
movl 0x8(%rsp), %edx
movl 0xc(%rsp), %r8d
movq 0x38(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %r10
cvtsi2sd %rbx, %xmm0
mulsd %xmm0, %xmm4
cvttsd2si %xmm4, %rsi
movl 0x20(%rsp), %ebx
subl %esi, %ebx
movl %r14d, %esi
movl %r14d, %ecx
movl 0x1c(%rsp), %r14d
subl %r14d, %esi
cvtsi2sd %rsi, %xmm0
mulsd %xmm0, %xmm5
cvttsd2si %xmm5, %r11
addl %r14d, %r11d
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rsi
subl %esi, %ecx
cvtsi2sd %rbp, %xmm0
cvtsi2sd %rdi, %xmm1
divsd %xmm2, %xmm1
mulsd %xmm3, %xmm1
cvttsd2si %xmm1, %rsi
movl 0x18(%rsp), %edi
subl %esi, %edi
leaq 0x1(%r10), %rsi
movq %rsi, (%r9)
imulq $0x28, %r10, %rsi
movl %r11d, (%rax,%rsi)
movl %ecx, 0x4(%rax,%rsi)
divsd %xmm2, %xmm0
mulsd %xmm3, %xmm0
movl %ebx, 0x8(%rax,%rsi)
cvttsd2si %xmm0, %r10
movl 0x24(%rsp), %r11d
movl %r11d, 0x10(%rax,%rsi)
addl %edx, %r10d
movl %r10d, 0x14(%rax,%rsi)
movl %edi, 0x18(%rax,%rsi)
movl %r13d, 0x1c(%rax,%rsi)
movl %r12d, 0x20(%rax,%rsi)
movl %r8d, 0x24(%rax,%rsi)
pushq $0x1
popq %r10
movl 0x14(%rsp), %esi
movl 0x10(%rsp), %edi
movq 0x60(%rsp), %r14
addq $0x28, %r15
jmp 0x81abf
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq