// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: flat_bubbles_advance(asg_t*, ma_hit_t_alloc*, R_to_U*, unsigned long)
void flat_bubbles_advance(asg_t *sg, ma_hit_t_alloc* sources, R_to_U* ruIndex, uint64_t het_thres)
{
    // fprintf(stderr, "het_thres-%lu\n", het_thres);
    ma_ug_t *ug = NULL;
    ug = ma_ug_gen(sg);
    ma_utg_t *u = NULL; 
    ma_hit_t *h;
    uint32_t n_vtx = ug->g->n_seq<<1, v, i, k, m, rId, tn, is_Unitig, n_pop = 0;
    uint64_t C_bases, R_bases;
    buf_t b; memset(&b, 0, sizeof(buf_t)); b.a = (binfo_t*)calloc(n_vtx, sizeof(binfo_t));
    uint8_t* bs_flag = (uint8_t*)calloc(n_vtx, 1);
    uint8_t* r_flag = (uint8_t*)calloc(sg->n_seq, sizeof(uint8_t));
    uint64_t tLen = get_bub_pop_max_dist_advance(ug->g, &b);
    
    for (v = 0; v < ug->g->n_seq; ++v) 
    {
        if(ug->g->seq[v].del) continue;
        ug->g->seq[v].c = PRIMARY_LABLE;
        EvaluateLen(ug->u, v) = ug->u.a[v].n;
    }

    n_pop = 1; ///round = 0;
    while(n_pop > 0)
    {
        for (v = n_pop = 0; v < n_vtx; ++v) 
        {
            if(ug->g->seq[v>>1].del) continue;
            if(asg_arc_n(ug->g, v) < 2) continue;
            if(get_real_length(ug->g, v, NULL) < 2) continue;
            if(bs_flag[v] == 1) continue;
            if(bs_flag[v] == 0) bs_flag[v] = 1;

            if(asg_bub_pop1_primary_trio(ug->g, ug, v, tLen, &b, (uint32_t)-1, (uint32_t)-1, 0, NULL, NULL, NULL, 0, 0, NULL))
            {
                bs_flag[v] = 2; bs_flag[b.S.a[0]^1] = 2;
                //note b.b include end, does not include beg
                for (i = 0; i < b.b.n; i++)
                {
                    u = &(ug->u.a[b.b.a[i]>>1]);
                    for (k = 0; k < u->n; k++) r_flag[u->a[k]>>33] = 1;
                }

                u = &(ug->u.a[v>>1]);
                for (k = 0; k < u->n; k++) r_flag[u->a[k]>>33] = 1;

                u = &(ug->u.a[b.S.a[0]>>1]);
                for (k = 0; k < u->n; k++) r_flag[u->a[k]>>33] = 1;
                
                




                C_bases = 0;
                for (i = 0; i < b.b.n; i++)
                {
                    if((b.b.a[i]>>1) == (v>>1) || (b.b.a[i]>>1) == (b.S.a[0]>>1)) continue;
                    u = &(ug->u.a[b.b.a[i]>>1]);
                    for (k = 0; k < u->n; k++)
                    {
                        rId = u->a[k]>>33;
                        // R_bases += sg->seq[rId].len;
                        for (m = 0; m < (uint64_t)(sources[rId].length); m++)
                        {
                            h = &(sources[rId].buffer[m]);
                            ///if(h->el != 1) continue;
                            tn = Get_tn((*h));
                            if(sg->seq[tn].del == 1)
                            {
                                ///get the id of read that contains it 
                                get_R_to_U(ruIndex, tn, &tn, &is_Unitig);
                                if(tn == (uint32_t)-1 || is_Unitig == 1 || sg->seq[tn].del == 1) continue;
                            }
                            if(sg->seq[tn].del == 1) continue;
                            if(r_flag[tn] == 0) continue;
                            C_bases += (Get_qe((*h)) - Get_qs((*h)));
                        }
                    }                    
                }

                R_bases = get_primary_path_len(sg, ug, v, &b);

                if((C_bases/R_bases) <= het_thres)
                {
                    // fprintf(stderr, "s-utg%.6ul\te-utg%.6ul\tC_bases:%lu\tR_bases:%lu\n", (v>>1)+1, (b.S.a[0]>>1)+1, C_bases, R_bases);
                    asg_bub_pop1_primary_trio(ug->g, ug, v, tLen, &b, (uint32_t)-1, (uint32_t)-1, 1, NULL, NULL, NULL, 0, 0, NULL);
                    n_pop++;
                }
                

                for (i = 0; i < b.b.n; i++)
                {
                    u = &(ug->u.a[b.b.a[i]>>1]);
                    for (k = 0; k < u->n; k++) r_flag[u->a[k]>>33] = 0;
                }

                u = &(ug->u.a[v>>1]);
                for (k = 0; k < u->n; k++) r_flag[u->a[k]>>33] = 0;

                u = &(ug->u.a[b.S.a[0]>>1]);
                for (k = 0; k < u->n; k++) r_flag[u->a[k]>>33] = 0;
            }
        }
        ///round++;
    }

    /*******************************for debug************************************/
    // kvec_t(uint64_t) occ_sort; kv_init(occ_sort);
    // for (v = n_pop = 0; v < ug->g->n_seq; ++v) 
    // {
    //     if(ug->g->seq[v].del) continue;
    //     if(ug->g->seq[v].c != ALTER_LABLE) continue;
    //     u = &(ug->u.a[v]);
        
    //     kv_push(uint64_t, occ_sort, (uint64_t)((uint32_t)(-1) - (uint32_t)(u->n)) << 32 | (v));
    // }
    // radix_sort_arch64(occ_sort.a, occ_sort.a + occ_sort.n);
    // for (i = 0; i < occ_sort.n; ++i) 
    // {
    //     fprintf(stderr, "-utg%.6ul, n=%u\n", ((uint32_t)occ_sort.a[i])+1, 
    //                                         (uint32_t)(-1) - (uint32_t)(occ_sort.a[i]>>32));
    // }
    // kv_destroy(occ_sort);
    /*******************************for debug************************************/

    clean_sg_by_utg(sg, ug);

    free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a);
    ma_ug_destroy(ug); free(bs_flag); free(r_flag);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rcx, 0x90(%rsp)
movq %rdx, %r12
movq %rsi, %rbp
movq %rdi, %r14
callq 0xb10cd
movq %rax, 0x68(%rsp)
movq 0x18(%rax), %rbx
movl 0x14(%rbx), %r15d
addl %r15d, %r15d
xorps %xmm0, %xmm0
leaq 0xb0(%rsp), %r13
movups %xmm0, 0x58(%r13)
movups %xmm0, 0x48(%r13)
movups %xmm0, 0x38(%r13)
movups %xmm0, 0x28(%r13)
movups %xmm0, 0x18(%r13)
movups %xmm0, 0x8(%r13)
movl $0x1c, %esi
movq %r15, %rdi
callq 0x2120
movq %rax, (%r13)
movl $0x1, %esi
movq %r15, 0x58(%rsp)
movq %r15, %rdi
callq 0x2120
movq %rax, 0x60(%rsp)
movq %r14, 0x70(%rsp)
movl 0x14(%r14), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
movq %rax, %r14
movq %rbx, %rdi
movq %r13, %rsi
callq 0xc0162
movq %rax, 0x78(%rsp)
movl 0x14(%rbx), %ecx
testl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
je 0x103c4a
movq 0x20(%rbx), %rax
movl $0x2, %edx
xorl %esi, %esi
cmpl $0x0, (%rax,%rsi,8)
js 0x103c36
movb $0x0, 0x4(%rax,%rsi,8)
movq 0x68(%rsp), %rcx
movq 0x10(%rcx), %rcx
movl (%rcx,%rdx,8), %edi
movl %edi, -0xc(%rcx,%rdx,8)
movl 0x14(%rbx), %ecx
incq %rsi
movl %ecx, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq $0x5, %rdx
cmpq %rdi, %rsi
jb 0x103c18
movq 0x58(%rsp), %r9
testq %r9, %r9
movq 0x68(%rsp), %r15
movq 0x60(%rsp), %r13
je 0x10410b
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
xorl %edx, %edx
xorl %r11d, %r11d
movq 0x18(%r15), %rdi
movq 0x20(%rdi), %rax
movl %edx, %ebx
shrl %ebx
cmpl $0x0, (%rax,%rbx,8)
js 0x1040e9
movq 0x28(%rdi), %rax
movq (%rax,%rdx,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x1040e9
movq %rax, %rcx
andq %r10, %rcx
je 0x1040e9
movq %rdx, 0x50(%rsp)
movq 0x8(%rdi), %rdx
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rax
addq $0xc, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl (%rax,%rdx), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0x103cc7
cmpl $0x2, %esi
jl 0x1040e4
movq 0x50(%rsp), %rdx
movzbl (%r13,%rdx), %eax
cmpl $0x1, %eax
je 0x1040e9
movl %r11d, 0x4c(%rsp)
testl %eax, %eax
jne 0x103d0a
movb $0x1, (%r13,%rdx)
xorl %eax, %eax
movq %rax, 0x38(%rsp)
movl %eax, 0x30(%rsp)
movl %eax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl %eax, 0x8(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq %r15, %rsi
movq %rdx, %r13
movq 0x78(%rsp), %rcx
leaq 0xb0(%rsp), %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xc02d6
testq %rax, %rax
je 0x103f4b
movb $0x2, %cl
movq 0x60(%rsp), %rdx
movb %cl, (%rdx,%r13)
movq 0xc8(%rsp), %rax
movl (%rax), %eax
movq %rax, %r11
xorq $0x1, %rax
movb %cl, (%rdx,%rax)
movq 0xe8(%rsp), %r10
movq 0x10(%r15), %r9
testq %r10, %r10
je 0x103dce
movq 0xf8(%rsp), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
shrl %edx
leaq (%rdx,%rdx,4), %rsi
movl 0x10(%r9,%rsi,8), %edx
testq %rdx, %rdx
je 0x103dc4
leaq (%r9,%rsi,8), %rsi
movq 0x18(%rsi), %rsi
xorl %edi, %edi
movq (%rsi,%rdi,8), %r8
shrq $0x21, %r8
movb $0x1, (%r14,%r8)
incq %rdi
cmpq %rdi, %rdx
jne 0x103daf
incq %rcx
movl %ecx, %edx
cmpq %rdx, %r10
ja 0x103d92
leaq (%rbx,%rbx,4), %rdi
movl 0x10(%r9,%rdi,8), %eax
testq %rax, %rax
je 0x103dfb
leaq (%r9,%rdi,8), %rcx
movq 0x18(%rcx), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
shrq $0x21, %rsi
movb $0x1, (%r14,%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x103de6
shrl %r11d
movq %r11, 0xa0(%rsp)
leaq (%r11,%r11,4), %rcx
movl 0x10(%r9,%rcx,8), %eax
testq %rax, %rax
je 0x103e33
leaq (%r9,%rcx,8), %rcx
movq 0x18(%rcx), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
shrq $0x21, %rsi
movb $0x1, (%r14,%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x103e1e
movq %rdi, 0x98(%rsp)
movq %r9, 0x80(%rsp)
movq %r10, 0x88(%rsp)
testq %r10, %r10
je 0x103f68
movq 0xf8(%rsp), %rax
movq %rax, 0xa8(%rsp)
xorl %edx, %edx
xorl %r13d, %r13d
movq 0xa8(%rsp), %rax
movl (%rax,%rdx,4), %eax
shrl %eax
cmpl %ebx, %eax
je 0x103f36
cmpl 0xa0(%rsp), %eax
je 0x103f36
leaq (%rax,%rax,4), %rax
movq 0x80(%rsp), %rcx
movl 0x10(%rcx,%rax,8), %esi
testq %rsi, %rsi
je 0x103f36
leaq (%rcx,%rax,8), %rax
movq 0x18(%rax), %rdi
xorl %r8d, %r8d
movq (%rdi,%r8,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rcx
movl 0xc(%rbp,%rcx,8), %eax
testq %rax, %rax
je 0x103f2a
leaq (,%rcx,8), %rcx
addq %rbp, %rcx
movq (%rcx), %r9
movq 0x70(%rsp), %rcx
movq 0x20(%rcx), %r10
shlq $0x3, %rax
leaq (%rax,%rax,4), %r11
xorl %eax, %eax
movl 0xc(%r9,%rax), %ecx
cmpl $0x0, (%r10,%rcx,8)
js 0x103f06
cmpb $0x0, (%r14,%rcx)
je 0x103f21
movl 0x8(%r9,%rax), %ecx
subl (%r9,%rax), %ecx
addq %rcx, %r13
jmp 0x103f21
cmpq %rcx, (%r12)
jbe 0x103f21
movq 0x8(%r12), %r15
movslq (%r15,%rcx,4), %rcx
testq %rcx, %rcx
js 0x103f21
cmpl $0x0, (%r10,%rcx,8)
jns 0x103ef1
addq $0x28, %rax
cmpq %rax, %r11
jne 0x103ee5
incq %r8
cmpq %rsi, %r8
jne 0x103eaf
incq %rdx
movl %edx, %eax
cmpq %rax, 0x88(%rsp)
ja 0x103e69
jmp 0x103f6b
movq 0x58(%rsp), %r9
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movq %r13, %rdx
movl 0x4c(%rsp), %r11d
movq 0x60(%rsp), %r13
jmp 0x1040e9
xorl %r13d, %r13d
movq 0x70(%rsp), %rdi
movq 0x68(%rsp), %r15
movq %r15, %rsi
movq 0x50(%rsp), %rdx
leaq 0xb0(%rsp), %rbx
movq %rbx, %rcx
callq 0x103aca
movq %rax, %rcx
movq %r13, %rax
xorl %edx, %edx
divq %rcx
cmpq 0x90(%rsp), %rax
jbe 0x103fc4
movq 0x58(%rsp), %r9
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movl 0x4c(%rsp), %r11d
movq 0x80(%rsp), %rbx
movq 0x88(%rsp), %r13
jmp 0x10402d
movq 0x18(%r15), %rdi
xorl %eax, %eax
movq %rax, 0x38(%rsp)
movl %eax, 0x30(%rsp)
movl %eax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movl $0x1, 0x8(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq %r15, %rsi
movq 0x50(%rsp), %rdx
movq 0x78(%rsp), %rcx
movq %rbx, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xc02d6
movl 0x4c(%rsp), %r11d
incl %r11d
movq 0xe8(%rsp), %r13
movq 0x10(%r15), %rbx
movq 0x58(%rsp), %r9
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
testq %r13, %r13
je 0x104077
movq 0xf8(%rsp), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
shrl %edx
leaq (%rdx,%rdx,4), %rsi
movl 0x10(%rbx,%rsi,8), %edx
testq %rdx, %rdx
je 0x10406d
leaq (%rbx,%rsi,8), %rsi
movq 0x18(%rsi), %rsi
xorl %edi, %edi
movq (%rsi,%rdi,8), %r8
shrq $0x21, %r8
movb $0x0, (%r14,%r8)
incq %rdi
cmpq %rdi, %rdx
jne 0x104058
incq %rcx
movl %ecx, %edx
cmpq %rdx, %r13
ja 0x10403c
movq 0x98(%rsp), %rcx
movl 0x10(%rbx,%rcx,8), %eax
testq %rax, %rax
je 0x1040a7
leaq (%rbx,%rcx,8), %rcx
movq 0x18(%rcx), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
shrq $0x21, %rsi
movb $0x0, (%r14,%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x104092
movq 0xc8(%rsp), %rax
movl (%rax), %eax
shrl %eax
leaq (%rax,%rax,4), %rcx
movl 0x10(%rbx,%rcx,8), %eax
testq %rax, %rax
movq 0x60(%rsp), %r13
je 0x1040e4
leaq (%rbx,%rcx,8), %rcx
movq 0x18(%rcx), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
shrq $0x21, %rsi
movb $0x0, (%r14,%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x1040cf
movq 0x50(%rsp), %rdx
incq %rdx
cmpq %r9, %rdx
jne 0x103c6d
testl %r11d, %r11d
je 0x10410b
xorl %edx, %edx
movl $0x0, %r11d
testl %r9d, %r9d
jne 0x103c6d
movq 0x70(%rsp), %rdi
movq %r15, %rsi
callq 0x102e41
movq 0xb0(%rsp), %rdi
callq 0x2220
movq 0xc8(%rsp), %rdi
callq 0x2220
movq 0xe0(%rsp), %rdi
callq 0x2220
movq 0xf8(%rsp), %rdi
callq 0x2220
movq 0x110(%rsp), %rdi
callq 0x2220
movq %r15, %rdi
callq 0xa0761
movq %r13, %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq