// File: /workspace/github_cpp_makefile_star2_O2/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 $0x58, %rsp
movq %rsi, 0x8(%rsp)
movq %rdi, %r14
callq 0xefea7
movsd %xmm0, 0x38(%rsp)
leaq 0x38(%r14), %rbx
xorl %r15d, %r15d
movq %rbx, 0x28(%rsp)
movl %r15d, %r15d
movq (%rbx), %rax
cmpq %r15, %rax
jbe 0x10ce7e
movq 0x48(%r14), %rax
movq %r15, 0x30(%rsp)
imulq $0x48, %r15, %rdx
movq 0x10(%rax,%rdx), %r12
movl 0x18(%rax,%rdx), %r13d
testq %r13, %r13
movl 0x1c(%rax,%rdx), %ebp
movq %r12, %r15
cmoveq %r13, %r15
movabsq $-0x100000002, %rcx     # imm = 0xFFFFFFFEFFFFFFFE
movq %rcx, 0x24(%rax,%rdx)
andq $0x0, 0x40(%rax,%rdx)
xorpd %xmm0, %xmm0
movq %rdx, (%rsp)
movupd %xmm0, 0x30(%rax,%rdx)
movapd %xmm0, 0x10(%rsp)
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x10ccd4
movl (%r15,%rbx,4), %esi
movq %r14, %rdi
callq 0x121555
cltq
incl 0x18(%rsp,%rax,4)
incq %rbx
jmp 0x10ccb8
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movq %r15, %rdx
movl %r13d, %ecx
callq 0x10c812
leaq (%r12,%r13,4), %rdx
movq %rdx, %rbx
movq 0x48(%r14), %rax
movq (%rsp), %r12
movsd %xmm0, 0x30(%rax,%r12)
testq %rbp, %rbp
cmoveq %rbp, %rbx
je 0x10cdad
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movl %ebp, %ecx
callq 0x10c812
movq 0x48(%r14), %rax
movsd %xmm0, 0x38(%rax,%r12)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
xorl %r12d, %r12d
cmpq %r12, %rbp
je 0x10cd4d
movl (%rbx,%r12,4), %esi
movq %r14, %rdi
callq 0x121555
cltq
incl 0x48(%rsp,%rax,4)
incq %r12
jmp 0x10cd31
xorl %edx, %edx
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x8(%rsp), %rsi
cmpq $0x4, %rdx
je 0x10cd75
leal -0x2(%rdx), %edi
cmpl %r13d, 0x10(%rsp,%rdx,4)
cmovel %edi, %eax
cmpl %ebp, 0x40(%rsp,%rdx,4)
cmovel %edi, %ecx
incq %rdx
jmp 0x10cd58
movl %ecx, %edi
imull %eax, %edi
movq 0x48(%r14), %rdx
cmpl $-0x1, %edi
je 0x10cdbf
movq (%rsp), %rcx
leaq (%rdx,%rcx), %rax
addq $0x28, %rax
addq %rcx, %rdx
movsd 0x8(%rax), %xmm0
ucomisd 0x10(%rax), %xmm0
jae 0x10cdcf
orl $-0x1, 0x24(%rdx)
movl $0x1, (%rax)
jmp 0x10ce3e
cmpl %r13d, 0x10(%rsp)
jne 0x10cddb
movl $0xfffffffe, 0x24(%rax,%r12) # imm = 0xFFFFFFFE
jmp 0x10ce31
movq (%rsp), %r12
movl %eax, 0x24(%rdx,%r12)
movl %ecx, 0x28(%rdx,%r12)
jmp 0x10ce42
movl $0x1, 0x24(%rdx)
orl $-0x1, (%rax)
jmp 0x10ce3e
movl 0x1c(%rsp), %ecx
orl 0x14(%rsp), %ecx
je 0x10ce12
andl $0x0, 0x18(%rsp)
andl $0x0, 0x10(%rsp)
xorl %ecx, %ecx
xorl %esi, %esi
pushq $-0x2
popq %rdx
cmpq $0x4, %rcx
je 0x10ce28
movl 0x10(%rsp,%rcx,4), %edi
leal -0x2(%rcx), %r8d
cmpl %esi, %edi
cmoval %edi, %esi
cmoval %r8d, %edx
incq %rcx
jmp 0x10cdf6
cmpl $0x0, 0x18(%rsp)
leaq 0x24(%rax,%r12), %rax
movq 0x8(%rsp), %rsi
je 0x10ce38
andl $0x0, (%rax)
jmp 0x10ce3e
movq (%rsp), %r12
movl %edx, 0x24(%rax,%r12)
movq 0x8(%rsp), %rsi
jmp 0x10ce42
movl $0xfffffffe, (%rax)     # imm = 0xFFFFFFFE
movq (%rsp), %r12
movq %r15, %rdx
movl %r13d, %ecx
movq %rbx, %r8
movl %ebp, %r9d
callq 0x10c8e2
movq 0x48(%r14), %rax
movsd %xmm0, 0x40(%rax,%r12)
movq %r14, %rdi
movq 0x28(%rsp), %rbx
movq %rbx, %rsi
movq 0x30(%rsp), %r15
movl %r15d, %ecx
callq 0x10c96a
incl %r15d
jmp 0x10cc59
xorl %ecx, %ecx
movq 0x8(%rsp), %r13
movl %ecx, %ecx
cmpq %rcx, %rax
jbe 0x10cf2e
movq 0x48(%r14), %rax
imulq $0x48, %rcx, %rbp
movq 0x10(%rax,%rbp), %r15
movl 0x18(%rax,%rbp), %ebx
testq %rbx, %rbx
movq %rcx, 0x30(%rsp)
leaq (%r15,%rbx,4), %rcx
movl 0x1c(%rax,%rbp), %eax
movl %eax, (%rsp)
cmoveq %rbx, %r15
testl %eax, %eax
movl $0x0, %r12d
cmovneq %rcx, %r12
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
movl %ebx, %ecx
callq 0x10c812
movq 0x48(%r14), %rax
movsd %xmm0, 0x30(%rax,%rbp)
movq %r14, %rdi
movq %r13, %rsi
movq %r12, %rdx
movl (%rsp), %ecx
callq 0x10c812
movq 0x48(%r14), %rax
movq %rax, 0x28(%rsp)
movsd %xmm0, 0x38(%rax,%rbp)
movq %r13, %rsi
movq %r15, %rdx
movl %ebx, %ecx
movq %r12, %r8
movl (%rsp), %r9d
callq 0x10c8e2
movq 0x28(%rsp), %rax
movsd %xmm0, 0x40(%rax,%rbp)
movq 0x30(%rsp), %rax
leal 0x1(%rax), %ecx
movq 0x38(%r14), %rax
jmp 0x10ce85
movq 0x1460ab(%rip), %rax    # 0x252fe0
movq (%rax), %rbx
callq 0xefea7
subsd 0x38(%rsp), %xmm0
leaq 0xe0b91(%rip), %rsi     # 0x1edadb
leaq 0xe0f3d(%rip), %rdx     # 0x1ede8e
movq %rbx, %rdi
movb $0x1, %al
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x22b0