// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
movq %r9, 0x20(%rsp)
movq %r8, %r15
movq %rcx, %r12
movq %rdx, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, 0x30(%rsp)
movq 0x88(%rsp), %rbx
movq (%rbx), %rsi
pushq $0x2
popq %rdx
cmpq 0x8(%rbx), %rsi
jne 0x1020dd
leaq (%rsi,%rsi), %rcx
testq %rsi, %rsi
cmoveq %rdx, %rcx
movq %rcx, 0x8(%rbx)
movq 0x10(%rbx), %rdi
imulq $0x48, %rcx, %rsi
callq 0x2370
movq %rax, %rbp
movq %rax, 0x10(%rbx)
movq (%rbx), %rsi
jmp 0x1020e1
movq 0x10(%rbx), %rbp
leaq 0x1(%rsi), %rcx
movq %rcx, (%rbx)
imulq $0x48, %rsi, %r13
movq %rbp, %rbx
addq %r13, %rbx
movq 0x18(%r15), %rcx
movq %rbp, 0x28(%rsp)
movl 0x14(%rcx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %r14d, %r14d
movq %r12, %rdi
xorl %esi, %esi
callq 0x20e0
andq $0x0, (%rsp)
leaq 0x1c(%rsp), %rdx
leaq 0x44(%rsp), %rcx
leaq 0x38(%rsp), %r8
leaq 0x18(%rsp), %r9
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0xf6ab5
movb $0x0, 0x20(%rbp,%r13)
leaq 0x1c(%rsp), %rax
movl (%rax), %esi
movl $0x1, (%rsp)
movq %r15, %rdi
movq 0x10(%rsp), %rcx
movq 0x20(%rsp), %rbp
movq %rbp, %r8
movq %r12, %r9
callq 0x101a01
leaq 0x1c(%rsp), %rax
movl (%rax), %esi
movl $0x2, (%rsp)
movq %r15, %rdi
movq 0x10(%rsp), %rcx
movq %rbp, %r8
movq %r12, %r9
callq 0x101a01
movq 0x28(%rsp), %rax
andq $0x0, 0x10(%rax,%r13)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax,%r13)
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x18(%r15), %rax
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rbp
jae 0x1021fe
testb $0x1, (%r12,%rbp)
je 0x1021f9
cmpq 0x8(%rbx), %r13
jne 0x1021eb
leaq (,%r13,2), %rsi
testq %r13, %r13
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x2, %rsi
movq %r14, %rdi
callq 0x2370
movq %rax, %r14
movq %rax, 0x10(%rbx)
movq (%rbx), %r13
leaq 0x1(%r13), %rax
movq %rax, (%rbx)
movl %ebp, (%r14,%r13,4)
movq %rax, %r13
incq %rbp
jmp 0x1021a0
movl %r13d, 0x18(%rbx)
xorl %ebp, %ebp
movl 0x14(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpq %rcx, %rbp
jae 0x102262
testb $0x2, (%r12,%rbp)
je 0x10225d
cmpq 0x8(%rbx), %r13
jne 0x10224f
leaq (,%r13,2), %rsi
testq %r13, %r13
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%rbx)
shlq $0x2, %rsi
movq %r14, %rdi
callq 0x2370
movq %rax, %r14
movq %rax, 0x10(%rbx)
movq (%rbx), %r13
movq 0x18(%r15), %rax
leaq 0x1(%r13), %rcx
movq %rcx, (%rbx)
movl %ebp, (%r14,%r13,4)
movq %rcx, %r13
incq %rbp
jmp 0x102204
movl 0x18(%rbx), %r12d
movl %r13d, %ebp
subl %r12d, %ebp
movl %ebp, 0x1c(%rbx)
movl 0x18(%rsp), %r15d
cmpl $0x2, %r15d
jne 0x102280
movb $0x1, 0x20(%rbx)
jmp 0x102286
cmpb $0x0, 0x20(%rbx)
je 0x1022ae
xorl %eax, %eax
movq 0x88(%rsp), %rdi
movl %eax, %eax
cmpq %rax, %r13
jbe 0x102530
movl (%rdi), %ecx
decl %ecx
movq 0x18(%rdi), %rdx
movl (%r14,%rax,4), %esi
movl %ecx, (%rdx,%rsi,4)
incl %eax
jmp 0x102290
leaq (%r14,%r12,4), %rax
movq %rax, 0x10(%rsp)
movq %r14, %rdi
movl %r12d, %esi
movq %r14, %rdx
movl %r12d, %ecx
movq 0x20(%rsp), %r8
callq 0x101ff9
xorpd %xmm1, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x10(%rsp), %rdi
movl %ebp, %esi
movq %rdi, %rdx
movl %ebp, %ecx
movq 0x20(%rsp), %r8
callq 0x101ff9
addsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
movl %r12d, %esi
movq 0x10(%rsp), %rdx
movl %ebp, %ecx
movq 0x20(%rsp), %r8
callq 0x101ff9
addsd 0xe124c(%rip), %xmm0    # 0x1e3568
addsd %xmm0, %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x10227a
andq $0x0, (%rbx)
movq 0x88(%rsp), %rdx
movq (%rdx), %rax
leaq -0x1(%rax), %rcx
movq %rcx, (%rdx)
movl %eax, %ebx
xorl %r14d, %r14d
xorl %ebp, %ebp
pushq $0x2
popq %r13
movl %r15d, %eax
cmpq %rax, %r14
jae 0x102530
movq 0x38(%rsp), %rax
movl (%rax,%r14,4), %r12d
shrl %r12d
movq 0x80(%rsp), %rdx
movq 0x10(%rdx), %rdx
imulq $0x30, %r12, %rsi
addq %rdx, %rsi
addq $0x18, %rsi
movq (%rsi), %rdi
xorl %r8d, %r8d
xorl %r9d, %r9d
movl %r8d, %r8d
cmpq %r8, %rdi
jbe 0x1023b0
movq 0x10(%rsi), %r10
imulq $0x28, %r8, %r11
cmpb $0x0, 0x18(%r10,%r11)
js 0x1023ab
addq %r11, %r10
incl %r9d
movl 0x8(%r10), %ebp
incl %r8d
jmp 0x102389
cmpl $0x1, %r9d
jne 0x102410
movl %ebp, %esi
imulq $0x30, %rsi, %rsi
addq %rsi, %rdx
addq $0x18, %rdx
movq (%rdx), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
movl %edi, %edi
cmpq %rdi, %rsi
jbe 0x1023f0
movq 0x10(%rdx), %r9
imulq $0x28, %rdi, %r10
cmpb $0x0, 0x18(%r9,%r10)
js 0x1023ec
addq %r10, %r9
incl %r8d
movl 0x8(%r9), %ecx
incl %edi
jmp 0x1023cb
cmpl $0x1, %r8d
jne 0x102410
cmpl %r12d, %ecx
jne 0x102410
xorl %ecx, %ecx
cmpq %rcx, %r14
je 0x10241e
movl (%rax,%rcx,4), %edx
shrl %edx
incq %rcx
cmpl %ebp, %edx
jne 0x1023fd
jmp 0x102413
movl %ecx, %r12d
incq %r14
movl %r12d, %ecx
jmp 0x102351
movq 0x88(%rsp), %r15
movq (%r15), %rcx
movq 0x10(%r15), %rax
cmpq 0x8(%r15), %rcx
jne 0x102455
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
cmoveq %r13, %rdx
movq %rdx, 0x8(%r15)
imulq $0x48, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
imulq $0x48, %rcx, %r15
addq %rax, %r15
cmpq %rbx, %rdx
jbe 0x10247c
andq $0x0, 0x10(%r15)
xorpd %xmm0, %xmm0
movupd %xmm0, (%r15)
xorl %eax, %eax
xorl %ecx, %ecx
jmp 0x102483
movq (%r15), %rcx
movq 0x8(%r15), %rax
movb $0x0, 0x20(%r15)
movabsq $0x100000001, %rdx      # imm = 0x100000001
movq %rdx, 0x18(%r15)
cmpq %rax, %rcx
jne 0x1024c0
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r13, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rcx
jmp 0x1024c4
movq 0x10(%r15), %rax
leaq 0x1(%rcx), %rdx
movq %rdx, (%r15)
movl %r12d, (%rax,%rcx,4)
cmpq 0x8(%r15), %rdx
jne 0x1024f7
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
cmoveq %r13, %rsi
movq %rsi, 0x8(%r15)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r15)
movq (%r15), %rdx
leaq 0x1(%rdx), %rcx
movq %rcx, (%r15)
movl %ebp, (%rax,%rdx,4)
xorl %edx, %edx
movl %edx, %edx
cmpq %rdx, %rcx
jbe 0x102526
movq 0x88(%rsp), %rdi
movl (%rdi), %esi
decl %esi
movq 0x18(%rdi), %rdi
movl (%rax,%rdx,4), %r8d
movl %esi, (%rdi,%r8,4)
incl %edx
jmp 0x102503
movl 0x18(%rsp), %r15d
jmp 0x102413
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq