// File: /workspace/github_cpp_makefile_star2_O1/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 $0x128, %rsp            # imm = 0x128
movq %rcx, 0x98(%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r14
callq 0xb0285
movq %rax, 0x80(%rsp)
movq 0x18(%rax), %rbx
movl 0x14(%rbx), %r15d
addl %r15d, %r15d
xorps %xmm0, %xmm0
leaq 0xc0(%rsp), %rbp
movaps %xmm0, 0x50(%rbp)
movaps %xmm0, 0x40(%rbp)
movaps %xmm0, 0x30(%rbp)
movaps %xmm0, 0x20(%rbp)
movaps %xmm0, 0x10(%rbp)
movaps %xmm0, (%rbp)
movq $0x0, 0x60(%rbp)
movl $0x1c, %esi
movq %r15, %rdi
callq 0x2120
movq %rax, (%rbp)
movl $0x1, %esi
movq %r15, 0x68(%rsp)
movq %r15, %rdi
callq 0x2120
movq %rax, 0x58(%rsp)
movq %r14, 0x78(%rsp)
movl 0x14(%r14), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movl $0x1, %esi
callq 0x2120
movq %rax, %r14
movq %rbx, %rdi
movq 0x80(%rsp), %rbx
movq %rbp, %rsi
callq 0xbf22d
movq %rax, 0x88(%rsp)
movq 0x18(%rbx), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
je 0xff8b4
movq 0x20(%rax), %rcx
movl $0x2, %edx
xorl %esi, %esi
cmpl $0x0, (%rcx,%rsi,8)
js 0xff89f
movb $0x0, 0x4(%rcx,%rsi,8)
movq 0x10(%rbx), %rdi
movl (%rdi,%rdx,8), %r8d
movl %r8d, -0xc(%rdi,%rdx,8)
incq %rsi
movl 0x14(%rax), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq $0x5, %rdx
cmpq %rdi, %rsi
jb 0xff887
cmpl $0x0, 0x68(%rsp)
je 0xffdae
movl 0x68(%rsp), %r11d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
leaq 0xc0(%rsp), %r15
movq %r11, 0x70(%rsp)
xorl %ebp, %ebp
movl $0x0, 0x54(%rsp)
movq 0x18(%rbx), %rdi
movq 0x20(%rdi), %rax
movl %ebp, %ecx
shrl %ecx
movq %rcx, 0x90(%rsp)
cmpl $0x0, (%rax,%rcx,8)
js 0xffd90
movq 0x28(%rdi), %rax
movq (%rax,%rbp,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xffd90
movq %rax, %rcx
andq %r9, %rcx
je 0xffd90
movq %rbp, 0x48(%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 0xff943
cmpl $0x2, %esi
jl 0xffd8b
movq 0x58(%rsp), %rax
movq 0x48(%rsp), %rbp
movzbl (%rax,%rbp), %eax
cmpl $0x1, %eax
je 0xffd90
testl %eax, %eax
jne 0xff987
movq 0x58(%rsp), %rax
movb $0x1, (%rax,%rbp)
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 %rbx, %rsi
movl %ebp, %edx
movq 0x88(%rsp), %rcx
movq %r15, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xbf39f
testq %rax, %rax
je 0xffc14
movq 0x58(%rsp), %rcx
movb $0x2, %dl
movb %dl, (%rcx,%rbp)
movq 0xd8(%rsp), %rax
movl (%rax), %eax
movq %rax, %r10
xorq $0x1, %rax
movb %dl, (%rcx,%rax)
movq 0xf8(%rsp), %r11
testq %r11, %r11
je 0xffa47
movq 0x10(%rbx), %rax
movq 0x108(%rsp), %rcx
xorl %edx, %edx
movl (%rcx,%rdx,4), %esi
shrl %esi
leaq (%rsi,%rsi,4), %rdi
movl 0x10(%rax,%rdi,8), %esi
testq %rsi, %rsi
je 0xffa3d
leaq (%rax,%rdi,8), %rdi
movq 0x18(%rdi), %rdi
xorl %r8d, %r8d
movq (%rdi,%r8,8), %r9
shrq $0x21, %r9
movb $0x1, (%r14,%r9)
incq %r8
cmpq %r8, %rsi
jne 0xffa28
incq %rdx
movl %edx, %esi
cmpq %rsi, %r11
ja 0xffa0b
movq 0x10(%rbx), %rcx
movq 0x90(%rsp), %rax
leaq (%rax,%rax,4), %rdi
movq %rcx, 0x60(%rsp)
movl 0x10(%rcx,%rdi,8), %eax
testq %rax, %rax
je 0xffa89
movq 0x60(%rsp), %rcx
leaq (%rcx,%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 0xffa74
shrl %r10d
movq %r10, 0xa8(%rsp)
leaq (%r10,%r10,4), %rcx
movq 0x60(%rsp), %rax
movl 0x10(%rax,%rcx,8), %eax
testq %rax, %rax
je 0xffaca
movq 0x60(%rsp), %rdx
leaq (%rdx,%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 0xffab5
testq %r11, %r11
movq %rdi, 0xa0(%rsp)
je 0xffc24
movq 0x108(%rsp), %rax
movq %rax, 0xb0(%rsp)
xorl %edi, %edi
xorl %ebx, %ebx
movq %r11, 0xb8(%rsp)
movq 0xb0(%rsp), %rax
movl (%rax,%rdi,4), %eax
shrl %eax
cmpl 0x90(%rsp), %eax
je 0xffbff
cmpl 0xa8(%rsp), %eax
je 0xffbff
leaq (%rax,%rax,4), %rax
movq 0x60(%rsp), %rcx
movl 0x10(%rcx,%rax,8), %r8d
testq %r8, %r8
je 0xffbff
leaq (%rcx,%rax,8), %rax
movq 0x18(%rax), %r9
xorl %r10d, %r10d
movq (%r9,%r10,8), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movl 0xc(%r13,%rax,8), %ecx
testq %rcx, %rcx
je 0xffbf3
leaq (,%rax,8), %rax
addq %r13, %rax
movq (%rax), %r11
movq 0x78(%rsp), %rax
movq 0x20(%rax), %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,4), %rdx
xorl %esi, %esi
movl 0xc(%r11,%rsi), %r15d
cmpl $0x0, (%rax,%r15,8)
movl %r15d, %ecx
js 0xffba7
movl %ecx, %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xffbea
cmpb $0x0, (%r14,%rcx)
je 0xffbea
movl 0x8(%r11,%rsi), %ecx
subl (%r11,%rsi), %ecx
addq %rcx, %rbx
jmp 0xffbea
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %r15, (%r12)
jbe 0xffbd1
movq 0x8(%r12), %rbp
movl (%rbp,%r15,4), %r15d
cmpl $-0x1, %r15d
je 0xffbd1
movl %r15d, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
shrl $0x1f, %r15d
jmp 0xffbd4
xorl %r15d, %r15d
cmpl $-0x1, %ecx
sete %bpl
orb %r15b, %bpl
jne 0xffbea
movl %ecx, %r15d
cmpl $0x0, (%rax,%r15,8)
jns 0xffb8a
addq $0x28, %rsi
cmpq %rsi, %rdx
jne 0xffb7b
incq %r10
cmpq %r8, %r10
jne 0xffb40
incq %rdi
movl %edi, %eax
cmpq %rax, 0xb8(%rsp)
ja 0xffaf7
jmp 0xffc26
movq 0x70(%rsp), %r11
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
jmp 0xffd90
xorl %ebx, %ebx
movq 0x78(%rsp), %rdi
movq 0x80(%rsp), %r15
movq %r15, %rsi
movq 0x48(%rsp), %rdx
leaq 0xc0(%rsp), %rbp
movq %rbp, %rcx
callq 0xff727
movq %rax, %rcx
movq %rbx, %rax
xorl %edx, %edx
divq %rcx
cmpq 0x98(%rsp), %rax
ja 0xffcb1
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 0x48(%rsp), %rdx
movq 0x88(%rsp), %rcx
movq %rbp, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xbf39f
incl 0x54(%rsp)
movq %r15, %rbx
movq 0x70(%rsp), %r11
movq 0xf8(%rsp), %rax
testq %rax, %rax
leaq 0xc0(%rsp), %r15
je 0xffd19
movq 0x10(%rbx), %rcx
movq 0x108(%rsp), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
shrl %edi
leaq (%rdi,%rdi,4), %r8
movl 0x10(%rcx,%r8,8), %edi
testq %rdi, %rdi
je 0xffd0f
leaq (%rcx,%r8,8), %r8
movq 0x18(%r8), %r8
xorl %r9d, %r9d
movq (%r8,%r9,8), %r10
shrq $0x21, %r10
movb $0x0, (%r14,%r10)
incq %r9
cmpq %r9, %rdi
jne 0xffcfa
incq %rsi
movl %esi, %edi
cmpq %rdi, %rax
ja 0xffcdc
movq 0x10(%rbx), %rax
movq 0xa0(%rsp), %rdx
movl 0x10(%rax,%rdx,8), %ecx
testq %rcx, %rcx
je 0xffd4d
leaq (%rax,%rdx,8), %rdx
movq 0x18(%rdx), %rdx
xorl %esi, %esi
movq (%rdx,%rsi,8), %rdi
shrq $0x21, %rdi
movb $0x0, (%r14,%rdi)
incq %rsi
cmpq %rsi, %rcx
jne 0xffd38
movq 0xd8(%rsp), %rcx
movl (%rcx), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rdx
movl 0x10(%rax,%rdx,8), %ecx
testq %rcx, %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
je 0xffd8b
leaq (%rax,%rdx,8), %rax
movq 0x18(%rax), %rax
xorl %edx, %edx
movq (%rax,%rdx,8), %rsi
shrq $0x21, %rsi
movb $0x0, (%r14,%rsi)
incq %rdx
cmpq %rdx, %rcx
jne 0xffd76
movq 0x48(%rsp), %rbp
incq %rbp
cmpq %r11, %rbp
jne 0xff8e1
cmpl $0x0, 0x54(%rsp)
je 0xffdae
cmpl $0x0, 0x68(%rsp)
jne 0xff8d7
movq 0x78(%rsp), %rdi
movq %rbx, %rsi
callq 0xfeae9
movq 0xc0(%rsp), %rdi
callq 0x2220
movq 0xd8(%rsp), %rdi
callq 0x2220
movq 0xf0(%rsp), %rdi
callq 0x2220
movq 0x108(%rsp), %rdi
callq 0x2220
movq 0x120(%rsp), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0xa0004
movq 0x58(%rsp), %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq