// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movl %r9d, 0x14(%rsp)
movss %xmm0, 0x10(%rsp)
movq %r8, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movq %rsi, %r13
movq %rdi, %rbx
movq 0x70(%rsp), %rbp
movq 0x8(%rdi), %r15
addl %r15d, %r15d
callq 0x21a08d
movq %rax, %r14
movl $0x0, 0xc(%rsp)
movq %rbp, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x20e0
movq %r14, 0x18(%rsp)
testq %r14, %r14
sete %al
testl %r15d, %r15d
sete %cl
orb %al, %cl
jne 0x21b2ee
movl %r15d, %r14d
xorl %r12d, %r12d
cmpb $0x0, (%rbp,%r12)
je 0x21b0c7
incq %r12
cmpq %r14, %r12
jne 0x21b0b2
jmp 0x21b191
movl %r12d, %eax
shrl %eax
imulq $0x78, %rax, %rax
addq (%rbx), %rax
movl %r12d, %ecx
andl $0x1, %ecx
leaq (%rcx,%rcx,2), %rcx
movq 0x10(%rax,%rcx,8), %rdx
testl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
je 0x21b0ba
cmpb $0x0, 0x70(%rax)
jne 0x21b0ba
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 0x21b0fd
cmpl $0x2, %edx
jb 0x21b0ba
movq %rbx, %rdi
movl %r12d, %esi
movq 0x18(%rsp), %rdx
movq $-0x1, %rcx
movq %r13, %r8
callq 0x21a209
movb $0x1, %dil
testq %rax, %rax
je 0x21b0ba
cmpq $0x0, 0x38(%r13)
je 0x21b177
movl $0x1, %eax
xorl %ecx, %ecx
movq 0x48(%r13), %rdx
movl (%rdx,%rcx,4), %edx
cmpq %rdx, %r12
je 0x21b16d
movq 0x18(%r13), %rsi
cmpl (%rsi), %edx
je 0x21b16d
xorq $0x1, %rdx
movb %dil, (%rbp,%rdx)
movq 0x48(%r13), %rdx
movl (%rdx,%rcx,4), %ecx
movb %dil, (%rbp,%rcx)
movl %eax, %ecx
incl %eax
cmpq %rcx, 0x38(%r13)
ja 0x21b144
movb $0x2, (%rbp,%r12)
movq 0x18(%r13), %rax
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%rbp,%rax)
jmp 0x21b0ba
testl %r15d, %r15d
je 0x21b2e6
xorl %r15d, %r15d
movb $0x1, %r12b
movl $0x0, 0xc(%rsp)
cmpb $0x2, (%rbp,%r15)
jne 0x21b2d8
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 0x21b2d8
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 0x21b1e4
cmpl $0x2, %esi
jb 0x21b2d8
movq %rbx, %rdi
movl %r15d, %esi
movq 0x18(%rsp), %rdx
movq $-0x1, %rcx
movq %r13, %r8
callq 0x21a209
testq %rax, %rax
je 0x21b2d8
cmpq $0x0, 0x38(%r13)
je 0x21b279
xorl %edx, %edx
movl $0x1, %eax
xorl %ecx, %ecx
movq 0x48(%r13), %rsi
movl (%rsi,%rdx,4), %esi
cmpq %rsi, %r15
je 0x21b26b
movq 0x18(%r13), %rdi
cmpl (%rdi), %esi
je 0x21b26b
xorq $0x1, %rsi
movb %r12b, (%rbp,%rsi)
movq 0x48(%r13), %rsi
movl (%rsi,%rdx,4), %esi
movb %r12b, (%rbp,%rsi)
movq 0x48(%r13), %rsi
movl (%rsi,%rdx,4), %edx
movl %ecx, %edi
incl %ecx
movl %edx, (%rsi,%rdi,4)
movl %eax, %edx
incl %eax
cmpq %rdx, 0x38(%r13)
ja 0x21b234
movl %ecx, %eax
jmp 0x21b27b
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 0x14(%rsp), %r9d
movss 0x10(%rsp), %xmm0
pushq 0x28(%rsp)
pushq 0x28(%rsp)
pushq 0x88(%rsp)
pushq 0x48(%rsp)
callq 0x21abd0
addq $0x20, %rsp
addl %eax, 0xc(%rsp)
incq %r15
cmpq %r14, %r15
jne 0x21b1a8
jmp 0x21b2ee
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %ebp
testl %ebp, %ebp
je 0x21b313
leaq 0x6f45b(%rip), %rax     # 0x28a758
movl 0x7c(%rax), %edi
movq 0x8(%rbx), %rcx
leaq -0x13023(%rip), %rsi    # 0x2082e8
movq %rbx, %rdx
callq 0x1047b4
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq