// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: purge_ovlp_cov(unsigned int, kv_u_trans_t*, ma_ug_t*, asg_t*, asg64_v*, unsigned long*, unsigned long)
void purge_ovlp_cov(uint32_t id, kv_u_trans_t *ta, ma_ug_t *ug, asg_t *sg, asg64_v *b64, uint64_t *cc, uint64_t hom_cut)
{
    ma_utg_t *u = &(ug->u.a[id]); uint32_t k, n, cn;
    u_trans_t *a = NULL, t; 
    kv_resize(uint64_t, *b64, u->n);
    for (k = 0; k < u->n; k++) b64->a[k] = cc[u->a[k]>>33];
    a = u_trans_a(*ta, id); n = u_trans_n(*ta, id); 
    // if(id == 315 || id == 1055)  {
    //     fprintf(stderr, "\n[M::%s]\tutg%.6u%c\tn::%u\n", __func__, id+1, "lc"[ug->u.a[id].circ], n);
    // }
    for (k = cn = 0; k < n; k++) {///RC_0/RC_1 to a[0, r_n)
        // if(id == 315 || id == 1055)  {
        //     fprintf(stderr, "-0-[M::%s]\tutg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\tis_reliable::%u\tw::%f\n", __func__, 
        //     a[k].qn+1, "lc"[ug->u.a[a[k].qn].circ], ug->u.a[a[k].qn].len, a[k].qs, a[k].qe, "+-"[a[k].rev],
        //     a[k].tn+1, "lc"[ug->u.a[a[k].tn].circ], ug->u.a[a[k].tn].len, a[k].ts, a[k].te, 
        //     ((a[k].f == RC_0) || (a[k].f == RC_1))?1:0, a[k].nw);
        // }
        if((a[k].f == RC_0) || (a[k].f == RC_1)) {
            if(k != cn) {
                t = a[k]; a[k] = a[cn]; a[cn] = t;
            }
            cn++;
        }
    }
    // if(id == 315 || id == 1055)  {
    //     fprintf(stderr, "[M::%s]\tutg%.6u%c\tn::%u\tcn::%u\n", __func__, 
    //         id+1, "lc"[ug->u.a[id].circ], n, cn);
    // }
    if(cn >= n) return;
    if(n-cn > 1) qsort(a+cn, n-cn, sizeof((*a)), cmp_u_trans_weight);
    for (k = 0; k < cn; k++) {///calculate coverage for 
        append_cov_line(id, b64->a, cc, &(a[k]), ug, sg, ((uint64_t)-1), -1);
    }

    for (k = cn; k < n; k++) {
        if(append_cov_line(id, b64->a, cc, &(a[k]), ug, sg, hom_cut, 0.51)) {
            append_cov_line(id, b64->a, cc, &(a[k]), ug, sg, ((uint64_t)-1), -1);
            continue;
        }
        a[k].del = 1;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, %rbx
movq %r8, %r14
movq %rcx, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %r13
movq 0x10(%rdx), %rax
movl %edi, %r15d
imulq $0x28, %r15, %rcx
leaq (%rax,%rcx), %rbp
movl 0x10(%rax,%rcx), %eax
cmpq %rax, 0x8(%r8)
jae 0x8b5af
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(%r14)
movq 0x10(%r14), %rdi
leaq 0x8(,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movl 0x10(%rbp), %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0x8b5d3
movq 0x18(%rbp), %rdx
movq (%rdx,%rcx,8), %rdx
shrq $0x21, %rdx
movq (%rbx,%rdx,8), %rdx
movq 0x10(%r14), %rsi
movq %rdx, (%rsi,%rcx,8)
incq %rcx
jmp 0x8b5b1
movq 0x10(%r13), %rdx
movq 0x28(%r13), %rax
movq (%rax,%r15,8), %rax
movq %rax, %rcx
shrq $0x20, %rcx
imulq $0x30, %rcx, %r8
movq %rdx, 0x10(%rsp)
leaq (%rdx,%r8), %r13
movl %eax, %ebp
xorl %ecx, %ecx
movq %r13, %rsi
xorl %edx, %edx
movl %edx, %r12d
cmpq %rcx, %rbp
je 0x8b676
testb $0x3e, 0x28(%rsi)
jne 0x8b66d
cmpq %r12, %rcx
je 0x8b66b
movups (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movups 0x20(%rsi), %xmm2
movaps %xmm2, 0x50(%rsp)
movaps %xmm1, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
imulq $0x30, %r12, %rdi
movups (%r13,%rdi), %xmm0
movups 0x10(%r13,%rdi), %xmm1
movups 0x20(%r13,%rdi), %xmm2
movups %xmm2, 0x20(%rsi)
movups %xmm1, 0x10(%rsi)
movups %xmm0, (%rsi)
movaps 0x30(%rsp), %xmm0
movaps 0x40(%rsp), %xmm1
movaps 0x50(%rsp), %xmm2
movups %xmm2, 0x20(%r13,%rdi)
movups %xmm1, 0x10(%r13,%rdi)
movups %xmm0, (%r13,%rdi)
incl %edx
incq %rcx
addq $0x30, %rsi
jmp 0x8b5fc
subl %edx, %eax
jbe 0x8b777
movq %r8, 0x8(%rsp)
cmpl $0x2, %eax
jb 0x8b6a0
imulq $0x30, %r12, %rdi
addq %r13, %rdi
movl %eax, %esi
leaq -0x3de6(%rip), %rcx     # 0x878b2
pushq $0x30
popq %rdx
callq 0x23f0
movq %r12, %r15
subq $0x1, %r15
jb 0x8b6d5
movq 0x10(%r14), %rsi
orq $-0x1, (%rsp)
movq %rbx, %rdx
movq %r13, %rcx
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %r9
movsd 0x157eb6(%rip), %xmm0   # 0x1e3580
callq 0x8b265
addq $0x30, %r13
jmp 0x8b6a3
movq 0x10(%rsp), %rax
addq 0x8(%rsp), %rax
imulq $0x30, %r12, %r13
addq %rax, %r13
movq 0x18(%rsp), %r15
movq %rbx, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq %rbp, 0x28(%rsp)
cmpq %rbp, %r12
jae 0x8b777
movq 0x10(%r14), %rsi
movq 0xa0(%rsp), %rax
movq %rax, (%rsp)
movq %rbx, %rdx
movq %r13, %rcx
movq 0x20(%rsp), %rbp
movq %rbp, %r8
movq %r15, %r9
movsd 0x157e70(%rip), %xmm0   # 0x1e3598
callq 0x8b265
testl %eax, %eax
je 0x8b755
movq 0x10(%r14), %rsi
orq $-0x1, (%rsp)
movq %rbx, %rdx
movq %r13, %rcx
movq %rbp, %r8
movq %r15, %r9
movsd 0x157e32(%rip), %xmm0   # 0x1e3580
callq 0x8b265
jmp 0x8b75a
orb $-0x80, 0x28(%r13)
incq %r12
addq $0x30, %r13
movq 0x28(%rsp), %rbp
movq 0x10(%rsp), %rbx
movq 0x8(%rsp), %r14
movq 0x18(%rsp), %r15
jmp 0x8b6fa
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq