// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: usg_bub_clean0(usg_t*, unsigned int*, unsigned int, unsigned int, unsigned int, unsigned int, int, float, unsigned char*, asg64_v*, asg64_v*)
uint64_t usg_bub_clean0(usg_t *g, uint32_t *a, uint32_t a_n, uint32_t sid, uint32_t eid, 
uint32_t is_topo, int32_t max_ext, float len_rat, uint8_t *f, asg64_v *in_0, asg64_v *in_1)
{
    uint32_t i, k, v, w, nv, nw, kv, kw, ol_max, ou_max, to_del, cnt = 0, tip_v, mm_ol; 
    asg64_v tx = {0,0,0}, tz = {0,0,0}, *b = NULL, *ub = NULL; int32_t n_tip, r_tip;
    uint64_t kocc[2]; usg_arc_t *av, *aw, *ve, *we;
    b = ((in_0)?(in_0):(&tx)); ub = ((in_1)?(in_1):(&tz));

    for (k = b->n = ub->n = 0; k < a_n; ++k) {
        extracr_clean_arc(g, a[k], b, ub); 
        extracr_clean_arc(g, a[k]^1, b, ub);
    }
    extracr_clean_arc(g, sid, b, ub); 
    extracr_clean_arc(g, eid, b, ub);
    

    radix_sort_srt64(b->a, b->a + b->n);
    for (k = 0; k < b->n; k++) {
        v = ub->a[(uint32_t)b->a[k]]>>32; 
        ve = &(usg_arc_a(g, v)[(uint32_t)(ub->a[(uint32_t)b->a[k]])]);
        w = ve->v^1;
        if(ve->del || g->a[v>>1].del || g->a[w>>1].del || ve->ou) continue;
        nv = usg_arc_n(g, v); nw = usg_arc_n(g, w);
        av = usg_arc_a(g, v); aw = usg_arc_a(g, w);
        if(nv<=1 && nw <= 1) continue;    

        // if(is_trio) {
        //     if(get_arcs(g, v, NULL, 0)<=1 && get_arcs(g, w, NULL, 0)<=1) continue;///speedup
        //     trioF = get_tip_trio_infor(g, v^1);
        //     ntrioF = (trioF==FATHER? MOTHER : (trioF==MOTHER? FATHER : (uint32_t)-1));
        // }    
        for (i = 0; i < nw; ++i) {
            if (aw[i].v == (v^1)) {
                we = &(aw[i]);
                break;
            }
        }
        mm_ol = MIN(ve->ol, we->ol); kocc[0] = kocc[1] = 0;

        for (i = kv = ol_max = ou_max = 0; i < nv; ++i) {
            if(av[i].del) continue;
            kv++; 
            if(av[i].ou != 1) kocc[!!(av[i].ou)]++; ///if av[i].ou == 1, ignore it
            // if(is_trio && get_tip_trio_infor(g, av[i].v) == ntrioF) continue;
            if(ol_max < av[i].ol) ol_max = av[i].ol;
        }
        if (kv < 1 || kocc[0] < 1 || kocc[1] < 1) continue;
        if (kv >= 2) {
            if (mm_ol > ol_max*len_rat) continue;
        }

        
        for (i = kw = ol_max = ou_max = 0; i < nw; ++i) {
            if(aw[i].del) continue;
            kw++;
            // if(is_trio && get_tip_trio_infor(g, aw[i].v) == ntrioF) continue;
            if(ol_max < aw[i].ol) ol_max = aw[i].ol;
        }
        if (kw < 1) continue;
        if (kw >= 2) {
            if (mm_ol > ol_max*len_rat) continue;
        }

        if (kv <= 1 && kw <= 1) continue;

        to_del = 1; n_tip = 0; tip_v = (uint32_t)-1;
        if(is_topo) {
            to_del = 0;
            if (kv > 1 && kw > 1) {
                to_del = 1;
            } else if (kw == 1) {
                n_tip = usg_naive_topocut_aux(g, w^1, max_ext, f, b, ub);
                if (n_tip < max_ext) to_del = 1; tip_v = w^1;
            } else if (kv == 1) {
                n_tip = usg_naive_topocut_aux(g, v^1, max_ext, f, b, ub);
                if (n_tip < max_ext) to_del = 1; tip_v = v^1;
            }
        } 

        if (to_del) {
            // ve->del = we->del = 1; 
            if(n_tip > 0 && tip_v != ((uint32_t)-1)) {
                r_tip = usg_tip_del(g, tip_v, b, ub);
                assert(n_tip == r_tip);
            }
            cnt++;
        }
    }

    if(in_0) free(tx.a); if(in_1) free(tz.a);
    return cnt;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movss %xmm0, 0x20(%rsp)
movl %r9d, 0x34(%rsp)
movl %r8d, 0x8(%rsp)
movl %ecx, 0x10(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
movq 0xd8(%rsp), %r12
movq 0xd0(%rsp), %r15
xorps %xmm0, %xmm0
leaq 0x70(%rsp), %rcx
movaps %xmm0, (%rcx)
xorl %eax, %eax
movq %rax, 0x10(%rcx)
leaq 0x50(%rsp), %rsi
movq %rax, 0x10(%rsi)
testq %r15, %r15
cmoveq %rcx, %r15
testq %r12, %r12
cmoveq %rsi, %r12
movaps %xmm0, (%rsi)
movq %rax, (%r12)
movq %rax, (%r15)
testl %edx, %edx
je 0x21ac73
movl %edx, %r13d
xorl %ebp, %ebp
movl (%rbx,%rbp,4), %esi
movq %r14, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x21a729
movl (%rbx,%rbp,4), %esi
xorl $0x1, %esi
movq %r14, %rdi
movq %r15, %rdx
movq %r12, %rcx
callq 0x21a729
incq %rbp
cmpq %rbp, %r13
jne 0x21ac46
movq %r14, %rdi
movl 0x10(%rsp), %esi
movq %r15, %rdx
movq %r12, %rcx
callq 0x21a729
movq %r14, %rdi
movl 0x8(%rsp), %esi
movq %r15, %rdx
movq %r12, %rcx
callq 0x21a729
movq 0xd0(%rsp), %rax
testq %rax, %rax
leaq 0x70(%rsp), %rcx
cmovneq %rax, %rcx
movq %rcx, 0x10(%rsp)
movq 0x10(%rcx), %rdi
movq (%r15), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1e3b6a
xorl %r13d, %r13d
cmpq $0x0, (%r15)
je 0x21afd9
movq 0xd8(%rsp), %rbp
testq %rbp, %rbp
leaq 0x50(%rsp), %rbx
cmovneq %rbp, %rbx
xorps %xmm2, %xmm2
movl $0x0, 0x24(%rsp)
movq 0x10(%rsp), %r11
movq %rbx, 0x8(%rsp)
movq 0x10(%rbx), %rax
movq 0x10(%r11), %rcx
movl (%rcx,%r13,8), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, %rdx
movl %eax, %esi
shrq $0x20, %rax
movq (%r14), %rcx
shrq $0x21, %rdx
imulq $0x78, %rdx, %r8
addq %rcx, %r8
movl %eax, %edx
andl $0x1, %edx
leaq (%rdx,%rdx,2), %r9
movq 0x20(%r8,%r9,8), %rdi
shlq $0x5, %rsi
movl 0xc(%rdi,%rsi), %edx
testl %edx, %edx
js 0x21ad91
cmpb $0x0, 0x70(%r8)
jne 0x21ad91
movl 0x8(%rdi,%rsi), %ebx
movl %ebx, %r10d
shrl %r10d
imulq $0x78, %r10, %r10
cmpb $0x0, 0x70(%rcx,%r10)
jne 0x21ad8c
addq %rdi, %rsi
cmpl $0x0, 0x10(%rsi)
jne 0x21ad8c
leaq (%r8,%r9,8), %rsi
addq $0x10, %rsi
addq %r10, %rcx
movq (%rsi), %r9
movl %ebx, %esi
notl %esi
andl $0x1, %esi
leaq (%rsi,%rsi,2), %rsi
movq 0x10(%rcx,%rsi,8), %r8
movq 0x20(%rcx,%rsi,8), %rcx
cmpl $0x1, %r9d
ja 0x21ada2
cmpl $0x2, %r8d
jae 0x21ada2
movq 0x8(%rsp), %rbx
incl %r13d
cmpq %r13, (%r15)
ja 0x21acf6
jmp 0x21afd2
movq %rcx, %rsi
movl %r8d, %r10d
movq %r8, 0x28(%rsp)
testl %r8d, %r8d
je 0x21adc8
movl 0x8(%rsi), %r11d
xorl %eax, %r11d
cmpl $0x1, %r11d
je 0x21adcd
addq $0x20, %rsi
decl %r10d
jne 0x21adb2
movq 0x38(%rsp), %rsi
movq %rsi, 0x38(%rsp)
movl 0xc(%rsi), %r8d
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %r8d
cmpl %r8d, %edx
cmovbl %edx, %r8d
movl %r8d, 0x1c(%rsp)
movaps %xmm2, 0x40(%rsp)
testl %r9d, %r9d
je 0x21ae45
movl %r9d, %r9d
shlq $0x5, %r9
xorl %r10d, %r10d
xorl %edx, %edx
xorl %r11d, %r11d
movl 0xc(%rdi,%r10), %ebp
testl %ebp, %ebp
js 0x21ae2d
movl 0x10(%rdi,%r10), %esi
cmpl $0x1, %esi
je 0x21ae24
xorl %r8d, %r8d
testl %esi, %esi
setne %r8b
incq 0x40(%rsp,%r8,8)
incl %edx
cmpl %ebp, %r11d
cmovbel %ebp, %r11d
addq $0x20, %r10
cmpq %r10, %r9
jne 0x21ae03
cvtsi2ss %r11d, %xmm0
movq 0xd8(%rsp), %rbp
jmp 0x21ae4a
xorl %edx, %edx
xorps %xmm0, %xmm0
testl %edx, %edx
movq 0x10(%rsp), %r11
movq 0x28(%rsp), %rsi
je 0x21ad8c
cmpq $0x0, 0x40(%rsp)
je 0x21ad8c
cmpq $0x0, 0x48(%rsp)
je 0x21ad8c
cmpl $0x2, %edx
jb 0x21ae8e
cvtsi2ssl 0x1c(%rsp), %xmm1
mulss 0x20(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x21ad8c
testl %esi, %esi
je 0x21aee2
movl %esi, %r8d
shlq $0x5, %r8
xorl %r9d, %r9d
xorl %edi, %edi
xorl %r10d, %r10d
movl %r10d, %esi
movl 0xc(%rcx,%r9), %r11d
cmpl %r11d, %r10d
movl %r11d, %r10d
cmoval %esi, %r10d
xorl %ebp, %ebp
testl %r11d, %r11d
cmovsl %esi, %r10d
setns %bpl
addl %ebp, %edi
addq $0x20, %r9
cmpq %r9, %r8
jne 0x21aea1
xorps %xmm0, %xmm0
cvtsi2ss %r10d, %xmm0
movq 0xd8(%rsp), %rbp
movq 0x10(%rsp), %r11
jmp 0x21aee7
xorl %edi, %edi
xorps %xmm0, %xmm0
testl %edi, %edi
je 0x21ad8c
cmpl $0x1, %edi
je 0x21af0c
xorps %xmm1, %xmm1
cvtsi2ssl 0x1c(%rsp), %xmm1
mulss 0x20(%rsp), %xmm0
ucomiss %xmm0, %xmm1
ja 0x21ad8c
cmpl $0x1, %edx
ja 0x21af1a
cmpl $0x1, %edi
je 0x21ad8c
cmpl $0x2, %edx
setae %r8b
cmpl $0x1, %edi
setne %r9b
xorl %r10d, %r10d
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movb $0x1, %cl
cmpl $0x0, 0x34(%rsp)
je 0x21af8b
andb %r9b, %r8b
jne 0x21af8b
cmpl $0x1, %edi
je 0x21af55
xorl %ecx, %ecx
movl $0x0, %r10d
cmpl $0x1, %edx
jne 0x21af8b
xorl $0x1, %eax
movl %eax, %ebx
movq %r14, %rdi
movl %ebx, %esi
movl 0xc0(%rsp), %edx
movq 0xc8(%rsp), %rcx
movq %r15, %r8
movq %r12, %r9
callq 0x209405
xorps %xmm2, %xmm2
movq 0x10(%rsp), %r11
movl %eax, %r10d
cmpl 0xc0(%rsp), %eax
setl %cl
movl %ebx, %esi
testb %cl, %cl
je 0x21ad8c
testl %r10d, %r10d
setle %al
cmpl $-0x1, %esi
sete %cl
orb %al, %cl
movq 0x8(%rsp), %rbx
jne 0x21afc9
movq %r14, %rdi
movq %r15, %rdx
movq %r12, %rcx
movl %r10d, 0x28(%rsp)
callq 0x21a887
xorps %xmm2, %xmm2
movq 0x10(%rsp), %r11
cmpl %eax, 0x28(%rsp)
jne 0x21b01d
incl 0x24(%rsp)
jmp 0x21ad91
movl 0x24(%rsp), %r13d
jmp 0x21afe1
movq 0xd8(%rsp), %rbp
cmpq $0x0, 0xd0(%rsp)
je 0x21aff9
movq 0x80(%rsp), %rdi
callq 0x2220
testq %rbp, %rbp
je 0x21b008
movq 0x60(%rsp), %rdi
callq 0x2220
movq %r13, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x22bde(%rip), %rdi     # 0x23dc02
leaq 0x1e2fa(%rip), %rsi     # 0x239325
leaq 0x22bdf(%rip), %rcx     # 0x23dc11
movl $0x3e23, %edx           # imm = 0x3E23
callq 0x2110