// File: /workspace/github_cpp_makefile_star2_O1/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 $0x38, %rsp
movq %rsi, 0x20(%rsp)
movq %rdi, %r14
callq 0x1303c7
movl 0x24(%r14), %ebp
movb 0x2c(%r14), %cl
shrl %cl, %ebp
movsd %xmm0, 0x18(%rsp)
incl %ebp
movq (%r14), %r15
leaq (,%r15,4), %rbx
movq %rbx, %rdi
callq 0x2280
andq $0x0, 0x68(%r14)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x58(%r14)
movq %rax, 0x70(%r14)
testq %r15, %r15
je 0x18bbd
movq %rax, %rdi
movl $0xff, %esi
movq %rbx, %rdx
callq 0x20e0
leaq 0x58(%r14), %rax
movq %rax, 0x10(%rsp)
testl %ebp, %ebp
je 0x18e66
movl %ebp, %eax
movq %rax, 0x28(%rsp)
xorl %r15d, %r15d
pushq $0x2
popq %r12
movq 0x60(%r14), %rax
cmpq %rax, 0x58(%r14)
jne 0x18c06
leaq (%rax,%rax), %rcx
testq %rax, %rax
cmoveq %r12, %rcx
movq %rcx, 0x60(%r14)
movq 0x68(%r14), %rdi
imulq $0x48, %rcx, %rsi
callq 0x2370
movq %rax, 0x68(%r14)
movq 0x58(%r14), %rax
movq 0x68(%r14), %rdx
leaq 0x1(%rax), %rcx
movq %rcx, 0x58(%r14)
imulq $0x48, %rax, %rcx
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rdx,%rcx)
movupd %xmm0, (%rdx,%rcx)
movb $0x0, 0x20(%rdx,%rcx)
movabsq $-0xffffffff, %rax      # imm = 0xFFFFFFFF00000001
movq %rax, 0x24(%rdx,%rcx)
andq $0x0, 0x40(%rdx,%rcx)
leaq (%rdx,%rcx), %rbp
movq %rdx, 0x8(%rsp)
movq %rcx, 0x30(%rsp)
movupd %xmm0, 0x30(%rdx,%rcx)
cmpq $0x0, (%r14)
je 0x18cce
xorl %ebx, %ebx
movq 0x10(%r14), %rax
movl (%rax,%rbx,4), %eax
movb 0x2c(%r14), %cl
movl %eax, %edx
shrl %cl, %edx
cmpq %rdx, %r15
jne 0x18cc7
testl %eax, 0x20(%r14)
jne 0x18cc7
movl 0x1c(%r14), %ecx
andl %eax, %ecx
jne 0x18cc7
andl 0x18(%r14), %eax
je 0x18cc7
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0x18cb1
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r12, %rsi
movq %rsi, 0x8(%rbp)
movq 0x10(%rbp), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%rbp)
movq (%rbp), %rax
movq 0x10(%rbp), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbp)
movl %ebx, (%rcx,%rax,4)
incl 0x18(%rbp)
incl %ebx
cmpq %rbx, (%r14)
ja 0x18c5f
cmpq $0x0, (%r14)
je 0x18d3d
xorl %ebx, %ebx
movq 0x10(%r14), %rax
movl (%rax,%rbx,4), %eax
movb 0x2c(%r14), %cl
movl %eax, %edx
shrl %cl, %edx
cmpq %rdx, %r15
jne 0x18d36
testl %eax, 0x20(%r14)
jne 0x18d36
andl 0x1c(%r14), %eax
je 0x18d36
movq 0x8(%rbp), %rax
cmpq %rax, (%rbp)
jne 0x18d20
leaq (%rax,%rax), %rsi
testq %rax, %rax
cmoveq %r12, %rsi
movq %rsi, 0x8(%rbp)
movq 0x10(%rbp), %rdi
shlq $0x2, %rsi
callq 0x2370
movq %rax, 0x10(%rbp)
movq (%rbp), %rax
movq 0x10(%rbp), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, (%rbp)
movl %ebx, (%rcx,%rax,4)
incl 0x1c(%rbp)
incl %ebx
cmpq %rbx, (%r14)
ja 0x18cd6
cmpl $0x0, 0x18(%rbp)
je 0x18d6c
movl 0x58(%r14), %eax
decl %eax
movq 0x70(%r14), %rdx
movq 0x10(%rbp), %rsi
xorl %ecx, %ecx
movl (%rsi,%rcx,4), %edi
movl %eax, (%rdx,%rdi,4)
movl (%rsi,%rcx,4), %edi
shll (%rdx,%rdi,4)
incq %rcx
movl 0x18(%rbp), %edi
cmpq %rdi, %rcx
jb 0x18d53
jmp 0x18d6e
xorl %ecx, %ecx
movq (%rbp), %rax
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x18db1
movl 0x58(%r14), %edx
decl %edx
movq 0x70(%r14), %rsi
movq 0x10(%rbp), %rdi
leal 0x1(%rcx), %r8d
movl (%rdi,%rcx,4), %r9d
movl %edx, (%rsi,%r9,4)
movl (%rdi,%rcx,4), %ecx
movl (%rsi,%rcx,4), %r9d
leal 0x1(,%r9,2), %r9d
movl %r9d, (%rsi,%rcx,4)
movl %r8d, %ecx
incl %r8d
cmpq %rcx, %rax
ja 0x18d8b
movq 0x68(%r14), %rax
imulq $0x48, %r15, %rcx
movq 0x10(%rax,%rcx), %r12
movl 0x18(%rax,%rcx), %r13d
leaq (%r12,%r13,4), %rdx
movl 0x1c(%rax,%rcx), %r9d
testl %r9d, %r9d
movl $0x0, %ebx
cmovneq %rdx, %rbx
testq %r13, %r13
cmoveq %r13, %r12
movl %r9d, 0x4(%rsp)
je 0x18e1c
movq 0x8(%rsp), %rax
movq 0x30(%rsp), %rcx
addq %rcx, %rax
addq $0x30, %rax
movq %rax, 0x8(%rsp)
movq 0x30(%r14), %rsi
movq %r14, %rdi
movq %r12, %rdx
movl %r13d, %ecx
callq 0x14d809
movl 0x4(%rsp), %r9d
movq 0x8(%rsp), %rax
movsd %xmm0, (%rax)
testl %r9d, %r9d
je 0x18e3d
movq 0x30(%r14), %rsi
movq %r14, %rdi
movq %rbx, %rdx
movl %r9d, %ecx
callq 0x14d809
movl 0x4(%rsp), %r9d
movsd %xmm0, 0x38(%rbp)
movq 0x30(%r14), %rsi
movq %r12, %rdx
movl %r13d, %ecx
movq %rbx, %r8
callq 0x14d8ef
movsd %xmm0, 0x40(%rbp)
incq %r15
cmpq 0x28(%rsp), %r15
pushq $0x2
popq %r12
jne 0x18bdc
movq 0x271173(%rip), %rax    # 0x289fe0
movq (%rax), %rbx
callq 0x1303c7
subsd 0x18(%rsp), %xmm0
leaq 0x216b19(%rip), %rsi    # 0x22f99b
leaq 0x217049(%rip), %rdx    # 0x22fed2
movq %rbx, %rdi
movb $0x1, %al
callq 0x22b0
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq 0x20(%rsp), %rdx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1501b5