// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/gfa_ut.cpp
// Function: usg_bub_clean(usg_t*, buf_t*, asg64_v*, asg64_v*, int, float, unsigned int, unsigned char*, unsigned char*)
uint64_t usg_bub_clean(usg_t *g, buf_t *b, asg64_v *in_0, asg64_v *in_1, int32_t max_ext, float len_rat, 
uint32_t is_topo, uint8_t *bs, uint8_t *f)
{
    uint32_t v, m, n_vtx = g->n<<1, n_arc, nv, i, n_cut = 0;
    uint64_t max_dist; usg_arc_t *av = NULL;
    // for (i = 0; i < g->n; ++i) b->a[i].s = 0;
    max_dist = usg_bub_dis(g, b); 
    memset(bs, 0, sizeof((*bs))*n_vtx);

    if(max_dist > 0) {
        for (v = 0; v < n_vtx; ++v) {
            if(bs[v]) continue;
            nv = usg_arc_n(g, v); av = usg_arc_a(g, v);
            if (nv < 2 || g->a[v>>1].del) continue;
            for (i = n_arc = 0; i < nv; ++i) // asg_bub_pop1() may delete some edges/arcs
                if (!av[i].del) ++n_arc;
            if (n_arc < 2) continue;
            if(usg_bub_identify(g, v, max_dist, (uint64_t)-1, b)) {
                //beg is v, end is b.S.a[0]
                //note b.b include end, does not include beg
                for (i = 0; i < b->b.n; i++) {
                    if(b->b.a[i]==v || b->b.a[i]==b->S.a[0]) continue;
                    bs[b->b.a[i]] = bs[b->b.a[i]^1] = 1;
                }
                bs[v] = 2; bs[b->S.a[0]^1] = 3;
            }
        }

        //traverse all node with two directions 
        for (v = 0; v < n_vtx; ++v) {
            if(bs[v] != 2) continue;
            nv = usg_arc_n(g, v); av = usg_arc_a(g, v);
            for (i = n_arc = 0; i < nv; ++i) // asg_bub_pop1() may delete some edges/arcs
                if (!av[i].del) ++n_arc;
            if (n_arc < 2) continue;
            if(usg_bub_identify(g, v, max_dist, (uint64_t)-1, b)) {
                for (i = m = 0; i < b->b.n; i++) {
                    if(b->b.a[i]==v || b->b.a[i]==b->S.a[0]) continue;
                    bs[b->b.a[i]] = bs[b->b.a[i]^1] = 1;
                    b->b.a[m++] = b->b.a[i];
                }
                b->b.n = m; bs[v] = 2; bs[b->S.a[0]^1] = 3;
                n_cut += usg_bub_clean0(g, b->b.a, b->b.n, v, b->S.a[0]^1, is_topo, max_ext, len_rat, f, in_0, in_1);
            }
        }
    }
    if (n_cut > 0) usg_cleanup(g);
    return n_cut;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %r9d, 0x4(%rsp)
movss %xmm0, (%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, %r13
movq %rdi, %rbx
movq 0x60(%rsp), %rbp
movl 0x8(%rdi), %r14d
addl %r14d, %r14d
callq 0x2255d1
movq %rax, %r15
xorl %r12d, %r12d
movq %rbp, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x20e0
movq %r15, 0x8(%rsp)
testq %r15, %r15
je 0x226807
testq %r14, %r14
je 0x226804
xorl %r15d, %r15d
movb $0x1, %r12b
cmpb $0x0, (%rbp,%r15)
je 0x2265c1
incq %r15
cmpq %r14, %r15
jne 0x2265ac
jmp 0x22668d
movl %r15d, %eax
shrl %eax
imulq $0x78, %rax, %rax
addq (%rbx), %rax
movl %r15d, %ecx
andl $0x1, %ecx
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %rdx
testl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
je 0x2265b4
cmpb $0x0, 0x70(%rax)
jne 0x2265b4
movq 0x20(%rax,%rcx,8), %rax
movl %edx, %ecx
addq $0xc, %rax
xorl %edx, %edx
movl (%rax), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
addq $0x20, %rax
decq %rcx
jne 0x2265f7
cmpl $0x2, %edx
jb 0x2265b4
movq %rbx, %rdi
movl %r15d, %esi
movq 0x8(%rsp), %rdx
movq $-0x1, %rcx
movq %r13, %r8
callq 0x22574c
testq %rax, %rax
je 0x2265b4
movq 0x38(%r13), %rcx
testq %rcx, %rcx
je 0x226673
movl $0x1, %eax
xorl %edx, %edx
movq 0x48(%r13), %rsi
movl (%rsi,%rdx,4), %esi
cmpq %rsi, %r15
je 0x22666a
movq 0x18(%r13), %rdi
cmpl (%rdi), %esi
je 0x22666a
xorq $0x1, %rsi
movb %r12b, (%rbp,%rsi)
movq 0x48(%r13), %rcx
movl (%rcx,%rdx,4), %ecx
movb %r12b, (%rbp,%rcx)
movq 0x38(%r13), %rcx
movl %eax, %edx
incl %eax
cmpq %rdx, %rcx
ja 0x22663d
movb $0x2, (%rbp,%r15)
movq 0x18(%r13), %rax
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%rbp,%rax)
jmp 0x2265b4
xorl %r12d, %r12d
testl %r14d, %r14d
je 0x226807
xorl %r15d, %r15d
cmpb $0x2, (%rbp,%r15)
jne 0x2267d1
movl %r15d, %eax
shrl %eax
movl %r15d, %edx
imulq $0x78, %rax, %rcx
addq (%rbx), %rcx
andl $0x1, %edx
leaq (%rdx,%rdx,2), %rdx
movl 0x10(%rcx,%rdx,8), %eax
testq %rax, %rax
je 0x2267d1
movq 0x20(%rcx,%rdx,8), %rcx
shlq $0x5, %rax
xorl %edx, %edx
xorl %esi, %esi
movl 0xc(%rcx,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x20, %rdx
cmpq %rdx, %rax
jne 0x2266d8
cmpl $0x2, %esi
jb 0x2267d1
movq %rbx, %rdi
movl %r15d, %esi
movq 0x8(%rsp), %rdx
movq $-0x1, %rcx
movq %r13, %r8
callq 0x22574c
testq %rax, %rax
je 0x2267d1
movq 0x38(%r13), %rsi
testq %rsi, %rsi
movb $0x1, %r10b
je 0x226777
movq 0x48(%r13), %rdx
xorl %edi, %edi
movl $0x1, %eax
xorl %ecx, %ecx
movl (%rdx,%rdi,4), %r8d
cmpq %r8, %r15
je 0x22676a
movq 0x18(%r13), %r9
cmpl (%r9), %r8d
je 0x22676a
xorq $0x1, %r8
movb %r10b, (%rbp,%r8)
movq 0x48(%r13), %rdx
movl (%rdx,%rdi,4), %edx
movb %r10b, (%rbp,%rdx)
movq 0x48(%r13), %rdx
movl (%rdx,%rdi,4), %esi
movl %ecx, %edi
incl %ecx
movl %esi, (%rdx,%rdi,4)
movq 0x38(%r13), %rsi
movl %eax, %edi
incl %eax
cmpq %rdi, %rsi
ja 0x226731
movl %ecx, %eax
jmp 0x226779
xorl %eax, %eax
movq %rax, 0x38(%r13)
movb $0x2, (%rbp,%r15)
movq 0x18(%r13), %rax
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%rbp,%rax)
movq 0x48(%r13), %rsi
movl 0x38(%r13), %edx
movq 0x18(%r13), %rax
movl (%rax), %r8d
xorl $0x1, %r8d
movq %rbx, %rdi
movl %r15d, %ecx
movl 0x4(%rsp), %r9d
movss (%rsp), %xmm0
pushq 0x18(%rsp)
pushq 0x18(%rsp)
pushq 0x78(%rsp)
pushq 0x38(%rsp)
callq 0x2260ae
addq $0x20, %rsp
addl %eax, %r12d
incq %r15
cmpq %r14, %r15
jne 0x22669c
testl %r12d, %r12d
je 0x226804
leaq 0x6ef6f(%rip), %rax     # 0x295758
movl 0x7c(%rax), %edi
movq 0x8(%rbx), %rcx
leaq -0x13014(%rip), %rsi    # 0x2137e3
movq %rbx, %rdx
callq 0x108f2c
movl %r12d, %r12d
jmp 0x226807
xorl %r12d, %r12d
movq %r12, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq