// File: chhylp123[P]hifiasm/tovlp.cpp
// Function: extract_sub_overlaps_utg_thit(unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, kv_utg_thit_t_t*, unsigned int)
void extract_sub_overlaps_utg_thit(uint32_t i_ts, uint32_t i_te, uint32_t i_tus, uint32_t i_tue,
uint32_t tn, kv_utg_thit_t_t* ktb, uint32_t bn)
{
    uint32_t i, ovlp, found, beg, end, offS, offE;
    utg_thit_t *q = NULL, x;
    for (i = found = 0; i < bn; i++)
    {
        q = &(ktb->a[i]);///for q, already know [qs, qe), [qus, que), [ts, te)

        ovlp = ((MIN(i_te, q->te) > MAX(i_ts, q->ts))?
                                        MIN(i_te, q->te) - MAX(i_ts, q->ts):0);
        if(found == 1 && ovlp == 0) break;
        if(ovlp > 0) found = 1;
        if(ovlp == 0) continue;

        
        beg = MAX(i_ts, q->ts); end = MIN(i_te, q->te);
        offS = beg - q->ts; offE = q->te - end;
        x.ts = q->ts + offS; 
        x.te = q->te - offE;
        //x.qs = q->qs + offS; 
        x.qs = q->qs + get_offset_adjust(offS, q->te-q->ts, q->qe-q->qs);        
        ///x.qe = q->qe - offE;
        x.qe = q->qe - get_offset_adjust(offE, q->te-q->ts, q->qe-q->qs);  

        x.qn = q->qn;
        offS = beg - q->ts; offE = q->te - end;
        if((x.qn&1) == 0)
        {
            // x.qus = q->qus + offS; 
            x.qus = q->qus + get_offset_adjust(offS, q->te-q->ts, q->que-q->qus);  
            // x.que = q->que - offE;
            x.que = q->que - get_offset_adjust(offE, q->te-q->ts, q->que-q->qus);
        }
        else
        {
            // x.qus = q->qus + offE; 
            x.qus = q->qus + get_offset_adjust(offE, q->te-q->ts, q->que-q->qus);
            // x.que = q->que - offS;
            x.que = q->que - get_offset_adjust(offS, q->te-q->ts, q->que-q->qus);
        }

        x.tn = tn; 
        offS = beg - i_ts; offE = i_te - end;
        if((x.tn&1) == 0)
        {
            // x.tus = i_tus + offS; 
            x.tus = i_tus +  get_offset_adjust(offS, i_te-i_ts, i_tue-i_tus); 
            // x.tue = i_tue - offE;
            x.tue = i_tue - get_offset_adjust(offE, i_te-i_ts, i_tue-i_tus); 
        }
        else
        {
            // x.tus = i_tus + offE; 
            x.tus = i_tus + get_offset_adjust(offE, i_te-i_ts, i_tue-i_tus); 
            // x.tue = i_tue - offS;
            x.tue = i_tue - get_offset_adjust(offS, i_te-i_ts, i_tue-i_tus);
        }
        
        kv_push(utg_thit_t, *ktb, x);

        // if(x.tus >= x.tue || x.qus >= x.que)
        // {
        //     fprintf(stderr, "\n*********x.qn: %u, x.tn: %u\n", x.qn, x.tn);
        //     fprintf(stderr, "x.qus: %u, x.que: %u, x.tus: %u, x.tue: %u\n", 
        //     x.qus, x.que, x.tus, x.tue);
        //     fprintf(stderr, "q->qs: %u, q->qe: %u, q->qus: %u, q->que: %u\n", 
        //     q->qs, q->qe, q->qus, q->que);
        //     fprintf(stderr, "q->ts: %u, q->te: %u, q->tus: %u, q->tue: %u\n", 
        //     q->ts, q->te, q->tus, q->tue);
        //     fprintf(stderr, "i_ts: %u, i_te: %u, i_tus: %u, i_tue: %u\n", 
        //     i_ts, i_te, i_tus, i_tue);
        // }
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
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 0x26f4fe
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 0x18(%rcx), %eax
ja 0x26f07e
movl -0x8(%rbp), %eax
movl %eax, -0x74(%rbp)
jmp 0x26f088
movq -0x48(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0x4(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jb 0x26f0a2
movl -0x4(%rbp), %eax
movl %eax, -0x7c(%rbp)
jmp 0x26f0ac
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x7c(%rbp)
movl -0x78(%rbp), %eax
movl -0x7c(%rbp), %ecx
cmpl %ecx, %eax
jbe 0x26f117
movl -0x8(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x18(%rcx), %eax
ja 0x26f0ca
movl -0x8(%rbp), %eax
movl %eax, -0x80(%rbp)
jmp 0x26f0d4
movq -0x48(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x84(%rbp)
movl -0x4(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jb 0x26f0f4
movl -0x4(%rbp), %eax
movl %eax, -0x88(%rbp)
jmp 0x26f101
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x88(%rbp)
movl -0x84(%rbp), %eax
movl -0x88(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x8c(%rbp)
jmp 0x26f121
xorl %eax, %eax
movl %eax, -0x8c(%rbp)
jmp 0x26f121
movl -0x8c(%rbp), %eax
movl %eax, -0x28(%rbp)
cmpl $0x1, -0x2c(%rbp)
jne 0x26f13b
cmpl $0x0, -0x28(%rbp)
jne 0x26f13b
jmp 0x26f4fe
cmpl $0x0, -0x28(%rbp)
jbe 0x26f148
movl $0x1, -0x2c(%rbp)
cmpl $0x0, -0x28(%rbp)
jne 0x26f153
jmp 0x26f4f0
movl -0x4(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jb 0x26f16a
movl -0x4(%rbp), %eax
movl %eax, -0x90(%rbp)
jmp 0x26f177
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x90(%rbp)
movl -0x90(%rbp), %eax
movl %eax, -0x30(%rbp)
movl -0x8(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpl 0x18(%rcx), %eax
ja 0x26f197
movl -0x8(%rbp), %eax
movl %eax, -0x94(%rbp)
jmp 0x26f1a4
movq -0x48(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x94(%rbp)
movl -0x94(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x30(%rbp), %eax
movq -0x48(%rbp), %rcx
subl 0x14(%rcx), %eax
movl %eax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movl 0x18(%rax), %eax
subl -0x34(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
addl -0x38(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x48(%rbp), %rax
movl 0x18(%rax), %eax
subl -0x3c(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x48(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x9c(%rbp)
movl -0x38(%rbp), %edi
movq -0x48(%rbp), %rax
movl 0x18(%rax), %esi
movq -0x48(%rbp), %rax
subl 0x14(%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 0x18(%rax), %esi
movq -0x48(%rbp), %rax
subl 0x14(%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)
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x68(%rbp)
movl -0x30(%rbp), %eax
movq -0x48(%rbp), %rcx
subl 0x14(%rcx), %eax
movl %eax, -0x38(%rbp)
movq -0x48(%rbp), %rax
movl 0x18(%rax), %eax
subl -0x34(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x68(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x26f307
movq -0x48(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0xa4(%rbp)
movl -0x38(%rbp), %edi
movq -0x48(%rbp), %rax
movl 0x18(%rax), %esi
movq -0x48(%rbp), %rax
subl 0x14(%rax), %esi
movq -0x48(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x48(%rbp), %rax
subl 0xc(%rax), %edx
callq 0xc14d0
movl %eax, %ecx
movl -0xa4(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x64(%rbp)
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0xa0(%rbp)
movl -0x3c(%rbp), %edi
movq -0x48(%rbp), %rax
movl 0x18(%rax), %esi
movq -0x48(%rbp), %rax
subl 0x14(%rax), %esi
movq -0x48(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x48(%rbp), %rax
subl 0xc(%rax), %edx
callq 0xc14d0
movl %eax, %ecx
movl -0xa0(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x60(%rbp)
jmp 0x26f383
movq -0x48(%rbp), %rax
movl 0xc(%rax), %eax
movl %eax, -0xac(%rbp)
movl -0x3c(%rbp), %edi
movq -0x48(%rbp), %rax
movl 0x18(%rax), %esi
movq -0x48(%rbp), %rax
subl 0x14(%rax), %esi
movq -0x48(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x48(%rbp), %rax
subl 0xc(%rax), %edx
callq 0xc14d0
movl %eax, %ecx
movl -0xac(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x64(%rbp)
movq -0x48(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0xa8(%rbp)
movl -0x38(%rbp), %edi
movq -0x48(%rbp), %rax
movl 0x18(%rax), %esi
movq -0x48(%rbp), %rax
subl 0x14(%rax), %esi
movq -0x48(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x48(%rbp), %rax
subl 0xc(%rax), %edx
callq 0xc14d0
movl %eax, %ecx
movl -0xa8(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x60(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x54(%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 -0x54(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
jne 0x26f3fc
movl -0xc(%rbp), %eax
movl %eax, -0xb4(%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 -0xb4(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x50(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0xb0(%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 -0xb0(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x4c(%rbp)
jmp 0x26f450
movl -0xc(%rbp), %eax
movl %eax, -0xbc(%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 -0xbc(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x50(%rbp)
movl -0x10(%rbp), %eax
movl %eax, -0xb8(%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 -0xb8(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x4c(%rbp)
jmp 0x26f452
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq -0x20(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x26f4c0
movq -0x20(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x26f482
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0xc8(%rbp)
jmp 0x26f490
movl $0x2, %eax
movq %rax, -0xc8(%rbp)
jmp 0x26f490
movq -0xc8(%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 0x26f4f0
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x26f048
addq $0xd0, %rsp
popq %rbp
retq
nopw (%rax,%rax)