// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/hic.cpp
// Function: phasing_improvement(H_partition*, G_partition*, ha_ug_index*, bubble_type*, hc_links*)
uint32_t phasing_improvement(H_partition* h, G_partition* g_p, ha_ug_index* idx, bubble_type* bub, hc_links* link)
{   
    uint32_t i, occ = 0, round = 0;
    double pre_w, pre_total, current_w;
    mul_block_phase_type b_x;
    init_mul_block_phase_type(&b_x, g_p, bub, asm_opt.thread_num, h);
    ///double index_time = yak_realtime();

    while(1)
    {
        pre_w = get_total_weight(h, g_p);
        pre_total = pre_w;

        while (1)
        {
            memset(h->lock, 0, sizeof(uint8_t)*g_p->n);
            while (1)
            {
                i = get_max_unitig(h, g_p, link, bub);
                if(i == (uint32_t)-1) break;
                h->lock[i] = 1;
                flip_unitig(g_p, link, bub, i);
                occ++;
            }
            current_w = get_total_weight(h, g_p);
            ///fprintf(stderr, "[M::%s::round single %u, pre_w: %f, current_w: %f]\n", __func__, round, pre_w, current_w);
            if(ceil(current_w) <= ceil(pre_w)) break;
            round++;
            pre_w = current_w;
        }

        
        pre_w = get_total_weight(h, g_p);
        round = 0;
        while (1)
        {
            ///fprintf(stderr, "[M::%s::round block %u, h->n: %lu]\n", __func__, round, h->n);
            phasing_improvement_by_block(h, g_p, bub, &b_x);
            current_w = get_total_weight(h, g_p);
            ///fprintf(stderr, "[M::%s::round block %u, pre_w: %f, current_w: %f]\n", __func__, round, pre_w, current_w);
            if(ceil(current_w) <= ceil(pre_w)) break;
            round++;
            pre_w = current_w;
        }
        
        if(ceil(current_w) <= ceil(pre_total)) break;
    }

    destory_mul_block_phase_type(&b_x);
    ///fprintf(stderr, "[M::%s:Flipping time:%.3f]\n", __func__, yak_realtime()-index_time);


    for (i = 0; i < g_p->n; i++)
    {
        update_partition_flag(h, g_p, link, i);
    }

    ///print_phase_group(g_p, bub, "Small");
    // double w0 = 0, w1 = 0;
    // uint32_t *h0, h0_n, *h1, h1_n;
    // get_phased_block(g_p, NULL, 2973, NULL, NULL, &h0, &h0_n, &h1, &h1_n, NULL, NULL);
    // w0 = get_cluster_weight_debug(g_p, h->link, h0, h0_n); 
    // w1 = get_cluster_weight_debug(g_p, h->link, h1, h1_n); 
    // fprintf(stderr, "debug-w0: %f, g_p->a[2973].weight[0]: %f\n", w0, g_p->a[2973].weight[0]);
    // fprintf(stderr, "debug-w1: %f, g_p->a[2973].weight[1]: %f\n", w1, g_p->a[2973].weight[1]);

    return !!occ;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %r15
movq %rcx, %r12
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x13d8b5(%rip), %rax    # 0x253758
movl 0x7c(%rax), %ecx
leaq 0x20(%rsp), %r13
movq %r13, %rdi
movq %r12, %rdx
movq %r14, %r8
callq 0x10ebd5
xorl %ebp, %ebp
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10e8d3
movsd %xmm0, 0x18(%rsp)
movsd %xmm0, (%rsp)
movq 0x8(%r14), %rdi
movq (%rbx), %rdx
xorl %esi, %esi
callq 0x20e0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10f68a
cmpl $-0x1, %eax
je 0x115f0a
movq 0x8(%r14), %rcx
movl %eax, %edx
movb $0x1, (%rcx,%rdx)
movq %rbx, %rdi
movq %r15, %rsi
movl %eax, %ecx
callq 0x10dc0d
incl %ebp
jmp 0x115edf
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10e8d3
movsd %xmm0, 0x10(%rsp)
callq 0x2040
movsd %xmm0, 0x8(%rsp)
movsd (%rsp), %xmm0
callq 0x2040
movsd 0x10(%rsp), %xmm1
ucomisd 0x8(%rsp), %xmm0
movapd %xmm1, %xmm0
jb 0x115ecc
movsd %xmm1, 0x10(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x10f44f
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10e8d3
movsd %xmm0, 0x8(%rsp)
callq 0x2040
movsd %xmm0, (%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x2040
ucomisd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
jb 0x115f42
movsd 0x18(%rsp), %xmm0
callq 0x2040
ucomisd (%rsp), %xmm0
jb 0x115ebb
leaq 0x20(%rsp), %rdi
callq 0x10ecfd
xorl %r15d, %r15d
movl %r15d, %eax
cmpq %rax, (%rbx)
jbe 0x115fca
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %ecx
callq 0x10c96a
incl %r15d
jmp 0x115faf
xorl %eax, %eax
testl %ebp, %ebp
setne %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq