// File: /workspace/github_cpp_makefile_star2_O2/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, 0x10(%rsp)
movq %rdi, %r14
callq 0xefea7
movl 0x24(%r14), %eax
movb 0x2c(%r14), %cl
shrl %cl, %eax
movsd %xmm0, 0x8(%rsp)
incl %eax
movq %rax, 0x18(%rsp)
leaq 0x58(%r14), %rdi
movq (%r14), %rsi
movq %rdi, (%rsp)
callq 0x1018c5
xorl %r15d, %r15d
pushq $0x2
popq %rbx
cmpq 0x18(%rsp), %r15
je 0x10fa9d
movq 0x58(%r14), %rcx
movq 0x68(%r14), %rax
cmpq 0x60(%r14), %rcx
jne 0x10f87c
leaq (%rcx,%rcx), %rdx
testq %rcx, %rcx
cmoveq %rbx, %rdx
movq %rdx, 0x60(%r14)
imulq $0x48, %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x68(%r14)
movq 0x58(%r14), %rcx
leaq 0x1(%rcx), %rdx
movq (%rsp), %rsi
movq %rdx, (%rsi)
imulq $0x48, %rcx, %rcx
leaq (%rax,%rcx), %r13
addq %rax, %rcx
addq $0x30, %rcx
xorpd %xmm0, %xmm0
movupd %xmm0, -0x20(%rcx)
movupd %xmm0, -0x30(%rcx)
movb $0x0, -0x10(%rcx)
movabsq $-0xffffffff, %rax      # imm = 0xFFFFFFFF00000001
movq %rax, -0xc(%rcx)
movupd %xmm0, (%rcx)
movq %rcx, 0x28(%rsp)
andq $0x0, 0x10(%rcx)
xorl %ebp, %ebp
movl %ebp, %ecx
movq (%r14), %rax
cmpq %rcx, %rax
jbe 0x10f935
movq 0x10(%r14), %rax
movl (%rax,%rcx,4), %eax
movb 0x2c(%r14), %cl
shrl %cl, %eax
cmpq %rax, %r15
jne 0x10f931
movq %r14, %rdi
movl %ebp, %esi
callq 0x121555
cmpl $0x1, %eax
jne 0x10f931
movq (%r13), %rcx
movq 0x10(%r13), %rax
cmpq 0x8(%r13), %rcx
jne 0x10f922
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %rbx, %rsi
movq %rsi, 0x8(%r13)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r13)
movq (%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r13)
movl %ebp, (%rax,%rcx,4)
incl 0x18(%r13)
incl %ebp
jmp 0x10f8c6
xorl %ebp, %ebp
movl %ebp, %ecx
cmpq %rcx, %rax
jbe 0x10f9a6
movq 0x10(%r14), %rax
movl (%rax,%rcx,4), %eax
movb 0x2c(%r14), %cl
shrl %cl, %eax
cmpq %rax, %r15
jne 0x10f99f
movq %r14, %rdi
movl %ebp, %esi
callq 0x121555
cmpl $-0x1, %eax
jne 0x10f99f
movq (%r13), %rcx
movq 0x10(%r13), %rax
cmpq 0x8(%r13), %rcx
jne 0x10f990
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %rbx, %rsi
movq %rsi, 0x8(%r13)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r13)
movq (%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r13)
movl %ebp, (%rax,%rcx,4)
incl 0x1c(%r13)
incl %ebp
movq (%r14), %rax
jmp 0x10f937
xorl %eax, %eax
movl 0x18(%r13), %ecx
cmpq %rcx, %rax
jae 0x10f9d0
movl 0x58(%r14), %ecx
decl %ecx
movq 0x70(%r14), %rdx
movq 0x10(%r13), %rsi
movl (%rsi,%rax,4), %edi
movl %ecx, (%rdx,%rdi,4)
movl (%rsi,%rax,4), %ecx
shll (%rdx,%rcx,4)
incq %rax
jmp 0x10f9a8
movq (%r13), %rcx
movl %eax, %eax
cmpq %rax, %rcx
jbe 0x10fa05
movl 0x58(%r14), %edx
decl %edx
movq 0x70(%r14), %rsi
movq 0x10(%r13), %rdi
movl (%rdi,%rax,4), %r8d
movl %edx, (%rsi,%r8,4)
movl (%rdi,%rax,4), %edx
movl (%rsi,%rdx,4), %edi
leal 0x1(,%rdi,2), %edi
movl %edi, (%rsi,%rdx,4)
incl %eax
jmp 0x10f9d4
movq 0x30(%r14), %rsi
movq 0x68(%r14), %rax
imulq $0x48, %r15, %rcx
movq 0x10(%rax,%rcx), %rdx
movl 0x18(%rax,%rcx), %r12d
leaq (%rdx,%r12,4), %r8
movl 0x1c(%rax,%rcx), %ebp
testl %ebp, %ebp
movl $0x0, %eax
cmovneq %r8, %rax
movq %rax, 0x30(%rsp)
movq %r12, %rbx
testq %r12, %r12
cmovneq %rdx, %rbx
je 0x10fa61
movq %r14, %rdi
movl %r12d, %ecx
movq %r8, 0x20(%rsp)
callq 0x10c812
movq 0x20(%rsp), %r8
movq 0x28(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x30(%r14), %rsi
testl %ebp, %ebp
je 0x10fa7c
movq %r14, %rdi
movq %r8, %rdx
movl %ebp, %ecx
callq 0x10c812
movsd %xmm0, 0x38(%r13)
movq 0x30(%r14), %rsi
movq %rbx, %rdx
movl %r12d, %ecx
movq 0x30(%rsp), %r8
movl %ebp, %r9d
callq 0x10c8e2
movsd %xmm0, 0x40(%r13)
incq %r15
jmp 0x10f83d
movq 0x14353c(%rip), %rax    # 0x252fe0
movq (%rax), %rbx
callq 0xefea7
subsd 0x8(%rsp), %xmm0
leaq 0xde022(%rip), %rsi     # 0x1edadb
leaq 0xde50e(%rip), %rdx     # 0x1edfce
movq %rbx, %rdi
movb $0x1, %al
callq 0x22b0
movq %r14, %rdi
movq (%rsp), %rsi
movq 0x10(%rsp), %rdx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10f508