// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: phase_bubble(unsigned long, bub_p_t_warp*, bubble_type*, unsigned char*, ma_ug_t const*, min_cut_t*, hc_links*, G_partition*)
void phase_bubble(uint64_t bid, bub_p_t_warp *b, bubble_type* bub, uint8_t* flag, const ma_ug_t *ug, 
min_cut_t* m, hc_links* link, G_partition* x)
{
    #define HAP1_LAB 1
    #define HAP2_LAB 2

    partition_warp* res = NULL;
    kv_pushp(partition_warp, *x, &res);
    memset(flag, 0, ug->g->n_seq);
    uint32_t beg, sink, n, *a, i, k;
    get_bubbles(bub, bid, &beg, &sink, &a, &n, NULL);
    res->full_bub = 0;
    trace_phase_path((ma_ug_t *)ug, beg, sink, b, m, flag, HAP1_LAB);
    trace_phase_path((ma_ug_t *)ug, beg, sink, b, m, flag, HAP2_LAB);
    kv_init(res->a);
    for (i = 0; i < ug->g->n_seq; i++)
    {
        if(flag[i] & (uint8_t)HAP1_LAB) kv_push(uint32_t, res->a, i);
    }
    res->h[0] = res->a.n;
    for (i = 0; i < ug->g->n_seq; i++)
    {
        if(flag[i] & (uint8_t)HAP2_LAB) kv_push(uint32_t, res->a, i);
    }
    res->h[1] = res->a.n - res->h[0];
    if(n == 2) res->full_bub = 1;
    if(res->full_bub == 0)
    {
        double self = 0, intersec = 0;
        uint32_t *h0 = NULL, *h1 = NULL;
        h0 = res->a.a; h1 = res->a.a + res->h[0];
        self += get_co_weight(h0, res->h[0], h0, res->h[0], m);
        self += get_co_weight(h1, res->h[1], h1, res->h[1], m);

        intersec += get_co_weight(h0, res->h[0], h1, res->h[1], m);
        intersec = intersec * 2;

        if(self > intersec) res->full_bub = 1;
    }

    if(res->full_bub == 0)
    {
        res->a.n = 0;
        uint32_t v, u = 0, uv, k_n, pre_n = x->n;
        hc_linkeage* t = NULL;
        x->n--;
        for (i = 0; i < n; i++)
        {
            v = a[i]>>1;
            t = &(link->a.a[v]);
            for (k = k_n = 0; k < t->f.n; k++)
            {
                if(t->f.a[k].del) continue;
                k_n++;
                u = t->f.a[k].uID;
            }
            if(k_n != 1) continue;

            t = &(link->a.a[u]);
            for (k = k_n = 0; k < t->f.n; k++)
            {
                if(t->f.a[k].del) continue;
                k_n++;
                uv = t->f.a[k].uID;
            }
            if(k_n != 1) continue;
            if(uv != v) continue;
            
            ///avoid dups
            for (k = 0; k < i; k++)
            {
                if((a[k]>>1) == u) break;
            }
            if(k < i) continue;
            

            kv_pushp(partition_warp, *x, &res);
            if(x->n > pre_n) kv_init(res->a);
            res->full_bub = 0;
            res->h[0] = res->h[1] = 1;
            kv_push(uint32_t, res->a, v);
            kv_push(uint32_t, res->a, u);

            for (k = 0; k < res->a.n; k++)
            {
                x->index[res->a.a[k]] = x->n-1;
            }
        }
    }
    else
    {
        for (k = 0; k < res->a.n; k++)
        {
            x->index[res->a.a[k]] = x->n-1;
        }
    }
    
    /*******************************for debug************************************/
    // for (i = 0; i < ug->g->n_seq; i++)
    // {
    //     if(flag[i] & (uint8_t)3)
    //     {
    //         uint32_t k;
    //         for (k = 0; k < n; k++)
    //         {
    //             if((a[k]>>1) == i)
    //             {
    //                 break;
    //             }
    //         }

    //         if(k == n) fprintf(stderr, "ERROR5\n");
    //     }
    // }
    /*******************************for debug************************************/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x20(%rsp)
movq %r8, %r15
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, 0x18(%rsp)
movq %rdi, %rbp
movq 0x78(%rsp), %r14
movq (%r14), %rcx
cmpq 0x8(%r14), %rcx
jne 0x149e5c
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %ecx
cmovneq %rax, %rcx
movq %rcx, 0x8(%r14)
movq 0x10(%r14), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,8), %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
jmp 0x149e60
movq 0x10(%r14), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
leaq (%rcx,%rcx,8), %rcx
leaq (%rax,%rcx,8), %r13
movq 0x18(%r15), %rax
movl 0x14(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %r14d, %r14d
movq %r12, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x28(%rbx), %rcx
movq 0x40(%rbx), %rax
movl (%rax,%rbp,4), %edx
movl 0x4(%rax,%rbp,4), %eax
subl %edx, %eax
movl %eax, 0xc(%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movl (%rcx,%rdx,4), %esi
movl %esi, 0x10(%rsp)
movb $0x0, 0x20(%r13)
movl $0x1, (%rsp)
movq %r15, %rdi
movq %r15, %rbx
movq 0x18(%rsp), %r15
movq %r15, %rcx
movq 0x20(%rsp), %rbp
movq %rbp, %r8
movq %r12, %r9
callq 0x149646
movl $0x2, (%rsp)
movq %rbx, %rdi
movl 0x10(%rsp), %esi
movq %r15, %rcx
movq %rbx, %r15
movq %rbp, %r8
movq %r12, %r9
callq 0x149646
xorpd %xmm0, %xmm0
movupd %xmm0, (%r13)
movq $0x0, 0x10(%r13)
movq 0x18(%rbx), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
je 0x149f80
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %ebx, %ebx
testb $0x1, (%r12,%rbx)
je 0x149f6d
cmpq 0x8(%r13), %rbp
jne 0x149f5e
leaq (,%rbp,2), %rsi
testq %rbp, %rbp
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r13)
shlq $0x2, %rsi
movq %r14, %rdi
callq 0x2370
movq %rax, %r14
movq %rax, 0x10(%r13)
movq (%r13), %rbp
movq 0x18(%r15), %rax
leaq 0x1(%rbp), %rcx
movq %rcx, (%r13)
movl %ebx, (%r14,%rbp,4)
movq %rcx, %rbp
incq %rbx
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rbx
jb 0x149f1e
jmp 0x149f82
xorl %ebp, %ebp
addl $-0x2, 0xc(%rsp)
movl %ebp, 0x18(%r13)
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
je 0x149ffe
xorl %ebx, %ebx
testb $0x2, (%r12,%rbx)
je 0x149fe5
cmpq 0x8(%r13), %rbp
jne 0x149fd6
leaq (,%rbp,2), %rsi
testq %rbp, %rbp
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r13)
shlq $0x2, %rsi
movq %r14, %rdi
callq 0x2370
movq %rax, %r14
movq %rax, 0x10(%r13)
movq (%r13), %rbp
movq 0x18(%r15), %rax
leaq 0x1(%rbp), %rcx
movq %rcx, (%r13)
movl %ebx, (%r14,%rbp,4)
movq %rcx, %rbp
incq %rbx
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rbx
jb 0x149f96
movl 0x18(%r13), %r12d
movl %ebp, %ebx
jmp 0x14a003
movl %ebp, %ebx
movl %ebp, %r12d
subl %r12d, %ebx
movl %ebx, 0x1c(%r13)
cmpl $0x2, 0xc(%rsp)
jne 0x14a018
movb $0x1, 0x20(%r13)
jmp 0x14a01f
cmpb $0x0, 0x20(%r13)
je 0x14a053
testq %rbp, %rbp
je 0x14a044
movq 0x78(%rsp), %rcx
movl (%rcx), %eax
decl %eax
movq 0x18(%rcx), %rcx
xorl %edx, %edx
movl (%r14,%rdx,4), %esi
movl %eax, (%rcx,%rsi,4)
incq %rdx
movl %edx, %esi
cmpq %rsi, %rbp
ja 0x14a033
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r12d, %eax
leaq (%r14,%rax,4), %rax
movq %rax, 0x18(%rsp)
movq %r14, %rdi
movl %r12d, %esi
movq %r14, %rdx
movl %r12d, %ecx
movq 0x20(%rsp), %r15
movq %r15, %r8
callq 0x149d5a
xorpd %xmm1, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x18(%rsp), %rdi
movl %ebx, %esi
movq %rdi, %rdx
movl %ebx, %ecx
movq %r15, %r8
callq 0x149d5a
addsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movl %r12d, %esi
movq 0x18(%rsp), %rdx
movl %ebx, %ecx
movq %r15, %r8
movl 0xc(%rsp), %ebx
callq 0x149d5a
addsd 0xe7551(%rip), %xmm0    # 0x231618
addsd %xmm0, %xmm0
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x14a011
movq $0x0, (%r13)
movq 0x78(%rsp), %rdx
movq (%rdx), %rax
leaq -0x1(%rax), %rcx
movq %rcx, (%rdx)
testl %ebx, %ebx
je 0x14a044
movq 0x70(%rsp), %r10
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %rdx
leaq (%rcx,%rdx,4), %r14
addq $0x8, %r14
movl %eax, %eax
movq %rax, 0x20(%rsp)
movl %ebx, %r12d
xorl %r13d, %r13d
xorl %ebp, %ebp
movl (%r14,%r13,4), %r15d
shrl %r15d
movq 0x10(%r10), %rcx
leaq (%r15,%r15,2), %rsi
shlq $0x4, %rsi
movq 0x18(%rcx,%rsi), %rdx
testq %rdx, %rdx
je 0x14a327
addq %rcx, %rsi
addq $0x18, %rsi
movq 0x10(%rsi), %rsi
addq $0x18, %rsi
xorl %edi, %edi
movl $0x1, %r8d
cmpb $0x0, (%rsi)
js 0x14a162
incl %edi
movl -0x10(%rsi), %ebp
movl %r8d, %r9d
addq $0x28, %rsi
incl %r8d
cmpq %r9, %rdx
ja 0x14a158
cmpl $0x1, %edi
jne 0x14a327
movl %ebp, %edx
leaq (%rdx,%rdx,2), %rsi
shlq $0x4, %rsi
movq 0x18(%rcx,%rsi), %rdx
testq %rdx, %rdx
je 0x14a327
addq %rsi, %rcx
addq $0x18, %rcx
movq 0x10(%rcx), %rcx
addq $0x18, %rcx
xorl %esi, %esi
movl $0x1, %edi
cmpb $0x0, (%rcx)
js 0x14a1b2
incl %esi
movl -0x10(%rcx), %eax
movl %edi, %r8d
addq $0x28, %rcx
incl %edi
cmpq %r8, %rdx
ja 0x14a1a8
cmpl $0x1, %esi
jne 0x14a327
cmpl %r15d, %eax
jne 0x14a327
testq %r13, %r13
je 0x14a1ef
xorl %eax, %eax
movl (%r14,%rax,4), %ecx
shrl %ecx
cmpl %ebp, %ecx
je 0x14a324
incq %rax
cmpq %rax, %r13
jne 0x14a1d9
movq 0x78(%rsp), %rbx
movq (%rbx), %rcx
movq 0x10(%rbx), %rax
cmpq 0x8(%rbx), %rcx
jne 0x14a22c
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rdx
movq %rdx, 0x8(%rbx)
shlq $0x3, %rdx
leaq (%rdx,%rdx,8), %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
leaq (%rcx,%rcx,8), %rcx
leaq (%rax,%rcx,8), %rbx
cmpq 0x20(%rsp), %rdx
jbe 0x14a26b
movb $0x0, 0x20(%rbx)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x18(%rbx)
movl $0x2, %esi
jmp 0x14a299
movq (%rbx), %rcx
movq 0x8(%rbx), %rax
movb $0x0, 0x20(%rbx)
movabsq $0x100000001, %rdx      # imm = 0x100000001
movq %rdx, 0x18(%rbx)
cmpq %rax, %rcx
jne 0x14a2b3
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rcx
jmp 0x14a2b7
movq 0x10(%rbx), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%rbx)
movl %r15d, (%rax,%rcx,4)
cmpq 0x8(%rbx), %rdx
jne 0x14a2ef
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rdx
movq 0x70(%rsp), %r10
movq %rdx, %rcx
incq %rcx
movq %rcx, (%rbx)
movl %ebp, (%rax,%rdx,4)
je 0x14a324
movq 0x78(%rsp), %rsi
movl (%rsi), %edx
decl %edx
movq 0x18(%rsi), %rsi
xorl %edi, %edi
movl (%rax,%rdi,4), %r8d
movl %edx, (%rsi,%r8,4)
incq %rdi
movl %edi, %r8d
cmpq %r8, %rcx
ja 0x14a311
movl %r15d, %eax
incq %r13
cmpq %r12, %r13
jne 0x14a120
jmp 0x14a044