// File: /workspace/github_cpp_makefile_star2_O2/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, 0x80(%rsp)
movq %rdx, %r13
movq %rsi, 0xa8(%rsp)
movq %rdi, %r12
callq 0x76d91
movq %rax, 0x60(%rsp)
movq 0x18(%rax), %rbx
movl 0x14(%rbx), %ebp
addl %ebp, %ebp
xorps %xmm0, %xmm0
leaq 0xb0(%rsp), %r15
movups %xmm0, 0x58(%r15)
movups %xmm0, 0x48(%r15)
movups %xmm0, 0x38(%r15)
movups %xmm0, 0x28(%r15)
movups %xmm0, 0x18(%r15)
movups %xmm0, 0x8(%r15)
pushq $0x1c
popq %rsi
movq %rbp, %rdi
callq 0x2120
movq %rax, (%r15)
pushq $0x1
popq %r14
movq %rbp, 0x90(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2120
movq %rax, 0x48(%rsp)
movl 0x14(%r12), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
movq %r14, %rsi
callq 0x2120
movq %rax, %r14
movq %rbx, %rdi
movq %r15, %rsi
callq 0x84975
movq %rax, 0x70(%rsp)
pushq $0x2
popq %rax
xorl %ecx, %ecx
movl 0x14(%rbx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %rcx
jae 0xc0ac7
movq 0x20(%rbx), %rdx
cmpl $0x0, (%rdx,%rcx,8)
js 0xc0abe
movb $0x0, 0x4(%rdx,%rcx,8)
movq 0x60(%rsp), %rdx
movq 0x10(%rdx), %rdx
movl (%rdx,%rax,8), %esi
movl %esi, -0xc(%rdx,%rax,8)
incq %rcx
addq $0x5, %rax
jmp 0xc0a91
pushq $0x1
popq %rax
movl %eax, 0x44(%rsp)
movq 0x60(%rsp), %rbx
cmpl $0x0, 0x44(%rsp)
je 0xc0ec4
xorl %r15d, %r15d
movl $0x0, 0x44(%rsp)
cmpq 0x90(%rsp), %r15
je 0xc0ad3
movq 0x18(%rbx), %rdi
movq 0x20(%rdi), %rax
movl %r15d, %edx
shrl %edx
cmpl $0x0, (%rax,%rdx,8)
js 0xc0ebc
movq 0x28(%rdi), %rax
testl $0xfffffffe, (%rax,%r15,8) # imm = 0xFFFFFFFE
je 0xc0ebc
movq %rdx, 0x78(%rsp)
movl %r15d, %esi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x2, %eax
jl 0xc0ebc
movq 0x48(%rsp), %rax
movzbl (%rax,%r15), %eax
cmpl $0x1, %eax
je 0xc0ebc
testl %eax, %eax
jne 0xc0b55
movq 0x48(%rsp), %rax
movb $0x1, (%rax,%r15)
movq 0x18(%rbx), %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
andq $0x0, 0x38(%rsp)
andl $0x0, 0x30(%rsp)
andl $0x0, 0x28(%rsp)
andq $0x0, 0x20(%rsp)
andl $0x0, 0x8(%rsp)
orl $-0x1, (%rsp)
movq %rbx, %rsi
movl %r15d, %edx
movq 0x70(%rsp), %rcx
leaq 0xb0(%rsp), %r8
pushq $-0x1
popq %r9
callq 0x84a86
testq %rax, %rax
je 0xc0ebc
movq 0x48(%rsp), %rcx
movb $0x2, %dl
movq %r15, 0x68(%rsp)
movb %dl, (%rcx,%r15)
movq 0xc8(%rsp), %rax
movl (%rax), %eax
movq %rax, %r9
xorq $0x1, %rax
movb %dl, (%rcx,%rax)
movq 0xe8(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x10(%rbx), %r10
xorl %edx, %edx
movq 0x78(%rsp), %r8
movl %edx, %edx
cmpq %rdx, 0x58(%rsp)
jbe 0xc0c2f
movq 0x50(%rsp), %rax
movl (%rax,%rdx,4), %eax
shrl %eax
imulq $0x28, %rax, %rcx
leaq (%r10,%rcx), %rax
movl 0x10(%r10,%rcx), %esi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0xc0c2b
movq 0x18(%rax), %rcx
movq (%rcx,%rdi,8), %rcx
shrq $0x21, %rcx
movb $0x1, (%r14,%rcx)
incq %rdi
jmp 0xc0c10
incl %edx
jmp 0xc0bee
imulq $0x28, %r8, %rcx
leaq (%r10,%rcx), %rax
movq %rcx, 0x88(%rsp)
movl 0x10(%r10,%rcx), %edx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0xc0c61
movq 0x18(%rax), %rcx
movq (%rcx,%rsi,8), %rcx
shrq $0x21, %rcx
movb $0x1, (%r14,%rcx)
incq %rsi
jmp 0xc0c46
shrl %r9d
movq %r9, 0xa0(%rsp)
imulq $0x28, %r9, %rcx
leaq (%r10,%rcx), %rax
movl 0x10(%r10,%rcx), %edx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0xc0c96
movq 0x18(%rax), %rcx
movq (%rcx,%rsi,8), %rcx
shrq $0x21, %rcx
movb $0x1, (%r14,%rcx)
incq %rsi
jmp 0xc0c7b
movq %r10, 0x98(%rsp)
xorl %ebx, %ebx
xorl %edx, %edx
movl %edx, %edx
cmpq %rdx, 0x58(%rsp)
jbe 0xc0d72
movq 0x50(%rsp), %rax
movl (%rax,%rdx,4), %eax
shrl %eax
cmpl %r8d, %eax
je 0xc0d6b
cmpl 0xa0(%rsp), %eax
je 0xc0d6b
imulq $0x28, %rax, %rax
movq 0x98(%rsp), %rcx
leaq (%rcx,%rax), %rsi
movl 0x10(%rcx,%rax), %edi
xorl %r8d, %r8d
cmpq %rdi, %r8
je 0xc0d66
movq 0x18(%rsi), %rax
movq (%rax,%r8,8), %rax
shrq $0x21, %rax
imulq $0x18, %rax, %rax
movq 0xa8(%rsp), %rcx
leaq (%rcx,%rax), %r9
movl 0xc(%rcx,%rax), %eax
imulq $0x28, %rax, %r10
xorl %r11d, %r11d
cmpq %r11, %r10
je 0xc0d61
movq (%r9), %rax
movl 0xc(%rax,%r11), %r15d
movq 0x20(%r12), %rcx
cmpl $0x0, (%rcx,%r15,8)
js 0xc0d40
cmpb $0x0, (%r14,%r15)
je 0xc0d5b
movl 0x8(%rax,%r11), %ecx
subl (%rax,%r11), %ecx
addq %rcx, %rbx
jmp 0xc0d5b
cmpq %r15, (%r13)
jbe 0xc0d5b
movq 0x8(%r13), %rbp
movslq (%rbp,%r15,4), %r15
testq %r15, %r15
js 0xc0d5b
cmpl $0x0, (%rcx,%r15,8)
jns 0xc0d2b
addq $0x28, %r11
jmp 0xc0d12
incq %r8
jmp 0xc0ce6
movq 0x78(%rsp), %r8
incl %edx
jmp 0xc0ca2
movq %r12, %rdi
movq 0x60(%rsp), %r15
movq %r15, %rsi
movq 0x68(%rsp), %rdx
leaq 0xb0(%rsp), %rbp
movq %rbp, %rcx
callq 0xc0978
movq %rax, %rcx
movq %rbx, %rax
xorl %edx, %edx
divq %rcx
cmpq 0x80(%rsp), %rax
ja 0xc0e0c
movq 0x18(%r15), %rdi
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
andq $0x0, 0x38(%rsp)
andl $0x0, 0x30(%rsp)
andl $0x0, 0x28(%rsp)
andq $0x0, 0x20(%rsp)
orl $-0x1, (%rsp)
movl $0x1, 0x8(%rsp)
movq %r15, %rsi
movq 0x68(%rsp), %rdx
movq 0x70(%rsp), %rcx
movq %rbp, %r8
pushq $-0x1
popq %r9
callq 0x84a86
incl 0x44(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq %r15, %rbx
movq 0x10(%r15), %rax
xorl %ecx, %ecx
movl %ecx, %ecx
cmpq %rcx, 0x58(%rsp)
jbe 0xc0e55
movq 0x50(%rsp), %rdx
movl (%rdx,%rcx,4), %edx
shrl %edx
imulq $0x28, %rdx, %rsi
leaq (%rax,%rsi), %rdx
movl 0x10(%rax,%rsi), %esi
xorl %edi, %edi
cmpq %rdi, %rsi
je 0xc0e51
movq 0x18(%rdx), %r8
movq (%r8,%rdi,8), %r8
shrq $0x21, %r8
movb $0x0, (%r14,%r8)
incq %rdi
jmp 0xc0e36
incl %ecx
jmp 0xc0e15
movq 0x88(%rsp), %rdx
leaq (%rax,%rdx), %rcx
movl 0x10(%rax,%rdx), %edx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0xc0e82
movq 0x18(%rcx), %rdi
movq (%rdi,%rsi,8), %rdi
shrq $0x21, %rdi
movb $0x0, (%r14,%rdi)
incq %rsi
jmp 0xc0e67
movq 0xc8(%rsp), %rcx
movl (%rcx), %ecx
shrl %ecx
imulq $0x28, %rcx, %rdx
leaq (%rax,%rdx), %rcx
movl 0x10(%rax,%rdx), %eax
xorl %edx, %edx
cmpq %rdx, %rax
je 0xc0eb7
movq 0x18(%rcx), %rsi
movq (%rsi,%rdx,8), %rsi
shrq $0x21, %rsi
movb $0x0, (%r14,%rsi)
incq %rdx
jmp 0xc0e9c
movq 0x68(%rsp), %r15
incq %r15
jmp 0xc0ae9
movq %r12, %rdi
movq %rbx, %rsi
callq 0xc005e
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 %rbx, %rdi
callq 0x6921d
movq 0x48(%rsp), %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