// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movq %rcx, 0x10(%rsp)
movq %rdx, %rbx
movq %rdi, %r15
movq (%rsi), %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x20(%rsp)
movq 0x18(%r12), %rax
movl 0x14(%rax), %r14d
addl %r14d, %r14d
pushq $0x38
popq %rsi
movq %r14, %rdi
callq 0x2120
xorps %xmm0, %xmm0
movq %rax, 0x20(%rsp)
testl %r14d, %r14d
je 0x191bc
movl %r14d, %ecx
addq $0x10, %rax
imulq $0x38, %rcx, %rcx
xorl %edx, %edx
andl $0x0, -0x8(%rax,%rdx)
andq $0x0, -0x10(%rax,%rdx)
andl $0x0, 0x20(%rax,%rdx)
movups %xmm0, (%rax,%rdx)
movups %xmm0, 0x10(%rax,%rdx)
addq $0x38, %rdx
cmpq %rdx, %rcx
jne 0x1919a
movq 0x18(%r12), %rax
movl 0x14(%rax), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
pushq $0x1
popq %rsi
callq 0x2120
movq %rax, %r13
movq (%r12), %rbp
movq %rbp, (%r15)
leaq (,%rbp,4), %r14
movq %r14, %rdi
callq 0x2280
movq %rax, 0x10(%r15)
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x20e0
movq %rbp, %rdi
callq 0x2280
movq %rax, 0x8(%r15)
movq %rax, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x20e0
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0x18(%r15)
movl $0x4, 0x20(%r15)
movq 0x10(%rsp), %rax
movq %rax, 0x30(%r15)
movabsq $0x800000000, %rax      # imm = 0x800000000
movq %rax, 0x24(%r15)
movl $0x3, 0x2c(%r15)
xorps %xmm0, %xmm0
movups %xmm0, 0x98(%r15)
movups %xmm0, 0x78(%r15)
movups %xmm0, 0x88(%r15)
movq %rbp, 0x98(%r15)
movq %rbp, %rdi
callq 0x2280
movq %rax, 0xa0(%r15)
movq %rbp, 0x90(%r15)
cmpq $0x0, 0xc8(%rbx)
je 0x192ce
pushq $0x1
popq %rbp
xorl %eax, %eax
leaq 0x20(%rsp), %r14
movq 0xd8(%rbx), %rcx
shlq $0x5, %rax
cmpb $0x0, 0x18(%rcx,%rax)
jne 0x192c1
addq %rax, %rcx
movl 0x10(%rcx), %r9d
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq %r13, %r8
callq 0x14e3ea
movl %ebp, %eax
incl %ebp
cmpq %rax, 0xc8(%rbx)
ja 0x19294
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), %r8
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x14d6bf
cmpl $-0x1, %eax
je 0x1933e
leaq 0x20(%rsp), %r14
leaq 0xc(%rsp), %rbp
movl 0xc(%rsp), %r9d
movq %r15, %rdi
movq %r12, %rsi
movq %r14, %rdx
movq %rbx, %rcx
movl %eax, %r8d
callq 0x14d73c
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r13, %rcx
movq %rbp, %r8
callq 0x14d6bf
cmpl $-0x1, %eax
jne 0x1930c
cmpq $0x0, (%r15)
je 0x19386
xorl %ebp, %ebp
movq 0x10(%r15), %rax
movl (%rax,%rbp,4), %edx
testl %edx, 0x18(%r15)
je 0x1937f
testl %edx, 0x1c(%r15)
je 0x1937f
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 0x14d638
incl %ebp
cmpq %rbp, (%r15)
ja 0x19346
movq 0x20(%rsp), %rdi
callq 0x2220
movq 0x38(%rsp), %rdi
callq 0x2220
movq 0x50(%rsp), %rdi
callq 0x2220
movq 0x68(%rsp), %rdi
callq 0x2220
movq 0x80(%rsp), %rdi
callq 0x2220
leaq 0x38(%r15), %rax
movq %rax, 0x18(%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 0x193fd
movq %rax, %rdi
movl $0xff, %esi
movq %r14, %rdx
callq 0x20e0
movq %r15, %rdi
movq %rbx, %rsi
callq 0x18b55
movq (%r15), %rsi
movq 0x18(%rsp), %r14
movq %r14, %rdi
movq 0x10(%rsp), %rbp
movq %rbp, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x15085b
movq %r15, %rdi
movq %rbp, %rsi
callq 0x185e1
pushq $0x1
popq %rcx
movq %r12, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x148f46
movq %r12, %rdi
movq %rbx, %rsi
callq 0x1484d1
movq %r12, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x170a1
movq %r12, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x17e2e
movl (%r15), %ecx
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x18ff1
movq %r13, %rdi
callq 0x2220
pushq $0x1
popq %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq