// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movl %edx, -0xc(%rbp)
movl %ecx, -0x10(%rbp)
movl %r8d, -0x14(%rbp)
movq %r9, -0x20(%rbp)
movq $0x0, -0x48(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl 0x10(%rbp), %eax
jae 0x23a63a
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x24(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movl -0x8(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x20(%rcx), %eax
ja 0x23a2a1
movl -0x8(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0x23a2ab
movq -0x48(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0x4(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jb 0x23a2c5
movl -0x4(%rbp), %eax
movl %eax, -0x7c(%rbp)
jmp 0x23a2cf
movq -0x48(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x7c(%rbp)
movl -0x78(%rbp), %eax
movl -0x7c(%rbp), %ecx
cmpl %ecx, %eax
jbe 0x23a33a
movl -0x8(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x20(%rcx), %eax
ja 0x23a2ed
movl -0x8(%rbp), %eax
movl %eax, -0x80(%rbp)
jmp 0x23a2f7
movq -0x48(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x4(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jb 0x23a317
movl -0x4(%rbp), %eax
movl %eax, -0x88(%rbp)
jmp 0x23a324
movq -0x48(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x88(%rbp)
movl -0x84(%rbp), %eax
movl -0x88(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x8c(%rbp)
jmp 0x23a344
xorl %eax, %eax
movl %eax, -0x8c(%rbp)
jmp 0x23a344
movl -0x8c(%rbp), %eax
movl %eax, -0x28(%rbp)
cmpl $0x1, -0x2c(%rbp)
jne 0x23a35e
cmpl $0x0, -0x28(%rbp)
jne 0x23a35e
jmp 0x23a63a
cmpl $0x0, -0x28(%rbp)
jbe 0x23a36b
movl $0x1, -0x2c(%rbp)
cmpl $0x0, -0x28(%rbp)
jne 0x23a376
jmp 0x23a62c
movl -0x4(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jb 0x23a38d
movl -0x4(%rbp), %eax
movl %eax, -0x90(%rbp)
jmp 0x23a39a
movq -0x48(%rbp), %rax
movl 0x1c(%rax), %eax
movl %eax, -0x90(%rbp)
movl -0x90(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x8(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x20(%rcx), %eax
ja 0x23a3ba
movl -0x8(%rbp), %eax
movl %eax, -0x94(%rbp)
jmp 0x23a3c7
movq -0x48(%rbp), %rax
movl 0x20(%rax), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x30(%rbp), %eax
movq -0x48(%rbp), %rcx
subl 0x1c(%rcx), %eax
movl %eax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movl 0x20(%rax), %eax
subl -0x34(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq -0x48(%rbp), %rax
movl 0x1c(%rax), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x54(%rbp)
movq -0x48(%rbp), %rax
movl 0x20(%rax), %eax
subl -0x3c(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x60(%rbp)
movl -0x30(%rbp), %eax
movq -0x48(%rbp), %rcx
subl 0x1c(%rcx), %eax
movl %eax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movl 0x20(%rax), %eax
subl -0x34(%rbp), %eax
movl %eax, -0x3c(%rbp)
cmpl $0x0, 0x18(%rbp)
jne 0x23a4a9
movq -0x48(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x9c(%rbp)
movl -0x38(%rbp), %edi
movq -0x48(%rbp), %rax
movl 0x20(%rax), %esi
movq -0x48(%rbp), %rax
subl 0x1c(%rax), %esi
movq -0x48(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x48(%rbp), %rax
subl (%rax), %edx
callq 0xc14d0
movl %eax, %ecx
movl -0x9c(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x70(%rbp)
movq -0x48(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x98(%rbp)
movl -0x3c(%rbp), %edi
movq -0x48(%rbp), %rax
movl 0x20(%rax), %esi
movq -0x48(%rbp), %rax
subl 0x1c(%rax), %esi
movq -0x48(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x48(%rbp), %rax
subl (%rax), %edx
callq 0xc14d0
movl %eax, %ecx
movl -0x98(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x6c(%rbp)
jmp 0x23a522
movq -0x48(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xa4(%rbp)
movl -0x3c(%rbp), %edi
movq -0x48(%rbp), %rax
movl 0x20(%rax), %esi
movq -0x48(%rbp), %rax
subl 0x1c(%rax), %esi
movq -0x48(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x48(%rbp), %rax
subl (%rax), %edx
callq 0xc14d0
movl %eax, %ecx
movl -0xa4(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x70(%rbp)
movq -0x48(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0xa0(%rbp)
movl -0x38(%rbp), %edi
movq -0x48(%rbp), %rax
movl 0x20(%rax), %esi
movq -0x48(%rbp), %rax
subl 0x1c(%rax), %esi
movq -0x48(%rbp), %rax
movl 0x4(%rax), %edx
movq -0x48(%rbp), %rax
subl (%rax), %edx
callq 0xc14d0
movl %eax, %ecx
movl -0xa0(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x6c(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x30(%rbp), %eax
subl -0x4(%rbp), %eax
movl %eax, -0x38(%rbp)
movl -0x8(%rbp), %eax
subl -0x34(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0xc(%rbp), %eax
movl %eax, -0xac(%rbp)
movl -0x38(%rbp), %edi
movl -0x8(%rbp), %esi
subl -0x4(%rbp), %esi
movl -0x10(%rbp), %edx
subl -0xc(%rbp), %edx
callq 0xc14d0
movl %eax, %ecx
movl -0xac(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x5c(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0xa8(%rbp)
movl -0x3c(%rbp), %edi
movl -0x8(%rbp), %esi
subl -0x4(%rbp), %esi
movl -0x10(%rbp), %edx
subl -0xc(%rbp), %edx
callq 0xc14d0
movl %eax, %ecx
movl -0xa8(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x58(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x23a5fc
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x23a5be
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xb8(%rbp)
jmp 0x23a5cc
movl $0x2, %eax
movq %rax, -0xb8(%rbp)
jmp 0x23a5cc
movq -0xb8(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rax
imulq $0x28, 0x8(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rcx
movq (%rcx), %rax
movq %rax, %rdx
addq $0x1, %rdx
movq %rdx, (%rcx)
imulq $0x28, %rax, %rax
addq %rax, %rdi
leaq -0x70(%rbp), %rsi
movl $0x28, %edx
callq 0x2180
jmp 0x23a62c
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x23a26b
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)