// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rdi
callq 0x10ba70
movq %rax, -0x28(%rbp)
movq $0x0, -0x48(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x1273bf
cmpq $0x0, -0x20(%rbp)
je 0x127915
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x28(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x127426
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
cmpq 0x80(%rcx), %rax
jae 0x127419
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
orl $0x80000000, %edx       # imm = 0x80000000
movl %edx, (%rax,%rcx,8)
jmp 0x12741b
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1273c6
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x28(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl 0x4(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x1274e9
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x8(%rcx), %rcx
movl -0x2c(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
shrq $0x21, %rcx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
cmpq 0x80(%rcx), %rax
jb 0x1274b1
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x8(%rcx), %rcx
movl -0x2c(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0x8(%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
cmpq 0x80(%rcx), %rax
jae 0x1274d9
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
jmp 0x1274db
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x12742d
movq -0x18(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x1276fe
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x1276fc
movq -0x48(%rbp), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jae 0x1276ec
movq -0x50(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x50(%rbp), %rcx
movq 0x30(%rcx), %rcx
movl -0x30(%rbp), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movq -0x50(%rbp), %rcx
movq 0x30(%rcx), %rcx
movl -0x30(%rbp), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
subl 0xc(%rcx), %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x1275db
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0x58(%rbp), %rcx
movl -0x38(%rbp), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
movl (%rax,%rcx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
orl $0x0, %edx
movl %edx, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x12759d
movl $0x1, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x1276dc
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
shll %eax
movl %eax, -0x3c(%rbp)
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movb 0x20(%rax), %al
shrb $0x5, %al
andb $0x1, %al
movzbl %al, %eax
orl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
shll %eax
movl %eax, -0x40(%rbp)
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movb 0x20(%rax), %al
shrb $0x5, %al
andb $0x1, %al
movzbl %al, %eax
orl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x3c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
cmpq 0x80(%rcx), %rax
jb 0x12769c
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x40(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
cmpq 0x80(%rcx), %rax
jae 0x1276cc
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x3c(%rbp), %esi
movl -0x40(%rbp), %edx
xorl %ecx, %ecx
callq 0xd6530
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x40(%rbp), %esi
xorl $0x1, %esi
movl -0x3c(%rbp), %edx
xorl $0x1, %edx
xorl %ecx, %ecx
callq 0xd6530
jmp 0x1276ce
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1275e2
jmp 0x1276de
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x127530
jmp 0x1276ee
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x127503
jmp 0x1276fe
movq -0x20(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x127913
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x48(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x127911
movq -0x48(%rbp), %rax
movq (%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x58, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jae 0x127901
movq -0x50(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x50(%rbp), %rcx
movq 0x30(%rcx), %rcx
movl -0x30(%rbp), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x30(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movq -0x50(%rbp), %rcx
movq 0x30(%rcx), %rcx
movl -0x30(%rbp), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
subl 0xc(%rcx), %eax
movl %eax, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x1277f0
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movq -0x58(%rbp), %rcx
movl -0x38(%rbp), %edx
imulq $0x24, %rdx, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
movl (%rax,%rcx,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
orl $0x0, %edx
movl %edx, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1277b2
movl $0x1, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x1278f1
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
shll %eax
movl %eax, -0x3c(%rbp)
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movb 0x20(%rax), %al
shrb $0x5, %al
andb $0x1, %al
movzbl %al, %eax
orl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
shll %eax
movl %eax, -0x40(%rbp)
movq -0x58(%rbp), %rax
movl -0x38(%rbp), %ecx
imulq $0x24, %rcx, %rcx
addq %rcx, %rax
movb 0x20(%rax), %al
shrb $0x5, %al
andb $0x1, %al
movzbl %al, %eax
orl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x3c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
cmpq 0x80(%rcx), %rax
jb 0x1278b1
movq -0x10(%rbp), %rax
movq (%rax), %rax
movl -0x40(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,4), %eax
movq -0x10(%rbp), %rcx
cmpq 0x80(%rcx), %rax
jae 0x1278e1
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x3c(%rbp), %esi
movl -0x40(%rbp), %edx
xorl %ecx, %ecx
callq 0xd6530
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x40(%rbp), %esi
xorl $0x1, %esi
movl -0x3c(%rbp), %edx
xorl $0x1, %edx
xorl %ecx, %ecx
callq 0xd6530
jmp 0x1278e3
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1277f7
jmp 0x1278f3
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x127745
jmp 0x127903
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x127718
jmp 0x127913
jmp 0x127915
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x28(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl 0x14(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0x1279f7
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x20(%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x1279e7
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
movl -0x2c(%rbp), %esi
callq 0xd9510
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0xc(%rax)
je 0x1279e5
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x10(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0xc(%rax)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rdi
callq 0x2210
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq $0x0, 0x18(%rax)
jmp 0x1279e7
jmp 0x1279e9
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x12791c
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0xcdd10
movq -0x28(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq
nop