// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: trans_sec_cut0(kv_u_trans_t*, asg64_v*, unsigned int, double, unsigned long, ma_ug_t*)
uint64_t trans_sec_cut0(kv_u_trans_t *ta, asg64_v *srt, uint32_t id, double sec_rate, uint64_t bd, ma_ug_t *ug) 
{
    u_trans_t *a = NULL, *mz, *cz, t, *ca;
    uint32_t a_n, r_n, c_n, k, l, z, i, m, wm, wl, len, b_n = srt->n;
    uint32_t ovq, os, oe; uint64_t *ss, *hs, *bu, *wu, occ = 0;

    a = u_trans_a(*ta, id); a_n = u_trans_n(*ta, id);
    for (k = r_n = 0; k < a_n; k++) {///RC_0/RC_1 to a[0, r_n)
        if(a[k].del) continue;
        if(k != r_n) {
            t = a[k]; a[k] = a[r_n]; a[r_n] = t;
        }
        r_n++;
    }
    if(r_n <= 1) return occ;
    qsort(a, r_n, sizeof((*a)), cmp_u_trans_weight);
    
    kv_resize(uint64_t, *srt, (b_n+(r_n<<2)));
    for (k = 0; k < r_n; k++) {
        srt->a[srt->n] = a[k].qs;
        srt->a[srt->n] <<= 32;
        srt->a[srt->n] |= k;
        srt->n++;
    }

    ss = srt->a + b_n; hs = ss + r_n; bu = hs + r_n; wu = bu + r_n;
    radix_sort_arch64(ss, ss + r_n);
    for (k = 0; k < r_n; k++) {
        ss[k] = (uint32_t)ss[k]; hs[ss[k]] = k;
    }

    for (k = 0; k < r_n; k++) {
        cz = &(a[k]); len = (cz->qe-cz->qs)*sec_rate; m = wm = 0;
        for (z = l = wl = 0; z < r_n; z++) {
            if(z == hs[k]) {
                assert(ss[z] == k);
                continue;
            }
            if(ss[z] > k) continue;///smaller nw than a[k]
            mz = &(a[ss[z]]);
            if(mz->qs >= cz->qe) break;
            if(mz->del) continue;///has been deleted
            // if((mz->f != RC_0) && (mz->f != RC_1) && (cz->nw > (mz->nw*0.95))) continue;
            os = MAX(cz->qs, mz->qs); oe = MIN(cz->qe, mz->qe);
            ovq = ((oe > os)? (oe - os):0);
            if(!ovq) continue;
            if(is_connect_arc(cz, mz, ug, 0.04) || is_connect_arc(mz, cz, ug, 0.04)) continue;

            if((m > 0) && (((uint32_t)bu[m-1]) >= os)) {
                if(oe > ((uint32_t)bu[m-1])) {
                    l += (oe - ((uint32_t)bu[m-1]));
                    bu[m-1] += (oe - ((uint32_t)bu[m-1]));
                }
            } else {
                l += oe - os;
                bu[m] = os; bu[m] <<= 32; bu[m] |= oe; m++;
            }

            if((wm > 0) && (((uint32_t)wu[wm-1]) >= mz->qs)) {
                if(mz->qe > ((uint32_t)wu[wm-1])) {
                    wl += (mz->qe - ((uint32_t)wu[wm-1]));
                    wu[wm-1] += (mz->qe - ((uint32_t)wu[wm-1]));
                }
            } else {
                wl += mz->qe - mz->qs;
                wu[wm] = mz->qs; wu[wm] <<= 32; wu[wm] |= mz->qe; wm++;
            }

            if((l >= bd) && ((l>=len) || (l>=(wl*sec_rate)))) {
                cz->del = 1; occ++; 
                ca = u_trans_a(*ta, cz->tn); c_n = u_trans_n(*ta, cz->tn);
                for (i = 0; i < c_n; i++) {
                    if(ca[i].tn == cz->qn) ca[i].del = 1;
                }
                break;
            }
        }        
    }

    srt->n = b_n;
    return occ;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rcx, 0x90(%rsp)
movsd %xmm0, 0x70(%rsp)
movq 0x28(%rdi), %rax
movl %edx, %ecx
movq (%rax,%rcx,8), %rcx
movl %ecx, %eax
shrq $0x20, %rcx
imulq $0x30, %rcx, %r9
movq %rdi, 0x88(%rsp)
addq 0x10(%rdi), %r9
movq %rsi, 0x68(%rsp)
movl (%rsi), %ecx
movq %rcx, 0x40(%rsp)
xorl %ecx, %ecx
movq %r9, %rdx
xorl %ebx, %ebx
movl %ebx, %esi
cmpq %rcx, %rax
je 0x87f8d
cmpb $0x0, 0x28(%rdx)
js 0x87f81
cmpq %rsi, %rcx
je 0x87f7f
movups (%rdx), %xmm0
movups 0x10(%rdx), %xmm1
movups 0x20(%rdx), %xmm2
movaps %xmm2, 0xc0(%rsp)
movaps %xmm1, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
imulq $0x30, %rsi, %rsi
movups (%r9,%rsi), %xmm0
movups 0x10(%r9,%rsi), %xmm1
movups 0x20(%r9,%rsi), %xmm2
movups %xmm2, 0x20(%rdx)
movups %xmm1, 0x10(%rdx)
movups %xmm0, (%rdx)
movaps 0xa0(%rsp), %xmm0
movapd 0xb0(%rsp), %xmm1
movapd 0xc0(%rsp), %xmm2
movupd %xmm2, 0x20(%r9,%rsi)
movupd %xmm1, 0x10(%r9,%rsi)
movups %xmm0, (%r9,%rsi)
incl %ebx
incq %rcx
addq $0x30, %rdx
jmp 0x87ef9
cmpl $0x2, %ebx
jae 0x87f99
xorl %eax, %eax
jmp 0x883fb
movq %r8, 0x10(%rsp)
leaq -0x6f3(%rip), %rcx      # 0x878b2
pushq $0x30
popq %rdx
movq %r9, 0x28(%rsp)
movq %r9, %rdi
movq %rsi, 0x20(%rsp)
callq 0x23f0
movq 0x40(%rsp), %rax
leal (%rax,%rbx,4), %eax
movq 0x68(%rsp), %rbx
cmpq %rax, 0x8(%rbx)
jae 0x88020
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
jmp 0x88024
movq 0x10(%rbx), %rax
xorl %ecx, %ecx
movq 0x28(%rsp), %rdx
movq 0x20(%rsp), %r8
cmpq %rcx, %r8
je 0x88059
movl (%rdx), %esi
movq (%rbx), %rdi
movq %rsi, (%rax,%rdi,8)
movq (%rbx), %rsi
shlq $0x20, (%rax,%rsi,8)
movq (%rbx), %rsi
orq %rcx, (%rax,%rsi,8)
incq (%rbx)
incq %rcx
addq $0x30, %rdx
jmp 0x88030
movq 0x40(%rsp), %rcx
leaq (%rax,%rcx,8), %rbx
leaq (%rbx,%r8,8), %r14
leaq (%r14,%r8,8), %rax
movq %rax, 0x30(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x66c25
movq %r14, %r11
movq %rbx, %r10
movq 0x20(%rsp), %r9
xorl %eax, %eax
movq 0x10(%rsp), %rdx
cmpq %rax, %r9
je 0x880a2
movl (%r10,%rax,8), %ecx
movq %rcx, (%r10,%rax,8)
movq %rax, (%r11,%rcx,8)
incq %rax
jmp 0x8808c
movq 0x30(%rsp), %rax
leaq (%rax,%r9,8), %rax
movq %rax, 0x48(%rsp)
xorl %ecx, %ecx
movsd 0x15b47e(%rip), %xmm0   # 0x1e3538
movq $0x0, 0x60(%rsp)
movq 0x28(%rsp), %r8
movq %r10, 0x58(%rsp)
movq %r11, 0x50(%rsp)
cmpq %r9, %rcx
je 0x883e9
imulq $0x30, %rcx, %rax
leaq (%r8,%rax), %rbx
movl (%r8,%rax), %edi
movl 0x4(%r8,%rax), %esi
movl %esi, %eax
subl %edi, %eax
cvtsi2sd %rax, %xmm1
mulsd 0x70(%rsp), %xmm1
cvttsd2si %xmm1, %rax
movq %rax, 0x98(%rsp)
xorl %r13d, %r13d
movl $0x0, 0x8(%rsp)
xorl %r12d, %r12d
movq $0x0, 0x78(%rsp)
xorl %ebp, %ebp
movq %rcx, 0x18(%rsp)
movl %edi, 0xc(%rsp)
movl %esi, 0x4(%rsp)
cmpq %r13, %r9
je 0x883e1
movq (%r10,%r13,8), %rax
cmpq (%r11,%rcx,8), %r13
jne 0x8814f
cmpq %rcx, %rax
je 0x88230
jmp 0x8840d
cmpq %rcx, %rax
ja 0x88230
imulq $0x30, %rax, %r15
movl (%r8,%r15), %r14d
cmpl %esi, %r14d
jae 0x883e1
addq %r8, %r15
cmpb $0x0, 0x28(%r15)
js 0x88230
movl %ebp, (%rsp)
movq %r12, 0x38(%rsp)
cmpl %r14d, %edi
movl %r14d, %ebp
cmoval %edi, %ebp
movl 0x4(%r15), %eax
cmpl %eax, %esi
movl %eax, %r12d
cmovbl %esi, %r12d
cmpl %ebp, %r12d
jbe 0x88228
movq %rax, 0x80(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x87a7b
testl %eax, %eax
je 0x881d8
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %r9
movq 0x58(%rsp), %r10
movq 0x50(%rsp), %r11
movsd 0x15b362(%rip), %xmm0   # 0x1e3538
jmp 0x8821b
movq %r15, %rdi
movq %rbx, %r15
movq %rbx, %rsi
movq 0x10(%rsp), %rdx
movsd 0x15b34a(%rip), %xmm0   # 0x1e3538
callq 0x87a7b
testl %eax, %eax
je 0x88238
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %r9
movq 0x58(%rsp), %r10
movq 0x50(%rsp), %r11
movsd 0x15b320(%rip), %xmm0   # 0x1e3538
movq %r15, %rbx
movq 0x18(%rsp), %rcx
movl 0xc(%rsp), %edi
movl 0x4(%rsp), %esi
movq 0x38(%rsp), %r12
movl (%rsp), %ebp
incq %r13
jmp 0x8812e
movq 0x78(%rsp), %rbx
testl %ebx, %ebx
movq 0x28(%rsp), %r8
movq 0x20(%rsp), %r9
movq 0x58(%rsp), %r10
movq 0x50(%rsp), %r11
movl 0xc(%rsp), %edi
movl 0x4(%rsp), %esi
je 0x88271
leal -0x1(%rbx), %eax
movq 0x30(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
cmpl %ecx, %ebp
jbe 0x882fa
movl (%rsp), %eax
subl %ebp, %eax
addl %r12d, %eax
movl %eax, (%rsp)
movl %ebx, %eax
shlq $0x20, %rbp
movl %r12d, %ecx
orq %rbp, %rcx
movq 0x30(%rsp), %rdx
movq %rcx, (%rdx,%rax,8)
incl %ebx
movq %rbx, 0x78(%rsp)
movq 0x10(%rsp), %rdx
movsd 0x15b293(%rip), %xmm0   # 0x1e3538
movq %r15, %rbx
movq 0x38(%rsp), %r12
testl %r12d, %r12d
je 0x882c5
leal -0x1(%r12), %eax
movq 0x48(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
cmpl %ecx, %r14d
jbe 0x88325
movl 0x8(%rsp), %eax
subl %r14d, %eax
movq 0x80(%rsp), %rcx
addl %ecx, %eax
movl %eax, 0x8(%rsp)
movl %r12d, %eax
shlq $0x20, %r14
orq %rcx, %r14
movq 0x48(%rsp), %rcx
movq %r14, (%rcx,%rax,8)
incl %r12d
movq 0x18(%rsp), %rcx
movl (%rsp), %ebp
jmp 0x88352
subl %ecx, %r12d
movq 0x10(%rsp), %rdx
movsd 0x15b22e(%rip), %xmm0   # 0x1e3538
movq %r15, %rbx
jbe 0x882a8
addl %r12d, (%rsp)
addq %r12, %rcx
movq 0x30(%rsp), %rsi
movq %rcx, (%rsi,%rax,8)
movl 0x4(%rsp), %esi
jmp 0x882a8
movq 0x80(%rsp), %r14
cmpl %ecx, %r14d
movl (%rsp), %ebp
jbe 0x8834d
subl %ecx, %r14d
addl %r14d, 0x8(%rsp)
addq %r14, %rcx
movq 0x48(%rsp), %rsi
movq %rcx, (%rsi,%rax,8)
movl 0x4(%rsp), %esi
movq 0x18(%rsp), %rcx
movl %ebp, %eax
cmpq 0x90(%rsp), %rax
jb 0x88230
cmpl 0x98(%rsp), %ebp
jae 0x88389
cvtsi2sd %rax, %xmm2
movl 0x8(%rsp), %eax
cvtsi2sd %rax, %xmm1
mulsd 0x70(%rsp), %xmm1
ucomisd %xmm1, %xmm2
jb 0x88230
orb $-0x80, 0x28(%rbx)
movq 0x88(%rsp), %rax
movq 0x10(%rax), %rcx
movq 0x28(%rax), %rax
movl 0x14(%rbx), %esi
movq (%rax,%rsi,8), %rax
movl %eax, %edi
movq %rax, %rsi
shrq $0x20, %rsi
imulq $0x30, %rdi, %rax
imulq $0x30, %rsi, %rsi
addq %rsi, %rcx
addq $0x28, %rcx
xorl %edi, %edi
cmpq %rdi, %rax
je 0x883d7
movl -0x14(%rcx,%rdi), %esi
cmpl 0x8(%r15), %esi
jne 0x883d1
orb $-0x80, (%rcx,%rdi)
addq $0x30, %rdi
jmp 0x883be
incq 0x60(%rsp)
movq 0x18(%rsp), %rcx
incq %rcx
jmp 0x880d2
movq 0x68(%rsp), %rax
movq 0x40(%rsp), %rcx
movq %rcx, (%rax)
movq 0x60(%rsp), %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x160729(%rip), %rdi    # 0x1e8b3d
leaq 0x15f976(%rip), %rsi    # 0x1e7d91
leaq 0x160726(%rip), %rcx    # 0x1e8b48
movl $0x3bda, %edx           # imm = 0x3BDA
callq 0x2110