// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: rescue_bubbles_by_missing_ovlp(ma_ug_t*, asg_t*, ma_hit_t_alloc*, ma_sub_t*, R_to_U*, int, int, unsigned int, unsigned int, unsigned int, bubble_type*, bub_label_t*)
void rescue_bubbles_by_missing_ovlp(ma_ug_t *i_u_g, asg_t *r_g, ma_hit_t_alloc* sources, ma_sub_t *coverage_cut, 
R_to_U* ruIndex, int max_hang, int min_ovlp, uint32_t chainLenThres, uint32_t beg_idx, uint32_t occ, bubble_type* bub, 
bub_label_t* b_mask_t)
{
    asg_t* nsg = NULL;
    uint32_t beg_utg, sink_utg, *a = NULL, n, i, bub_i, k_i, k_v, k, uId, endRid, is_Unitig, v, w;
    uint64_t l_bub, m_bub, r_bub/**, bub_0, bub_1**/;
    ma_ug_t* ug = i_u_g;
    ma_utg_t *nsu = NULL, *u = NULL;
    uint8_t* expect_vis = NULL; CALLOC(expect_vis, r_g->n_seq);
    asg_arc_t t, r_edge;

    for (v = 0; v < ug->u.n; v++) ug->g->seq[v].c = PRIMARY_LABLE;
    
    kvec_t(asg_arc_t) new_edges;
    kv_init(new_edges);

    kvec_t(asg_arc_t) rbub_edges;
    kv_init(rbub_edges);


    nsg = ug->g;
    for (v = 0; v < nsg->n_seq; v++)
    {
        uId = v;
        nsu = &(ug->u.a[uId]);
        if(nsu->m == 0) continue;
        if(nsg->seq[v].del) continue;
        for (k = 0; k < nsu->n; k++) 
        {
            set_R_to_U(ruIndex, nsu->a[k]>>33, uId, 1, &(r_g->seq[nsu->a[k]>>33].c));
        }
    }

    for (i = 0; i < bub->b_ug->u.n; i++)
    {
        u = &(bub->b_ug->u.a[i]);
        if(u->n < 3) continue; ///should be at least 3
        for (bub_i = 1; bub_i+1 < u->n; bub_i++)
        {
            m_bub = u->a[bub_i]>>33; l_bub = r_bub = (uint64_t)-1;
            if(m_bub < beg_idx || m_bub >= beg_idx + occ) continue;
            l_bub = u->a[bub_i-1]>>33;
            r_bub = u->a[bub_i+1]>>33;

            set_rtg_flag_by_bubble(bub, ug, r_g, l_bub, expect_vis, 1);
            set_rtg_flag_by_bubble(bub, ug, r_g, r_bub, expect_vis, 1);
            set_rtg_flag_by_bubble(bub, ug, r_g, m_bub, expect_vis, 1);

            get_bubbles(bub, m_bub, &beg_utg, &sink_utg, &a, &n, NULL);

            for (k_i = 0; k_i < n; k_i++)
            {
                uId = a[k_i]>>1;
                nsu = &(ug->u.a[uId]);
                if(nsu->m == 0) continue;
                for (k_v = 0; k_v < 2; k_v++)
                {
                    v = (uId<<1) + k_v;
                    if(get_real_length(nsg, v, NULL) != 0) continue;

                    if(v&1)
                    {
                        endRid = nsu->start^1;
                    }
                    else
                    {
                        endRid = nsu->end^1;
                    }


                    k = 0; rbub_edges.n = 0;
                    while(get_edge2existing_node_advance_by_broken_bub(ug, r_g, sources, coverage_cut, 
                        ruIndex, expect_vis, max_hang, min_ovlp, endRid, &k, &r_edge, 1))
                    {
                        kv_push(asg_arc_t, rbub_edges, r_edge);
                    }

                    if(rbub_edges.n > 0)
                    {
                        ///need to do transitive reduction
                        ///note here is different to standard transitive reduction
                        minor_transitive_reduction_r_g(r_g, rbub_edges.a, rbub_edges.n);
                        for (k = 0; k < rbub_edges.n; k++)
                        {
                            t = rbub_edges.a[k];
                            if(t.del) continue;
                    
                            kv_push(asg_arc_t, new_edges, t);
                            get_edge_from_source(sources, coverage_cut, ruIndex, max_hang, min_ovlp, 
                            (t.v^1), ((t.ul>>32)^1), &t);
                            kv_push(asg_arc_t, new_edges, t);
                        }
                    }
                }
            }
            

            set_rtg_flag_by_bubble(bub, ug, r_g, l_bub, expect_vis, 0);
            set_rtg_flag_by_bubble(bub, ug, r_g, r_bub, expect_vis, 0);
            set_rtg_flag_by_bubble(bub, ug, r_g, m_bub, expect_vis, 0);
        }
    }

    asg_arc_t* p = NULL;
    for (k = 0; k < new_edges.n; k++)
    {
        p = asg_arc_pushp(r_g);
        *p = new_edges.a[k];
    }
    if(new_edges.n != 0)
    {
        free(r_g->idx);
        r_g->idx = 0;
        r_g->is_srt = 0;
        asg_cleanup(r_g);
        asg_symm(r_g);
    }

    pre_clean(sources, coverage_cut, r_g, 0);

    lable_all_bubbles(r_g, b_mask_t);


    for (k = 0; k < new_edges.n; k++)
    {
        v = new_edges.a[k].ul>>32;
        w = new_edges.a[k].v;

        if(r_g->seq[v>>1].del) continue;
        if(r_g->seq[w>>1].del) continue;
        ///if this edge is at a bubble
        if(r_g->seq_vis[v]!=0 && r_g->seq_vis[w^1]!=0) continue;

        asg_arc_del(r_g, v, w, 1);
        asg_arc_del(r_g, w^1, v^1, 1);
    }
    asg_cleanup(r_g);
    asg_symm(r_g);

    
    for (v = 0; v < ruIndex->len; v++)
    {
        get_R_to_U(ruIndex, v, &uId, &is_Unitig);
        if(is_Unitig == 1) ruIndex->index[v] = (uint32_t)-1;
    }
    // fprintf(stderr, "M::%s has done!\n", __func__);
    // print_bubble_filling_status(ug, r_g, ruIndex, bub, beg_idx, occ, new_edges.a, new_edges.n);

    kv_destroy(new_edges);
    kv_destroy(rbub_edges);
    free(expect_vis);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %r9, 0x98(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, 0x90(%rsp)
movq %rdx, 0x58(%rsp)
movq %rdi, %r14
movl 0x168(%rsp), %r13d
andq $0x0, 0xa8(%rsp)
movl 0x160(%rsp), %ebx
movq %rsi, 0x10(%rsp)
movl 0x14(%rsi), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
pushq $0x1
popq %rsi
callq 0x2120
movq %rax, 0x40(%rsp)
movq (%r14), %rax
movq %r14, 0x38(%rsp)
movq 0x18(%r14), %rsi
xorl %ecx, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0xb9d53
movq 0x20(%rsi), %rdx
movb $0x0, 0x4(%rdx,%rcx,8)
incl %ecx
jmp 0xb9d3f
movq %rsi, 0x30(%rsp)
xorl %r15d, %r15d
pushq $0x1
popq %rbp
movq 0x30(%rsp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r15
jae 0xb9de2
movq 0x38(%rsp), %rax
movq 0x10(%rax), %r14
imulq $0x28, %r15, %rax
cmpl $0x0, 0xc(%r14,%rax)
je 0xb9dda
movq 0x30(%rsp), %rcx
movq 0x20(%rcx), %rcx
cmpl $0x0, (%rcx,%r15,8)
js 0xb9dda
addq %rax, %r14
xorl %r12d, %r12d
movl %r12d, 0xc(%rsp)
movl 0x10(%r14), %eax
cmpq %rax, %r12
jae 0xb9dda
movq 0x18(%r14), %rax
movq (%rax,%r12,8), %rsi
shrq $0x21, %rsi
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
leaq (%rax,%rsi,8), %r8
addq $0x4, %r8
movq 0x48(%rsp), %rdi
movl %r15d, %edx
movl %ebp, %ecx
callq 0x6ab56
incq %r12
jmp 0xb9d9b
incq %r15
jmp 0xb9d5e
movl %ebx, %r8d
addl %ebx, %r13d
xorl %r9d, %r9d
movq $0x0, 0x18(%rsp)
movq $0x0, 0x28(%rsp)
xorl %edi, %edi
xorl %ebp, %ebp
movq $0x0, 0xa0(%rsp)
movq 0x10(%rsp), %r14
movq %r13, 0xb8(%rsp)
movq %r8, 0xb0(%rsp)
movl %r9d, %r9d
movq 0x170(%rsp), %rax
movq 0xc0(%rax), %rax
cmpq %r9, (%rax)
jbe 0xba30d
movq 0x10(%rax), %r10
imulq $0x28, %r9, %rcx
movl 0x10(%r10,%rcx), %eax
cmpl $0x3, %eax
jae 0xb9e54
incl %r9d
jmp 0xb9e22
addq %rcx, %r10
pushq $0x1
popq %rdx
movl %eax, %eax
leaq 0x1(%rdx), %r11
cmpq %rax, %r11
jae 0xb9e4f
movq 0x18(%r10), %rbx
movq (%rbx,%rdx,8), %r15
shrq $0x21, %r15
cmpq %r8, %r15
setb %dl
cmpq %r13, %r15
setae %sil
orb %dl, %sil
movq %r11, %rdx
jne 0xb9e5c
movq %r10, 0xe0(%rsp)
movq %r9, 0xe8(%rsp)
movq -0x10(%rbx,%r11,8), %rcx
movq %r11, 0xd8(%rsp)
movq (%rbx,%r11,8), %rbx
shrq $0x21, %rcx
shrq $0x21, %rbx
movq %rdi, 0x20(%rsp)
movq 0x170(%rsp), %rdi
movq 0x38(%rsp), %r12
movq %r12, %rsi
movq %r14, %rdx
movq %rcx, 0xc0(%rsp)
movq 0x40(%rsp), %r13
movq %r13, %r8
pushq $0x1
popq %r9
callq 0xb7865
movq 0x170(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rdx
movq %rbx, 0xc8(%rsp)
movl %ebx, %ecx
movq %r13, %r8
pushq $0x1
popq %r9
callq 0xb7865
movq %r14, %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rdx
movl %r15d, %ecx
movq %r13, %r8
movq 0x10(%rsp), %rbx
pushq $0x1
popq %r9
callq 0xb7865
subq $0x8, %rsp
movq %r14, %rdi
movq %r15, 0xd8(%rsp)
movq %r15, %rsi
leaq 0x104(%rsp), %rdx
leaq 0x100(%rsp), %rcx
leaq 0xb0(%rsp), %r8
leaq 0x74(%rsp), %r9
pushq $0x0
callq 0xf6ab5
movq 0x30(%rsp), %rdx
addq $0x10, %rsp
xorl %ecx, %ecx
movq 0x30(%rsp), %r15
movq 0x18(%rsp), %r14
movq 0x28(%rsp), %rsi
movl 0x6c(%rsp), %eax
cmpq %rax, %rcx
jae 0xba25d
movq 0xa8(%rsp), %rax
movq %rcx, 0x50(%rsp)
movl (%rax,%rcx,4), %r8d
movl %r8d, %eax
shrl %eax
movq 0x38(%rsp), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %rax, %rax
cmpl $0x0, 0xc(%rcx,%rax)
je 0xba250
addq %rax, %rcx
movq %rcx, 0xf0(%rsp)
andl $-0x2, %r8d
xorl %eax, %eax
movq 0x50(%rsp), %rcx
movl %r8d, 0x64(%rsp)
cmpl $0x2, %eax
je 0xba255
movq %rsi, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movl %eax, %esi
orl %r8d, %esi
movq %r15, %rdi
xorl %edx, %edx
movl %eax, %r12d
callq 0xc2e7a
testl %eax, %eax
movl %r12d, 0x68(%rsp)
je 0xba00b
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rsi
jmp 0xba227
movq %r14, 0x18(%rsp)
xorl %eax, %eax
testl %r12d, %r12d
sete %al
movq 0xf0(%rsp), %rcx
movl 0x4(%rcx,%rax,4), %r13d
andl $0x0, 0xc(%rsp)
xorl $0x1, %r13d
xorl %r12d, %r12d
xorl %r15d, %r15d
leaq 0x100(%rsp), %r14
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
movq 0x58(%rsp), %rdx
movq 0x90(%rsp), %rcx
movq 0x48(%rsp), %r8
movq 0x40(%rsp), %r9
pushq $0x1
pushq %r14
leaq 0x1c(%rsp), %rax
pushq %rax
pushq %r13
movl 0x170(%rsp), %eax
pushq %rax
pushq 0xc0(%rsp)
callq 0xc4a03
addq $0x30, %rsp
testl %eax, %eax
je 0xba0e1
movq 0xa0(%rsp), %rax
cmpq %rax, %r15
jne 0xba0ba
leaq (%rax,%rax), %rbx
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rbx
imulq $0x18, %rbx, %rsi
movq %rbp, %rdi
callq 0x2370
movq %rax, %rbp
movq %rbx, 0xa0(%rsp)
movq 0x10(%rsp), %rbx
incq %r15
movq 0x110(%rsp), %rax
movq %rax, 0x10(%rbp,%r12)
movups 0x100(%rsp), %xmm0
movups %xmm0, (%rbp,%r12)
addq $0x18, %r12
jmp 0xba03c
testq %r15, %r15
je 0xba23c
movq %rbx, %rdi
movq %rbp, %rsi
movl %r15d, %edx
callq 0xb7e0d
xorl %eax, %eax
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rsi
movl %eax, 0xc(%rsp)
movl %eax, %ecx
cmpq %rcx, %r15
jbe 0xba222
imulq $0x18, %rcx, %rcx
movups (%rbp,%rcx), %xmm0
movaps %xmm0, 0x70(%rsp)
movq 0x10(%rbp,%rcx), %rcx
movq %rcx, 0x80(%rsp)
cmpl $0x0, 0x7c(%rsp)
js 0xba21b
cmpq %rsi, %rdx
jne 0xba168
leaq (%rsi,%rsi), %rbx
testq %rsi, %rsi
pushq $0x2
popq %rax
cmoveq %rax, %rbx
imulq $0x18, %rbx, %rsi
movq %rdx, %r12
movq %r14, %rdi
callq 0x2370
movq %r12, %rdx
movq %rax, %r14
jmp 0xba16b
movq %rsi, %rbx
leaq 0x1(%rdx), %r12
movq %rdx, %r13
imulq $0x18, %rdx, %rax
movq 0x80(%rsp), %rcx
movq %rcx, 0x10(%r14,%rax)
movaps 0x70(%rsp), %xmm0
movups %xmm0, (%r14,%rax)
movl 0x74(%rsp), %eax
movl 0x78(%rsp), %r9d
xorl $0x1, %r9d
xorl $0x1, %eax
movq 0x58(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x98(%rsp), %rcx
movl 0x150(%rsp), %r8d
leaq 0x70(%rsp), %rdx
pushq %rdx
pushq %rax
callq 0x79204
popq %rcx
popq %rdx
cmpq %rbx, %r12
movq %rbx, %rsi
jne 0xba1f0
leaq (%rsi,%rsi), %rbx
testq %rsi, %rsi
pushq $0x2
popq %rax
cmoveq %rax, %rbx
imulq $0x18, %rbx, %rsi
movq %r14, %rdi
callq 0x2370
movq %rax, %r14
movq %rbx, %rsi
movq 0x10(%rsp), %rbx
movq %r13, %rdx
addq $0x2, %rdx
imulq $0x18, %r12, %rax
movq 0x80(%rsp), %rcx
movq %rcx, 0x10(%r14,%rax)
movaps 0x70(%rsp), %xmm0
movups %xmm0, (%r14,%rax)
movl 0xc(%rsp), %eax
incl %eax
jmp 0xba109
movq 0x30(%rsp), %r15
movl 0x68(%rsp), %eax
incl %eax
movq 0x50(%rsp), %rcx
movl 0x64(%rsp), %r8d
jmp 0xb9fce
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %rdx
movq 0x30(%rsp), %r15
jmp 0xba001
movq 0x50(%rsp), %rcx
incq %rcx
jmp 0xb9f78
movq %rsi, 0x28(%rsp)
movq %r14, 0x18(%rsp)
movq %rdx, %r15
movq 0x170(%rsp), %r13
movq %r13, %rdi
movq 0x38(%rsp), %r12
movq %r12, %rsi
movq %rbx, %rdx
movq 0xc0(%rsp), %rcx
movq 0x40(%rsp), %rbx
movq %rbx, %r8
xorl %r9d, %r9d
callq 0xb7865
movq %r13, %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rdx
movq 0xc8(%rsp), %rcx
movq %rbx, %r8
xorl %r9d, %r9d
callq 0xb7865
movq %r13, %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rdx
movq 0xd0(%rsp), %rcx
movq %rbx, %r8
movq 0x10(%rsp), %r14
xorl %r9d, %r9d
callq 0xb7865
movq %r15, %rdi
movq 0xe0(%rsp), %r10
movl 0x10(%r10), %eax
movq 0xb8(%rsp), %r13
movq 0xb0(%rsp), %r8
movq 0xe8(%rsp), %r9
movq 0xd8(%rsp), %rdx
jmp 0xb9e5a
xorl %eax, %eax
movq %rdi, 0x20(%rsp)
movl %eax, 0xc(%rsp)
movl %eax, %eax
cmpq %rax, %rdi
jbe 0xba34f
movq %r14, %rdi
callq 0x6c45d
movq 0x20(%rsp), %rdi
movl 0xc(%rsp), %ecx
imulq $0x18, %rcx, %rdx
movq 0x18(%rsp), %r8
movq 0x10(%r8,%rdx), %rsi
movq %rsi, 0x10(%rax)
movups (%r8,%rdx), %xmm0
movups %xmm0, (%rax)
leal 0x1(%rcx), %eax
jmp 0xba314
testq %rdi, %rdi
je 0xba377
movq 0x28(%r14), %rdi
callq 0x2220
andq $0x0, 0x28(%r14)
andb $0x7f, 0x7(%r14)
movq %r14, %rdi
callq 0x694eb
movq %r14, %rdi
callq 0x69638
xorl %r12d, %r12d
movq 0x58(%rsp), %rdi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0xb4c38
movq %r14, %rdi
movq 0x178(%rsp), %rsi
callq 0xb5068
pushq $0x1
popq %rbx
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdi
movl %r12d, 0xc(%rsp)
movl %r12d, %eax
cmpq %rax, %rsi
jbe 0xba442
imulq $0x18, %rax, %rcx
movq (%rdi,%rcx), %r14
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movq %r14, %rdx
shrq $0x21, %rdx
cmpl $0x0, (%rax,%rdx,8)
js 0xba43a
movl 0x8(%rdi,%rcx), %edx
movl %edx, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xba43a
shrq $0x20, %r14
movq 0x10(%rsp), %rax
movq 0x30(%rax), %rax
movq %rdx, %r15
xorq $0x1, %r15
cmpb $0x0, (%rax,%r14)
je 0xba405
cmpb $0x0, (%rax,%r15)
jne 0xba43a
movq 0x10(%rsp), %r12
movq %r12, %rdi
movl %r14d, %esi
movl %ebx, %ecx
callq 0x6db50
xorl $0x1, %r14d
movq %r12, %rdi
movl %r15d, %esi
movl %r14d, %edx
movl %ebx, %ecx
callq 0x6db50
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movl 0xc(%rsp), %r12d
incl %r12d
jmp 0xba3a6
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
movq 0x18(%rsp), %rdi
movq 0x48(%rsp), %rax
movq (%rax), %rax
xorl %ecx, %ecx
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0xba484
movq 0x48(%rsp), %rdx
movq 0x8(%rdx), %rdx
cmpl $-0x2, (%rdx,%rcx,4)
jg 0xba480
orl $-0x1, (%rdx,%rcx,4)
incl %ecx
jmp 0xba466
callq 0x2220
movq %rbp, %rdi
callq 0x2220
movq 0x40(%rsp), %rdi
callq 0x2220
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq