// File: 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
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
callq 0x1b7ca0
movsd %xmm0, -0x18(%rbp)
movq -0x8(%rbp), %rax
movl 0x24(%rax), %eax
movq -0x8(%rbp), %rcx
movl 0x2c(%rcx), %ecx
shrl %cl, %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movq -0x8(%rbp), %rdi
addq $0x58, %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rsi
callq 0x1d9a60
movq $0x0, -0x50(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0x1f371b
jmp 0x1f3284
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x8(%rbp), %rcx
cmpq 0x60(%rcx), %rax
jne 0x1f32ea
movq -0x8(%rbp), %rax
cmpq $0x0, 0x60(%rax)
je 0x1f32b2
movq -0x8(%rbp), %rax
movq 0x60(%rax), %rax
shlq %rax
movq %rax, -0x58(%rbp)
jmp 0x1f32bd
movl $0x2, %eax
movq %rax, -0x58(%rbp)
jmp 0x1f32bd
movq -0x58(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x8(%rbp), %rax
movq 0x68(%rax), %rdi
movq -0x8(%rbp), %rax
imulq $0x48, 0x60(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x8(%rbp), %rax
movq 0x68(%rax), %rax
movq -0x8(%rbp), %rdx
movq 0x58(%rdx), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x58(%rdx)
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x50(%rbp), %rax
movq $0x0, (%rax)
movq -0x50(%rbp), %rax
movq $0x0, 0x10(%rax)
movq -0x50(%rbp), %rax
movb $0x0, 0x20(%rax)
movq -0x50(%rbp), %rax
movl $0x0, 0x1c(%rax)
movq -0x50(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x50(%rbp), %rax
movl $0x1, 0x24(%rax)
movq -0x50(%rbp), %rax
movl $0xffffffff, 0x28(%rax) # imm = 0xFFFFFFFF
movq -0x50(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rax)
movq -0x50(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x38(%rax)
movq -0x50(%rbp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x30(%rax)
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1f3472
movq -0x8(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0x214d60
cmpl -0x1c(%rbp), %eax
jne 0x1f3462
movq -0x8(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0x2145e0
cmpl $0x1, %eax
jne 0x1f3462
jmp 0x1f33ce
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq -0x50(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x1f3436
movq -0x50(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1f33fb
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x60(%rbp)
jmp 0x1f3406
movl $0x2, %eax
movq %rax, -0x60(%rbp)
jmp 0x1f3406
movq -0x60(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x20(%rbp), %edx
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movl %edx, (%rax,%rcx,4)
movq -0x50(%rbp), %rax
movl 0x18(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x18(%rax)
jmp 0x1f3464
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1f3392
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1f3559
movq -0x8(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0x214d60
cmpl -0x1c(%rbp), %eax
jne 0x1f3549
movq -0x8(%rbp), %rdi
movl -0x20(%rbp), %esi
callq 0x2145e0
cmpl $-0x1, %eax
jne 0x1f3549
jmp 0x1f34b5
movq -0x50(%rbp), %rax
movq (%rax), %rax
movq -0x50(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jne 0x1f351d
movq -0x50(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1f34e2
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rax
shlq %rax
movq %rax, -0x68(%rbp)
jmp 0x1f34ed
movl $0x2, %eax
movq %rax, -0x68(%rbp)
jmp 0x1f34ed
movq -0x68(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x50(%rbp), %rax
movq 0x8(%rax), %rsi
shlq $0x2, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x10(%rax)
movl -0x20(%rbp), %edx
movq -0x50(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x50(%rbp), %rsi
movq (%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, (%rsi)
movl %edx, (%rax,%rcx,4)
movq -0x50(%rbp), %rax
movl 0x1c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x1c(%rax)
jmp 0x1f354b
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1f3479
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl 0x18(%rcx), %eax
jae 0x1f35d2
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
subq $0x1, %rax
movl %eax, %edx
movq -0x8(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x50(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x20(%rbp), %esi
movl (%rcx,%rsi,4), %ecx
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x50(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x20(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movl (%rax,%rcx,4), %edx
shll %edx
movq -0x8(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x50(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x20(%rbp), %esi
movl (%rcx,%rsi,4), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1f3560
jmp 0x1f35d4
movl -0x20(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1f3649
movq -0x8(%rbp), %rax
movq 0x58(%rax), %rax
subq $0x1, %rax
movl %eax, %edx
movq -0x8(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x50(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x20(%rbp), %esi
movl (%rcx,%rsi,4), %ecx
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x50(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x20(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movl (%rax,%rcx,4), %edx
shll %edx
addl $0x1, %edx
movq -0x8(%rbp), %rax
movq 0x70(%rax), %rax
movq -0x50(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl -0x20(%rbp), %esi
movl (%rcx,%rsi,4), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1f35d4
movq -0x8(%rbp), %rdi
addq $0x58, %rdi
movl -0x1c(%rbp), %eax
movl %eax, %edx
xorl %eax, %eax
movl %eax, %r8d
leaq -0x30(%rbp), %r9
leaq -0x34(%rbp), %r11
leaq -0x40(%rbp), %r10
leaq -0x44(%rbp), %rax
movq %r8, %rsi
movq %r8, %rcx
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
callq 0x214690
cmpl $0x0, -0x34(%rbp)
jbe 0x1f36bd
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rsi
movq -0x30(%rbp), %rdx
movl -0x34(%rbp), %ecx
callq 0x1edfb0
movq -0x50(%rbp), %rax
movsd %xmm0, 0x30(%rax)
cmpl $0x0, -0x44(%rbp)
jbe 0x1f36e4
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rsi
movq -0x40(%rbp), %rdx
movl -0x44(%rbp), %ecx
callq 0x1edfb0
movq -0x50(%rbp), %rax
movsd %xmm0, 0x38(%rax)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rsi
movq -0x30(%rbp), %rdx
movl -0x34(%rbp), %ecx
movq -0x40(%rbp), %r8
movl -0x44(%rbp), %r9d
callq 0x1ee160
movq -0x50(%rbp), %rax
movsd %xmm0, 0x40(%rax)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1f3276
movq 0x1d08be(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0x70(%rbp)
callq 0x1b7ca0
movq -0x70(%rbp), %rdi
subsd -0x18(%rbp), %xmm0
leaq 0x1990ca(%rip), %rsi    # 0x38c808
leaq 0x1995fa(%rip), %rdx    # 0x38cd3f
movb $0x1, %al
callq 0x22a0
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rsi
addq $0x58, %rsi
movq -0x10(%rbp), %rdx
callq 0x1f2ca0
addq $0xa0, %rsp
popq %rbp
retq
nopw (%rax,%rax)