// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/hic.cpp
// Function: link_phase_group(H_partition*, bubble_type*)
void link_phase_group(H_partition* hap, bubble_type* bub)
{
    double index_time = yak_realtime();
    uint32_t i, k, n = (hap->label>>hap->label_shift)+1, *h0, h0_n, *h1, h1_n;;
    init_G_partition(&(hap->group_g_p), hap->n);
    partition_warp *res = NULL;
    for (i = 0; i < n; i++)///how many haplotype group
    {
        kv_pushp(partition_warp, hap->group_g_p, &res);
        kv_init(res->a);
        res->full_bub = 0;
        res->h[0] = res->h[1] = 0;
        res->status[0] = 1; res->status[1] = -1;
        res->weight[0] = res->weight[1] = res->weight_convex = 0;
        ///all unitigs
        for (k = 0; k < hap->n; k++)
        {
            if(get_phase_group(hap, k) == i && get_phase_status(hap, k) == 1)
            {
                kv_push(uint32_t, res->a, k);
                res->h[0]++;
            }
        }

        for (k = 0; k < hap->n; k++)
        {
            if(get_phase_group(hap, k) == i && get_phase_status(hap, k) == -1)
            {
                kv_push(uint32_t, res->a, k);
                res->h[1]++;
            }
        }

        for (k = 0; k < res->h[0]; k++)
        {
            ///if(hap->group_g_p.index[res->a.a[k]] != (uint32_t)-1) fprintf(stderr, "ERROR---00\n");
            hap->group_g_p.index[res->a.a[k]] = hap->group_g_p.n-1;
            hap->group_g_p.index[res->a.a[k]] = hap->group_g_p.index[res->a.a[k]] << 1;
        } 

        for (; k < res->a.n; k++)
        {
            ///if(hap->group_g_p.index[res->a.a[k]] != (uint32_t)-1) fprintf(stderr, "ERROR---11\n");
            hap->group_g_p.index[res->a.a[k]] = hap->group_g_p.n-1;
            hap->group_g_p.index[res->a.a[k]] = (hap->group_g_p.index[res->a.a[k]] << 1) + 1;
        }

        get_phased_block(&(hap->group_g_p), NULL, i, NULL, NULL, &h0, &h0_n, &h1, &h1_n, NULL, NULL);
        if(h0_n >0) res->weight[0] = get_cluster_weight(hap, hap->link, h0, h0_n); 
        if(h1_n >0) res->weight[1] = get_cluster_weight(hap, hap->link, h1, h1_n); 
        res->weight_convex = get_cluster_inner_weight(hap, hap->link, h0, h0_n, h1, h1_n);
    }

    fprintf(stderr, "[M::%s::%.3f]\n", __func__, yak_realtime()-index_time);
    // for (i = 0; i < n; i++)
    // {
    //     double w0 = 0, w1 = 0;
    //     res = &(hap->group_g_p.a[i]);
    //     fprintf(stderr, "%u-th group: # %d = %u, # %d = %u\n", i,
    //     res->status[0], res->h[0], res->status[1], res->h[1]);
    //     get_phased_block(&(hap->group_g_p), NULL, i, NULL, NULL, &h0, &h0_n, &h1, &h1_n, NULL, NULL);
    //     if(h0_n >0) w0 = get_cluster_weight_debug(&(hap->group_g_p), hap->link, h0, h0_n); 
    //     if(h1_n >0) w1 = get_cluster_weight_debug(&(hap->group_g_p), hap->link, h1, h1_n); 
    //     if(w0 != res->weight[0]) fprintf(stderr, "i: %u, ERROR: w0: %f, weight[0]: %f\n", i, w0, res->weight[0]);
    //     if(w1 != res->weight[1]) fprintf(stderr, "i: %u, ERROR: w1: %f, weight[1]: %f\n", i, w1, res->weight[1]);


    //     for (k = 0; k < res->h[0]; k++)
    //     {
    //         fprintf(stderr, "%d: utg%.6ul\n", res->status[0], int(res->a.a[k]+1));
    //     }

    //     for (; k < res->a.n; k++)
    //     {
    //         fprintf(stderr, "%d: utg%.6ul\n", res->status[1], int(res->a.a[k]+1));
    //     }
    // }

    /*******************************for debug************************************/
    // for (i = 0; i < hap->n; i++)
    // {
    //     if(hap->link->a.a[i].e.n == 0) continue;
    //     if(get_phase_status(hap, i) == -2)
    //     {
    //         fprintf(stderr, "ERROR+++: i: %u, group: %u, bub->index: %u\n", i, get_phase_group(hap, i), bub->index[i]);
    //         for (k = 0; k < hap->link->a.a[i].e.n; k++)
    //         {
    //             fprintf(stderr, "k: %u, uID: %u, weight: %f, del: %u\n", k, hap->link->a.a[i].e.a[k].uID, 
    //                         hap->link->a.a[i].e.a[k].weight, hap->link->a.a[i].e.a[k].del);   
    //         }
    //     } 
    // }
    /*******************************for debug************************************/
    

    flip_by_chain(hap, &(hap->group_g_p), bub);
    ///print_phase_group(&(hap->group_g_p), bub, "Large");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, 0x28(%rsp)
movq %rdi, %r14
callq 0x1365ef
movl 0x24(%r14), %ebp
movb 0x2c(%r14), %cl
shrl %cl, %ebp
movsd %xmm0, 0x20(%rsp)
incl %ebp
movq (%r14), %r15
leaq (,%r15,4), %rbx
movq %rbx, %rdi
callq 0x2280
movq %r15, %r9
andq $0x0, 0x68(%r14)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x58(%r14)
movq %rax, 0x70(%r14)
testq %r15, %r15
je 0x1839a
movq %rax, %rdi
movl $0xff, %esi
movq %rbx, %rdx
movq %r9, %rbx
callq 0x20e0
movq %rbx, %r9
leaq 0x58(%r14), %rax
movq %rax, 0x10(%rsp)
testl %ebp, %ebp
je 0x186a4
movl %ebp, %eax
movq %rax, 0x30(%rsp)
xorl %r8d, %r8d
pushq $0x2
popq %r15
movq %r9, %r13
xorl %ebp, %ebp
movq 0x58(%r14), %rax
cmpq 0x60(%r14), %rax
jne 0x183f4
leaq (%rax,%rax), %rcx
testq %rax, %rax
cmoveq %r15, %rcx
movq %rcx, 0x60(%r14)
imulq $0x48, %rcx, %rsi
movq %r8, %rdi
callq 0x2370
movq %rax, %r8
movq %rax, 0x68(%r14)
movq (%r14), %r9
movq 0x58(%r14), %rax
movq %r9, %r13
leaq 0x1(%rax), %rcx
movq 0x10(%rsp), %rdx
movq %rcx, (%rdx)
imulq $0x48, %rax, %rcx
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%r8,%rcx)
movupd %xmm0, (%r8,%rcx)
movb $0x0, 0x20(%r8,%rcx)
movabsq $-0xffffffff, %rax      # imm = 0xFFFFFFFF00000001
movq %rax, 0x24(%r8,%rcx)
andq $0x0, 0x40(%r8,%rcx)
leaq (%r8,%rcx), %r12
movupd %xmm0, 0x30(%r8,%rcx)
testq %r13, %r13
je 0x18551
movq %rcx, 0x8(%rsp)
movq %r8, (%rsp)
xorl %esi, %esi
xorl %eax, %eax
xorl %edx, %edx
xorl %ecx, %ecx
xorl %ebx, %ebx
movq 0x10(%r14), %rdi
movl (%rdi,%rcx,4), %edi
movb 0x2c(%r14), %cl
movl %edi, %r8d
shrl %cl, %r8d
cmpq %r8, %rbp
jne 0x184cd
testl %edi, 0x20(%r14)
jne 0x184cd
movl 0x1c(%r14), %ecx
andl %edi, %ecx
jne 0x184cd
andl 0x18(%r14), %edi
je 0x184cd
cmpq 0x8(%r12), %rdx
jne 0x184b0
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
cmoveq %r15, %rsi
movq %rsi, 0x8(%r12)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r12)
movq (%r12), %rdx
movq (%r14), %r9
leaq 0x1(%rdx), %rcx
movq %rcx, (%r12)
movl %ebx, (%rax,%rdx,4)
movl 0x18(%r12), %esi
incl %esi
movl %esi, 0x18(%r12)
movq %r9, %r13
movq %rcx, %rdx
incl %ebx
movq %rbx, %rcx
cmpq %rbx, %r13
ja 0x18457
testq %r13, %r13
je 0x1855b
xorl %ebx, %ebx
movq 0x10(%r14), %rcx
movl (%rcx,%rbx,4), %esi
movb 0x2c(%r14), %cl
movl %esi, %edi
shrl %cl, %edi
cmpq %rdi, %rbp
jne 0x18540
testl %esi, 0x20(%r14)
jne 0x18540
andl 0x1c(%r14), %esi
je 0x18540
cmpq 0x8(%r12), %rdx
jne 0x1852d
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
cmoveq %r15, %rsi
movq %rsi, 0x8(%r12)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r12)
movq (%r12), %rdx
movq (%r14), %r9
leaq 0x1(%rdx), %rcx
movq %rcx, (%r12)
movl %ebx, (%rax,%rdx,4)
incl 0x1c(%r12)
movq %rcx, %rdx
incl %ebx
cmpq %rbx, %r9
ja 0x184de
movl 0x18(%r12), %esi
movq %r9, %r13
jmp 0x1855e
movq %r9, 0x18(%rsp)
xorl %r13d, %r13d
jmp 0x185d8
xorl %r13d, %r13d
testl %esi, %esi
je 0x1858d
movl 0x58(%r14), %esi
decl %esi
movq 0x70(%r14), %rdi
xorl %ecx, %ecx
movl (%rax,%rcx,4), %r8d
movl %esi, (%rdi,%r8,4)
movl (%rax,%rcx,4), %r8d
shll (%rdi,%r8,4)
incq %rcx
movl 0x18(%r12), %r8d
cmpq %r8, %rcx
jb 0x1856e
jmp 0x1858f
xorl %ecx, %ecx
movl %ecx, %esi
cmpq %rsi, %rdx
movq %r9, 0x18(%rsp)
jbe 0x185cf
movl 0x58(%r14), %ecx
decl %ecx
movq 0x70(%r14), %rdi
leal 0x1(%rsi), %r8d
movl (%rax,%rsi,4), %r9d
movl %ecx, (%rdi,%r9,4)
movl (%rax,%rsi,4), %esi
movl (%rdi,%rsi,4), %r9d
leal 0x1(,%r9,2), %r9d
movl %r9d, (%rdi,%rsi,4)
movl %r8d, %esi
incl %r8d
cmpq %rsi, %rdx
ja 0x185a9
movq (%rsp), %r8
movq 0x8(%rsp), %rcx
movq 0x30(%r14), %rsi
movq 0x68(%r14), %rdi
imulq $0x48, %rbp, %rax
movq 0x10(%rdi,%rax), %rdx
movl 0x18(%rdi,%rax), %ebx
leaq (%rdx,%rbx,4), %r9
movq %rdi, 0x8(%rsp)
movl 0x1c(%rdi,%rax), %r15d
testl %r15d, %r15d
movl $0x0, %eax
cmovneq %r9, %rax
movq %rax, 0x50(%rsp)
movq %rbx, %rax
testq %rbx, %rbx
cmovneq %rdx, %rax
movq %rax, 0x48(%rsp)
movq %rsi, (%rsp)
je 0x1864f
leaq (%r8,%rcx), %rax
addq $0x30, %rax
movq %rax, 0x38(%rsp)
movq %r14, %rdi
movl %ebx, %ecx
movq %r9, 0x40(%rsp)
callq 0x153e0c
movq 0x40(%rsp), %r9
movq (%rsp), %rsi
movq 0x38(%rsp), %rax
movsd %xmm0, (%rax)
testl %r15d, %r15d
je 0x1866d
movq %r14, %rdi
movq %r9, %rdx
movl %r15d, %ecx
callq 0x153e0c
movq (%rsp), %rsi
movsd %xmm0, 0x38(%r12)
movq 0x48(%rsp), %rdx
movl %ebx, %ecx
movq 0x50(%rsp), %r8
movl %r15d, %r9d
callq 0x153edf
movsd %xmm0, 0x40(%r12)
incq %rbp
cmpq 0x30(%rsp), %rbp
pushq $0x2
popq %r15
movq 0x18(%rsp), %r9
movq 0x8(%rsp), %r8
jne 0x183be
movq 0x27c935(%rip), %rax    # 0x294fe0
movq (%rax), %rbx
callq 0x1365ef
subsd 0x20(%rsp), %xmm0
leaq 0x22352b(%rip), %rsi    # 0x23bbeb
leaq 0x223a17(%rip), %rdx    # 0x23c0de
movq %rbx, %rdi
movb $0x1, %al
callq 0x22b0
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x156719