// File: chhylp123[P]hifiasm/hic.cpp
// Function: hap_label_fliping(H_partition*, G_partition*, bubble_type*, hc_links*, unsigned int)
void hap_label_fliping(H_partition* hap, G_partition* g_p, bubble_type* bub, hc_links* link, uint32_t chain_id)
{
    long long c_bid, c_uid, l_bid, l_uid;
    uint32_t gid;
    ma_utg_t *u = &(bub->b_ug->u.a[chain_id]);
    hap->b.bid = hap->b.uid = 0;
    while (1)
    {
        gid = next_hap_label_id(&(hap->b), g_p, bub, u, 1, &c_bid, &c_uid);
        if(gid == (uint32_t)-1) break;
        identify_best_interval(&(hap->b), hap->lock, g_p, bub, u, link, c_bid, c_uid, &l_bid, &l_uid);
        if(l_bid == -1 || l_uid == -1) continue;
        
        // fprintf(stderr, "\nbefore weight: %f\n", get_total_weight(hap, g_p));
        // identify_best_interval_debug(&(hap->b), g_p, bub, u, link, c_bid, c_uid, &l_bid, &l_uid);
        if(flip_block(&(hap->b), g_p, bub, u, link, hap->lock, c_bid, c_uid, l_bid, l_uid))
        {
            ///fprintf(stderr, "after weight +: %f\n", get_total_weight(hap, g_p));
            hap->b.bid = l_bid;
            hap->b.uid = l_uid;
            gid = next_hap_label_id(&(hap->b), g_p, bub, u, 1, &c_bid, &c_uid);
            if(gid == (uint32_t)-1) break;
        }
        ///fprintf(stderr, "after weight -: %f\n", get_total_weight(hap, g_p));
        // exit(0);
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0x78, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movl %r8d, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movq 0xc0(%rax), %rax
movq 0x10(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, 0xb8(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0xb0(%rax)
movq -0x10(%rbp), %rdi
addq $0x90, %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x60(%rbp), %rcx
movl $0x1, %r8d
leaq -0x38(%rbp), %r9
leaq -0x40(%rbp), %rax
movq %rax, (%rsp)
callq 0x1f0aa0
movl %eax, -0x54(%rbp)
cmpl $-0x1, -0x54(%rbp)
jne 0x1f1ac4
jmp 0x1f1bd0
movq -0x10(%rbp), %rdi
addq $0x90, %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
movq -0x60(%rbp), %r8
movq -0x28(%rbp), %r9
movq -0x38(%rbp), %rbx
movq -0x40(%rbp), %r11
leaq -0x48(%rbp), %r10
leaq -0x50(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x1f1280
cmpq $-0x1, -0x48(%rbp)
je 0x1f1b1d
cmpq $-0x1, -0x50(%rbp)
jne 0x1f1b22
jmp 0x1f1a88
movq -0x10(%rbp), %rdi
addq $0x90, %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x60(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x10(%rbp), %rax
movq 0x8(%rax), %r9
movq -0x38(%rbp), %rbx
movq -0x40(%rbp), %r11
movq -0x48(%rbp), %r10
movq -0x50(%rbp), %rax
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movq %r10, 0x10(%rsp)
movq %rax, 0x18(%rsp)
callq 0x1f16b0
cmpl $0x0, %eax
je 0x1f1bcb
movq -0x48(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xb0(%rax)
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xb8(%rax)
movq -0x10(%rbp), %rdi
addq $0x90, %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x60(%rbp), %rcx
movl $0x1, %r8d
leaq -0x38(%rbp), %r9
leaq -0x40(%rbp), %rax
movq %rax, (%rsp)
callq 0x1f0aa0
movl %eax, -0x54(%rbp)
cmpl $-0x1, -0x54(%rbp)
jne 0x1f1bc9
jmp 0x1f1bd0
jmp 0x1f1bcb
jmp 0x1f1a88
addq $0x78, %rsp
popq %rbx
popq %rbp
retq
nopw (%rax,%rax)