// File: 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
movq %rsp, %rbp
pushq %rbx
subq $0x78, %rsp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movss %xmm0, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movl %eax, -0x40(%rbp)
movl $0x0, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x36c800
movq %rax, -0x58(%rbp)
movq 0x10(%rbp), %rdi
movl -0x40(%rbp), %eax
movl %eax, %edx
shlq $0x0, %rdx
xorl %esi, %esi
callq 0x2100
cmpq $0x0, -0x58(%rbp)
jbe 0x36e93c
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0x36e6ea
movq 0x10(%rbp), %rax
movl -0x38(%rbp), %ecx
cmpb $0x0, (%rax,%rcx)
je 0x36e540
jmp 0x36e6dc
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x38(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x38(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x60(%rbp)
cmpl $0x2, -0x48(%rbp)
jb 0x36e5bf
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
cmpb $0x0, 0x70(%rax)
je 0x36e5c4
jmp 0x36e6dc
movl $0x0, -0x44(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x36e609
movq -0x60(%rbp), %rax
movl -0x4c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x36e5fc
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x36e5fe
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x36e5d2
cmpl $0x2, -0x44(%rbp)
jae 0x36e614
jmp 0x36e6dc
movq -0x10(%rbp), %rdi
movl -0x38(%rbp), %esi
movq -0x58(%rbp), %rdx
movq -0x18(%rbp), %r8
movq $-0x1, %rcx
callq 0x36caf0
cmpq $0x0, %rax
je 0x36e6da
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0x36e6b8
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x38(%rbp), %eax
je 0x36e67a
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpl (%rcx), %eax
jne 0x36e67c
jmp 0x36e6ad
movq 0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x48(%rcx), %rcx
movl -0x4c(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movb $0x1, (%rax,%rcx)
movq 0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x48(%rcx), %rcx
movl -0x4c(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movb $0x1, (%rax,%rcx)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x36e640
movq 0x10(%rbp), %rax
movl -0x38(%rbp), %ecx
movb $0x2, (%rax,%rcx)
movq 0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl (%rcx), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movb $0x3, (%rax,%rcx)
jmp 0x36e6dc
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x36e522
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0x36e93a
movq 0x10(%rbp), %rax
movl -0x38(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x2, %eax
je 0x36e712
jmp 0x36e92c
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x38(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
movl %eax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x38(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x78, %rcx, %rcx
addq %rcx, %rax
addq $0x10, %rax
movl -0x38(%rbp), %ecx
andl $0x1, %ecx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x48(%rbp), %eax
jae 0x36e7b5
movq -0x60(%rbp), %rax
movl -0x4c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x36e7a8
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x36e7aa
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x36e77e
cmpl $0x2, -0x44(%rbp)
jae 0x36e7c0
jmp 0x36e92c
movq -0x10(%rbp), %rdi
movl -0x38(%rbp), %esi
movq -0x58(%rbp), %rdx
movq -0x18(%rbp), %r8
movq $-0x1, %rcx
callq 0x36caf0
cmpq $0x0, %rax
je 0x36e92a
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0x36e898
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x38(%rbp), %eax
je 0x36e831
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpl (%rcx), %eax
jne 0x36e833
jmp 0x36e88a
movq 0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x48(%rcx), %rcx
movl -0x4c(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movb $0x1, (%rax,%rcx)
movq 0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x48(%rcx), %rcx
movl -0x4c(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movb $0x1, (%rax,%rcx)
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,4), %edx
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rax
movl -0x3c(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0x3c(%rbp)
movl %ecx, %ecx
movl %edx, (%rax,%rcx,4)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x36e7f3
movl -0x3c(%rbp), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq 0x10(%rbp), %rax
movl -0x38(%rbp), %ecx
movb $0x2, (%rax,%rcx)
movq 0x10(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl (%rcx), %ecx
xorl $0x1, %ecx
movl %ecx, %ecx
movb $0x3, (%rax,%rcx)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rax
movl %eax, %edx
movl -0x38(%rbp), %ecx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl (%rax), %r8d
xorl $0x1, %r8d
movl -0x34(%rbp), %r9d
movl -0x2c(%rbp), %ebx
movss -0x30(%rbp), %xmm0
movq 0x18(%rbp), %r11
movq -0x20(%rbp), %r10
movq -0x28(%rbp), %rax
movl %ebx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x36dcb0
movq %rax, %rcx
movl -0x50(%rbp), %eax
addq %rcx, %rax
movl %eax, -0x50(%rbp)
jmp 0x36e92c
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x36e6f1
jmp 0x36e93c
cmpl $0x0, -0x50(%rbp)
jbe 0x36e94b
movq -0x10(%rbp), %rdi
callq 0x34d830
movl -0x50(%rbp), %eax
addq $0x78, %rsp
popq %rbx
popq %rbp
retq
nopw %cs:(%rax,%rax)