// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq 0x1c5a9c(%rip), %rax    # 0x3c47f8
movl 0x7c(%rax), %ecx
movq -0x8(%rbp), %r8
leaq -0x98(%rbp), %rdi
callq 0x1f1d70
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1f1860
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
shlq $0x0, %rdx
xorl %esi, %esi
callq 0x2100
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x1f2f00
movl %eax, -0x2c(%rbp)
cmpl $-0x1, -0x2c(%rbp)
jne 0x1fedc5
jmp 0x1fedf3
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x2c(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movq -0x10(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl -0x2c(%rbp), %ecx
callq 0x1f0330
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1feda5
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1f1860
movsd %xmm0, -0x50(%rbp)
movsd -0x50(%rbp), %xmm0
callq 0x2040
movsd %xmm0, -0xa0(%rbp)
movsd -0x40(%rbp), %xmm0
callq 0x2040
movsd -0xa0(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x1fee31
jmp 0x1fee49
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
jmp 0x1fed8b
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1f1860
movsd %xmm0, -0x40(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
leaq -0x98(%rbp), %rcx
callq 0x1f2bb0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1f1860
movsd %xmm0, -0x50(%rbp)
movsd -0x50(%rbp), %xmm0
callq 0x2040
movsd %xmm0, -0xa8(%rbp)
movsd -0x40(%rbp), %xmm0
callq 0x2040
movsd -0xa8(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x1feeb8
jmp 0x1feecd
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movsd -0x50(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
jmp 0x1fee62
movsd -0x50(%rbp), %xmm0
callq 0x2040
movsd %xmm0, -0xb0(%rbp)
movsd -0x48(%rbp), %xmm0
callq 0x2040
movsd -0xb0(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x1feef9
jmp 0x1feefe
jmp 0x1fed6f
leaq -0x98(%rbp), %rdi
callq 0x1f1ff0
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1fef3c
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl -0x2c(%rbp), %ecx
callq 0x1ee2c0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x1fef11
cmpl $0x0, -0x30(%rbp)
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
addq $0xb0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)