// File: /workspace/github_cpp_makefile_star2_O2/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 $0x98, %rsp
movss %xmm0, 0x18(%rsp)
movl %r9d, 0x14(%rsp)
movl %r8d, 0x30(%rsp)
movl %ecx, 0x28(%rsp)
movq %rsi, %rbx
movq 0xe8(%rsp), %r15
movq 0xe0(%rsp), %r14
xorps %xmm0, %xmm0
leaq 0x80(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x60(%rsp), %rcx
andq $0x0, 0x10(%rcx)
testq %r14, %r14
cmoveq %rax, %r14
testq %r15, %r15
cmoveq %rcx, %r15
movaps %xmm0, (%rcx)
andq $0x0, (%r15)
movq %rdi, %r12
andq $0x0, (%r14)
movl %edx, %ebp
xorl %r13d, %r13d
cmpq %r13, %rbp
je 0x1d6e18
movl (%rbx,%r13,4), %esi
movq %r12, %rdi
movq %r14, %rdx
movq %r15, %rcx
callq 0x1d68ea
movl (%rbx,%r13,4), %esi
xorl $0x1, %esi
movq %r12, %rdi
movq %r14, %rdx
movq %r15, %rcx
callq 0x1d68ea
incq %r13
jmp 0x1d6de7
movq %r12, %rdi
movl 0x28(%rsp), %esi
movq %r14, %rdx
movq %r15, %rcx
callq 0x1d68ea
movq %r12, %rdi
movl 0x30(%rsp), %esi
movq %r14, %rdx
movq %r15, %rcx
callq 0x1d68ea
movq 0xe0(%rsp), %rax
testq %rax, %rax
leaq 0x80(%rsp), %rcx
cmovneq %rax, %rcx
movq %rcx, 0x30(%rsp)
movq 0x10(%rcx), %rdi
movq (%r14), %rax
leaq (%rdi,%rax,8), %rsi
callq 0x1a3def
movq 0xe8(%rsp), %rax
testq %rax, %rax
leaq 0x60(%rsp), %r11
cmovneq %rax, %r11
movl $0x0, 0x1c(%rsp)
xorps %xmm2, %xmm2
xorl %ebx, %ebx
movl %ebx, %ebx
cmpq %rbx, (%r14)
jbe 0x1d715e
movq 0x10(%r11), %rax
movq 0x30(%rsp), %rcx
movq 0x10(%rcx), %rcx
movl (%rcx,%rbx,8), %ecx
movq (%rax,%rcx,8), %r13
movq %r13, %rdx
movl %r13d, %ecx
shrq $0x20, %r13
movq (%r12), %rax
shrq $0x21, %rdx
imulq $0x78, %rdx, %r8
addq %rax, %r8
movl %r13d, %edx
andl $0x1, %edx
imulq $0x18, %rdx, %r9
movq 0x20(%r9,%r8), %rdx
shlq $0x5, %rcx
movl 0xc(%rdx,%rcx), %esi
testl %esi, %esi
js 0x1d6f32
cmpb $0x0, 0x70(%r8)
jne 0x1d6f32
movl 0x8(%rdx,%rcx), %ebp
movl %ebp, %r10d
shrl %r10d
imulq $0x78, %r10, %r10
cmpb $0x0, 0x70(%rax,%r10)
jne 0x1d6f32
addq %rdx, %rcx
cmpl $0x0, 0x10(%rcx)
jne 0x1d6f32
leaq (%r9,%r8), %rcx
addq $0x10, %rcx
addq %r10, %rax
movq (%rcx), %rcx
movl %ebp, %r8d
notl %r8d
andl $0x1, %r8d
imulq $0x18, %r8, %r9
movq 0x10(%rax,%r9), %r10
movl %ecx, %r8d
orl %r10d, %r8d
testl $0xfffffffe, %r8d       # imm = 0xFFFFFFFE
jne 0x1d6f3f
movq %rdi, %rbp
incl %ebx
movq %rbp, %rdi
jmp 0x1d6e89
movq %rbp, 0x20(%rsp)
movl %ecx, %r8d
movq 0x20(%rax,%r9), %rcx
movl %r10d, %eax
movq %rax, 0x28(%rsp)
leaq 0x1(%rax), %r9
leaq -0x20(%rcx), %rbp
decq %r9
je 0x1d6f72
movl 0x28(%rbp), %eax
xorl %r13d, %eax
addq $0x20, %rbp
cmpl $0x1, %eax
jne 0x1d6f5c
jmp 0x1d6f75
movq %rdi, %rbp
movq %rcx, 0x58(%rsp)
movq %r11, 0x8(%rsp)
movl 0xc(%rbp), %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %ecx
movl %ecx, 0x3c(%rsp)
movaps %xmm2, 0x40(%rsp)
shlq $0x5, %r8
xorl %r11d, %r11d
xorl %r10d, %r10d
xorl %edi, %edi
cmpq %r11, %r8
je 0x1d6fd2
movl 0xc(%rdx,%r11), %r9d
testl %r9d, %r9d
js 0x1d6fcc
movl 0x10(%rdx,%r11), %eax
cmpl $0x1, %eax
je 0x1d6fc3
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
incq 0x40(%rsp,%rcx,8)
incl %edi
cmpl %r9d, %r10d
cmovbel %r9d, %r10d
addq $0x20, %r11
jmp 0x1d6f9e
movl 0x3c(%rsp), %eax
cmpl %eax, %esi
cmovbl %esi, %eax
testl %edi, %edi
je 0x1d70a8
cmpq $0x0, 0x40(%rsp)
movq 0x8(%rsp), %r11
je 0x1d6f35
cmpq $0x0, 0x48(%rsp)
je 0x1d6f35
cvtsi2ss %eax, %xmm0
cmpl $0x2, %edi
jb 0x1d701d
cvtsi2ss %r10d, %xmm1
mulss 0x18(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1d6f35
shlq $0x5, 0x28(%rsp)
xorl %r8d, %r8d
xorl %edx, %edx
xorl %esi, %esi
movq 0x58(%rsp), %r10
cmpq %r8, 0x28(%rsp)
je 0x1d7057
movl 0xc(%r10,%r8), %eax
cmpl %eax, %edx
movl %eax, %ecx
cmoval %edx, %ecx
xorl %r9d, %r9d
testl %eax, %eax
setns %r9b
cmovnsl %ecx, %edx
addl %r9d, %esi
addq $0x20, %r8
jmp 0x1d702f
testl %esi, %esi
je 0x1d6f35
cmpl $0x1, %esi
jne 0x1d70b2
cmpl $0x2, %edi
jb 0x1d6f35
cmpl $0x0, 0x14(%rsp)
je 0x1d7148
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movl 0xd0(%rsp), %r13d
movl %r13d, %edx
movq 0xd8(%rsp), %rcx
movq %r14, %r8
movq %r15, %r9
callq 0x1c6b51
cmpl %r13d, %eax
jl 0x1d7112
jmp 0x1d7151
movq 0x8(%rsp), %r11
jmp 0x1d6f35
cvtsi2ss %edx, %xmm1
mulss 0x18(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x1d6f35
cmpl $0x2, %edi
setae %al
cmpl $0x0, 0x14(%rsp)
sete %cl
orb %al, %cl
jne 0x1d7148
cmpl $0x1, %edi
jne 0x1d6f35
xorl $0x1, %r13d
movq %r12, %rdi
movl %r13d, %esi
movl 0xd0(%rsp), %edx
movq 0xd8(%rsp), %rcx
movq %r14, %r8
movq %r15, %r9
callq 0x1c6b51
cmpl 0xd0(%rsp), %eax
jge 0x1d7151
movq %r13, 0x20(%rsp)
movq 0x8(%rsp), %r11
xorps %xmm2, %xmm2
testl %eax, %eax
jle 0x1d7148
cmpl $-0x1, 0x20(%rsp)
je 0x1d7148
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movq %r14, %rdx
movq %r15, %rcx
movl %eax, %r13d
callq 0x1d6a52
xorps %xmm2, %xmm2
movq 0x8(%rsp), %r11
cmpl %eax, %r13d
jne 0x1d71a1
incl 0x1c(%rsp)
jmp 0x1d6f35
movq 0x8(%rsp), %r11
xorps %xmm2, %xmm2
jmp 0x1d6f35
cmpq $0x0, 0xe0(%rsp)
je 0x1d7176
movq 0x90(%rsp), %rdi
callq 0x2220
cmpq $0x0, 0xe8(%rsp)
je 0x1d718b
movq 0x70(%rsp), %rdi
callq 0x2220
movl 0x1c(%rsp), %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x24cfd(%rip), %rdi     # 0x1fbea5
leaq 0x2032a(%rip), %rsi     # 0x1f74d9
leaq 0x24cfe(%rip), %rcx     # 0x1fbeb4
movl $0x3e23, %edx           # imm = 0x3E23
callq 0x2110