// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl -0x4(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x30(%rbp), %rcx
movl 0x10(%rcx), %ecx
cmpq %rcx, %rax
jae 0x109c03
movq -0x30(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
shrq %rcx
movq -0x18(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x2, %rcx
movq -0x18(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x4, %rcx
movq -0x18(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x8, %rcx
movq -0x18(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
shrq $0x10, %rcx
movq -0x18(%rbp), %rax
orq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
jmp 0x109c05
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x4(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shlq $0x3, %rax
addq %rax, %rsi
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movl -0x4(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, %edx
shlq $0x3, %rdx
callq 0x2180
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x4(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x4(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0x109d55
movq -0x48(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x109cd6
movq -0x48(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movb 0x28(%rax), %al
andb $0x3f, %al
movzbl %al, %eax
cmpl $0x1, %eax
jne 0x109d45
movl -0x34(%rbp), %eax
cmpl -0x3c(%rbp), %eax
je 0x109d3c
movq -0x48(%rbp), %rsi
movl -0x34(%rbp), %eax
imulq $0x30, %rax, %rax
addq %rax, %rsi
leaq -0x78(%rbp), %rdi
movl $0x30, %edx
callq 0x2180
movq -0x48(%rbp), %rsi
movl -0x3c(%rbp), %eax
imulq $0x30, %rax, %rax
addq %rax, %rsi
movq -0x48(%rbp), %rdi
movl -0x34(%rbp), %eax
imulq $0x30, %rax, %rax
addq %rax, %rdi
movl $0x30, %edx
callq 0x2180
movq -0x48(%rbp), %rdi
movl -0x3c(%rbp), %eax
imulq $0x30, %rax, %rax
addq %rax, %rdi
leaq -0x78(%rbp), %rsi
movl $0x30, %edx
callq 0x2180
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x109d47
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x109c94
movl -0x3c(%rbp), %eax
cmpl -0x38(%rbp), %eax
jb 0x109d62
jmp 0x109e87
movl -0x38(%rbp), %eax
subl -0x3c(%rbp), %eax
cmpl $0x1, %eax
jbe 0x109d96
movq -0x48(%rbp), %rdi
movl -0x3c(%rbp), %eax
imulq $0x30, %rax, %rax
addq %rax, %rdi
movl -0x38(%rbp), %eax
subl -0x3c(%rbp), %eax
movl %eax, %eax
movl %eax, %esi
movl $0x30, %edx
leaq -0x7ba1(%rip), %rcx     # 0x1021f0
callq 0x23e0
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0x109de3
movl -0x4(%rbp), %eax
movl %eax, %edi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x48(%rbp), %rdx
movl -0x34(%rbp), %eax
imulq $0x30, %rax, %rax
addq %rax, %rdx
movq -0x20(%rbp), %rcx
movq $-0x1, %r8
movsd 0x2784a5(%rip), %xmm0   # 0x382278
callq 0x108a40
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x109d9d
movl -0x3c(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jae 0x109e87
movl -0x4(%rbp), %eax
movl %eax, %edi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x48(%rbp), %rdx
movl -0x34(%rbp), %eax
imulq $0x30, %rax, %rax
addq %rax, %rdx
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %r8
movsd 0x278498(%rip), %xmm0   # 0x3822b8
callq 0x108a40
cmpl $0x0, %eax
je 0x109e5f
movl -0x4(%rbp), %eax
movl %eax, %edi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x48(%rbp), %rdx
movl -0x34(%rbp), %eax
imulq $0x30, %rax, %rax
addq %rax, %rdx
movq -0x20(%rbp), %rcx
movq $-0x1, %r8
movsd 0x278420(%rip), %xmm0   # 0x382278
callq 0x108a40
jmp 0x109e79
movq -0x48(%rbp), %rax
movl -0x34(%rbp), %ecx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movb 0x28(%rax), %cl
andb $0x7f, %cl
orb $-0x80, %cl
movb %cl, 0x28(%rax)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x109de9
addq $0x80, %rsp
popq %rbp
retq