// File: /workspace/github_cpp_makefile_star2_O1/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, 0x68(%rsp)
movq %rdx, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdi, %rbp
movq 0xd0(%rsp), %rdx
leaq 0x80(%rsp), %rbx
movq %rcx, %rdi
movq %rbx, %rsi
callq 0x163dec
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), %rbx
movsd 0xc1543(%rip), %xmm0    # 0x225538
movsd %xmm0, 0x18(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x20(%rsp)
movb $0x1, %cl
movl $0xffffffff, 0x14(%rsp) # imm = 0xFFFFFFFF
movq %rax, 0x38(%rsp)
leaq (%rax,%rax,4), %rdx
movq %rbx, 0x40(%rsp)
movq %r14, 0x48(%rsp)
movl $0xffffffff, 0x10(%rsp) # imm = 0xFFFFFFFF
movl %ecx, 0x28(%rsp)
movq 0xc0(%rbp), %rax
movq 0x10(%rax), %rax
movl 0x10(%rax,%rdx,8), %ecx
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpq %rcx, %r14
jae 0x1640b2
movq %rdx, %r15
leaq (%rax,%rdx,8), %r13
movq 0x18(%r13), %rax
movq (%rax,%r14,8), %rsi
shrq $0x21, %rsi
movq $0x0, (%rsp)
movq %rbp, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0x78(%rsp), %r8
leaq 0x34(%rsp), %r9
callq 0x13812e
movl 0x34(%rsp), %eax
cmpq %rax, %rbx
jb 0x164097
incq %r14
movl 0x10(%r13), %eax
movl $0x0, %ebx
cmpq %rax, %r14
jb 0x16404f
xorl %ebx, %ebx
jmp 0x1640af
movq 0x78(%rsp), %rax
movl (%rax,%rbx,4), %r12d
shrl %r12d
movl %ebx, 0x30(%rsp)
incq %rbx
movl %r14d, 0x2c(%rsp)
movq %r15, %rdx
cmpl $-0x1, %r12d
je 0x16415d
movq 0x70(%rsp), %rax
movq 0x10(%rax), %r13
movl %r12d, %r15d
cmpb $0x0, (%r13,%r15)
jne 0x16402e
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq %rdx, 0x50(%rsp)
movq %r13, %rdx
movl %r12d, %ecx
callq 0x163ed3
movq 0x50(%rsp), %rdx
movb $0x1, (%r13,%r15)
movq 0x68(%rsp), %rax
cmpb $0x0, (%rax,%r15)
movsd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x20(%rsp)
setne %al
testb %al, 0x28(%rsp)
movb $0x1, %cl
jne 0x16402a
movsd 0x18(%rsp), %xmm1
movsd 0x20(%rsp), %xmm0
ucomisd %xmm1, %xmm0
movl 0x14(%rsp), %eax
cmoval 0x30(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0x10(%rsp), %eax
cmoval 0x2c(%rsp), %eax
movl %eax, 0x10(%rsp)
maxsd %xmm1, %xmm0
xorl %ecx, %ecx
movsd %xmm0, 0x18(%rsp)
jmp 0x16402a
movq %r14, 0x88(%rsp)
movq %rbx, 0x90(%rsp)
xorpd %xmm0, %xmm0
ucomisd 0x18(%rsp), %xmm0
movl 0x14(%rsp), %edx
movl 0x10(%rsp), %esi
jae 0x1641ec
cmpl $-0x1, %esi
je 0x1641ec
cmpl $-0x1, %edx
je 0x1641ec
movq 0xd8(%rsp), %rax
movsd 0x18(%rsp), %xmm0
ucomisd 0x18(%rax), %xmm0
ja 0x1641b1
jne 0x1641ec
jp 0x1641ec
movl 0x14(%rax), %eax
cmpq 0xd0(%rsp), %rax
jbe 0x1641ec
movq 0xd8(%rsp), %rax
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq 0xd0(%rsp), %rcx
movl %ecx, 0x14(%rax)
movq 0x38(%rsp), %rcx
movl %ecx, (%rax)
movl %esi, 0xc(%rax)
movl %edx, 0x10(%rax)
movq 0x48(%rsp), %rcx
movl %ecx, 0x4(%rax)
movq 0x40(%rsp), %rcx
movl %ecx, 0x8(%rax)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq