// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/rcut.cpp
// Function: select_min_bp(bubble_type*, mc_match_t*, mc_svaux_t*, mc_bp_t*, unsigned char*, bits_p*, unsigned long, mc_bp_res*)
void select_min_bp(bubble_type* bub, mc_match_t *ma, mc_svaux_t *b_aux, mc_bp_t* bp,
uint8_t *lock, bits_p *vis, uint64_t id, mc_bp_res* r)
{
	uint32_t uid, val = 0, max_bid, max_uid, c_bid, c_uid, f_bid, f_uid;
	t_w_t w = 0, max_w = -1;
	mc_bp_iter i;
	reset_mc_bp_iter(bp, &i, id);
	memset(vis->a, 0, vis->n);
	max_bid = max_uid = (uint32_t)-1;
	f_bid = i.bid; f_uid = i.uid;///f_bid::bubble id, f_uid::unitig id

	while (1)
	{
		uid = next_uid(&i, bub, &c_bid, &c_uid);
		if(uid == (uint32_t)-1) break;
		if(vis->a[uid]) continue; ///already flip uid
		///update w
		w += incre_weight(b_aux, ma, vis->a, uid);
		vis->a[uid] = 1;
		if(lock[uid] == 0) val = 1;
        if(val == 0) continue;
		///update max_w
		if(max_w < w)
		{
			max_w = w;
			max_bid = c_bid;
			max_uid = c_uid;
		}
	}

	if(max_w <= 0 || max_bid == (uint32_t)-1 || max_uid == (uint32_t)-1) return;
	if((max_w > r->w) || (max_w == r->w && id < r->id))
	{
		r->w = max_w;
		r->id = id;
		r->chain_id = i.chain_id;
		r->l_bid = max_bid;
		r->l_uid = max_uid;
		r->f_bid = f_bid;
		r->f_uid = f_uid;
	}
	///if((res->min_w > i_b->weight) || (res->min_w == i_b->weight && id < res->min_idx))
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, %r14
movq %r8, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, %rbp
movq 0xd0(%rsp), %rdx
leaq 0x80(%rsp), %rbx
movq %rcx, %rdi
movq %rbx, %rsi
callq 0x16a786
movq (%r14), %rdx
movq %r14, 0x70(%rsp)
movq 0x10(%r14), %rdi
xorl %esi, %esi
callq 0x20e0
movq (%rbx), %rax
movq 0x8(%rbx), %r14
movq 0x10(%rbx), %rcx
movsd 0xc6cad(%rip), %xmm0    # 0x231638
movsd %xmm0, 0x10(%rsp)
xorpd %xmm2, %xmm2
movb $0x1, %dl
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
movq %rax, 0x58(%rsp)
leaq (%rax,%rax,4), %rax
movq %rax, 0x20(%rsp)
leaq 0x78(%rsp), %r15
leaq 0x1c(%rsp), %r12
movq %rcx, 0x60(%rsp)
movq %rcx, %rsi
movq %r14, 0x68(%rsp)
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
movl %edx, 0x18(%rsp)
movsd %xmm2, 0x50(%rsp)
movq 0xc0(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x20(%rsp), %rcx
movl 0x10(%rax,%rcx,8), %ecx
cmpq %rcx, %r14
jae 0x16aaef
movq 0x20(%rsp), %rcx
leaq (%rax,%rcx,8), %r13
movq %rsi, %rbx
movq 0x18(%r13), %rax
movq (%rax,%r14,8), %rsi
shrq $0x21, %rsi
movq $0x0, (%rsp)
movq %rbp, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
movq %r15, %r8
movq %r12, %r9
callq 0x13df01
movl 0x1c(%rsp), %eax
cmpq %rax, %rbx
jb 0x16aa44
incq %r14
movl 0x10(%r13), %eax
movl $0x0, %ebx
cmpq %rax, %r14
jb 0x16a9ff
jmp 0x16aaef
movq 0x78(%rsp), %rax
movl (%rax,%rbx,4), %r13d
shrl %r13d
leaq 0x1(%rbx), %rsi
movq 0x70(%rsp), %rax
movq 0x10(%rax), %rax
cmpb $0x0, (%rax,%r13)
jne 0x16a9d6
movq 0x40(%rsp), %rdi
movq %rsi, 0x30(%rsp)
movq 0x38(%rsp), %rsi
movq %rax, %rdx
movq %rax, 0x28(%rsp)
movl %r13d, %ecx
callq 0x16a869
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rax
movb $0x1, (%rax,%r13)
movq 0x48(%rsp), %rax
cmpb $0x0, (%rax,%r13)
movsd 0x50(%rsp), %xmm2
addsd %xmm0, %xmm2
setne %al
testb %al, 0x18(%rsp)
movb $0x1, %dl
jne 0x16a9cc
movsd 0x10(%rsp), %xmm1
ucomisd %xmm1, %xmm2
movl 0xc(%rsp), %eax
cmoval %ebx, %eax
movl %eax, 0xc(%rsp)
movl 0x8(%rsp), %eax
cmoval %r14d, %eax
movl %eax, 0x8(%rsp)
movapd %xmm2, %xmm0
maxsd %xmm1, %xmm0
xorl %edx, %edx
movsd %xmm0, 0x10(%rsp)
jmp 0x16a9cc
xorpd %xmm0, %xmm0
movsd 0x10(%rsp), %xmm1
ucomisd %xmm1, %xmm0
movl 0xc(%rsp), %edx
movl 0x8(%rsp), %esi
jae 0x16ab66
cmpl $-0x1, %esi
je 0x16ab66
cmpl $-0x1, %edx
je 0x16ab66
movq 0xd8(%rsp), %rax
ucomisd 0x18(%rax), %xmm1
ja 0x16ab31
jne 0x16ab66
jp 0x16ab66
movl 0x14(%rax), %eax
cmpq 0xd0(%rsp), %rax
jbe 0x16ab66
movq 0xd8(%rsp), %rax
movsd %xmm1, 0x18(%rax)
movq 0xd0(%rsp), %rcx
movl %ecx, 0x14(%rax)
movq 0x58(%rsp), %rcx
movl %ecx, (%rax)
movl %esi, 0xc(%rax)
movl %edx, 0x10(%rax)
movq 0x68(%rsp), %rcx
movl %ecx, 0x4(%rax)
movq 0x60(%rsp), %rcx
movl %ecx, 0x8(%rax)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq