// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movq %r8, %r14
movq %rcx, %r15
movq %rdx, %rbp
movq %rsi, %r12
movq %rdi, %r13
movq 0x78(%rsp), %rbx
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x1438b4
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmovneq %rcx, %rax
movq %rax, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x3, %rax
leaq (%rax,%rax,8), %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
leaq (%rax,%rax,8), %rax
leaq (%rcx,%rax,8), %rbx
movq 0x18(%r14), %rax
movl 0x14(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq %r15, %rdi
xorl %esi, %esi
callq 0x20e0
movq 0x28(%rbp), %rcx
movq 0x40(%rbp), %rax
movl (%rax,%r13,4), %edx
movl 0x4(%rax,%r13,4), %eax
subl %edx, %eax
movl %eax, 0x8(%rsp)
movq %rcx, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movl (%rcx,%rdx,4), %ebp
movb $0x0, 0x20(%rbx)
movl $0x1, (%rsp)
movq %r14, %rdi
movl %ebp, %esi
movq %r12, %rcx
movq 0x10(%rsp), %r13
movq %r13, %r8
movq %r15, %r9
callq 0x14305f
movl $0x2, (%rsp)
movq %r14, %rdi
movl %ebp, %esi
movq %r12, %rcx
movq %r13, %r8
movq %r15, %r9
callq 0x14305f
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
movq 0x18(%r14), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
je 0x1439be
xorl %r12d, %r12d
movl $0x2, %r13d
testb $0x1, (%r15,%r12)
je 0x1439aa
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x143998
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r13, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movl %r12d, (%rcx,%rax,4)
incq %r12
movq 0x18(%r14), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r12
jb 0x143968
addl $-0x2, 0x8(%rsp)
movl (%rbx), %eax
movl %eax, 0x18(%rbx)
movq 0x18(%r14), %rax
testl $0x7fffffff, 0x14(%rax) # imm = 0x7FFFFFFF
je 0x143a34
xorl %r12d, %r12d
movl $0x2, %r13d
testb $0x2, (%r15,%r12)
je 0x143a20
movq 0x8(%rbx), %rax
cmpq %rax, (%rbx)
jne 0x143a0e
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r13, %rsi
movq %rsi, 0x8(%rbx)
movq 0x10(%rbx), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%rbx)
movq (%rbx), %rax
movq 0x10(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbx)
movl %r12d, (%rcx,%rax,4)
incq %r12
movq 0x18(%r14), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r12
jb 0x1439de
movq (%rbx), %r13
movl 0x18(%rbx), %eax
movl %r13d, %ebp
subl %eax, %ebp
movl %ebp, 0x1c(%rbx)
movl 0x8(%rsp), %r15d
cmpl $0x2, %r15d
jne 0x143a51
movb $0x1, 0x20(%rbx)
cmpb $0x0, 0x20(%rbx)
movq 0x78(%rsp), %r12
jne 0x143af5
movq 0x10(%rbx), %r14
movl 0x18(%rbx), %r15d
leaq (%r14,%r15,4), %rax
movq %rax, 0x20(%rsp)
movq %r14, %rdi
movl %r15d, %esi
movq %r14, %rdx
movl %r15d, %ecx
movq 0x10(%rsp), %r12
movq %r12, %r8
callq 0x1437b9
xorpd %xmm1, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x20(%rsp), %rdi
movl %ebp, %esi
movq %rdi, %rdx
movl %ebp, %ecx
movq %r12, %r8
callq 0x1437b9
addsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %r14, %rdi
movl %r15d, %esi
movl 0x8(%rsp), %r15d
movq 0x20(%rsp), %rdx
movq 0x78(%rsp), %r12
movl %ebp, %ecx
movq 0x10(%rsp), %r8
callq 0x1437b9
addsd 0xe1a37(%rip), %xmm0    # 0x225518
addsd %xmm0, %xmm0
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x143af5
movb $0x1, 0x20(%rbx)
cmpb $0x0, 0x20(%rbx)
je 0x143b2a
testq %r13, %r13
je 0x143da1
movl (%r12), %eax
decl %eax
movq 0x18(%r12), %rcx
movq 0x10(%rbx), %rdx
xorl %esi, %esi
movl (%rdx,%rsi,4), %edi
movl %eax, (%rcx,%rdi,4)
incq %rsi
movl %esi, %edi
cmpq %rdi, %r13
ja 0x143b15
jmp 0x143da1
movq $0x0, (%rbx)
movq (%r12), %rax
leaq -0x1(%rax), %rcx
movq %rcx, (%r12)
testl %r15d, %r15d
je 0x143da1
movq 0x70(%rsp), %r9
movq 0x30(%rsp), %rcx
movq 0x28(%rsp), %rdx
leaq (%rcx,%rdx,4), %r14
movl %eax, %eax
movq %rax, 0x10(%rsp)
movl %r15d, %r10d
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %r10, 0x8(%rsp)
movl 0x8(%r14,%r13,4), %ebx
shrl %ebx
movq 0x10(%r9), %rax
leaq (%rbx,%rbx,2), %rsi
shlq $0x4, %rsi
movq 0x18(%rax,%rsi), %rdx
xorl %ecx, %ecx
testq %rdx, %rdx
je 0x143bc0
addq %rax, %rsi
addq $0x18, %rsi
movq 0x10(%rsi), %rsi
addq $0x18, %rsi
movl $0x1, %edi
cmpb $0x0, (%rsi)
js 0x143bac
incl %ecx
movl -0x10(%rsi), %r15d
movl %edi, %r8d
addq $0x28, %rsi
incl %edi
cmpq %r8, %rdx
ja 0x143ba1
cmpl $0x1, %ecx
sete %cl
testb %cl, %cl
je 0x143d95
movl %r15d, %ecx
leaq (%rcx,%rcx,2), %rsi
shlq $0x4, %rsi
movq 0x18(%rax,%rsi), %rdx
xorl %ecx, %ecx
testq %rdx, %rdx
je 0x143c10
addq %rsi, %rax
addq $0x18, %rax
movq 0x10(%rax), %rax
addq $0x18, %rax
movl $0x1, %esi
cmpb $0x0, (%rax)
js 0x143bfd
incl %ecx
movl -0x10(%rax), %ebp
movl %esi, %edi
addq $0x28, %rax
incl %esi
cmpq %rdi, %rdx
ja 0x143bf3
cmpl $0x1, %ecx
sete %cl
testb %cl, %cl
je 0x143d95
cmpl %ebx, %ebp
jne 0x143d95
testq %r13, %r13
je 0x143c44
movl $0x2, %eax
movl (%r14,%rax,4), %ecx
shrl %ecx
cmpl %r15d, %ecx
je 0x143d95
leal -0x1(%rax), %ecx
incq %rax
cmpq %rcx, %r13
jne 0x143c2a
movq 0x8(%r12), %rax
cmpq %rax, (%r12)
jne 0x143c85
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x8(%r12)
movq 0x10(%r12), %rdi
shlq $0x3, %rcx
leaq (%rcx,%rcx,8), %rsi
callq 0x2370
movq 0x8(%rsp), %r10
movq 0x70(%rsp), %r9
movq %rax, 0x10(%r12)
movq (%r12), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%r12)
movq %r12, %rdx
leaq (%rax,%rax,8), %r12
shlq $0x3, %r12
addq 0x10(%rdx), %r12
cmpq 0x10(%rsp), %rcx
jbe 0x143cba
xorpd %xmm0, %xmm0
movupd %xmm0, (%r12)
movq $0x0, 0x10(%r12)
movb $0x0, 0x20(%r12)
movabsq $0x100000001, %rax      # imm = 0x100000001
movq %rax, 0x18(%r12)
movq 0x8(%r12), %rax
cmpq %rax, (%r12)
jne 0x143d0c
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r12)
movq 0x10(%r12), %rdi
shlq $0x2, %rsi
callq 0x2370
movq 0x8(%rsp), %r10
movq 0x70(%rsp), %r9
movq %rax, 0x10(%r12)
movq (%r12), %rax
movq 0x10(%r12), %rdi
leaq 0x1(%rax), %rcx
movq %rcx, (%r12)
movl %ebx, (%rdi,%rax,4)
movq 0x8(%r12), %rax
cmpq %rax, %rcx
jne 0x143d57
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r12)
shlq $0x2, %rsi
callq 0x2370
movq 0x8(%rsp), %r10
movq 0x70(%rsp), %r9
movq %rax, 0x10(%r12)
movq (%r12), %rdx
movq 0x10(%r12), %rax
movq %rdx, %rcx
incq %rcx
movq %rcx, (%r12)
movl %r15d, (%rax,%rdx,4)
movq 0x78(%rsp), %r12
je 0x143d95
movl (%r12), %edx
decl %edx
movq 0x18(%r12), %rsi
xorl %edi, %edi
movl (%rax,%rdi,4), %r8d
movl %edx, (%rsi,%r8,4)
incq %rdi
movl %edi, %r8d
cmpq %r8, %rcx
ja 0x143d82
incq %r13
cmpq %r10, %r13
jne 0x143b6e
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq