// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rcx, 0x10(%rsp)
movq %rdx, %rbx
movq %rdi, %r15
movq (%rsi), %r12
xorps %xmm0, %xmm0
movups %xmm0, 0x70(%rsp)
movups %xmm0, 0x60(%rsp)
movups %xmm0, 0x50(%rsp)
movups %xmm0, 0x40(%rsp)
movups %xmm0, 0x30(%rsp)
andq $0x0, 0x80(%rsp)
movups %xmm0, 0x20(%rsp)
movq 0x18(%r12), %rax
movl 0x14(%rax), %r13d
leal (,%r13,2), %r14d
pushq $0x38
popq %rsi
movq %r14, %rdi
callq 0x2120
movq %rax, 0x18(%rsp)
imulq $0x38, %r14, %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x110bd3
andl $0x0, 0x8(%rax,%rdx)
andq $0x0, (%rax,%rdx)
addq $0x38, %rdx
jmp 0x110bbe
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
pushq $0x1
popq %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2120
movq %rax, %r13
movq (%r12), %r14
movq %r14, (%r15)
leaq (,%r14,4), %rsi
movq %rbp, %rdi
callq 0x2120
movq %rax, 0x10(%r15)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x2120
movq %rax, 0x8(%r15)
movq 0x10(%rsp), %rax
movq %rax, 0x30(%r15)
movaps 0xdc4ab(%rip), %xmm0    # 0x1ed0d0
movups %xmm0, 0x18(%r15)
movabsq $0x300000008, %rax      # imm = 0x300000008
movq %rax, 0x28(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x78(%r15)
andq $0x0, 0x88(%r15)
movq %r14, 0x98(%r15)
movq %r14, %rdi
callq 0x2280
movq %rax, 0xa0(%r15)
movq %r14, 0x90(%r15)
xorl %ebp, %ebp
leaq 0x18(%rsp), %r14
movl %ebp, %ebp
cmpq %rbp, 0xc8(%rbx)
jbe 0x110cab
movq 0xd8(%rbx), %rax
movq %rbp, %rcx
shlq $0x5, %rcx
cmpb $0x0, 0x18(%rax,%rcx)
jne 0x110ca7
addq %rcx, %rax
movl 0x10(%rax), %r9d
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq %r13, %r8
callq 0x10d676
incl %ebp
jmp 0x110c6c
movq 0x18(%r12), %rax
movl 0x14(%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
pushq $0x1
popq %rsi
movq %r13, %rdi
callq 0x20e0
leaq 0xc(%rsp), %r14
leaq 0x18(%rsp), %rbp
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r13, %rcx
movq %r14, %r8
callq 0x10c6bc
cmpl $-0x1, %eax
je 0x110d02
movl 0xc(%rsp), %r9d
movq %r15, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
movl %eax, %r8d
callq 0x10c730
jmp 0x110cce
xorl %ebp, %ebp
movl %ebp, %ebp
cmpq %rbp, (%r15)
jbe 0x110d48
movq 0x10(%r15), %rax
movl (%rax,%rbp,4), %edx
testl %edx, 0x18(%r15)
je 0x110d44
testl %edx, 0x1c(%r15)
je 0x110d44
movb 0x2c(%r15), %cl
shrl %cl, %edx
movl %edx, (%rax,%rbp,4)
movb 0x2c(%r15), %cl
shll %cl, %edx
movl %edx, (%rax,%rbp,4)
orl 0x20(%r15), %edx
movl %edx, (%rax,%rbp,4)
movq %r15, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x10c638
incl %ebp
jmp 0x110d04
movq 0x18(%rsp), %rdi
callq 0x2220
movq 0x30(%rsp), %rdi
callq 0x2220
movq 0x48(%rsp), %rdi
callq 0x2220
movq 0x60(%rsp), %rdi
callq 0x2220
movq 0x78(%rsp), %rdi
callq 0x2220
leaq 0x38(%r15), %rbp
movq (%r15), %rsi
movq %rbp, %rdi
callq 0x1018c5
movq %r15, %rdi
movq %rbx, %rsi
callq 0x10f7f8
movq (%r15), %rsi
movq %rbp, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x10ff14
movq %r15, %rdi
movq %r14, %rsi
callq 0x10cc2c
pushq $0x1
popq %rcx
movq %r12, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x1072aa
movq %r12, %rdi
movq %rbx, %rsi
callq 0x106889
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x108bc1
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1097b2
movl (%r15), %ecx
movq %rbp, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x110a23
movq %r13, %rdi
callq 0x2220
pushq $0x1
popq %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq