// File: /workspace/github_cpp_makefile_star2_O1/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, 0x60(%rsp)
movq %rsi, %r15
movq (%rsi), %rbx
movq 0x28(%rdi), %rax
movl %edx, %ecx
movq (%rax,%rcx,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r9
shlq $0x4, %r9
addq 0x10(%rdi), %r9
testl %eax, %eax
je 0xc3678
movl %eax, %eax
xorl %ecx, %ecx
movq %r9, %rdx
xorl %r10d, %r10d
cmpb $0x0, 0x28(%rdx)
js 0xc3666
movl %r10d, %esi
cmpq %rsi, %rcx
je 0xc3663
movups (%rdx), %xmm0
movups 0x10(%rdx), %xmm1
movups 0x20(%rdx), %xmm2
movaps %xmm2, 0xc0(%rsp)
movaps %xmm1, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
leaq (%rsi,%rsi,2), %rsi
shlq $0x4, %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 %r10d
incq %rcx
addq $0x30, %rdx
cmpq %rcx, %rax
jne 0xc35e1
jmp 0xc367b
xorl %r10d, %r10d
cmpl $0x2, %r10d
jae 0xc3688
xorl %eax, %eax
jmp 0xc3af3
movq %rdi, 0x88(%rsp)
movq %r8, 0x78(%rsp)
movl %ebx, %eax
movq %rax, 0x58(%rsp)
movl %r10d, %r14d
leaq -0x789(%rip), %rcx      # 0xc2f1d
movl $0x30, %edx
movq %r9, 0x10(%rsp)
movq %r9, %rdi
movq %r14, %rsi
movq %r10, %r12
callq 0x23f0
movq %r12, %r8
leal (%rbx,%r12,4), %eax
cmpq %rax, 0x8(%r15)
jae 0xc371f
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(%r15)
movq 0x10(%r15), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %r12, %r8
movq %rax, 0x10(%r15)
movq 0x10(%r15), %rax
cmpl $0x1, %r8d
adcl $0x0, %r8d
xorl %ecx, %ecx
movq 0x10(%rsp), %rdx
movl (%rdx), %esi
movq (%r15), %rdi
movq %rsi, (%rax,%rdi,8)
movq (%r15), %rsi
shlq $0x20, (%rax,%rsi,8)
movq (%r15), %rsi
orq %rcx, (%rax,%rsi,8)
incq (%r15)
incq %rcx
addq $0x30, %rdx
cmpq %rcx, %r8
jne 0xc3732
movq 0x58(%rsp), %rax
leaq (,%rax,8), %rbx
addq 0x10(%r15), %rbx
leaq (%rbx,%r14,8), %r12
leaq (%r12,%r14,8), %rax
movq %rax, 0x18(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
movq %r8, 0x30(%rsp)
callq 0x9d53f
movq %r12, %r10
movq %rbx, %r9
movq 0x30(%rsp), %r8
xorl %eax, %eax
movl (%r9,%rax,8), %ecx
movq %rcx, (%r9,%rax,8)
movq %rax, (%r10,%rcx,8)
incq %rax
cmpq %rax, %r8
jne 0xc3794
movq 0x18(%rsp), %rax
leaq (%rax,%r14,8), %rax
movq %rax, 0x38(%rsp)
xorl %r14d, %r14d
movsd 0x161d27(%rip), %xmm0   # 0x2254e8
movq $0x0, 0x50(%rsp)
movq 0x10(%rsp), %r11
movq %r15, 0x20(%rsp)
movq %r9, 0x48(%rsp)
movq %r10, 0x40(%rsp)
leaq (%r14,%r14,2), %rax
shlq $0x4, %rax
leaq (%r11,%rax), %rdi
movl (%r11,%rax), %ecx
movl 0x4(%r11,%rax), %edx
movl %edx, %eax
subl %ecx, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd 0x60(%rsp), %xmm1
cvttsd2si %xmm1, %rax
movq %rax, 0x98(%rsp)
xorl %r13d, %r13d
xorl %esi, %esi
movq $0x0, 0x68(%rsp)
movq $0x0, 0x28(%rsp)
movl $0x0, 0x8(%rsp)
movl %ecx, 0x4(%rsp)
movl %edx, (%rsp)
movq %rdi, 0x80(%rsp)
movq (%r9,%r13,8), %rax
cmpq (%r10,%r14,8), %r13
jne 0xc3858
cmpq %r14, %rax
je 0xc392f
jmp 0xc3b05
cmpq %r14, %rax
ja 0xc392f
leaq (%rax,%rax,2), %rbx
shlq $0x4, %rbx
movl (%r11,%rbx), %r15d
cmpl %edx, %r15d
jae 0xc3ad5
addq %r11, %rbx
cmpb $0x0, 0x28(%rbx)
js 0xc392f
cmpl %r15d, %ecx
movl %r15d, %ebp
cmoval %ecx, %ebp
movl 0x4(%rbx), %eax
cmpl %eax, %edx
movl %eax, %r12d
cmovbl %edx, %r12d
cmpl %ebp, %r12d
jbe 0xc392f
movq %rax, 0x70(%rsp)
movl %esi, 0xc(%rsp)
movq %rbx, %rsi
movq 0x78(%rsp), %rdx
callq 0xc30f8
testl %eax, %eax
je 0xc38e1
movq 0x10(%rsp), %r11
movq 0x30(%rsp), %r8
movq 0x48(%rsp), %r9
movq 0x40(%rsp), %r10
movsd 0x161c11(%rip), %xmm0   # 0x2254e8
movq 0x80(%rsp), %rdi
jmp 0xc3924
movq %rbx, %rdi
movq 0x80(%rsp), %rsi
movq %rsi, %rbx
movq 0x78(%rsp), %rdx
movsd 0x161bec(%rip), %xmm0   # 0x2254e8
callq 0xc30f8
testl %eax, %eax
je 0xc3940
movq 0x10(%rsp), %r11
movq 0x30(%rsp), %r8
movq 0x48(%rsp), %r9
movq 0x40(%rsp), %r10
movsd 0x161bc7(%rip), %xmm0   # 0x2254e8
movq %rbx, %rdi
movl 0x4(%rsp), %ecx
movl (%rsp), %edx
movl 0xc(%rsp), %esi
incq %r13
cmpq %r13, %r8
jne 0xc3840
jmp 0xc3ad5
movq 0x68(%rsp), %rdi
testl %edi, %edi
movq 0x10(%rsp), %r11
movq 0x30(%rsp), %r8
movq 0x48(%rsp), %r9
movq 0x40(%rsp), %r10
movl 0xc(%rsp), %esi
je 0xc3975
leal -0x1(%rdi), %eax
movq 0x18(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
cmpl %ecx, %ebp
jbe 0xc3a2a
subl %ebp, %esi
addl %r12d, %esi
movl %edi, %eax
shlq $0x20, %rbp
movl %r12d, %ecx
orq %rbp, %rcx
movq 0x18(%rsp), %rdx
movq %rcx, (%rdx,%rax,8)
incl %edi
movq %rdi, 0x68(%rsp)
movsd 0x161b4a(%rip), %xmm0   # 0x2254e8
movq %rbx, %rdi
movq 0x28(%rsp), %rdx
testl %edx, %edx
je 0xc39bf
leal -0x1(%rdx), %eax
movq 0x38(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
cmpl %ecx, %r15d
jbe 0xc3a52
movl 0x8(%rsp), %eax
subl %r15d, %eax
movq 0x70(%rsp), %rcx
addl %ecx, %eax
movl %eax, 0x8(%rsp)
movl %edx, %eax
shlq $0x20, %r15
orq %rcx, %r15
movq 0x38(%rsp), %rcx
movq %r15, (%rcx,%rax,8)
incl %edx
movq %rdx, 0x28(%rsp)
movl 0x4(%rsp), %ecx
movl (%rsp), %edx
movl %esi, %eax
cmpq 0x90(%rsp), %rax
jb 0xc392f
cmpl 0x98(%rsp), %esi
jae 0xc3a72
cvtsi2sd %rax, %xmm2
movl 0x8(%rsp), %eax
cvtsi2sd %rax, %xmm1
mulsd 0x60(%rsp), %xmm1
ucomisd %xmm1, %xmm2
jb 0xc392f
jmp 0xc3a72
subl %ecx, %r12d
movsd 0x161ab3(%rip), %xmm0   # 0x2254e8
movq %rbx, %rdi
jbe 0xc39a1
addl %r12d, %esi
addq %r12, %rcx
movq 0x18(%rsp), %rdx
movq %rcx, (%rdx,%rax,8)
jmp 0xc39a1
movq 0x70(%rsp), %rdx
cmpl %ecx, %edx
jbe 0xc39ea
subl %ecx, %edx
addl %edx, 0x8(%rsp)
addq %rdx, %rcx
movq 0x38(%rsp), %rdx
movq %rcx, (%rdx,%rax,8)
jmp 0xc39ea
orb $-0x80, 0x28(%rdi)
incq 0x50(%rsp)
movq 0x88(%rsp), %rdx
movq 0x28(%rdx), %rax
movl 0x14(%rdi), %ecx
movq (%rax,%rcx,8), %rax
testl %eax, %eax
je 0xc3ad5
movq 0x10(%rdx), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
movl %eax, %esi
leaq (%rdx,%rdx,2), %rax
shlq $0x4, %rax
addq %rcx, %rax
addq $0x28, %rax
shlq $0x4, %rsi
leaq (%rsi,%rsi,2), %rcx
xorl %edx, %edx
movq 0x20(%rsp), %r15
movl -0x14(%rax,%rdx), %esi
cmpl 0x8(%rdi), %esi
jne 0xc3aca
orb $-0x80, (%rax,%rdx)
addq $0x30, %rdx
cmpq %rdx, %rcx
jne 0xc3abd
jmp 0xc3ada
movq 0x20(%rsp), %r15
incq %r14
cmpq %r8, %r14
jne 0xc37de
movq 0x58(%rsp), %rax
movq %rax, (%r15)
movq 0x50(%rsp), %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x166f41(%rip), %rdi    # 0x22aa4d
leaq 0x16618e(%rip), %rsi    # 0x229ca1
leaq 0x166f3e(%rip), %rcx    # 0x22aa58
movl $0x3bda, %edx           # imm = 0x3BDA
callq 0x2110