// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/hic.cpp
// Function: adjust_contig_partition(H_partition*, hc_links*)
void adjust_contig_partition(H_partition* hap, hc_links* link)
{
    double index_time = yak_realtime();
    uint32_t i, k, *h0, h0_n, *h1, h1_n;
    uint32_t h0_status[4], h1_status[4], h0_status_max;
    int h0_h, h1_h;
    for (i = 0; i < hap->g_p.n; i++)
    {

        get_phased_block(&(hap->g_p), NULL, i, NULL, NULL, &h0, &h0_n, &h1, &h1_n, NULL, NULL);
        hap->g_p.a[i].status[0] = hap->g_p.a[i].status[1] = -2;
        hap->g_p.a[i].weight[0] = hap->g_p.a[i].weight[1] = hap->g_p.a[i].weight_convex = 0;


        h0_status[0] = h0_status[1] = h0_status[2] = h0_status[3] = 0;
        for (k = 0; k < h0_n; k++)
        {
            h0_status[get_phase_status(hap, h0[k])+2]++;
        }

        hap->g_p.a[i].weight[0] = get_cluster_weight(hap, link, h0, h0_n);
        if(h1_n == 0)
        {
            if(h0_status[0] == h0_n) ///unset, flag = -2
            {
                hap->g_p.a[i].status[0] = -2;
            }
            else
            {
                if(h0_status[1] > 0 || h0_status[3] > 0) ///phased flag = 1/-1
                {
                    h0_status[0] = h0_status[2] = 0;

                    h0_h = -2;
                    h0_status_max = 0;
                    for (k = 0; k < 4; k++)
                    {
                        if(h0_status[k] > h0_status_max)
                        {
                            h0_status_max = h0_status[k];
                            h0_h = (int)(k) - 2;
                        }
                    }
                    hap->g_p.a[i].status[0] = h0_h;
                }
                else if(h0_status[2] > 0) ///hom flag
                {
                    hap->g_p.a[i].status[0] = 0;
                }
                else  //unset flag
                {
                    hap->g_p.a[i].status[0] = -2;
                }
            }
        }
        else
        {
            hap->g_p.a[i].weight[1] = get_cluster_weight( hap, link, h1, h1_n);
            h1_status[0] = h1_status[1] = h1_status[2] = h1_status[3] = 0;
            for (k = 0; k < h1_n; k++)
            {
                h1_status[get_phase_status(hap, h1[k])+2]++;
            }


            h0_h = h1_h = 0;
            for (k = 0; k < 4; k++)
            {
                if(h0_status[k] == h0_n) h0_h = (int)(k) - 2;
                if(h1_status[k] == h1_n) h1_h = (int)(k) - 2;
            }

            if(h0_h * h1_h == -1)
            {
                hap->g_p.a[i].status[0] = h0_h;
                hap->g_p.a[i].status[1] = h1_h;
            }
            else
            {
                if(hap->g_p.a[i].weight[0] >= hap->g_p.a[i].weight[1])
                {
                    hap->g_p.a[i].status[0] = 1;
                    hap->g_p.a[i].status[1] = -1;
                }
                else
                {
                    hap->g_p.a[i].status[0] = -1;
                    hap->g_p.a[i].status[1] = 1;
                }
            }
        }
        hap->g_p.a[i].weight_convex = get_cluster_inner_weight(hap, link, h0, h0_n, h1, h1_n);
        update_partition_flag(hap, &(hap->g_p), link, i);
        ///update_partition_flag_debug(hap, &(hap->g_p), link, i);
    }

    for (i = 0; i < hap->g_p.n; i++)
    {
        get_phased_block(&(hap->g_p), NULL, i, NULL, NULL, &h0, &h0_n, &h1, &h1_n, NULL, NULL);
        hap->g_p.a[i].weight[0] = get_cluster_weight(hap, link, h0, h0_n);
        hap->g_p.a[i].weight[1] = get_cluster_weight(hap, link, h1, h1_n);
        hap->g_p.a[i].weight_convex = get_cluster_inner_weight(hap, link, h0, h0_n, h1, h1_n);
    }
    fprintf(stderr, "[M::%s::%.3f]\n", __func__, yak_realtime()-index_time);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rsi, 0x8(%rsp)
movq %rdi, %rbx
callq 0x1303c7
movq %rbx, %rdi
movsd %xmm0, 0x48(%rsp)
movq 0x38(%rbx), %rax
pushq $0x1
popq %r14
testq %rax, %rax
movq %rbx, 0x40(%rsp)
jne 0x186f6
testq %rax, %rax
movq 0x8(%rsp), %rbp
je 0x186bb
movq 0x48(%rdi), %r13
addq $0x10, %r13
movq %rax, 0x18(%rsp)
movq (%r13), %r15
movl 0x8(%r13), %ebx
testq %rbx, %rbx
leaq (%r15,%rbx,4), %r12
cmoveq %rbx, %r15
movl 0xc(%r13), %eax
movl %eax, (%rsp)
testl %eax, %eax
movl $0x0, %eax
cmoveq %rax, %r12
movq %rbp, %rsi
movq %r15, %rdx
movl %ebx, %ecx
callq 0x14d809
movsd %xmm0, 0x20(%r13)
movq 0x40(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
movl (%rsp), %ecx
callq 0x14d809
movsd %xmm0, 0x28(%r13)
movq %rbp, %rsi
movq %r15, %rdx
movl %ebx, %ecx
movq %r12, %r8
movl (%rsp), %r9d
callq 0x14d8ef
movq 0x40(%rsp), %rdi
movsd %xmm0, 0x30(%r13)
movl %r14d, %eax
addq $0x48, %r13
incl %r14d
cmpq %rax, 0x18(%rsp)
ja 0x18636
movq 0x27191e(%rip), %rax    # 0x289fe0
movq (%rax), %rbx
callq 0x1303c7
subsd 0x48(%rsp), %xmm0
leaq 0x2172c4(%rip), %rsi    # 0x22f99b
leaq 0x2176b4(%rip), %rdx    # 0x22fd92
movq %rbx, %rdi
movb $0x1, %al
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0
leaq 0x38(%rdi), %rax
movq %rax, 0x50(%rsp)
xorl %r12d, %r12d
pushq $0x2
popq %r11
movq 0x48(%rdi), %rax
movq %r12, 0x38(%rsp)
imulq $0x48, %r12, %rcx
movq %rax, %r12
movq 0x10(%rax,%rcx), %rbp
movl 0x18(%rax,%rcx), %r13d
leaq (,%r13,4), %rbx
addq %rbp, %rbx
movl 0x1c(%rax,%rcx), %r15d
testq %r15, %r15
cmoveq %r15, %rbx
andq $0x0, 0x40(%rax,%rcx)
movabsq $-0x100000002, %rax     # imm = 0xFFFFFFFEFFFFFFFE
movq %rax, 0x24(%r12,%rcx)
xorpd %xmm0, %xmm0
movq %rcx, (%rsp)
movupd %xmm0, 0x30(%r12,%rcx)
movapd %xmm0, 0x20(%rsp)
testq %r13, %r13
cmoveq %r13, %rbp
je 0x187ac
movq 0x10(%rdi), %rax
movl 0x18(%rdi), %ecx
movl 0x1c(%rdi), %edx
movl 0x20(%rdi), %esi
xorl %r10d, %r10d
movl (%rbp,%r10,4), %r8d
movl (%rax,%r8,4), %r8d
xorl %r9d, %r9d
testl %r8d, %ecx
setne %r9b
testl %r8d, %edx
leal (%r9,%r9,2), %r9d
cmovnel %r14d, %r9d
testl %r8d, %esi
cmovnel %r11d, %r9d
incl 0x20(%rsp,%r9,4)
incq %r10
cmpq %r10, %r13
jne 0x1877a
movq (%rsp), %rax
addq %r12, %rax
addq $0x24, %rax
movq %rax, 0x10(%rsp)
movq 0x8(%rsp), %rsi
movq %rbp, %rdx
movl %r13d, %ecx
movq %r12, 0x18(%rsp)
movq %rdi, %r12
callq 0x14d809
movq 0x10(%rsp), %rax
movsd %xmm0, 0xc(%rax)
testq %r15, %r15
je 0x188d0
movq (%rsp), %rax
movq 0x18(%rsp), %rcx
addq %rcx, %rax
addq $0x28, %rax
movq %rax, 0x58(%rsp)
movq %r12, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
movl %r15d, %ecx
callq 0x14d809
movq 0x58(%rsp), %r11
movsd %xmm0, 0x10(%r11)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movq 0x10(%r12), %rax
movl 0x18(%r12), %ecx
movl 0x1c(%r12), %edx
movl 0x20(%r12), %esi
xorl %edi, %edi
pushq $0x2
popq %r10
movl (%rbx,%rdi,4), %r8d
movl (%rax,%r8,4), %r8d
xorl %r9d, %r9d
testl %r8d, %ecx
setne %r9b
testl %r8d, %edx
leal (%r9,%r9,2), %r9d
cmovnel %r14d, %r9d
testl %r8d, %esi
cmovnel %r10d, %r9d
incl 0x60(%rsp,%r9,4)
incq %rdi
cmpq %rdi, %r15
jne 0x1883e
xorl %edx, %edx
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x38(%rsp), %r12
leal -0x2(%rdx), %esi
cmpl %r13d, 0x20(%rsp,%rdx,4)
cmovel %esi, %ecx
cmpl %r15d, 0x60(%rsp,%rdx,4)
cmovel %esi, %eax
incq %rdx
cmpq $0x4, %rdx
jne 0x1887a
movl %eax, %edx
imull %ecx, %edx
cmpl $-0x1, %edx
movq 0x8(%rsp), %rsi
je 0x188ee
movq 0x18(%rsp), %rcx
movq (%rsp), %rdx
leaq (%rcx,%rdx), %rax
movsd 0x30(%rcx,%rdx), %xmm0
ucomisd 0x38(%rcx,%rdx), %xmm0
jae 0x188fa
orl $-0x1, 0x24(%rax)
movl $0x1, (%r11)
jmp 0x18953
cmpl %r13d, 0x20(%rsp)
jne 0x18907
movq 0x10(%rsp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
movq 0x8(%rsp), %rsi
movq 0x38(%rsp), %r12
jmp 0x1894f
movq 0x10(%rsp), %rdx
movl %ecx, (%rdx)
movl %eax, (%r11)
jmp 0x1894f
movl $0x1, 0x24(%rax)
orl $-0x1, (%r11)
jmp 0x18953
movl 0x2c(%rsp), %eax
orl 0x24(%rsp), %eax
je 0x189ad
andl $0x0, 0x28(%rsp)
andl $0x0, 0x20(%rsp)
xorl %eax, %eax
pushq $-0x2
popq %rcx
xorl %edx, %edx
movq 0x8(%rsp), %rsi
movq 0x38(%rsp), %r12
movl 0x20(%rsp,%rax,4), %edi
cmpl %edx, %edi
cmoval %edi, %edx
leal -0x2(%rax), %edi
cmoval %edi, %ecx
incq %rax
cmpq $0x4, %rax
jne 0x18930
movq 0x10(%rsp), %rax
movl %ecx, (%rax)
movq (%rsp), %rdx
movq 0x18(%rsp), %rax
addq %rdx, %rax
addq $0x40, %rax
movq %rax, (%rsp)
movq %rbp, %rdx
movl %r13d, %ecx
movq %rbx, %r8
movl %r15d, %r9d
callq 0x14d8ef
movq (%rsp), %rax
movsd %xmm0, (%rax)
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
movq 0x50(%rsp), %rsi
movl %r12d, %ecx
callq 0x14d999
movq %rbx, %rdi
incl %r12d
movq 0x38(%rbx), %rax
cmpq %r12, %rax
pushq $0x2
popq %r11
ja 0x18706
jmp 0x1861b
cmpl $0x0, 0x28(%rsp)
movq 0x8(%rsp), %rsi
movq 0x38(%rsp), %r12
movq (%rsp), %rdx
je 0x189cc
movq 0x10(%rsp), %rax
andl $0x0, (%rax)
jmp 0x18953
movq 0x10(%rsp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
jmp 0x18953