// File: /workspace/github_cpp_makefile_star2_O2/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, %rbp
movq %rdi, %r14
movq 0x70(%rsp), %rbx
movl 0x8(%rdi), %r15d
addl %r15d, %r15d
callq 0x1d63e2
movq %rax, %r12
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x20e0
movq %r12, 0x18(%rsp)
testq %r12, %r12
je 0x1d743c
xorl %r13d, %r13d
movb $0x1, %r12b
cmpq %r15, %r13
je 0x1d72f6
cmpb $0x0, (%rbx,%r13)
je 0x1d723b
incq %r13
jmp 0x1d7226
movl %r13d, %eax
shrl %eax
imulq $0x78, %rax, %rax
addq (%r14), %rax
movl %r13d, %ecx
andl $0x1, %ecx
imulq $0x18, %rcx, %rcx
movq 0x10(%rcx,%rax), %rdx
testl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
je 0x1d7236
cmpb $0x0, 0x70(%rax)
jne 0x1d7236
movq 0x20(%rcx,%rax), %rax
movl %edx, %ecx
shlq $0x5, %rcx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rdx, %rcx
je 0x1d7289
movl 0xc(%rax,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x20, %rdx
jmp 0x1d7273
cmpl $0x2, %esi
jb 0x1d7236
movq %r14, %rdi
movl %r13d, %esi
movq 0x18(%rsp), %rdx
pushq $-0x1
popq %rcx
movq %rbp, %r8
callq 0x1d652a
testq %rax, %rax
je 0x1d7236
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, 0x38(%rbp)
jbe 0x1d72de
movq 0x48(%rbp), %rcx
movl (%rcx,%rax,4), %ecx
cmpq %rcx, %r13
je 0x1d72da
movq 0x18(%rbp), %rdx
cmpl (%rdx), %ecx
je 0x1d72da
xorq $0x1, %rcx
movb %r12b, (%rbx,%rcx)
movq 0x48(%rbp), %rcx
movl (%rcx,%rax,4), %ecx
movb %r12b, (%rbx,%rcx)
incl %eax
jmp 0x1d72ab
movb $0x2, (%rbx,%r13)
movq 0x18(%rbp), %rax
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%rbx,%rax)
jmp 0x1d7236
xorl %r13d, %r13d
movb $0x1, %r12b
movl $0x0, 0xc(%rsp)
cmpq %r15, %r13
je 0x1d7424
cmpb $0x2, (%rbx,%r13)
jne 0x1d741c
movl %r13d, %eax
shrl %eax
movl %r13d, %ecx
andl $0x1, %ecx
imulq $0x78, %rax, %rdx
addq (%r14), %rdx
imulq $0x18, %rcx, %rcx
movl 0x10(%rcx,%rdx), %eax
movq 0x20(%rcx,%rdx), %rcx
shlq $0x5, %rax
xorl %edx, %edx
xorl %esi, %esi
cmpq %rdx, %rax
je 0x1d7355
movl 0xc(%rcx,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x20, %rdx
jmp 0x1d733f
cmpl $0x2, %esi
jb 0x1d741c
movq %r14, %rdi
movl %r13d, %esi
movq 0x18(%rsp), %rdx
pushq $-0x1
popq %rcx
movq %rbp, %r8
callq 0x1d652a
testq %rax, %rax
je 0x1d741c
xorl %eax, %eax
xorl %ecx, %ecx
movl %ecx, %ecx
movl %eax, %edx
cmpq %rcx, 0x38(%rbp)
jbe 0x1d73c2
movq 0x48(%rbp), %rsi
movl (%rsi,%rcx,4), %esi
cmpq %rsi, %r13
je 0x1d73be
movq 0x18(%rbp), %rdi
cmpl (%rdi), %esi
je 0x1d73be
xorq $0x1, %rsi
movb %r12b, (%rbx,%rsi)
movq 0x48(%rbp), %rsi
movl (%rsi,%rcx,4), %esi
movb %r12b, (%rbx,%rsi)
movq 0x48(%rbp), %rsi
movl (%rsi,%rcx,4), %edi
incl %eax
movl %edi, (%rsi,%rdx,4)
incl %ecx
jmp 0x1d7381
movq %rdx, 0x38(%rbp)
movb $0x2, (%rbx,%r13)
movq 0x18(%rbp), %rax
movl (%rax), %eax
xorq $0x1, %rax
movb $0x3, (%rbx,%rax)
movq 0x48(%rbp), %rsi
movl 0x38(%rbp), %edx
movq 0x18(%rbp), %rax
movl (%rax), %r8d
xorl $0x1, %r8d
movq %r14, %rdi
movl %r13d, %ecx
movl 0x14(%rsp), %r9d
movss 0x10(%rsp), %xmm0
pushq 0x28(%rsp)
pushq 0x28(%rsp)
pushq 0x88(%rsp)
pushq 0x48(%rsp)
callq 0x1d6d71
addq $0x20, %rsp
addl %eax, 0xc(%rsp)
incq %r13
jmp 0x1d7304
movl 0xc(%rsp), %ebx
testl %ebx, %ebx
je 0x1d7439
movq %r14, %rdi
callq 0x1c5ce9
movl %ebx, %r13d
jmp 0x1d743c
xorl %r13d, %r13d
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq