// File: /workspace/github_cpp_makefile_star2_O1/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), %r10d
testl %r10d, %r10d
je 0x172316
movl %edi, %ebp
movl %esi, %eax
subl %edi, %eax
movl %ecx, %edi
subl %edx, %edi
cvtsi2sd %rax, %xmm3
cvtsi2sd %rdi, %xmm4
xorl %eax, %eax
movl $0x20, %r15d
movl $0x1, %r11d
movq %r9, 0x38(%rsp)
movl %r8d, 0x8(%rsp)
movl %ecx, 0x4(%rsp)
movl %edx, (%rsp)
movl %esi, 0x14(%rsp)
movl %ebp, 0x10(%rsp)
movsd %xmm3, 0x30(%rsp)
movsd %xmm4, 0x28(%rsp)
movq 0x10(%r9), %rdi
movl -0x4(%rdi,%r15), %r14d
movl (%rdi,%r15), %ebx
cmpl %esi, %ebx
movl %esi, %r12d
cmovbl %ebx, %r12d
cmpl %ebp, %r14d
movl %ebp, %r13d
cmoval %r14d, %r13d
cmpl $0x1, %eax
jne 0x17218c
cmpl %r13d, %r12d
jbe 0x172316
cmpl %r13d, %r12d
cmoval %r11d, %eax
jbe 0x172309
movl %eax, 0x20(%rsp)
movl %r10d, 0x24(%rsp)
movl %r13d, %eax
subl %r14d, %eax
movl %ebx, %ebp
subl %r12d, %ebp
movl -0x20(%rdi,%r15), %r10d
movl -0x1c(%rdi,%r15), %esi
movl %esi, 0x1c(%rsp)
cmpl $0x0, 0x88(%rsp)
movl %ebp, %esi
cmovel %eax, %esi
cmovnel %eax, %ebp
movl -0x10(%rdi,%r15), %eax
movl %eax, 0x18(%rsp)
movq 0x8(%r9), %rax
cmpq %rax, (%r9)
jne 0x172233
movq %rsi, 0x40(%rsp)
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r9)
shlq $0x3, %rsi
leaq (%rsi,%rsi,4), %rsi
movl %r10d, 0xc(%rsp)
callq 0x2370
movq 0x40(%rsp), %rsi
movl 0xc(%rsp), %r10d
movsd 0x28(%rsp), %xmm4
movsd 0x30(%rsp), %xmm3
movl (%rsp), %edx
movl 0x4(%rsp), %ecx
movl 0x8(%rsp), %r8d
movq 0x38(%rsp), %r9
movq %rax, 0x10(%r9)
subl %r14d, %ebx
movl 0x1c(%rsp), %r14d
movl %r14d, %eax
subl %r10d, %eax
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
cvtsi2sd %rax, %xmm1
cvtsi2sd %rsi, %xmm2
divsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
cvttsd2si %xmm2, %rax
addl %r10d, %eax
xorps %xmm2, %xmm2
cvtsi2sd %rbp, %xmm2
divsd %xmm0, %xmm2
mulsd %xmm1, %xmm2
cvttsd2si %xmm2, %rsi
subl %esi, %r14d
movl %r13d, %r10d
movl 0x10(%rsp), %ebp
subl %ebp, %r10d
movl 0x14(%rsp), %esi
movl %esi, %edi
subl %r12d, %edi
xorps %xmm0, %xmm0
cvtsi2sd %r10, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rdi, %xmm1
divsd %xmm3, %xmm1
mulsd %xmm4, %xmm1
cvttsd2si %xmm1, %rdi
movl %ecx, %r11d
subl %edi, %r11d
movq (%r9), %r10
movq 0x10(%r9), %rbx
leaq 0x1(%r10), %rdi
movq %rdi, (%r9)
leaq (%r10,%r10,4), %rdi
movl %eax, (%rbx,%rdi,8)
movl %r14d, 0x4(%rbx,%rdi,8)
divsd %xmm3, %xmm0
mulsd %xmm4, %xmm0
movl 0x18(%rsp), %eax
movl %eax, 0x10(%rbx,%rdi,8)
cvttsd2si %xmm0, %rax
addl %edx, %eax
movl %eax, 0x14(%rbx,%rdi,8)
movl %r11d, 0x18(%rbx,%rdi,8)
movl %r13d, 0x1c(%rbx,%rdi,8)
movl %r12d, 0x20(%rbx,%rdi,8)
movl %r8d, 0x24(%rbx,%rdi,8)
movl 0x24(%rsp), %r10d
movl 0x20(%rsp), %eax
movl $0x1, %r11d
addq $0x28, %r15
decl %r10d
jne 0x17215e
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq