// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %rsp
movq %rsi, %rbx
movq %rdi, %r14
callq 0x1365ef
movsd %xmm0, 0x48(%rsp)
cmpq $0x0, 0x38(%r14)
je 0x1814f
movq %r14, %rdi
movq %rbx, 0x18(%rsp)
leaq 0x38(%r14), %rax
movq %rax, 0x50(%rsp)
xorl %r12d, %r12d
pushq $0x1
popq %r14
pushq $0x2
popq %r11
movq %rdi, 0x38(%rsp)
movq 0x48(%rdi), %rax
movq %r12, 0x30(%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
movq %rbx, 0x68(%rsp)
cmoveq %r15, %rbx
andq $0x0, 0x40(%rax,%rcx)
movabsq $-0x100000002, %rax     # imm = 0xFFFFFFFEFFFFFFFE
movq %rax, 0x24(%r12,%rcx)
xorpd %xmm0, %xmm0
movq %rcx, 0x8(%rsp)
movupd %xmm0, 0x30(%r12,%rcx)
movapd %xmm0, 0x20(%rsp)
testq %r13, %r13
cmoveq %r13, %rbp
je 0x17e78
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 0x17e46
movq 0x8(%rsp), %rax
addq %r12, %rax
addq $0x24, %rax
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rsi
movq %rbp, %rdx
movl %r13d, %ecx
movq %r12, 0x40(%rsp)
movq %rdi, %r12
callq 0x153e0c
movq 0x10(%rsp), %rax
movsd %xmm0, 0xc(%rax)
testq %r15, %r15
je 0x17f9b
movsd %xmm0, 0x58(%rsp)
movq 0x8(%rsp), %rax
movq 0x40(%rsp), %rcx
addq %rcx, %rax
addq $0x28, %rax
movq %rax, 0x60(%rsp)
movq %r12, %rdi
movq 0x18(%rsp), %rsi
movq 0x68(%rsp), %rdx
movl %r15d, %ecx
callq 0x153e0c
movq 0x60(%rsp), %r11
movsd %xmm0, 0x10(%r11)
xorpd %xmm1, %xmm1
movapd %xmm1, 0x70(%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 0x70(%rsp,%r9,4)
incq %rdi
cmpq %rdi, %r15
jne 0x17f14
xorl %edx, %edx
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x30(%rsp), %r12
leal -0x2(%rdx), %esi
cmpl %r13d, 0x20(%rsp,%rdx,4)
cmovel %esi, %ecx
cmpl %r15d, 0x70(%rsp,%rdx,4)
cmovel %esi, %eax
incq %rdx
cmpq $0x4, %rdx
jne 0x17f50
movl %eax, %edx
imull %ecx, %edx
cmpl $-0x1, %edx
movq 0x18(%rsp), %rsi
je 0x17fb9
movsd 0x58(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x17fc5
movq 0x10(%rsp), %rax
orl $-0x1, (%rax)
movl $0x1, (%r11)
jmp 0x1801e
cmpl %r13d, 0x20(%rsp)
jne 0x17fd6
movq 0x10(%rsp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
movq 0x18(%rsp), %rsi
movq 0x30(%rsp), %r12
jmp 0x1801e
movq 0x10(%rsp), %rdx
movl %ecx, (%rdx)
movl %eax, (%r11)
jmp 0x1801e
movq 0x10(%rsp), %rax
movl $0x1, (%rax)
orl $-0x1, (%r11)
jmp 0x1801e
movl 0x2c(%rsp), %eax
orl 0x24(%rsp), %eax
je 0x1807c
andl $0x0, 0x28(%rsp)
andl $0x0, 0x20(%rsp)
xorl %eax, %eax
pushq $-0x2
popq %rcx
xorl %edx, %edx
movq 0x18(%rsp), %rsi
movq 0x30(%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 0x17fff
movq 0x10(%rsp), %rax
movl %ecx, (%rax)
movq 0x8(%rsp), %rcx
movq 0x40(%rsp), %rax
addq %rcx, %rax
addq $0x40, %rax
movq %rax, 0x8(%rsp)
movq %rbp, %rdx
movl %r13d, %ecx
movq %rbx, %r8
movl %r15d, %r9d
callq 0x153edf
movq 0x8(%rsp), %rax
movsd %xmm0, (%rax)
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq 0x50(%rsp), %rsi
movl %r12d, %ecx
callq 0x153f73
movq %rbx, %rdi
incl %r12d
movq 0x38(%rbx), %rax
cmpq %r12, %rax
pushq $0x2
popq %r11
ja 0x17dcc
jmp 0x180ac
cmpl $0x0, 0x28(%rsp)
movq 0x18(%rsp), %rsi
movq 0x30(%rsp), %r12
movq 0x8(%rsp), %rcx
je 0x1809c
movq 0x10(%rsp), %rax
andl $0x0, (%rax)
jmp 0x18023
movq 0x10(%rsp), %rax
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
jmp 0x18023
testq %rax, %rax
movq 0x18(%rsp), %r13
je 0x1814f
movq 0x48(%rdi), %rbp
addq $0x40, %rbp
movq %rax, 0x8(%rsp)
movq -0x30(%rbp), %r15
movl -0x28(%rbp), %ebx
testq %rbx, %rbx
leaq (%r15,%rbx,4), %r12
cmoveq %rbx, %r15
movl -0x24(%rbp), %eax
movl %eax, 0x10(%rsp)
testl %eax, %eax
movl $0x0, %eax
cmoveq %rax, %r12
movq %r13, %rsi
movq %r15, %rdx
movl %ebx, %ecx
callq 0x153e0c
movsd %xmm0, -0x10(%rbp)
movq 0x38(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movl 0x10(%rsp), %ecx
callq 0x153e0c
movsd %xmm0, -0x8(%rbp)
movq %r13, %rsi
movq %r15, %rdx
movl %ebx, %ecx
movq %r12, %r8
movl 0x10(%rsp), %r9d
callq 0x153edf
movq 0x38(%rsp), %rdi
movsd %xmm0, (%rbp)
movl %r14d, %eax
addq $0x48, %rbp
incl %r14d
cmpq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
ja 0x180c2
movq 0x27ce8a(%rip), %rax    # 0x294fe0
movq (%rax), %rbx
callq 0x1365ef
subsd 0x48(%rsp), %xmm0
leaq 0x223a80(%rip), %rsi    # 0x23bbeb
leaq 0x223e2c(%rip), %rdx    # 0x23bf9e
movq %rbx, %rdi
movb $0x1, %al
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0