// File: /workspace/github_cpp_makefile_star2_O2/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 %r12d, %r12d
xorl %esi, %esi
callq 0x20e0
movl %r12d, %r12d
cmpq %r12, 0xc8(%r15)
jbe 0x10f568
movq 0xd8(%r15), %rax
movq %r12, %rcx
shlq $0x5, %rcx
cmpb $0x0, 0x18(%rax,%rcx)
jne 0x10f563
addq %rcx, %rax
movl 0x10(%rax), %ecx
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x10db01
incl %r12d
jmp 0x10f52e
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10e8d3
movsd %xmm0, 0x10(%rsp)
movq (%r14), %rdx
movq 0x8(%r14), %rdi
xorl %esi, %esi
callq 0x20e0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10f68a
cmpl $-0x1, %eax
je 0x10f5b1
movq 0x8(%r14), %rcx
movl %eax, %edx
movb $0x1, (%rcx,%rdx)
movq 0x30(%r14), %rsi
movq %rbx, %rdi
movl %eax, %ecx
callq 0x10dc0d
jmp 0x10f587
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), %xmm0
jb 0x10f573
leaq 0x14416d(%rip), %rax    # 0x253758
movl 0x7c(%rax), %ecx
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r14, %r8
callq 0x10ebd5
movq %r14, %rdi
movq %rbx, %rsi
callq 0x10e8d3
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %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), %xmm0
jb 0x10f60f
leaq 0x18(%rsp), %rdi
callq 0x10ecfd
xorl %ebp, %ebp
movl %ebp, %eax
cmpq %rax, (%rbx)
jbe 0x10f67d
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %ecx
callq 0x10c96a
incl %ebp
jmp 0x10f665
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq