// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: purge_ovlp_cov_adv(unsigned int, kv_u_trans_t*, asg64_v*, ug_rid_cov_t*, unsigned long)
void purge_ovlp_cov_adv(uint32_t id, kv_u_trans_t *ta, asg64_v *b64, ug_rid_cov_t *cc, uint64_t hom_cut)
{
    ma_utg_t *u = &(cc->ug->u.a[id]); uint32_t k, n, cn;
    u_trans_t *a = NULL, t; 
    kv_resize(uint64_t, *b64, u->n);
    memcpy(b64->a, cc->cov.a+cc->idx[id], sizeof((*(b64->a)))*cc->ug->u.a[id].n);
    a = u_trans_a(*ta, id); n = u_trans_n(*ta, id); 
    // if(id == 7929)  {
    //     fprintf(stderr, "\n[M::%s]\tutg%.6u%c\tn::%u\n", __func__, id+1, "lc"[cc->ug->u.a[id].circ], n);
    // }
    for (k = cn = 0; k < n; k++) {///RC_0/RC_1 to a[0, r_n)
        // if(id == 7929)  {
        //     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"[cc->ug->u.a[a[k].qn].circ], cc->ug->u.a[a[k].qn].len, a[k].qs, a[k].qe, "+-"[a[k].rev],
        //     a[k].tn+1, "lc"[cc->ug->u.a[a[k].tn].circ], cc->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 == 7929)  {
    //     fprintf(stderr, "[M::%s]\tutg%.6u%c\tn::%u\tcn::%u\n", __func__, 
    //         id+1, "lc"[cc->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_ug_rid_cov_t(id, b64->a, &(a[k]), cc, ((uint64_t)-1), -1);
    }

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %rbp
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r13
movq (%rcx), %rax
movq 0x10(%rax), %rax
movl %edi, %ebx
leaq (%rbx,%rbx,4), %r12
movl 0x10(%rax,%r12,8), %eax
cmpq %rax, 0x8(%rdx)
jae 0xc7080
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)
movq 0x10(%r15), %rdi
movq 0x10(%r14), %rax
movq (%rax,%rbx,8), %rsi
shlq $0x3, %rsi
addq 0x28(%r14), %rsi
movq (%r14), %rax
movq 0x10(%rax), %rax
movl 0x10(%rax,%r12,8), %edx
shlq $0x3, %rdx
callq 0x2160
movq 0x10(%r13), %r12
movq 0x28(%r13), %rax
movq (%rax,%rbx,8), %r8
movq %r8, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rdi
shlq $0x4, %rdi
leaq (%r12,%rdi), %r13
testl %r8d, %r8d
je 0xc7158
movl %r8d, %eax
xorl %ecx, %ecx
movq %r13, %rdx
xorl %ebx, %ebx
testb $0x3e, 0x28(%rdx)
jne 0xc714a
movl %ebx, %esi
cmpq %rsi, %rcx
je 0xc7148
movups (%rdx), %xmm0
movups 0x10(%rdx), %xmm1
movups 0x20(%rdx), %xmm2
movaps %xmm2, 0x40(%rsp)
movaps %xmm1, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
leaq (%rsi,%rsi,2), %rsi
shlq $0x4, %rsi
movups (%r13,%rsi), %xmm0
movups 0x10(%r13,%rsi), %xmm1
movups 0x20(%r13,%rsi), %xmm2
movups %xmm2, 0x20(%rdx)
movups %xmm1, 0x10(%rdx)
movups %xmm0, (%rdx)
movaps 0x20(%rsp), %xmm0
movaps 0x30(%rsp), %xmm1
movaps 0x40(%rsp), %xmm2
movups %xmm2, 0x20(%r13,%rsi)
movups %xmm1, 0x10(%r13,%rsi)
movups %xmm0, (%r13,%rsi)
incl %ebx
incq %rcx
addq $0x30, %rdx
cmpq %rcx, %rax
jne 0xc70db
jmp 0xc715a
xorl %ebx, %ebx
movl %r8d, %eax
subl %ebx, %eax
jbe 0xc7247
movq %r8, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
movq %rbp, 0x18(%rsp)
movl %ebx, %ecx
movq %rcx, (%rsp)
cmpl $0x2, %eax
jb 0xc71a1
movq (%rsp), %rcx
leaq (%rcx,%rcx,2), %rdi
shlq $0x4, %rdi
addq %r13, %rdi
movl %eax, %esi
leaq -0x427a(%rip), %rcx     # 0xc2f1d
movl $0x30, %edx
callq 0x23f0
testl %ebx, %ebx
je 0xc71d0
movq (%rsp), %rbp
movq 0x10(%r15), %rsi
movq %r13, %rdx
movq %r14, %rcx
movq $-0x1, %r8
movsd 0x15e376(%rip), %xmm0   # 0x225538
callq 0xc6725
addq $0x30, %r13
decq %rbp
jne 0xc71a9
movq 0x8(%rsp), %rax
cmpl %ebx, %eax
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %rcx
jbe 0xc7247
movl %eax, %ebx
movq (%rsp), %rdx
leaq (%rdx,%rdx,2), %rax
shlq $0x4, %rax
addq %rax, %rcx
addq %rcx, %r12
subq %rdx, %rbx
movq 0x10(%r15), %rsi
movq %r12, %rdx
movq %r14, %rcx
movq %r13, %r8
movsd 0x15e339(%rip), %xmm0   # 0x225548
callq 0xc6725
testl %eax, %eax
je 0xc7238
movq 0x10(%r15), %rsi
movq %r12, %rdx
movq %r14, %rcx
movq $-0x1, %r8
movsd 0x15e307(%rip), %xmm0   # 0x225538
callq 0xc6725
jmp 0xc723e
orb $-0x80, 0x28(%r12)
addq $0x30, %r12
decq %rbx
jne 0xc71fa
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq