// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/horder.cpp
// Function: extract_switch_sub(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, kv_u_trans_hit_t*, unsigned int, unsigned int)
void extract_switch_sub(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 rev)
{
    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; //beg
        x.tEcur = q->tEcur - offE; //end
        
        
        x.qn = q->qn;
        offS = beg - q->tScur; offE = q->tEcur - end;
        if(rev == 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;
        // 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); 
        
        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 $0x48, %rsp
movl 0x80(%rsp), %eax
movl %esi, %r10d
movl %edi, (%rsp)
subl %edi, %r10d
movl %ecx, %edi
cvtsi2sd %r10, %xmm3
subl %edx, %edi
cvtsi2sd %rdi, %xmm4
imulq $0x28, %rax, %r10
xorl %r14d, %r14d
xorl %edi, %edi
movq %r9, 0x30(%rsp)
movl %r8d, 0xc(%rsp)
movl %ecx, 0x8(%rsp)
movl %edx, 0x4(%rsp)
movl %esi, 0x10(%rsp)
movsd %xmm3, 0x28(%rsp)
movsd %xmm4, 0x20(%rsp)
movq %r10, 0x40(%rsp)
cmpq %r14, %r10
je 0x135078
movq 0x10(%r9), %rax
movl 0x1c(%rax,%r14), %r15d
movl 0x20(%rax,%r14), %ebp
cmpl %esi, %ebp
movl %esi, %r12d
cmovbl %ebp, %r12d
movl (%rsp), %r13d
cmpl %r13d, %r15d
cmoval %r15d, %r13d
cmpl %r13d, %r12d
jbe 0x13506a
movl %r13d, %esi
subl %r15d, %esi
movl %ebp, %ebx
subl %r12d, %ebx
movl (%rax,%r14), %edi
movl %edi, 0x14(%rsp)
movl 0x4(%rax,%r14), %edi
movl %edi, 0x1c(%rsp)
cmpl $0x0, 0x88(%rsp)
movl %ebx, %r11d
cmovel %esi, %r11d
cmovnel %esi, %ebx
movl 0x10(%rax,%r14), %esi
movl %esi, 0x18(%rsp)
movq (%r9), %r10
cmpq 0x8(%r9), %r10
jne 0x134faf
leaq (%r10,%r10), %rsi
testq %r10, %r10
pushq $0x2
popq %rdi
cmoveq %rdi, %rsi
movq %rsi, 0x8(%r9)
imulq $0x28, %rsi, %rsi
movq %rax, %rdi
movq %r11, 0x38(%rsp)
callq 0x2370
movq 0x38(%rsp), %r11
movsd 0x20(%rsp), %xmm4
movsd 0x28(%rsp), %xmm3
movl 0x4(%rsp), %edx
movl 0x8(%rsp), %ecx
movl 0xc(%rsp), %r8d
movq 0x30(%rsp), %r9
movq %rax, 0x10(%r9)
movq (%r9), %r10
subl %r15d, %ebp
cvtsi2sd %rbp, %xmm0
movl 0x1c(%rsp), %ebp
movl %ebp, %esi
movl 0x14(%rsp), %edi
subl %edi, %esi
cvtsi2sd %rsi, %xmm1
cvtsi2sd %r11, %xmm2
divsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
cvttsd2si %xmm2, %r11
addl %edi, %r11d
cvtsi2sd %rbx, %xmm2
divsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
cvttsd2si %xmm2, %rbx
movl %r13d, %edi
subl (%rsp), %edi
cvtsi2sd %rdi, %xmm0
movl 0x10(%rsp), %esi
movl %esi, %edi
subl %r12d, %edi
cvtsi2sd %rdi, %xmm1
divsd %xmm3, %xmm1
mulsd %xmm4, %xmm1
cvttsd2si %xmm1, %rdi
subl %ebx, %ebp
movl %ecx, %ebx
subl %edi, %ebx
leaq 0x1(%r10), %rdi
movq %rdi, (%r9)
imulq $0x28, %r10, %rdi
movl %r11d, (%rax,%rdi)
divsd %xmm3, %xmm0
mulsd %xmm4, %xmm0
movl %ebp, 0x4(%rax,%rdi)
cvttsd2si %xmm0, %r10
movl 0x18(%rsp), %r11d
movl %r11d, 0x10(%rax,%rdi)
addl %edx, %r10d
movl %r10d, 0x14(%rax,%rdi)
movl %ebx, 0x18(%rax,%rdi)
movl %r13d, 0x1c(%rax,%rdi)
movl %r12d, 0x20(%rax,%rdi)
movl %r8d, 0x24(%rax,%rdi)
pushq $0x1
popq %rdi
movq 0x40(%rsp), %r10
jmp 0x13506f
cmpl $0x1, %edi
je 0x135078
addq $0x28, %r14
jmp 0x134eee
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq