// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rax, 0x8(%rsp)
movl 0x14(%rax), %r13d
leal (%r13,%r13), %r14d
pushq $0x38
popq %rsi
movq %r14, %rdi
callq 0x2120
movq %rax, 0x18(%rsp)
testl %r14d, %r14d
je 0x189fc
imulq $0x38, %r14, %rcx
xorl %edx, %edx
andl $0x0, 0x8(%rax,%rdx)
andq $0x0, (%rax,%rdx)
addq $0x38, %rdx
cmpq %rdx, %rcx
jne 0x189e9
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 0x222792(%rip), %xmm0   # 0x23b1e0
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)
movq 0xc8(%rbx), %rax
testq %rax, %rax
movq 0x8(%rsp), %rcx
je 0x18aee
pushq $0x1
popq %r14
xorl %ecx, %ecx
leaq 0x18(%rsp), %rbp
movq 0xd8(%rbx), %rdx
shlq $0x5, %rcx
cmpb $0x0, 0x18(%rdx,%rcx)
jne 0x18ade
addq %rcx, %rdx
movl 0x10(%rdx), %r9d
movq %r15, %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %rbx, %rcx
movq %r13, %r8
callq 0x15498f
movq 0xc8(%rbx), %rax
movl %r14d, %ecx
incl %r14d
cmpq %rcx, %rax
ja 0x18aaa
movq 0x18(%r12), %rcx
movl 0x14(%rcx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
pushq $0x1
popq %rsi
movq %r13, %rdi
callq 0x20e0
leaq 0x4(%rsp), %r8
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x153cc4
cmpl $-0x1, %eax
je 0x18b59
leaq 0x18(%rsp), %r14
leaq 0x4(%rsp), %rbp
movl 0x4(%rsp), %r9d
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rbx, %rcx
movl %eax, %r8d
callq 0x153d41
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r13, %rcx
movq %rbp, %r8
callq 0x153cc4
cmpl $-0x1, %eax
jne 0x18b27
movq (%r15), %rax
testq %rax, %rax
je 0x18ba6
xorl %ebp, %ebp
movq 0x10(%r15), %rdx
movl (%rdx,%rbp,4), %esi
testl %esi, 0x18(%r15)
je 0x18b9f
testl %esi, 0x1c(%r15)
je 0x18b9f
movb 0x2c(%r15), %cl
shrl %cl, %esi
movl %esi, (%rdx,%rbp,4)
movb 0x2c(%r15), %cl
shll %cl, %esi
movl %esi, (%rdx,%rbp,4)
orl 0x20(%r15), %esi
movl %esi, (%rdx,%rbp,4)
movq %r15, %rdi
movq %r13, %rsi
movl %ebp, %edx
callq 0x153c3d
movq (%r15), %rax
incl %ebp
cmpq %rbp, %rax
ja 0x18b63
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), %rax
movq %rax, 0x8(%rsp)
movq (%r15), %rbp
leaq (,%rbp,4), %r14
movq %r14, %rdi
callq 0x2280
andq $0x0, 0x48(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x38(%r15)
movq %rax, 0x50(%r15)
testq %rbp, %rbp
je 0x18c1a
movq %rax, %rdi
movl $0xff, %esi
movq %r14, %rdx
callq 0x20e0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x18329
movq (%r15), %rsi
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq 0x10(%rsp), %rbp
movq %rbp, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x156d70
movq %r15, %rdi
movq %rbp, %rsi
callq 0x17d7e
pushq $0x1
popq %rcx
movq %r12, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x14f560
movq %r12, %rdi
movq %rbx, %rsi
callq 0x14ea6c
movq %r12, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x167e2
movq %r12, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x175ff
movl (%r15), %ecx
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x1882f
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