// File: /workspace/github_cpp_makefile_star2_O3/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, 0x58(%rsp)
movq 0x28(%rdi), %rax
movl %edx, %ecx
movq (%rax,%rcx,8), %rdx
xorl %eax, %eax
testl %edx, %edx
je 0xc49c5
movq %r8, %r14
movq %rsi, %r15
movq %rdi, %r12
movq (%rsi), %rbx
movq %rdx, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rdi
shlq $0x4, %rdi
addq 0x10(%r12), %rdi
movl %ebx, %ecx
movq %rcx, 0x50(%rsp)
movl %edx, %r8d
movq %rdi, %rcx
xorl %edx, %edx
cmpb $0x0, 0x28(%rcx)
js 0xc4560
movl %eax, %esi
cmpq %rsi, %rdx
je 0xc455e
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movaps %xmm2, 0xc0(%rsp)
movaps %xmm1, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
leaq (%rsi,%rsi,2), %rsi
shlq $0x4, %rsi
movups (%rdi,%rsi), %xmm0
movups 0x10(%rdi,%rsi), %xmm1
movups 0x20(%rdi,%rsi), %xmm2
movups %xmm2, 0x20(%rcx)
movups %xmm1, 0x10(%rcx)
movups %xmm0, (%rcx)
movaps 0xa0(%rsp), %xmm0
movapd 0xb0(%rsp), %xmm1
movapd 0xc0(%rsp), %xmm2
movupd %xmm2, 0x20(%rdi,%rsi)
movupd %xmm1, 0x10(%rdi,%rsi)
movups %xmm0, (%rdi,%rsi)
incl %eax
incq %rdx
addq $0x30, %rcx
cmpq %rdx, %r8
jne 0xc44e3
cmpl $0x2, %eax
jae 0xc457c
xorl %eax, %eax
jmp 0xc49c5
movl %eax, %esi
leaq -0x763(%rip), %rcx      # 0xc3e22
movl $0x30, %edx
movq %rdi, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq %rax, %r13
callq 0x23f0
leal (%rbx,%r13,4), %eax
cmpq %rax, 0x8(%r15)
jae 0xc45f9
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 %rax, 0x10(%r15)
jmp 0xc45fd
movq 0x10(%r15), %rax
movq (%r15), %rsi
xorl %ecx, %ecx
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %r8
movl (%rdx), %edi
movq %rdi, (%rax,%rsi,8)
movq (%r15), %rsi
shlq $0x20, (%rax,%rsi,8)
movq (%r15), %rsi
orq %rcx, (%rax,%rsi,8)
movq (%r15), %rsi
incq %rsi
movq %rsi, (%r15)
incq %rcx
addq $0x30, %rdx
cmpq %rcx, %r8
jne 0xc460c
movq %r15, 0x88(%rsp)
movq %r14, 0x70(%rsp)
movq 0x50(%rsp), %rcx
leaq (%rax,%rcx,8), %rbx
leaq (%rbx,%r8,8), %r14
leaq (%r14,%r8,8), %rax
movq %rax, 0x20(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x9dc69
movq %r14, %r10
movq %rbx, %r9
movq 0x10(%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 0xc4671
movq 0x20(%rsp), %rax
leaq (%rax,%r8,8), %rax
movq %rax, 0x30(%rsp)
xorl %r15d, %r15d
movsd 0x16cf4a(%rip), %xmm0   # 0x2315e8
movq $0x0, 0x48(%rsp)
movq 0x18(%rsp), %r11
movq %r12, 0x80(%rsp)
movq %r9, 0x40(%rsp)
movq %r10, 0x38(%rsp)
leaq (%r15,%r15,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 0x58(%rsp), %xmm1
cvttsd2si %xmm1, %rax
movq %rax, 0x98(%rsp)
xorl %r13d, %r13d
xorl %esi, %esi
movq $0x0, 0x60(%rsp)
movq $0x0, 0x28(%rsp)
movl $0x0, 0x8(%rsp)
movl %ecx, 0x4(%rsp)
movl %edx, (%rsp)
movq %rdi, 0x78(%rsp)
movq (%r9,%r13,8), %rax
cmpq (%r10,%r15,8), %r13
jne 0xc4735
cmpq %r15, %rax
je 0xc4807
jmp 0xc49d7
cmpq %r15, %rax
ja 0xc4807
leaq (%rax,%rax,2), %r12
shlq $0x4, %r12
movl (%r11,%r12), %ebp
cmpl %edx, %ebp
jae 0xc49a4
addq %r11, %r12
cmpb $0x0, 0x28(%r12)
js 0xc4807
cmpl %ebp, %ecx
movl %ebp, %r14d
cmoval %ecx, %r14d
movl 0x4(%r12), %eax
cmpl %eax, %edx
movl %eax, %ebx
cmovbl %edx, %ebx
cmpl %r14d, %ebx
jbe 0xc4807
movq %rax, 0x68(%rsp)
movl %esi, 0xc(%rsp)
movq %r12, %rsi
movq 0x70(%rsp), %rdx
callq 0xc3ffd
testl %eax, %eax
je 0xc47bc
movq 0x18(%rsp), %r11
movq 0x10(%rsp), %r8
movq 0x40(%rsp), %r9
movq 0x38(%rsp), %r10
movsd 0x16ce33(%rip), %xmm0   # 0x2315e8
movq 0x78(%rsp), %rdi
jmp 0xc47fc
movq %r12, %rdi
movq 0x78(%rsp), %rsi
movq %rsi, %r12
movq 0x70(%rsp), %rdx
movsd 0x16ce14(%rip), %xmm0   # 0x2315e8
callq 0xc3ffd
testl %eax, %eax
je 0xc4818
movq 0x18(%rsp), %r11
movq 0x10(%rsp), %r8
movq 0x40(%rsp), %r9
movq 0x38(%rsp), %r10
movsd 0x16cdef(%rip), %xmm0   # 0x2315e8
movq %r12, %rdi
movl 0x4(%rsp), %ecx
movl (%rsp), %edx
movl 0xc(%rsp), %esi
incq %r13
cmpq %r13, %r8
jne 0xc471d
jmp 0xc49a4
movq 0x60(%rsp), %rdi
testl %edi, %edi
movq 0x18(%rsp), %r11
movq 0x10(%rsp), %r8
movq 0x40(%rsp), %r9
movq 0x38(%rsp), %r10
movl 0xc(%rsp), %esi
je 0xc484e
leal -0x1(%rdi), %eax
movq 0x20(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
cmpl %ecx, %r14d
jbe 0xc4900
subl %r14d, %esi
addl %ebx, %esi
movl %edi, %eax
shlq $0x20, %r14
movl %ebx, %ecx
orq %r14, %rcx
movq 0x20(%rsp), %rdx
movq %rcx, (%rdx,%rax,8)
incl %edi
movq %rdi, 0x60(%rsp)
movsd 0x16cd72(%rip), %xmm0   # 0x2315e8
movq %r12, %rdi
movq 0x28(%rsp), %rdx
testl %edx, %edx
je 0xc4896
leal -0x1(%rdx), %eax
movq 0x30(%rsp), %rcx
movq (%rcx,%rax,8), %rcx
cmpl %ecx, %ebp
jbe 0xc4926
movl 0x8(%rsp), %eax
subl %ebp, %eax
movq 0x68(%rsp), %rcx
addl %ecx, %eax
movl %eax, 0x8(%rsp)
movl %edx, %eax
shlq $0x20, %rbp
orq %rcx, %rbp
movq 0x30(%rsp), %rcx
movq %rbp, (%rcx,%rax,8)
incl %edx
movq %rdx, 0x28(%rsp)
movl 0x4(%rsp), %ecx
movl (%rsp), %edx
movl %esi, %eax
cmpq 0x90(%rsp), %rax
jb 0xc4807
cmpl 0x98(%rsp), %esi
jae 0xc4946
cvtsi2sd %rax, %xmm2
movl 0x8(%rsp), %eax
cvtsi2sd %rax, %xmm1
mulsd 0x58(%rsp), %xmm1
ucomisd %xmm1, %xmm2
jb 0xc4807
jmp 0xc4946
subl %ecx, %ebx
movsd 0x16ccde(%rip), %xmm0   # 0x2315e8
movq %r12, %rdi
jbe 0xc4879
addl %ebx, %esi
addq %rbx, %rcx
movq 0x20(%rsp), %rdx
movq %rcx, (%rdx,%rax,8)
jmp 0xc4879
movq 0x68(%rsp), %rdx
cmpl %ecx, %edx
jbe 0xc48c0
subl %ecx, %edx
addl %edx, 0x8(%rsp)
addq %rdx, %rcx
movq 0x30(%rsp), %rdx
movq %rcx, (%rdx,%rax,8)
jmp 0xc48c0
orb $-0x80, 0x28(%rdi)
incq 0x48(%rsp)
movq 0x80(%rsp), %r12
movq 0x28(%r12), %rax
movl 0x14(%rdi), %ecx
movq (%rax,%rcx,8), %rax
testl %eax, %eax
je 0xc49a4
movq 0x10(%r12), %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
movl -0x14(%rax,%rdx), %esi
cmpl 0x8(%rdi), %esi
jne 0xc499b
orb $-0x80, (%rax,%rdx)
addq $0x30, %rdx
cmpq %rdx, %rcx
jne 0xc498e
incq %r15
cmpq %r8, %r15
jne 0xc46be
movq 0x88(%rsp), %rax
movq 0x50(%rsp), %rcx
movq %rcx, (%rax)
movq 0x48(%rsp), %rax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x17225f(%rip), %rdi    # 0x236c3d
leaq 0x1714ac(%rip), %rsi    # 0x235e91
leaq 0x17225c(%rip), %rcx    # 0x236c48
movl $0x3bda, %edx           # imm = 0x3BDA
callq 0x2110