// File: /workspace/github_cpp_makefile_star2_O3/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
movq 0xc8(%r15), %rax
testq %rax, %rax
je 0x156785
movl $0x1, %ebp
xorl %ecx, %ecx
movq 0xd8(%r15), %rdx
shlq $0x5, %rcx
cmpb $0x0, 0x18(%rdx,%rcx)
jne 0x15677c
addq %rcx, %rdx
movl 0x10(%rdx), %ecx
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x154c9e
movq 0xc8(%r15), %rax
movl %ebp, %ecx
incl %ebp
cmpq %rcx, %rax
ja 0x15674f
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1559c4
movsd %xmm0, 0x10(%rsp)
movq (%r14), %rdx
movq 0x8(%r14), %rdi
xorl %esi, %esi
callq 0x20e0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1568ac
cmpl $-0x1, %eax
je 0x1567ce
movq 0x8(%r14), %rcx
movl %eax, %edx
movb $0x1, (%rcx,%rdx)
movq 0x30(%r14), %rsi
movq %rbx, %rdi
movl %eax, %ecx
callq 0x154db7
jmp 0x1567a4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1559c4
movsd %xmm0, 0x8(%rsp)
callq 0x2040
movsd %xmm0, (%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x2040
ucomisd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm0
jb 0x156790
leaq 0x13ef50(%rip), %rax    # 0x295758
movl 0x7c(%rax), %ecx
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r14, %r8
callq 0x155d01
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1559c4
movsd %xmm0, 0x10(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x15661d
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1559c4
movsd %xmm0, 0x8(%rsp)
callq 0x2040
movsd %xmm0, (%rsp)
movsd 0x10(%rsp), %xmm0
callq 0x2040
ucomisd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm0
jb 0x15682c
leaq 0x18(%rsp), %rdi
callq 0x155e51
cmpq $0x0, (%rbx)
je 0x15689f
xorl %r15d, %r15d
movq %r14, %rdi
movq %rbx, %rsi
movl %r15d, %ecx
callq 0x153f73
incl %r15d
cmpq %r15, (%rbx)
ja 0x156889
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq