// File: chhylp123[P]hifiasm/hic.cpp
// Function: flip_by_chain(H_partition*, G_partition*, bubble_type*)
void flip_by_chain(H_partition* h, G_partition* g_p, bubble_type* bub)
{
    uint32_t i;
    memset(h->lock, 0, sizeof(uint8_t)*h->n);
    for (i = 0; i < bub->chain_weight.n; i++)
    {
        if(bub->chain_weight.a[i].del) continue;
        merge_phase_group_by_chain(h, g_p, bub, bub->chain_weight.a[i].id);
    }

    double pre_w = get_total_weight(h, g_p), current_w;
    uint32_t round = 0;
    while (1)
    {
        memset(h->lock, 0, sizeof(uint8_t)*h->n);
        while (1)
        {
            i = get_max_unitig(h, g_p, h->link, bub);
            if(i == (uint32_t)-1) break;
            h->lock[i] = 1;
            flip_unitig(g_p, h->link, bub, i);
        }
        current_w = get_total_weight(h, g_p);
        ///fprintf(stderr, "[M::%s::round %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;
    }


    ///print_phase_group(g_p, bub, "Large-pre");
    
    ///fprintf(stderr, "[M::%s::round %u, before block flipping: %f]\n", __func__, round, get_total_weight(h, g_p));

    mul_block_phase_type b_x;
    init_mul_block_phase_type(&b_x, g_p, bub, asm_opt.thread_num, h);

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

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

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rdx
shlq $0x0, %rdx
xorl %esi, %esi
callq 0x2100
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpq 0xc8(%rcx), %rax
jae 0x1f2d3c
movq -0x18(%rbp), %rax
movq 0xd8(%rax), %rax
movl -0x1c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpb $0x0, 0x18(%rax)
je 0x1f2d05
jmp 0x1f2d31
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0xd8(%rax), %rax
movl -0x1c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movl %eax, %ecx
callq 0x1f01c0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1f2cd8
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1f1860
movsd %xmm0, -0x28(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x8(%rbp), %rax
movq (%rax), %rdx
shlq $0x0, %rdx
xorl %esi, %esi
callq 0x2100
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rdx
movq -0x18(%rbp), %rcx
callq 0x1f2f00
movl %eax, -0x1c(%rbp)
cmpl $-0x1, -0x1c(%rbp)
jne 0x1f2d93
jmp 0x1f2dbc
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x1c(%rbp), %ecx
movb $0x1, (%rax,%rcx)
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rsi
movq -0x18(%rbp), %rdx
movl -0x1c(%rbp), %ecx
callq 0x1f0330
jmp 0x1f2d6f
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1f1860
movsd %xmm0, -0x30(%rbp)
movsd -0x30(%rbp), %xmm0
callq 0x2040
movsd %xmm0, -0x88(%rbp)
movsd -0x28(%rbp), %xmm0
callq 0x2040
movsd -0x88(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x1f2dfa
jmp 0x1f2e12
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
jmp 0x1f2d55
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq 0x1d19d7(%rip), %rax    # 0x3c47f8
movl 0x7c(%rax), %ecx
movq -0x8(%rbp), %r8
leaq -0x80(%rbp), %rdi
callq 0x1f1d70
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1f1860
movsd %xmm0, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x80(%rbp), %rcx
callq 0x1f2bb0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1f1860
movsd %xmm0, -0x30(%rbp)
movsd -0x30(%rbp), %xmm0
callq 0x2040
movsd %xmm0, -0x90(%rbp)
movsd -0x28(%rbp), %xmm0
callq 0x2040
movsd -0x90(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x1f2e96
jmp 0x1f2eab
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
jmp 0x1f2e43
leaq -0x80(%rbp), %rdi
callq 0x1f1ff0
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x1f2eea
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rdx
movl -0x1c(%rbp), %ecx
callq 0x1ee2c0
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x1f2ebb
addq $0x90, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)