// File: /workspace/github_cpp_makefile_star2_O1/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 0xb0285
movq %rax, %rbx
movq 0x18(%rax), %r14
movl 0x14(%r14), %r15d
addl %r15d, %r15d
xorpd %xmm0, %xmm0
leaq 0x90(%rsp), %rbp
movapd %xmm0, 0x50(%rbp)
movapd %xmm0, 0x40(%rbp)
movapd %xmm0, 0x30(%rbp)
movapd %xmm0, 0x20(%rbp)
movapd %xmm0, 0x10(%rbp)
movapd %xmm0, (%rbp)
movq $0x0, 0x60(%rbp)
movl $0x1c, %esi
movq %r15, %rdi
callq 0x2120
movq %rax, (%rbp)
movl $0x1, %esi
movq %r15, 0x20(%rsp)
movq %r15, %rdi
callq 0x2120
movq %rax, %r15
movq %r14, %rdi
movq %rbp, %rsi
callq 0xbf22d
movq %rax, 0x58(%rsp)
movq 0x18(%rbx), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
je 0xff0b2
movq 0x20(%rax), %rcx
movl $0x2, %edx
xorl %esi, %esi
cmpl $0x0, (%rcx,%rsi,8)
js 0xff09d
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 0xff085
cmpl $0x0, 0x20(%rsp)
je 0xff6b7
movl 0x20(%rsp), %r13d
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movq %rbx, 0x50(%rsp)
movq %r15, 0x48(%rsp)
movq %r13, (%rsp)
xorl %edx, %edx
movl $0x0, 0x14(%rsp)
movq 0x18(%rbx), %rdi
movq 0x20(%rdi), %rax
movl %edx, %r14d
shrl %r14d
cmpl $0x0, (%rax,%r14,8)
js 0xff699
movq 0x28(%rdi), %rax
movq (%rax,%rdx,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xff699
movq %rax, %rcx
andq %r9, %rcx
je 0xff699
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 0xff139
cmpl $0x2, %esi
jl 0xff699
movzbl (%r15,%rdx), %eax
cmpl $0x1, %eax
je 0xff699
testl %eax, %eax
jne 0xff170
movb $0x1, (%r15,%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 0x98(%rsp), %rcx
movq %rbp, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xbf39f
addq $0x40, %rsp
testq %rax, %rax
je 0xff41a
movq 0xc8(%rsp), %rax
testq %rax, %rax
je 0xff220
movq 0xa8(%rsp), %rcx
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 0xff214
movl (%rcx), %r8d
shrl %r8d
cmpl %r8d, %edi
je 0xff214
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, %rax
ja 0xff1ef
jmp 0xff228
xorl %r10d, %r10d
movq 0x8(%rsp), %r9
movq %r10, 0x18(%rsp)
movb $0x2, %cl
movb %cl, (%r15,%r9)
movq 0xa8(%rsp), %rax
movl (%rax), %eax
xorq $0x1, %rax
movb %cl, (%r15,%rax)
movq $0x0, 0xc8(%rsp)
movq 0x18(%rbx), %rdi
movl %r9d, %edx
xorl $0x1, %edx
xorl %r13d, %r13d
xorl %esi, %esi
leaq 0x44(%rsp), %rcx
leaq 0x88(%rsp), %r8
leaq 0x80(%rsp), %r9
pushq %rbp
pushq $0x1
leaq 0x80(%rsp), %rax
pushq %rax
leaq 0x90(%rsp), %rax
pushq %rax
callq 0x10109f
addq $0x20, %rsp
movq 0xc8(%rsp), %rax
movq %rax, 0x38(%rsp)
testq %rax, %rax
je 0xff410
movq 0x10(%rbx), %rcx
movq 0xd8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x18(%rsp), %rax
leaq 0x1(%rax), %rsi
cmpq $0x6, %rsi
movl $0x5, %eax
cmovbq %rax, %rsi
xorl %edi, %edi
xorl %r8d, %r8d
xorl %r10d, %r10d
xorl %r9d, %r9d
xorl %r13d, %r13d
movq 0x125f5a(%rip), %xmm3   # 0x225240
movapd 0x125f62(%rip), %xmm4   # 0x225250
movsd 0x12a892(%rip), %xmm5   # 0x229b88
movsd 0x1261a2(%rip), %xmm6   # 0x2254a0
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), %r14d
testq %r14, %r14
je 0xff3e1
leaq (%rcx,%rax,8), %rbp
leal -0x1(%r14), %ebx
shlq $0x3, %r14
xorl %edx, %edx
movl %r13d, %eax
testb $0x1, %r11b
jne 0xff343
movq 0x18(%rbp), %r15
addq %rdx, %r15
jmp 0xff34e
movl %ebx, %r15d
shlq $0x3, %r15
addq 0x18(%rbp), %r15
movq (%r15), %r15
shrq $0x21, %r15
xorl %r13d, %r13d
xorl %ecx, %ecx
cmpb $0x0, (%r12,%r15)
setne %r13b
sete %cl
addq %rcx, %r10
addq %r13, %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,%r10), %r15
movq %r15, %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 %r15d, %r13d
ja 0xff3b6
movl %eax, %r13d
mulsd %xmm6, %xmm1
ucomisd %xmm1, %xmm0
movl $0x1, %eax
ja 0xff3c8
movl %r9d, %eax
cmpq %rsi, %r15
cmovel %eax, %r9d
addq $0x8, %rdx
decl %ebx
movl %r13d, %eax
cmpq %rdx, %r14
jne 0xff334
incq %rdi
movl %edi, %eax
cmpq %rax, 0x38(%rsp)
movq 0x30(%rsp), %rcx
ja 0xff303
testl %r9d, %r9d
sete %al
movq 0x50(%rsp), %rbx
movq 0x48(%rsp), %r15
leaq 0x90(%rsp), %rbp
jmp 0xff412
movb $0x1, %al
testb %al, %al
je 0xff42a
movq (%rsp), %r13
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rdx
jmp 0xff699
movq $0x0, 0xc8(%rsp)
movq 0x18(%rbx), %rdi
movq 0xa8(%rsp), %rax
movl (%rax), %edx
xorl %r14d, %r14d
xorl %esi, %esi
leaq 0x44(%rsp), %rcx
leaq 0x88(%rsp), %r8
leaq 0x80(%rsp), %r9
pushq %rbp
pushq $0x1
leaq 0x80(%rsp), %rax
pushq %rax
leaq 0x90(%rsp), %rax
pushq %rax
callq 0x10109f
addq $0x20, %rsp
movq 0xc8(%rsp), %rax
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0xff602
movq 0x10(%rbx), %rcx
movq 0xd8(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x18(%rsp), %rax
leaq 0x1(%rax), %rsi
cmpq $0x6, %rsi
movl $0x5, %eax
cmovbq %rax, %rsi
xorl %edx, %edx
xorl %r8d, %r8d
xorl %r10d, %r10d
xorl %r9d, %r9d
xorl %r14d, %r14d
movq 0x125d71(%rip), %xmm3   # 0x225240
movapd 0x125d79(%rip), %xmm4   # 0x225250
movsd 0x12a6a9(%rip), %xmm5   # 0x229b88
movsd 0x125fb9(%rip), %xmm6   # 0x2254a0
movq %rcx, 0x28(%rsp)
movq 0x68(%rsp), %rax
movq %rdx, 0x38(%rsp)
movl (%rax,%rdx,4), %r11d
movl %r11d, %eax
shrl %eax
leaq (%rax,%rax,4), %rax
movl 0x10(%rcx,%rax,8), %ebp
testq %rbp, %rbp
je 0xff5ce
leaq (%rcx,%rax,8), %rbx
leal -0x1(%rbp), %edx
shlq $0x3, %rbp
xorl %eax, %eax
movl %r14d, %ecx
testb $0x1, %r11b
jne 0xff52f
movq 0x18(%rbx), %r14
addq %rax, %r14
jmp 0xff53a
movl %edx, %r14d
shlq $0x3, %r14
addq 0x18(%rbx), %r14
movq (%r14), %r14
shrq $0x21, %r14
xorl %r15d, %r15d
xorl %edi, %edi
cmpb $0x0, (%r12,%r14)
setne %r15b
sete %dil
addq %rdi, %r10
addq %r15, %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,%r10), %r15
movq %r15, %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 %r15d, %r14d
ja 0xff5a3
movl %ecx, %r14d
mulsd %xmm6, %xmm1
ucomisd %xmm1, %xmm0
movl $0x1, %ecx
ja 0xff5b5
movl %r9d, %ecx
cmpq %rsi, %r15
cmovel %ecx, %r9d
addq $0x8, %rax
decl %edx
movl %r14d, %ecx
cmpq %rax, %rbp
jne 0xff520
movq 0x38(%rsp), %rdx
incq %rdx
movl %edx, %eax
cmpq %rax, 0x30(%rsp)
movq 0x28(%rsp), %rcx
ja 0xff4ec
testl %r9d, %r9d
setne %al
movq 0x50(%rsp), %rbx
movq 0x48(%rsp), %r15
leaq 0x90(%rsp), %rbp
jmp 0xff604
xorl %eax, %eax
movq 0x18(%rsp), %rcx
testb %al, %al
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rdx
je 0xff695
movl %r13d, %eax
cmpq %rax, %rcx
jae 0xff695
movl %r14d, %eax
cmpq %rax, %rcx
jae 0xff695
addl %r13d, %r14d
shlq $0x2, %rcx
cmpq %r14, %rcx
jae 0xff695
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 0x98(%rsp), %rcx
movq %rbp, %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
callq 0xbf39f
movq 0x48(%rsp), %rdx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
addq $0x40, %rsp
incl 0x14(%rsp)
movq (%rsp), %r13
incq %rdx
cmpq %r13, %rdx
jne 0xff0e0
cmpl $0x0, 0x14(%rsp)
je 0xff6b7
cmpl $0x0, 0x20(%rsp)
jne 0xff0d6
movq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0xfeae9
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 0xa0004
movq %r15, %rdi
callq 0x2220
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq