// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x60, %rsp
movq %rdx, %r15
movq %rsi, %rbx
movq %rdi, %r14
movq (%rdi), %rdx
movq 0x8(%rdi), %rdi
xorl %esi, %esi
callq 0x20e0
cmpq $0x0, 0xc8(%r15)
je 0x15021c
movl $0x1, %ebp
xorl %eax, %eax
movq 0xd8(%r15), %rcx
shlq $0x5, %rax
cmpb $0x0, 0x18(%rcx,%rax)
jne 0x15020f
addq %rax, %rcx
movl 0x10(%rcx), %ecx
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x14e712
movl %ebp, %eax
incl %ebp
cmpq %rax, 0xc8(%r15)
ja 0x1501e9
movq %r14, %rdi
movq %rbx, %rsi
callq 0x14f469
movsd %xmm0, 0x10(%rsp)
movq (%r14), %rdx
movq 0x8(%r14), %rdi
xorl %esi, %esi
callq 0x20e0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x150343
cmpl $-0x1, %eax
je 0x150265
movq 0x8(%r14), %rcx
movl %eax, %edx
movb $0x1, (%rcx,%rdx)
movq 0x30(%r14), %rsi
movq %rbx, %rdi
movl %eax, %ecx
callq 0x14e820
jmp 0x15023b
movq %r14, %rdi
movq %rbx, %rsi
callq 0x14f469
movsd %xmm0, 0x8(%rsp)
callq 0x2040
movsd %xmm0, (%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x2040
ucomisd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm0
jb 0x150227
leaq 0x13a4b9(%rip), %rax    # 0x28a758
movl 0x7c(%rax), %ecx
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r14, %r8
callq 0x14f7a6
movq %r14, %rdi
movq %rbx, %rsi
callq 0x14f469
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x1500b9
movq %r14, %rdi
movq %rbx, %rsi
callq 0x14f469
movsd %xmm0, 0x8(%rsp)
callq 0x2040
movsd %xmm0, (%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x2040
ucomisd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm0
jb 0x1502c3
leaq 0x18(%rsp), %rdi
callq 0x14f8e9
cmpq $0x0, (%rbx)
je 0x150336
xorl %r15d, %r15d
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %ecx
callq 0x14d999
incl %r15d
cmpq %r15, (%rbx)
ja 0x150320
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq