// File: chhylp123[P]hifiasm/hic.cpp
// Function: init_contig_partition(H_partition*, ha_ug_index*, bubble_type*, hc_links*)
uint32_t init_contig_partition(H_partition* hap, ha_ug_index* idx, bubble_type* bub, hc_links* link)
{
    ///hc_links* link = idx->link;
    ma_ug_t *ug = idx->ug;
    bub_p_t_warp b;
    memset(&b, 0, sizeof(bub_p_t_warp));
    CALLOC(b.a, ug->g->n_seq*2); 
    uint32_t i, nv = ug->g->n_seq * 2, max_i, max_hap_label;
    for (i = 0; i < nv; i++)
    {
        b.a[i].w[0] = b.a[i].w[1] = b.a[i].nh = 0;
        b.a[i].p =b.a[i].d = b.a[i].nc = b.a[i].uc = b.a[i].ac = b.a[i].r = b.a[i].s = 0;
    }
    uint8_t* hap_label_flag = NULL;
    CALLOC(hap_label_flag, ug->g->n_seq);
    

    hap->n = ug->u.n;
    MALLOC(hap->hap, hap->n);
    memset(hap->hap, 0, hap->n*sizeof(uint32_t));
    MALLOC(hap->lock, hap->n);
    memset(hap->lock, 0, hap->n);
    hap->m[0] = 1; hap->m[1] = 2; hap->m[2] = 4;
    hap->link = link;
    hap->label = 0;
    hap->label_add = 8;//1000, for hap group
    for(hap->label_shift=1; (uint64_t)(1<<hap->label_shift)<(uint64_t)hap->label_add; hap->label_shift++);
    kv_init(hap->label_buffer);
    kv_init(hap->b.vis); kv_malloc(hap->b.vis, hap->n); hap->b.vis.n = hap->n;

    ///sorted by weight
    for (i = 0; i < bub->chain_weight.n; i++)
    {
        if(bub->chain_weight.a[i].del) continue;
        phase_bubble_chain(hap, ug, &b, bub, hap_label_flag, bub->chain_weight.a[i].id);
        ///print_chain_phasing(hap, ug, bub, bub->chain_weight.a[i].id);
    }
    
    memset(hap_label_flag, 1, ug->g->n_seq);
    while (1)
    {
        max_i = get_unset_com(hap, bub, ug, hap_label_flag, &max_hap_label);
        if(max_i == (uint32_t)-1) break;
        phase_com(hap, ug, &b, bub, max_i, max_hap_label);
    }

    for (i = 0; i < hap->n; i++)
    {
        if((hap->hap[i]&hap->m[0])&&(hap->hap[i]&hap->m[1]))
        {
            hap->hap[i] >>= hap->label_shift;
            hap->hap[i] <<= hap->label_shift;
            hap->hap[i] |= hap->m[2];
            reset_ambiguous_label(hap, hap_label_flag, i);
        }
    }

    free(b.a); free(b.S.a); free(b.T.a); free(b.b.a); free(b.e.a);

    init_G_partition(&(hap->g_p), hap->n);

    link_phase_group(hap, bub);

    assign_per_unitig_G_partition(&(hap->g_p), hap->n, link, bub, 0);///warp unitigs

    adjust_contig_partition(hap, link);

    update_bubble_chain(ug, bub, 0, 1);

    resolve_bubble_chain_tangle(ug, bub);

    clean_bubble_chain_by_HiC(ug, link, bub);

    append_boundary_chain(ug, link, bub);

    sort_bubble_ele(&(hap->g_p), link, bub, hap->n);

    free(hap_label_flag);
    return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x28(%rbp)
leaq -0x98(%rbp), %rdi
xorl %esi, %esi
movl $0x70, %edx
callq 0x2100
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movslq %eax, %rdi
movl $0x38, %esi
callq 0x2140
movq %rax, -0x98(%rbp)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0xa0(%rbp)
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0xa0(%rbp), %eax
jae 0x1f5877
movq -0x98(%rbp), %rax
movl -0x9c(%rbp), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rax)
movq -0x98(%rbp), %rax
movl -0x9c(%rbp), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x20(%rax)
movq -0x98(%rbp), %rax
movl -0x9c(%rbp), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x18(%rax)
movq -0x98(%rbp), %rax
movl -0x9c(%rbp), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl 0x30(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x0, %ecx
movl %ecx, 0x30(%rax)
movq -0x98(%rbp), %rax
movl -0x9c(%rbp), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl 0x30(%rax), %ecx
andl $0x80000000, %ecx       # imm = 0x80000000
orl $0x0, %ecx
movl %ecx, 0x30(%rax)
movq -0x98(%rbp), %rax
movl -0x9c(%rbp), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x2c(%rax)
movq -0x98(%rbp), %rax
movl -0x9c(%rbp), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x28(%rax)
movq -0x98(%rbp), %rax
movl -0x9c(%rbp), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x8(%rax)
movq -0x98(%rbp), %rax
movl -0x9c(%rbp), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl $0x0, 0x4(%rax)
movq -0x98(%rbp), %rax
movl -0x9c(%rbp), %ecx
imulq $0x38, %rcx, %rcx
addq %rcx, %rax
movl $0x0, (%rax)
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x1f5731
movq $0x0, -0xb0(%rbp)
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x2140
movq %rax, -0xb0(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
shlq $0x2, %rdi
callq 0x2270
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rdx
shlq $0x2, %rdx
xorl %esi, %esi
callq 0x2100
movq -0x8(%rbp), %rax
movq (%rax), %rdi
shlq $0x0, %rdi
callq 0x2270
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rdx
xorl %esi, %esi
callq 0x2100
movq -0x8(%rbp), %rax
movl $0x1, 0x18(%rax)
movq -0x8(%rbp), %rax
movl $0x2, 0x1c(%rax)
movq -0x8(%rbp), %rax
movl $0x4, 0x20(%rax)
movq -0x20(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rax
movl $0x0, 0x24(%rax)
movq -0x8(%rbp), %rax
movl $0x8, 0x28(%rax)
movq -0x8(%rbp), %rax
movl $0x1, 0x2c(%rax)
movq -0x8(%rbp), %rax
movl 0x2c(%rax), %ecx
movl $0x1, %eax
shll %cl, %eax
cltq
movq -0x8(%rbp), %rcx
movl 0x28(%rcx), %ecx
cmpq %rcx, %rax
jae 0x1f5996
jmp 0x1f5987
movq -0x8(%rbp), %rax
movl 0x2c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x2c(%rax)
jmp 0x1f5969
movq -0x8(%rbp), %rax
movq $0x0, 0x80(%rax)
movq -0x8(%rbp), %rax
movq $0x0, 0x78(%rax)
movq -0x8(%rbp), %rax
movq $0x0, 0x88(%rax)
movq -0x8(%rbp), %rax
movq $0x0, 0x98(%rax)
movq -0x8(%rbp), %rax
movq $0x0, 0x90(%rax)
movq -0x8(%rbp), %rax
movq $0x0, 0xa0(%rax)
movq -0x8(%rbp), %rax
movq $0x0, 0x90(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x98(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rdi
shlq $0x0, %rdi
callq 0x2270
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0xa0(%rax)
movq -0x8(%rbp), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x90(%rax)
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq 0xc8(%rcx), %rax
jae 0x1f5acd
movq -0x18(%rbp), %rax
movq 0xd8(%rax), %rax
movl -0x9c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpb $0x0, 0x18(%rax)
je 0x1f5a7b
jmp 0x1f5ab9
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x18(%rbp), %rcx
movq -0xb0(%rbp), %r8
movq -0x18(%rbp), %rax
movq 0xd8(%rax), %rax
movl -0x9c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rax
movq 0x10(%rax), %rax
movl %eax, %r9d
leaq -0x98(%rbp), %rdx
callq 0x1ef990
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x1f5a48
movq -0xb0(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movl %eax, %edx
movl $0x1, %esi
callq 0x2100
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0xb0(%rbp), %rcx
leaq -0xa8(%rbp), %r8
callq 0x1edde0
movl %eax, -0xa4(%rbp)
cmpl $-0x1, -0xa4(%rbp)
jne 0x1f5b22
jmp 0x1f5b4a
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x18(%rbp), %rcx
movl -0xa4(%rbp), %r8d
movl -0xa8(%rbp), %r9d
leaq -0x98(%rbp), %rdx
callq 0x1ede90
jmp 0x1f5af2
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1f5c46
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x9c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq -0x8(%rbp), %rcx
andl 0x18(%rcx), %eax
cmpl $0x0, %eax
je 0x1f5c30
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x9c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movq -0x8(%rbp), %rcx
andl 0x1c(%rcx), %eax
cmpl $0x0, %eax
je 0x1f5c30
movq -0x8(%rbp), %rax
movl 0x2c(%rax), %ecx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x9c(%rbp), %edx
movq %rdx, -0xc0(%rbp)
movl (%rax,%rdx,4), %edx
shrl %cl, %edx
movq -0xc0(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rax
movl 0x2c(%rax), %ecx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x9c(%rbp), %edx
movq %rdx, -0xb8(%rbp)
movl (%rax,%rdx,4), %edx
shll %cl, %edx
movq -0xb8(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rax
movl 0x20(%rax), %edx
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x9c(%rbp), %ecx
orl (%rax,%rcx,4), %edx
movl %edx, (%rax,%rcx,4)
movq -0x8(%rbp), %rdi
movq -0xb0(%rbp), %rsi
movl -0x9c(%rbp), %edx
callq 0x1edd40
jmp 0x1f5c32
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x1f5b54
movq -0x98(%rbp), %rdi
callq 0x2210
movq -0x80(%rbp), %rdi
callq 0x2210
movq -0x68(%rbp), %rdi
callq 0x2210
movq -0x50(%rbp), %rdi
callq 0x2210
movq -0x38(%rbp), %rdi
callq 0x2210
movq -0x8(%rbp), %rdi
addq $0x38, %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rsi
callq 0x1d9a60
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1f3220
movq -0x8(%rbp), %rdi
addq $0x38, %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rsi
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rcx
xorl %r8d, %r8d
callq 0x1f3f50
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x1ee7e0
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x1e4260
movq -0x28(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x1e2f30
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x1e6e30
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x1e81d0
movq -0x8(%rbp), %rdi
addq $0x38, %rdi
movq -0x20(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x8(%rbp), %rax
movq (%rax), %rax
movl %eax, %ecx
callq 0x1f5520
movq -0xb0(%rbp), %rdi
callq 0x2210
movl $0x1, %eax
addq $0xc0, %rsp
popq %rbp
retq
nop