// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: gen_shallow_clean_ug(ma_ug_t*, bubble_type*, scaf_res_t*, scaf_res_t*)
ma_ug_t *gen_shallow_clean_ug(ma_ug_t *ref, bubble_type *bu, scaf_res_t *cp0, scaf_res_t *cp1)
{
    ma_ug_t *ug = copy_untig_graph(ref);
    uint32_t i, k, a_n, z, v, w; scaf_res_t *ctg = NULL; ul_vec_t *idx; uc_block_t *a; 
    if(cp0 || cp1) {
        ///just clean bubbles
        for (i = 0; i < ug->g->n_seq; i++) {
            // if(i < bu->f_bub) ug->g->seq[i].del = 1;///no need broken bubble; looks like a bug in scaffolding
            if(bu->index[i] < bu->f_bub) ug->g->seq[i].del = 1;
        }
        for (i = 0; i < ug->g->n_arc; i++) {
            if((bu->index[ug->g->arc[i].ul>>33] < bu->f_bub) || (bu->index[ug->g->arc[i].v>>1] < bu->f_bub)) {///no need broken bubble
                ug->g->arc[i].del = 1;
            }
        }
        
        ctg = cp0;
        if (ctg) {
            for(i = 0; i < ctg->n; i++) {
                idx = &(ctg->a[i]);
                for (k = 0; k < idx->bb.n; k++) {
                    a = idx->bb.a + idx->bb.a[k].ts; a_n = idx->bb.a[k].te - idx->bb.a[k].ts;
                    for (z = 0; z < a_n; z++) ug->g->seq[a[z].hid].del = 0; 
                    for (z = 1; z < a_n; z++) {
                        v = a[z-1].hid<<1; v |= (uint32_t)a[z-1].rev;
                        w = a[z].hid<<1; w |= (uint32_t)a[z].rev;
                        // if(((v>>1) < bu->f_bub) || ((w>>1) < bu->f_bub)) {
                        if((bu->index[(v>>1)] < bu->f_bub) || (bu->index[(w>>1)] < bu->f_bub)) {    
                            asg_arc_del(ug->g, v, w, 0);
                            asg_arc_del(ug->g, w^1, v^1, 0);
                        }
                    }
                }
            }
        }
        
        ctg = cp1;
        if (ctg) {
            for(i = 0; i < ctg->n; i++) {
                idx = &(ctg->a[i]);
                for (k = 0; k < idx->bb.n; k++) {
                    a = idx->bb.a + idx->bb.a[k].ts; a_n = idx->bb.a[k].te - idx->bb.a[k].ts;
                    for (z = 0; z < a_n; z++) ug->g->seq[a[z].hid].del = 0; 
                    for (z = 1; z < a_n; z++) {
                        v = a[z-1].hid<<1; v |= (uint32_t)a[z-1].rev;
                        w = a[z].hid<<1; w |= (uint32_t)a[z].rev;
                        // if(((v>>1) < bu->f_bub) || ((w>>1) < bu->f_bub)) {
                        if((bu->index[(v>>1)] < bu->f_bub) || (bu->index[(w>>1)] < bu->f_bub)) {
                            asg_arc_del(ug->g, v, w, 0);
                            asg_arc_del(ug->g, w^1, v^1, 0);
                        }
                    }
                }
            }
        }
    }
    

    for (i = 0; i < ug->g->n_seq; i++) {
        if(ug->g->seq[i].del == 1) {
            asg_seq_del(ug->g, i);
            if(ug->u.a[i].m!=0) {
                ug->u.a[i].m = ug->u.a[i].n = 0;
                free(ug->u.a[i].a);
                ug->u.a[i].a = NULL;
            }
        }
    }

    asg_cleanup(ug->g);

    return ug;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %rbx
movq %rdx, %r15
movq %rsi, %r12
callq 0xc8eab
movq %rax, 0x18(%rsp)
movq 0x18(%rax), %r14
movq %r15, %rax
movq %rbx, 0x8(%rsp)
orq %rbx, %rax
je 0xdb7d1
movl 0x14(%r14), %eax
testl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0xdb3da
xorl %ecx, %ecx
movq (%r12), %rdx
movl (%rdx,%rcx,4), %edx
cmpq %rdx, 0x80(%r12)
jbe 0xdb3ca
movq 0x20(%r14), %rax
orl $0x80000000, (%rax,%rcx,8) # imm = 0x80000000
movl 0x14(%r14), %eax
incq %rcx
movl %eax, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %rcx
jb 0xdb3aa
movl 0x4(%r14), %eax
testl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0xdb43c
movl $0xc, %ecx
xorl %edx, %edx
movq (%r12), %r8
movq 0x80(%r12), %rdi
movq 0x8(%r14), %rsi
movq -0xc(%rsi,%rcx), %r9
shrq $0x21, %r9
movl (%r8,%r9,4), %r9d
cmpq %r9, %rdi
ja 0xdb41f
movl -0x4(%rsi,%rcx), %r9d
shrl %r9d
movl (%r8,%r9,4), %r8d
cmpq %r8, %rdi
jbe 0xdb428
orb $-0x80, 0x3(%rsi,%rcx)
movl 0x4(%r14), %eax
incq %rdx
movl %eax, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq $0x18, %rcx
cmpq %rsi, %rdx
jb 0xdb3ec
testq %r15, %r15
je 0xdb5ff
movq 0x8(%r15), %rdx
testq %rdx, %rdx
je 0xdb5ff
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq %r15, 0x20(%rsp)
movq (%r15), %rsi
imulq $0x58, %rax, %rdi
cmpq $0x0, 0x20(%rsi,%rdi)
je 0xdb5f4
movq %rax, 0x10(%rsp)
leaq (%rsi,%rdi), %rdx
addq $0x20, %rdx
xorl %esi, %esi
movq 0x10(%rdx), %rdi
leaq (%rsi,%rsi,8), %r8
movl 0xc(%rdi,%r8,4), %r9d
movl 0x10(%rdi,%r8,4), %r8d
leaq (%r9,%r9,8), %r10
leaq (%rdi,%r10,4), %rdi
subl %r9d, %r8d
je 0xdb4bd
movl %r8d, %r9d
movq %rdi, %r10
movq 0x20(%r14), %r11
movl (%r10), %ebx
andl $0x7fffffff, (%r11,%rbx,8) # imm = 0x7FFFFFFF
addq $0x24, %r10
decq %r9
jne 0xdb4a5
cmpl $0x2, %r8d
jb 0xdb5db
movl %r8d, %r8d
movl $0x1, %r9d
leaq (%r9,%r9,8), %r15
movl -0x24(%rdi,%r15,4), %eax
movl -0x4(%rdi,%r15,4), %ebx
movl (%rdi,%r15,4), %r11d
movl 0x20(%rdi,%r15,4), %ebp
movq (%r12), %r13
movq 0x80(%r12), %r15
movl %eax, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl (%r13,%r10,4), %r10d
cmpq %r10, %r15
ja 0xdb51f
movl %r11d, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl (%r13,%r10,4), %r10d
cmpq %r10, %r15
jbe 0xdb5cf
shrl $0x5, %ebx
andl $0x1, %ebx
leal (%rbx,%rax,2), %r10d
shrl $0x5, %ebp
andl $0x1, %ebp
leal (%rbp,%r11,2), %r11d
movq 0x8(%r14), %rbp
movq 0x28(%r14), %rbx
movl %r10d, %eax
movq (%rbx,%rax,8), %r15
movq %r15, %r13
andq %rcx, %r13
je 0xdb588
shrq $0x20, %r15
shlq $0x3, %r13
leaq (,%r13,2), %rbx
addq %r13, %rbx
leaq (%r15,%r15,2), %rax
leaq 0xc(%rbp,%rax,8), %rbp
xorl %r15d, %r15d
cmpl %r11d, -0x4(%rbp,%r15)
jne 0xdb577
andb $0x7f, 0x3(%rbp,%r15)
addq $0x18, %r15
cmpq %r15, %rbx
jne 0xdb56a
movq 0x8(%r14), %rbp
movq 0x28(%r14), %rbx
xorl $0x1, %r11d
movq (%rbx,%r11,8), %rbx
movq %rbx, %r11
andq %rcx, %r11
je 0xdb5cf
shrq $0x20, %rbx
shlq $0x3, %r11
leaq (%r11,%r11,2), %r11
leaq (%rbx,%rbx,2), %rax
leaq 0xc(,%rax,8), %rbx
addq %rbp, %rbx
xorl %ebp, %ebp
movl -0x4(%rbx,%rbp), %eax
xorl %r10d, %eax
cmpl $0x1, %eax
jne 0xdb5c6
andb $0x7f, 0x3(%rbx,%rbp)
addq $0x18, %rbp
cmpq %rbp, %r11
jne 0xdb5b5
incq %r9
cmpq %r8, %r9
jne 0xdb4d0
incl %esi
cmpq %rsi, (%rdx)
ja 0xdb480
movq 0x20(%rsp), %r15
movq 0x8(%r15), %rdx
movq 0x10(%rsp), %rax
incl %eax
cmpq %rax, %rdx
ja 0xdb45e
cmpq $0x0, 0x8(%rsp)
je 0xdb7d1
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdx
testq %rdx, %rdx
je 0xdb7d1
xorl %r8d, %r8d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rax
movq (%rax), %rsi
imulq $0x58, %r8, %rdi
cmpq $0x0, 0x20(%rsi,%rdi)
je 0xdb7c5
movq %r8, 0x10(%rsp)
leaq (%rsi,%rdi), %rdx
addq $0x20, %rdx
xorl %esi, %esi
movq 0x10(%rdx), %rdi
leaq (%rsi,%rsi,8), %r8
movl 0xc(%rdi,%r8,4), %r9d
movl 0x10(%rdi,%r8,4), %r8d
leaq (%r9,%r9,8), %r10
leaq (%rdi,%r10,4), %rdi
subl %r9d, %r8d
je 0xdb689
movl %r8d, %r9d
movq %rdi, %r10
movq 0x20(%r14), %r11
movl (%r10), %ebx
andl $0x7fffffff, (%r11,%rbx,8) # imm = 0x7FFFFFFF
addq $0x24, %r10
decq %r9
jne 0xdb671
cmpl $0x2, %r8d
jb 0xdb7ac
movl %r8d, %r8d
movl $0x1, %r9d
leaq (%r9,%r9,8), %r15
movl -0x24(%rdi,%r15,4), %eax
movl -0x4(%rdi,%r15,4), %ebx
movl (%rdi,%r15,4), %r11d
movl 0x20(%rdi,%r15,4), %r13d
movq (%r12), %rbp
movq 0x80(%r12), %r15
movl %eax, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl (%rbp,%r10,4), %r10d
cmpq %r10, %r15
ja 0xdb6eb
movl %r11d, %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movl (%rbp,%r10,4), %r10d
cmpq %r10, %r15
jbe 0xdb7a0
shrl $0x5, %ebx
andl $0x1, %ebx
leal (%rbx,%rax,2), %r10d
shrl $0x5, %r13d
andl $0x1, %r13d
leal (%r13,%r11,2), %r11d
movq 0x8(%r14), %r13
movq 0x28(%r14), %rbx
movl %r10d, %eax
movq (%rbx,%rax,8), %r15
movq %r15, %rbp
andq %rcx, %rbp
je 0xdb756
shrq $0x20, %r15
shlq $0x3, %rbp
leaq (,%rbp,2), %rbx
addq %rbp, %rbx
leaq (%r15,%r15,2), %rax
leaq 0xc(%r13,%rax,8), %r13
xorl %r15d, %r15d
cmpl %r11d, -0x4(%r13,%r15)
jne 0xdb745
andb $0x7f, 0x3(%r13,%r15)
addq $0x18, %r15
cmpq %r15, %rbx
jne 0xdb738
movq 0x8(%r14), %r13
movq 0x28(%r14), %rbx
xorl $0x1, %r11d
movq (%rbx,%r11,8), %rbx
movq %rbx, %r11
andq %rcx, %r11
je 0xdb7a0
shrq $0x20, %rbx
shlq $0x3, %r11
leaq (%r11,%r11,2), %r11
leaq (%rbx,%rbx,2), %rax
leaq 0xc(,%rax,8), %rbx
addq %r13, %rbx
xorl %r13d, %r13d
movl -0x4(%rbx,%r13), %eax
xorl %r10d, %eax
cmpl $0x1, %eax
jne 0xdb797
andb $0x7f, 0x3(%rbx,%r13)
addq $0x18, %r13
cmpq %r13, %r11
jne 0xdb784
incq %r9
cmpq %r8, %r9
jne 0xdb69c
incl %esi
cmpq %rsi, (%rdx)
ja 0xdb64c
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rdx
movq 0x10(%rsp), %r8
incl %r8d
cmpq %r8, %rdx
ja 0xdb625
testl $0x7fffffff, 0x14(%r14) # imm = 0x7FFFFFFF
je 0xdb8f1
xorl %r15d, %r15d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
xorl %r13d, %r13d
movq 0x20(%r14), %rax
cmpl $0x0, (%rax,%r13,8)
jns 0xdb8dc
leaq (,%r13,2), %rax
movb $0x1, %cl
xorl %edx, %edx
orq %rax, %rdx
movq 0x28(%r14), %rsi
movq (%rsi,%rdx,8), %rdi
movq %rdi, %rsi
andq %r12, %rsi
je 0xdb891
shrq $0x20, %rdi
leaq (%rdi,%rdi,2), %rdi
shlq $0x3, %rdi
addq 0x8(%r14), %rdi
xorl %r8d, %r8d
leaq (%r8,%r8,2), %r9
orb $-0x80, 0xf(%rdi,%r9,8)
movl 0x8(%rdi,%r9,8), %r9d
xorq $0x1, %r9
movq 0x28(%r14), %r10
movq (%r10,%r9,8), %r10
movq %r10, %r9
andq %r12, %r9
je 0xdb889
movq 0x8(%r14), %r11
shrq $0x20, %r10
shlq $0x3, %r9
leaq (%r9,%r9,2), %r9
leaq (%r10,%r10,2), %r10
leaq (%r11,%r10,8), %r10
addq $0xc, %r10
xorl %r11d, %r11d
movl -0x4(%r10,%r11), %ebx
xorl %edx, %ebx
cmpl $0x1, %ebx
jne 0xdb880
orb $-0x80, 0x3(%r10,%r11)
addq $0x18, %r11
cmpq %r11, %r9
jne 0xdb86e
incq %r8
cmpq %rsi, %r8
jne 0xdb82c
movl $0x1, %edx
testb $0x1, %cl
movl $0x0, %ecx
jne 0xdb806
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
leaq (,%r13,4), %rcx
addq %r13, %rcx
cmpl $0x0, 0xc(%rax,%rcx,8)
je 0xdb8dc
leaq (%rax,%rcx,8), %rbx
movl %r15d, 0x10(%rbx)
movl %r15d, 0xc(%rbx)
movq 0x18(%rbx), %rdi
callq 0x2220
movq $0x0, 0x18(%rbx)
incq %r13
movl 0x14(%r14), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r13
jb 0xdb7eb
movq %r14, %rdi
callq 0xa0a7d
movq 0x18(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq