// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: flat_bubbles(asg_t*, unsigned char*)
void flat_bubbles(asg_t *sg, uint8_t* r_het)
{
    ma_ug_t *ug = NULL;
    ug = ma_ug_gen(sg);
    ma_utg_t *u = NULL; 
    uint32_t n_vtx = ug->g->n_seq<<1, v, convex, i, k, ori, is_het_b, is_het_s, n_pop = 0, pass_b, pass_s;
    long long nodeLen, baseLen, max_stop_nodeLen, max_stop_baseLen;
    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);
    uint64_t tLen = get_bub_pop_max_dist_advance(ug->g, &b), path, hom_occ, het_occ;
    
    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))
            {
                //note b.b include end, does not include beg
                for (i = path = 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;
                    }
                    path += ug->u.a[b.b.a[i]>>1].n;
                }



                bs_flag[v] = 2; bs_flag[b.S.a[0]^1] = 2;
                is_het_b = is_het_s = pass_b = pass_s = 0;
                //beg is v, end is b.S.a[0]
                b.b.n = 0;
                get_unitig(ug->g, NULL, v^1, &convex, &nodeLen, &baseLen, &max_stop_nodeLen, 
                                                                        &max_stop_baseLen, 1, &b);
                
                
                for (i = hom_occ = het_occ = 0; i < b.b.n; i++)
                {
                    u = &(ug->u.a[b.b.a[i]>>1]);
                    ori = b.b.a[i]&1;
                    for (k = 0; k < u->n; k++)
                    {
                        if(r_het[(ori == 1?(u->a[u->n-k-1]>>33):(u->a[k]>>33))] == N_HET)
                        {
                            hom_occ++;
                        }
                        else
                        {
                            het_occ++;
                        }
                        if(het_occ > ((het_occ+hom_occ)*0.85))
                        {
                            is_het_b = (het_occ+hom_occ);
                        }

                        if((het_occ+hom_occ) == MAX((path+1),5))
                        {
                            if(het_occ > ((het_occ+hom_occ)*0.7))
                            {
                                pass_b = 1;
                            }
                        }
                    }
                }
                if(pass_b == 0) continue;
                

                b.b.n = 0;
                get_unitig(ug->g, NULL, b.S.a[0], &convex, &nodeLen, &baseLen, &max_stop_nodeLen, 
                                                                        &max_stop_baseLen, 1, &b);
                for (i = hom_occ = het_occ= 0; i < b.b.n; i++)
                {
                    u = &(ug->u.a[b.b.a[i]>>1]);
                    ori = b.b.a[i]&1;
                    for (k = 0; k < u->n; k++)
                    {
                        if(r_het[(ori == 1?(u->a[u->n-k-1]>>33):(u->a[k]>>33))] == N_HET)
                        {
                            hom_occ++;
                        }
                        else
                        {
                            het_occ++;
                        }
                        if(het_occ > ((het_occ+hom_occ)*0.85))
                        {
                            is_het_s = (het_occ+hom_occ);
                        }

                        if((het_occ+hom_occ) == MAX((path+1),5))
                        {
                            if(het_occ > ((het_occ+hom_occ)*0.7))
                            {
                                pass_s = 1;
                            }
                        }
                    }
                }
                if(pass_s == 0) continue;


                if(is_het_b > path && is_het_s > path && (is_het_b+is_het_s)>(path<<2))
                {
                    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++;
                }
            }
        }
        ///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); 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %r12
movq %rdi, 0x60(%rsp)
callq 0xb10cd
movq %rax, %rbx
movq 0x18(%rax), %r15
movl 0x14(%r15), %ebp
addl %ebp, %ebp
xorpd %xmm0, %xmm0
leaq 0x90(%rsp), %r14
movupd %xmm0, 0x58(%r14)
movupd %xmm0, 0x48(%r14)
movupd %xmm0, 0x38(%r14)
movupd %xmm0, 0x28(%r14)
movupd %xmm0, 0x18(%r14)
movupd %xmm0, 0x8(%r14)
movl $0x1c, %esi
movq %rbp, %rdi
callq 0x2120
movq %rax, (%r14)
movl $0x1, %esi
movq %rbp, %rdi
callq 0x2120
movq %rax, %r13
movq %r15, %rdi
movq %r14, %rsi
callq 0xc0162
movq %rax, 0x50(%rsp)
movl 0x14(%r15), %ecx
testl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
je 0x103420
movq 0x20(%r15), %rax
movl $0x2, %edx
xorl %esi, %esi
cmpl $0x0, (%rax,%rsi,8)
js 0x10340c
movb $0x0, 0x4(%rax,%rsi,8)
movq 0x10(%rbx), %rcx
movl (%rcx,%rdx,8), %edi
movl %edi, -0xc(%rcx,%rdx,8)
movl 0x14(%r15), %ecx
incq %rsi
movl %ecx, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq $0x5, %rdx
cmpq %rdi, %rsi
jb 0x1033f2
testq %rbp, %rbp
leaq 0x90(%rsp), %r15
je 0x103a5a
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %edx, %edx
xorl %r11d, %r11d
movq %rbx, 0x20(%rsp)
movq %r13, 0x18(%rsp)
movq %rbp, 0x10(%rsp)
movq 0x18(%rbx), %rdi
movq 0x20(%rdi), %rax
movl %edx, %r14d
shrl %r14d
cmpl $0x0, (%rax,%r14,8)
js 0x103a0e
movq 0x28(%rdi), %rax
movq (%rax,%rdx,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x103a0e
movq %rax, %rcx
andq %r9, %rcx
je 0x103a0e
movq 0x8(%rdi), %rsi
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rsi,%rax,8), %rax
addq $0xc, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %r10d, %r10d
xorl %esi, %esi
movl (%rax,%r10), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %esi
addq $0x18, %r10
cmpq %r10, %rcx
jne 0x1034a4
cmpl $0x2, %esi
jl 0x103a0e
movzbl (%r13,%rdx), %eax
cmpl $0x1, %eax
je 0x103a0e
movl %r11d, 0x4(%rsp)
testl %eax, %eax
jne 0x1034e2
movb $0x1, (%r13,%rdx)
subq $0x40, %rsp
xorl %eax, %eax
movq %rax, 0x38(%rsp)
movl %eax, 0x30(%rsp)
movl %eax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
movl %eax, 0x8(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq %rbx, %rsi
movq %rdx, 0x48(%rsp)
movq 0x90(%rsp), %rcx
movq %r15, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xc02d6
addq $0x40, %rsp
testq %rax, %rax
je 0x1039fe
movq 0xa8(%rsp), %rax
movq 0xc8(%rsp), %rcx
testq %rcx, %rcx
je 0x103592
movq 0xd8(%rsp), %rdx
xorl %esi, %esi
xorl %r10d, %r10d
movq 0x8(%rsp), %r9
movl (%rdx,%rsi,4), %edi
shrl %edi
cmpl %r14d, %edi
je 0x103586
movl (%rax), %r8d
shrl %r8d
cmpl %r8d, %edi
je 0x103586
movq 0x10(%rbx), %r8
leaq (%rdi,%rdi,4), %rdi
movl 0x10(%r8,%rdi,8), %edi
addq %rdi, %r10
incq %rsi
movl %esi, %edi
cmpq %rdi, %rcx
ja 0x103561
jmp 0x10359a
xorl %r10d, %r10d
movq 0x8(%rsp), %r9
movq %r10, 0x48(%rsp)
movb $0x2, %cl
movb %cl, (%r13,%r9)
movl (%rax), %eax
xorq $0x1, %rax
movb %cl, (%r13,%rax)
movq $0x0, 0xc8(%rsp)
movq 0x18(%rbx), %rdi
movl %r9d, %edx
xorl $0x1, %edx
xorl %esi, %esi
leaq 0x44(%rsp), %rcx
leaq 0x88(%rsp), %r8
leaq 0x80(%rsp), %r9
pushq %r15
pushq $0x1
leaq 0x80(%rsp), %rax
pushq %rax
leaq 0x90(%rsp), %rax
pushq %rax
callq 0x1053a7
addq $0x20, %rsp
movq 0xc8(%rsp), %rax
movq %rax, 0x38(%rsp)
testq %rax, %rax
je 0x1039fe
movq 0x10(%rbx), %rcx
movq 0xd8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x48(%rsp), %rax
incq %rax
cmpq $0x6, %rax
movl $0x5, %esi
movq %rax, 0x58(%rsp)
cmovaeq %rax, %rsi
xorl %edi, %edi
xorl %r9d, %r9d
xorl %r10d, %r10d
xorl %r8d, %r8d
xorl %r15d, %r15d
movq 0x12dc2c(%rip), %xmm3   # 0x231280
movapd 0x12dc34(%rip), %xmm4   # 0x231290
movsd 0x13271c(%rip), %xmm5   # 0x235d80
movsd 0x12df34(%rip), %xmm6   # 0x2315a0
movq %rcx, 0x30(%rsp)
movq 0x28(%rsp), %rax
movl (%rax,%rdi,4), %r11d
movl %r11d, %eax
shrl %eax
leaq (%rax,%rax,4), %rax
movl 0x10(%rcx,%rax,8), %ebx
testq %rbx, %rbx
je 0x103748
leaq (%rcx,%rax,8), %rax
movq 0x18(%rax), %rbp
leal -0x1(%rbx), %edx
xorl %eax, %eax
movl %r15d, %r13d
movq %r10, %rcx
movq %r9, %r15
testb $0x1, %r11b
movl %edx, %r9d
cmoveq %rax, %r9
movq (%rbp,%r9,8), %r14
shrq $0x21, %r14
xorl %r9d, %r9d
xorl %r10d, %r10d
cmpb $0x0, (%r12,%r14)
setne %r9b
sete %r10b
addq %rcx, %r10
addq %r15, %r9
movq %r9, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm4, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
leaq (%r9,%r10), %rcx
movq %rcx, %xmm2
punpckldq %xmm3, %xmm2    # xmm2 = xmm2[0],xmm3[0],xmm2[1],xmm3[1]
subpd %xmm4, %xmm2
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addsd %xmm2, %xmm1
movapd %xmm1, %xmm2
mulsd %xmm5, %xmm2
ucomisd %xmm2, %xmm0
movl %ecx, %r15d
ja 0x10371d
movl %r13d, %r15d
mulsd %xmm6, %xmm1
ucomisd %xmm1, %xmm0
movl $0x1, %r13d
ja 0x103730
movl %r8d, %r13d
cmpq %rsi, %rcx
cmovel %r13d, %r8d
incq %rax
decl %edx
movl %r15d, %r13d
cmpq %rax, %rbx
jne 0x1036a0
incq %rdi
movl %edi, %eax
cmpq %rax, 0x38(%rsp)
movq 0x30(%rsp), %rcx
ja 0x103671
testl %r8d, %r8d
je 0x1039e7
movq $0x0, 0xc8(%rsp)
movq 0x20(%rsp), %rbx
movq 0x18(%rbx), %rdi
movq 0xa8(%rsp), %rax
movl (%rax), %edx
xorl %esi, %esi
leaq 0x44(%rsp), %rcx
leaq 0x88(%rsp), %r8
leaq 0x80(%rsp), %r9
leaq 0x90(%rsp), %rax
pushq %rax
pushq $0x1
leaq 0x80(%rsp), %rax
pushq %rax
leaq 0x90(%rsp), %rax
pushq %rax
callq 0x1053a7
addq $0x20, %rsp
movq 0xc8(%rsp), %rax
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x1039ec
movq 0x10(%rbx), %rdx
movq 0xd8(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x58(%rsp), %rsi
cmpq $0x6, %rsi
movl $0x5, %eax
cmovbq %rax, %rsi
xorl %r10d, %r10d
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %edi, %edi
xorl %ecx, %ecx
movq 0x12da70(%rip), %xmm3   # 0x231280
movapd 0x12da78(%rip), %xmm4   # 0x231290
movsd 0x132560(%rip), %xmm5   # 0x235d80
movsd 0x12dd78(%rip), %xmm6   # 0x2315a0
movq %rdx, 0x28(%rsp)
movq 0x68(%rsp), %rax
movq %r10, 0x38(%rsp)
movl (%rax,%r10,4), %r10d
movl %r10d, %eax
shrl %eax
leaq (%rax,%rax,4), %rax
movl 0x10(%rdx,%rax,8), %r11d
testq %r11, %r11
je 0x10390a
leaq (%rdx,%rax,8), %rax
movq 0x18(%rax), %rbp
leal -0x1(%r11), %edx
xorl %eax, %eax
movl %ecx, %r13d
movq %r9, %rcx
movq %r8, %rbx
testb $0x1, %r10b
movl %edx, %r8d
cmoveq %rax, %r8
movq (%rbp,%r8,8), %r14
shrq $0x21, %r14
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpb $0x0, (%r12,%r14)
setne %r8b
sete %r9b
addq %rcx, %r9
addq %rbx, %r8
movq %r8, %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm4, %xmm1
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
addsd %xmm1, %xmm0
leaq (%r8,%r9), %rbx
movq %rbx, %xmm2
punpckldq %xmm3, %xmm2    # xmm2 = xmm2[0],xmm3[0],xmm2[1],xmm3[1]
subpd %xmm4, %xmm2
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addsd %xmm2, %xmm1
movapd %xmm1, %xmm2
mulsd %xmm5, %xmm2
ucomisd %xmm2, %xmm0
movl %ebx, %ecx
ja 0x1038df
movl %r13d, %ecx
mulsd %xmm6, %xmm1
ucomisd %xmm1, %xmm0
movl $0x1, %r13d
ja 0x1038f2
movl %edi, %r13d
cmpq %rsi, %rbx
cmovel %r13d, %edi
incq %rax
decl %edx
movl %ecx, %r13d
cmpq %rax, %r11
jne 0x103863
movq 0x38(%rsp), %r10
incq %r10
movl %r10d, %eax
cmpq %rax, 0x30(%rsp)
movq 0x28(%rsp), %rdx
ja 0x10382d
testl %edi, %edi
movq 0x8(%rsp), %rdx
movl 0x4(%rsp), %r11d
movq 0x48(%rsp), %rsi
je 0x103a3b
movl %r15d, %eax
cmpq %rax, %rsi
movq 0x20(%rsp), %rbx
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %rbp
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
jae 0x103a31
movl %ecx, %eax
cmpq %rax, %rsi
jae 0x103a31
addl %r15d, %ecx
shlq $0x2, %rsi
cmpq %rcx, %rsi
jae 0x103a31
movq 0x18(%rbx), %rdi
subq $0x40, %rsp
xorl %eax, %eax
movq %rax, 0x38(%rsp)
movl %eax, 0x30(%rsp)
movl %eax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rsp)
movl $0x1, 0x8(%rsp)
movl $0xffffffff, (%rsp)     # imm = 0xFFFFFFFF
movq %rbx, %rsi
movq 0x90(%rsp), %rcx
leaq 0xd0(%rsp), %r15
movq %r15, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xc02d6
movl 0x44(%rsp), %r11d
movq 0x48(%rsp), %rdx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
addq $0x40, %rsp
incl %r11d
jmp 0x103a0e
movq 0x20(%rsp), %rbx
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %rbp
leaq 0x90(%rsp), %r15
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rdx
movl 0x4(%rsp), %r11d
incq %rdx
cmpq %rbp, %rdx
jne 0x10344b
testl %r11d, %r11d
je 0x103a5a
xorl %edx, %edx
movl $0x0, %r11d
testl %ebp, %ebp
jne 0x10344b
jmp 0x103a5a
leaq 0x90(%rsp), %r15
jmp 0x103a0e
movq 0x20(%rsp), %rbx
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %rbp
leaq 0x90(%rsp), %r15
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
jmp 0x103a0e
movq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0x102e41
movq 0x90(%rsp), %rdi
callq 0x2220
movq 0xa8(%rsp), %rdi
callq 0x2220
movq 0xc0(%rsp), %rdi
callq 0x2220
movq 0xd8(%rsp), %rdi
callq 0x2220
movq 0xf0(%rsp), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0xa0761
movq %r13, %rdi
callq 0x2220
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq