// File: /workspace/github_cpp_makefile_star2_O3/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, 0x18(%rsp)
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), %edx
cmpq %rdx, 0x8(%r15)
jae 0xc7e5a
decq %rdx
movq %rdx, %rax
shrq %rax
orq %rdx, %rax
movq %rax, %rcx
shrq $0x2, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x4, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x8, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x10, %rax
orq %rcx, %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%r15)
movq 0x10(%r15), %rdi
leaq 0x8(,%rax,8), %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, 0x10(%r15)
movq (%r14), %rax
movq 0x10(%rax), %rax
movl 0x10(%rax,%r12,8), %edx
jmp 0xc7e5e
movq 0x10(%r15), %rdi
movq 0x10(%r14), %rax
movq (%rax,%rbx,8), %rsi
shlq $0x3, %rsi
addq 0x28(%r14), %rsi
shlq $0x3, %rdx
callq 0x2160
movq 0x10(%r13), %r12
movq 0x28(%r13), %rax
movq (%rax,%rbx,8), %rbp
movq %rbp, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rdi
shlq $0x4, %rdi
leaq (%r12,%rdi), %r13
testl %ebp, %ebp
je 0xc7f24
movl %ebp, %eax
xorl %ecx, %ecx
movq %r13, %rdx
xorl %ebx, %ebx
testb $0x3e, 0x28(%rdx)
jne 0xc7f16
movl %ebx, %esi
cmpq %rsi, %rcx
je 0xc7f14
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 0xc7ea7
jmp 0xc7f26
xorl %ebx, %ebx
subl %ebx, %ebp
jbe 0xc7ff8
movq %rdi, 0x10(%rsp)
movl %ebx, %eax
movq %rax, 0x8(%rsp)
cmpl $0x2, %ebp
jb 0xc7f62
movq 0x8(%rsp), %rax
leaq (%rax,%rax,2), %rdi
shlq $0x4, %rdi
addq %r13, %rdi
movl %ebp, %esi
leaq -0x4136(%rip), %rcx     # 0xc3e22
movl $0x30, %edx
callq 0x23f0
testl %ebx, %ebx
je 0xc7f92
movq 0x8(%rsp), %rbx
movq 0x10(%r15), %rsi
movq %r13, %rdx
movq %r14, %rcx
movq $-0x1, %r8
movsd 0x1696b4(%rip), %xmm0   # 0x231638
callq 0xc74f4
addq $0x30, %r13
decq %rbx
jne 0xc7f6b
movq 0x8(%rsp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movq 0x10(%rsp), %rcx
addq %rax, %rcx
addq %rcx, %r12
movq 0x10(%r15), %rsi
movq %r12, %rdx
movq %r14, %rcx
movq 0x18(%rsp), %r8
movsd 0x169687(%rip), %xmm0   # 0x231648
callq 0xc74f4
testl %eax, %eax
je 0xc7fea
movq 0x10(%r15), %rsi
movq %r12, %rdx
movq %r14, %rcx
movq $-0x1, %r8
movsd 0x169655(%rip), %xmm0   # 0x231638
callq 0xc74f4
jmp 0xc7ff0
orb $-0x80, 0x28(%r12)
addq $0x30, %r12
decl %ebp
jne 0xc7faa
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq