// File: /workspace/github_cpp_makefile_star2_O2/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 $0xe8, %rsp
movq %rsi, %r12
movq %rdi, 0x50(%rsp)
callq 0x76d91
movq %rax, (%rsp)
movq 0x18(%rax), %rbp
movl 0x14(%rbp), %r15d
addl %r15d, %r15d
xorpd %xmm0, %xmm0
leaq 0x80(%rsp), %r13
movupd %xmm0, 0x58(%r13)
movupd %xmm0, 0x48(%r13)
movupd %xmm0, 0x38(%r13)
movupd %xmm0, 0x28(%r13)
movupd %xmm0, 0x18(%r13)
movupd %xmm0, 0x8(%r13)
pushq $0x1c
popq %rsi
movq %r15, %rdi
callq 0x2120
movq %rax, (%r13)
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x2120
movq %rax, %r14
movq %rbp, %rdi
movq %r13, %rsi
callq 0x84975
movq %rax, 0x48(%rsp)
pushq $0x2
popq %rax
xorl %ecx, %ecx
movl 0x14(%rbp), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
cmpq %rdx, %rcx
jae 0xc03e2
movq 0x20(%rbp), %rdx
cmpl $0x0, (%rdx,%rcx,8)
js 0xc03d9
movb $0x0, 0x4(%rdx,%rcx,8)
movq (%rsp), %rdx
movq 0x10(%rdx), %rdx
movl (%rdx,%rax,8), %esi
movl %esi, -0xc(%rdx,%rax,8)
incq %rcx
addq $0x5, %rax
jmp 0xc03ad
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
movq %r14, 0x20(%rsp)
movq %r15, 0x18(%rsp)
cmpl $0x0, 0x14(%rsp)
je 0xc0904
xorl %ecx, %ecx
movl $0x0, 0x14(%rsp)
cmpq %r15, %rcx
je 0xc03f3
movq (%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x20(%rdi), %rax
movl %ecx, %ebx
shrl %ebx
cmpl $0x0, (%rax,%rbx,8)
js 0xc08fc
movq 0x28(%rdi), %rax
testl $0xfffffffe, (%rax,%rcx,8) # imm = 0xFFFFFFFE
je 0xc08fc
movl %ecx, %esi
movq %rcx, %rbp
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x2, %eax
jge 0xc0451
movq %rbp, %rcx
jmp 0xc08fc
movzbl (%r14,%rbp), %eax
cmpl $0x1, %eax
movq %rbp, %rcx
je 0xc08fc
testl %eax, %eax
jne 0xc046b
movb $0x1, (%r14,%rcx)
movq (%rsp), %rsi
movq 0x18(%rsi), %rdi
subq $0x40, %rsp
xorpd %xmm0, %xmm0
movupd %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 %rcx, 0x48(%rsp)
movl %ecx, %edx
movq 0x88(%rsp), %rcx
movq %r13, %r8
pushq $-0x1
popq %r9
callq 0x84a86
addq $0x40, %rsp
testq %rax, %rax
je 0xc08f7
movq 0x98(%rsp), %rax
movq 0xb8(%rsp), %rcx
movq 0xc8(%rsp), %rdx
xorl %esi, %esi
movq $0x0, 0x28(%rsp)
movl %esi, %esi
cmpq %rsi, %rcx
jbe 0xc0520
movl (%rdx,%rsi,4), %edi
shrl %edi
cmpl %ebx, %edi
je 0xc051c
movl (%rax), %r8d
shrl %r8d
cmpl %r8d, %edi
je 0xc051c
movq (%rsp), %r8
movq 0x10(%r8), %r8
imulq $0x28, %rdi, %rdi
movl 0x10(%r8,%rdi), %edi
addq %rdi, 0x28(%rsp)
incl %esi
jmp 0xc04eb
movb $0x2, %cl
movq 0x8(%rsp), %rdx
movb %cl, (%r14,%rdx)
movl (%rax), %eax
xorq $0x1, %rax
andq $0x0, 0xb8(%rsp)
movb %cl, (%r14,%rax)
movq (%rsp), %rax
movq 0x18(%rax), %rdi
xorl $0x1, %edx
xorl %ebx, %ebx
xorl %esi, %esi
leaq 0x44(%rsp), %rcx
leaq 0x78(%rsp), %r8
leaq 0x70(%rsp), %r9
pushq %r13
pushq $0x1
leaq 0x70(%rsp), %rax
pushq %rax
leaq 0x80(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
movq 0xb8(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x28(%rsp), %rax
leaq 0x1(%rax), %r13
cmpq $0x6, %r13
pushq $0x5
popq %rax
cmovbq %rax, %r13
xorl %r15d, %r15d
xorl %edx, %edx
xorl %esi, %esi
xorl %edi, %edi
movq 0x122cb9(%rip), %xmm3   # 0x1e3270
movapd 0x122cc1(%rip), %xmm4   # 0x1e3280
movsd 0x1276b9(%rip), %xmm5   # 0x1e7c80
movsd 0x122f21(%rip), %xmm6   # 0x1e34f0
movl %ebx, %ebx
cmpq %rbx, 0x38(%rsp)
jbe 0xc06ba
movq (%rsp), %rax
movq 0x10(%rax), %rcx
movq 0x30(%rsp), %rax
movl (%rax,%rbx,4), %r8d
movl %r8d, %r9d
shrl %r9d
imulq $0x28, %r9, %r10
leaq (%rcx,%r10), %r9
movl 0x10(%rcx,%r10), %r11d
leal -0x1(%r11), %r10d
negq %r11
xorl %ebp, %ebp
movq %r11, %rcx
addq %rbp, %rcx
je 0xc06b3
testb $0x1, %r8b
movq 0x18(%r9), %rcx
movl %r10d, %r14d
cmoveq %rbp, %r14
movq (%rcx,%r14,8), %rcx
shrq $0x21, %rcx
xorl %r14d, %r14d
xorl %eax, %eax
cmpb $0x0, (%r12,%rcx)
setne %r14b
sete %al
addq %rax, %rsi
addq %r14, %rdi
movq %rdi, %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 (%rdi,%rsi), %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, %r14d
ja 0xc068d
movl %r15d, %r14d
mulsd %xmm6, %xmm1
ucomisd %xmm1, %xmm0
pushq $0x1
popq %r15
ja 0xc069e
movl %edx, %r15d
cmpq %r13, %rcx
cmovel %r15d, %edx
incq %rbp
decl %r10d
movl %r14d, %r15d
jmp 0xc0609
incl %ebx
jmp 0xc05cf
testl %edx, %edx
je 0xc08e5
andq $0x0, 0xb8(%rsp)
movq (%rsp), %rax
movq 0x18(%rax), %rdi
movq 0x98(%rsp), %rax
movl (%rax), %edx
xorl %ebx, %ebx
xorl %esi, %esi
leaq 0x44(%rsp), %rcx
leaq 0x78(%rsp), %r8
leaq 0x70(%rsp), %r9
leaq 0x80(%rsp), %rax
pushq %rax
pushq $0x1
leaq 0x70(%rsp), %rax
pushq %rax
leaq 0x80(%rsp), %rax
pushq %rax
callq 0xc2ebf
movsd 0x122dd9(%rip), %xmm6   # 0x1e34f0
movsd 0x127561(%rip), %xmm5   # 0x1e7c80
movapd 0x122b59(%rip), %xmm4   # 0x1e3280
movq 0x122b41(%rip), %xmm3   # 0x1e3270
addq $0x20, %rsp
movq 0xb8(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x58(%rsp)
xorl %esi, %esi
xorl %edx, %edx
xorl %edi, %edi
xorl %r8d, %r8d
movl %ebx, %ebx
cmpq %rbx, 0x30(%rsp)
jbe 0xc0846
movq (%rsp), %rax
movq 0x10(%rax), %rax
movq %rbx, 0x38(%rsp)
movq 0x58(%rsp), %rcx
movl (%rcx,%rbx,4), %r9d
movl %r9d, %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
leaq (%rax,%rcx), %r10
movl 0x10(%rax,%rcx), %ecx
leal -0x1(%rcx), %r11d
negq %rcx
xorl %ebp, %ebp
movq %rcx, %rax
addq %rbp, %rax
je 0xc083a
testb $0x1, %r9b
movq 0x18(%r10), %rax
movl %r11d, %r14d
cmoveq %rbp, %r14
movq (%rax,%r14,8), %rax
shrq $0x21, %rax
xorl %r14d, %r14d
xorl %ebx, %ebx
cmpb $0x0, (%r12,%rax)
setne %r14b
sete %bl
addq %rbx, %rdi
addq %r14, %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,%rdi), %rax
movq %rax, %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 %eax, %r14d
ja 0xc0817
movl %esi, %r14d
mulsd %xmm6, %xmm1
ucomisd %xmm1, %xmm0
pushq $0x1
popq %rsi
ja 0xc0826
movl %edx, %esi
cmpq %r13, %rax
cmovel %esi, %edx
incq %rbp
decl %r11d
movl %r14d, %esi
jmp 0xc0793
movq 0x38(%rsp), %rbx
incl %ebx
jmp 0xc0756
testl %edx, %edx
je 0xc08e5
movl %r15d, %eax
movq 0x28(%rsp), %rdx
cmpq %rax, %rdx
leaq 0x80(%rsp), %r13
movq 0x8(%rsp), %rcx
jae 0xc08d9
movl %esi, %eax
cmpq %rax, %rdx
jae 0xc08d9
addl %r15d, %esi
shlq $0x2, %rdx
cmpq %rsi, %rdx
jae 0xc08d9
movq (%rsp), %rsi
movq 0x18(%rsi), %rdi
subq $0x40, %rsp
xorpd %xmm0, %xmm0
movupd %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 0x48(%rsp), %rdx
movq 0x88(%rsp), %rcx
movq %r13, %r8
pushq $-0x1
popq %r9
callq 0x84a86
movq 0x48(%rsp), %rcx
addq $0x40, %rsp
incl 0x14(%rsp)
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %r15
jmp 0xc08fc
movq 0x20(%rsp), %r14
movq 0x18(%rsp), %r15
leaq 0x80(%rsp), %r13
movq 0x8(%rsp), %rcx
incq %rcx
jmp 0xc0408
movq 0x50(%rsp), %rdi
movq (%rsp), %rbx
movq %rbx, %rsi
callq 0xc005e
movq 0x80(%rsp), %rdi
callq 0x2220
movq 0x98(%rsp), %rdi
callq 0x2220
movq 0xb0(%rsp), %rdi
callq 0x2220
movq 0xc8(%rsp), %rdi
callq 0x2220
movq 0xe0(%rsp), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x6921d
movq %r14, %rdi
callq 0x2220
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq