// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/rcut.cpp
// Function: mb_solve_cc(mc_opt_t const*, mb_g_t*, mb_svaux_t*, unsigned int, unsigned int)
uint32_t mb_solve_cc(const mc_opt_t *opt, mb_g_t *mbg, mb_svaux_t *b, uint32_t cc_off, uint32_t cc_size)
{
	uint32_t j, k, n_iter = 0, flush = opt->max_iter * 50;
	t_w_t sc_opt = -(1<<30), sc;///problem-w
	b->cc_off = cc_off, b->cc_size = cc_size;
	if (b->cc_size <= 2) return 0;

	for (j = 0; j < b->cc_size; ++j) {///how many nodes
		k = (uint32_t)mbg->e->cc[b->cc_off + j];///node id
		b->cc_node[j] = k;
	}

	sc_opt = mb_score(mbg, b);
	for (j = 0; j < b->cc_size; ++j) {///backup s and z in s_opt and z_opt
		memcpy(b->u_opt[b->cc_node[j]].s, b->u[b->cc_node[j]].s, 2*sizeof(mc_node_t));
		memcpy(b->u_opt[b->cc_node[j]].z, b->u[b->cc_node[j]].z, 4*sizeof(t_w_t));
	}

	// print_sc(opt, mg->e, b, sc_opt, n_iter);
	sc = mb_optimize_local(opt, mbg, b, &n_iter);
	if (sc > sc_opt) {
		for (j = 0; j < b->cc_size; ++j) {
			memcpy(b->u_opt[b->cc_node[j]].s, b->u[b->cc_node[j]].s, 2*sizeof(mc_node_t));
			memcpy(b->u_opt[b->cc_node[j]].z, b->u[b->cc_node[j]].z, 4*sizeof(t_w_t));
		}
		sc_opt = sc;
	} else {
		for (j = 0; j < b->cc_size; ++j) {
			memcpy(b->u[b->cc_node[j]].s, b->u_opt[b->cc_node[j]].s, 2*sizeof(mc_node_t));
			memcpy(b->u[b->cc_node[j]].z, b->u_opt[b->cc_node[j]].z, 4*sizeof(t_w_t));
		}
	}
	// mc_reset_z_debug(mg->e, b);
	// print_sc(opt, mg->e, b, sc_opt, n_iter);
	// fprintf(stderr, "\ncc_size: %u, cc_off: %u\n", b->cc_size, b->cc_off);
	for (k = 0; k < (uint32_t)opt->n_perturb; ++k) {
		if (k&1) mb_perturb(opt, mbg, b);
		else mb_perturb_node(opt, mbg, b, 3);
		sc = mb_optimize_local(opt, mbg, b, &n_iter);
		// fprintf(stderr, "(%u) sc_opt: %f, sc: %f\n", k, sc_opt, sc);
		if (sc > sc_opt) {
			for (j = 0; j < b->cc_size; ++j) {
				memcpy(b->u_opt[b->cc_node[j]].s, b->u[b->cc_node[j]].s, 2*sizeof(mc_node_t));
				memcpy(b->u_opt[b->cc_node[j]].z, b->u[b->cc_node[j]].z, 4*sizeof(t_w_t));
			}
			sc_opt = sc;
		} else {
			for (j = 0; j < b->cc_size; ++j) {
				memcpy(b->u[b->cc_node[j]].s, b->u_opt[b->cc_node[j]].s, 2*sizeof(mc_node_t));
				memcpy(b->u[b->cc_node[j]].z, b->u_opt[b->cc_node[j]].z, 4*sizeof(t_w_t));
			}
		}

		if((n_iter%flush) == 0)
		{
			reset_mb_g_t_z(mbg);
			sc = mb_score(mbg, b);

			for (j = 0; j < b->cc_size; ++j) {
				memcpy(b->u_opt[b->cc_node[j]].z, b->u[b->cc_node[j]].z, 4*sizeof(t_w_t));
			}
			sc_opt = sc;
		}

		// print_sc(opt, mg->e, b, sc_opt, n_iter);
	}
	for (j = 0; j < b->cc_size; ++j)
	{
		memcpy(b->u[b->cc_node[j]].s, b->u_opt[b->cc_node[j]].s, 2*sizeof(mc_node_t));
		memcpy(b->u[b->cc_node[j]].z, b->u_opt[b->cc_node[j]].z, 4*sizeof(t_w_t));
	}
		
	return n_iter;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl $0x0, 0x4(%rsp)
imull $0x32, (%rdi), %eax
movl %eax, 0x10(%rsp)
movl %ecx, 0x8(%rdx)
movl %r8d, 0xc(%rdx)
xorl %eax, %eax
cmpl $0x3, %r8d
jb 0x16dfa2
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %rbp
movq 0x8(%rsi), %rax
movq 0x30(%rax), %rax
movq 0x28(%rdx), %rcx
xorl %edx, %edx
movl 0x8(%rbx), %esi
addl %edx, %esi
movl (%rax,%rsi,8), %esi
movl %esi, (%rcx,%rdx,4)
incq %rdx
movl 0xc(%rbx), %r14d
cmpq %r14, %rdx
jb 0x16da28
movq %r13, %rdi
movq %rbx, %rsi
callq 0x169e6b
movsd %xmm0, 0x8(%rsp)
testl %r14d, %r14d
je 0x16daaa
xorl %eax, %eax
movq 0x48(%rbx), %rcx
movq 0x28(%rbx), %rdx
movq 0x40(%rbx), %rsi
movl (%rdx,%rax,4), %edx
imulq $0x38, %rdx, %rdx
movzwl 0x10(%rsi,%rdx), %esi
movw %si, 0x10(%rcx,%rdx)
movq 0x48(%rbx), %rcx
movq 0x28(%rbx), %rdx
movq 0x40(%rbx), %rsi
movl (%rdx,%rax,4), %edx
imulq $0x38, %rdx, %rdx
movupd 0x18(%rsi,%rdx), %xmm0
movupd 0x28(%rsi,%rdx), %xmm1
movupd %xmm1, 0x28(%rcx,%rdx)
movupd %xmm0, 0x18(%rcx,%rdx)
incq %rax
movl 0xc(%rbx), %ecx
cmpq %rcx, %rax
jb 0x16da57
leaq 0x4(%rsp), %rcx
movq %rbp, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x16dfb1
ucomisd 0x8(%rsp), %xmm0
movl 0xc(%rbx), %eax
jbe 0x16db27
testl %eax, %eax
je 0x16db82
xorl %eax, %eax
movq 0x48(%rbx), %rcx
movq 0x28(%rbx), %rdx
movq 0x40(%rbx), %rsi
movl (%rdx,%rax,4), %edx
imulq $0x38, %rdx, %rdx
movzwl 0x10(%rsi,%rdx), %esi
movw %si, 0x10(%rcx,%rdx)
movq 0x48(%rbx), %rcx
movq 0x28(%rbx), %rdx
movq 0x40(%rbx), %rsi
movl (%rdx,%rax,4), %edx
imulq $0x38, %rdx, %rdx
movupd 0x18(%rsi,%rdx), %xmm1
movupd 0x28(%rsi,%rdx), %xmm2
movupd %xmm2, 0x28(%rcx,%rdx)
movupd %xmm1, 0x18(%rcx,%rdx)
incq %rax
movl 0xc(%rbx), %ecx
cmpq %rcx, %rax
jb 0x16dad2
jmp 0x16db84
testl %eax, %eax
je 0x16db8c
xorl %eax, %eax
movq 0x28(%rbx), %rcx
movq 0x40(%rbx), %rdx
movl (%rcx,%rax,4), %ecx
imulq $0x38, %rcx, %rcx
movq 0x48(%rbx), %rsi
movzwl 0x10(%rsi,%rcx), %esi
movw %si, 0x10(%rdx,%rcx)
movq 0x28(%rbx), %rcx
movq 0x40(%rbx), %rdx
movl (%rcx,%rax,4), %ecx
imulq $0x38, %rcx, %rcx
movq 0x48(%rbx), %rsi
movupd 0x18(%rsi,%rcx), %xmm0
movupd 0x28(%rsi,%rcx), %xmm1
movupd %xmm1, 0x28(%rdx,%rcx)
movupd %xmm0, 0x18(%rdx,%rcx)
incq %rax
movl 0xc(%rbx), %ecx
cmpq %rcx, %rax
jb 0x16db2d
jmp 0x16db8e
xorl %ecx, %ecx
movsd %xmm0, 0x8(%rsp)
jmp 0x16db8e
xorl %ecx, %ecx
cmpl $0x0, 0x4(%rbp)
je 0x16df45
xorl %eax, %eax
movq %rbx, 0x28(%rsp)
movq %r13, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movq %rax, 0x30(%rsp)
testb $0x1, %al
jne 0x16dd3e
movq (%rbx), %rax
movabsq $-0x61c8864680b583eb, %rdx # imm = 0x9E3779B97F4A7C15
addq %rdx, %rax
movq %rax, %rdx
shrq $0x1e, %rdx
xorq %rax, %rdx
movabsq $-0x40a7b892e31b1a47, %rax # imm = 0xBF58476D1CE4E5B9
imulq %rax, %rdx
movq %rdx, %rax
shrq $0x1b, %rax
xorq %rdx, %rax
movabsq $-0x6b2fb644ecceee15, %rdx # imm = 0x94D049BB133111EB
imulq %rdx, %rax
movq %rax, %rdx
shrq $0x1f, %rdx
movl %ecx, %esi
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
xorq %rax, %rdx
movsd 0x10(%rbp), %xmm0
movq %rdx, (%rbx)
shrq $0xc, %rdx
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
orq %rax, %rdx
movq %rdx, %xmm2
addsd 0xc3a08(%rip), %xmm2    # 0x231638
mulsd %xmm1, %xmm2
addsd 0xc03d4(%rip), %xmm2    # 0x22e010
cvttsd2si %xmm2, %rax
leal -0x1(%rcx), %edx
cmpl %eax, %ecx
cmoval %eax, %edx
mulsd %xmm1, %xmm0
movq 0x8(%r13), %rax
addl 0x8(%rbx), %edx
movq 0x30(%rax), %rcx
movq (%rcx,%rdx,8), %rcx
cvttsd2si %xmm0, %edx
movl %edx, 0x14(%rsp)
movl %ecx, %edx
movq 0x30(%rbx), %rsi
movq 0x38(%rbx), %rdi
movl %ecx, (%rsi)
movl %ecx, (%rdi,%rdx,4)
xorl %edx, %edx
movl $0x1, %r8d
xorl %r9d, %r9d
movl %r8d, %r10d
cmpl %r8d, %edx
jae 0x16dcdc
movq 0x10(%rax), %r11
movl %edx, %r12d
movl %r10d, %r13d
movl %r10d, %r8d
movl (%rsi,%r12,4), %edx
movq (%r11,%rdx,8), %r15
movq %r15, %r14
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %r14
je 0x16dcd4
shrq $0x20, %r15
movq 0x28(%rax), %rdx
movl %r15d, %ebp
leaq (%rbp,%rbp,4), %rbp
movl (%rdx,%rbp,8), %ebp
cmpl %ecx, (%rdi,%rbp,4)
je 0x16dccc
movl %r8d, %ebx
incl %r8d
movl %ebp, (%rsi,%rbx,4)
movl %ecx, (%rdi,%rbp,4)
incq %r15
decq %r14
jne 0x16dcb0
incq %r12
cmpq %r13, %r12
jne 0x16dc93
movl 0x14(%rsp), %r11d
testl %r11d, %r11d
setne %dl
cmpl %r11d, %r8d
seta %r11b
testb %r11b, %dl
jne 0x16dd03
incl %r9d
movl %r10d, %edx
cmpl $0x3, %r9d
jne 0x16dc7e
testl %r8d, %r8d
movq 0x28(%rsp), %rbx
movq 0x20(%rsp), %r13
movq 0x18(%rsp), %rbp
je 0x16dde8
movl %r8d, %r14d
xorl %r15d, %r15d
movq 0x30(%rbx), %rax
movl (%rax,%r15,4), %esi
movq %r13, %rdi
callq 0x17256a
incq %r15
cmpq %r15, %r14
jne 0x16dd21
jmp 0x16dde8
testl %ecx, %ecx
je 0x16dde8
xorl %r12d, %r12d
movq 0x8(%r13), %rax
movq 0x30(%rax), %rax
movl 0x8(%rbx), %edx
addl %r12d, %edx
movq (%rax,%rdx,8), %rsi
movq (%rbx), %rax
movabsq $-0x61c8864680b583eb, %rdx # imm = 0x9E3779B97F4A7C15
addq %rdx, %rax
movq %rax, %rdx
shrq $0x1e, %rdx
xorq %rax, %rdx
movabsq $-0x40a7b892e31b1a47, %rax # imm = 0xBF58476D1CE4E5B9
imulq %rax, %rdx
movq %rdx, %rax
shrq $0x1b, %rax
xorq %rdx, %rax
movabsq $-0x6b2fb644ecceee15, %rdx # imm = 0x94D049BB133111EB
imulq %rdx, %rax
movq %rax, %rdx
shrq $0x1f, %rdx
xorq %rax, %rdx
movq %rdx, (%rbx)
shrq $0xc, %rdx
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
orq %rax, %rdx
movq %rdx, %xmm0
addsd 0xc3872(%rip), %xmm0    # 0x231638
movsd 0x10(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x16dddc
movq %r13, %rdi
callq 0x17256a
movl 0xc(%rbx), %ecx
incl %r12d
cmpl %ecx, %r12d
jb 0x16dd49
movq %rbp, %rdi
movq %r13, %rsi
movq %rbx, %rdx
leaq 0x4(%rsp), %rcx
callq 0x16dfb1
ucomisd 0x8(%rsp), %xmm0
movl 0xc(%rbx), %eax
jbe 0x16de65
testl %eax, %eax
je 0x16dec0
xorl %eax, %eax
movq 0x48(%rbx), %rcx
movq 0x28(%rbx), %rdx
movq 0x40(%rbx), %rsi
movl (%rdx,%rax,4), %edx
imulq $0x38, %rdx, %rdx
movzwl 0x10(%rsi,%rdx), %esi
movw %si, 0x10(%rcx,%rdx)
movq 0x48(%rbx), %rcx
movq 0x28(%rbx), %rdx
movq 0x40(%rbx), %rsi
movl (%rdx,%rax,4), %edx
imulq $0x38, %rdx, %rdx
movupd 0x18(%rsi,%rdx), %xmm1
movupd 0x28(%rsi,%rdx), %xmm2
movupd %xmm2, 0x28(%rcx,%rdx)
movupd %xmm1, 0x18(%rcx,%rdx)
incq %rax
movl 0xc(%rbx), %ecx
cmpq %rcx, %rax
jb 0x16de10
jmp 0x16dec2
testl %eax, %eax
je 0x16deca
xorl %eax, %eax
movq 0x28(%rbx), %rcx
movq 0x40(%rbx), %rdx
movl (%rcx,%rax,4), %ecx
imulq $0x38, %rcx, %rcx
movq 0x48(%rbx), %rsi
movzwl 0x10(%rsi,%rcx), %esi
movw %si, 0x10(%rdx,%rcx)
movq 0x28(%rbx), %rcx
movq 0x40(%rbx), %rdx
movl (%rcx,%rax,4), %ecx
imulq $0x38, %rcx, %rcx
movq 0x48(%rbx), %rsi
movupd 0x18(%rsi,%rcx), %xmm0
movupd 0x28(%rsi,%rcx), %xmm1
movupd %xmm1, 0x28(%rdx,%rcx)
movupd %xmm0, 0x18(%rdx,%rcx)
incq %rax
movl 0xc(%rbx), %ecx
cmpq %rcx, %rax
jb 0x16de6b
jmp 0x16decc
xorl %ecx, %ecx
movsd %xmm0, 0x8(%rsp)
jmp 0x16decc
xorl %ecx, %ecx
movl 0x4(%rsp), %eax
xorl %edx, %edx
divl 0x10(%rsp)
testl %edx, %edx
jne 0x16df35
movq %r13, %rdi
callq 0x16e0b5
movq %r13, %rdi
movq %rbx, %rsi
callq 0x169e6b
movsd %xmm0, 0x8(%rsp)
cmpl $0x0, 0xc(%rbx)
je 0x16df33
xorl %eax, %eax
movq 0x48(%rbx), %rcx
movq 0x28(%rbx), %rdx
movq 0x40(%rbx), %rsi
movl (%rdx,%rax,4), %edx
imulq $0x38, %rdx, %rdx
movupd 0x18(%rsi,%rdx), %xmm0
movupd 0x28(%rsi,%rdx), %xmm1
movupd %xmm1, 0x28(%rcx,%rdx)
movupd %xmm0, 0x18(%rcx,%rdx)
incq %rax
movl 0xc(%rbx), %ecx
cmpq %rcx, %rax
jb 0x16defb
jmp 0x16df35
xorl %ecx, %ecx
movq 0x30(%rsp), %rax
incl %eax
cmpl 0x4(%rbp), %eax
jb 0x16dba9
testl %ecx, %ecx
je 0x16df9e
xorl %eax, %eax
movq 0x28(%rbx), %rcx
movq 0x40(%rbx), %rdx
movl (%rcx,%rax,4), %ecx
imulq $0x38, %rcx, %rcx
movq 0x48(%rbx), %rsi
movzwl 0x10(%rsi,%rcx), %esi
movw %si, 0x10(%rdx,%rcx)
movq 0x28(%rbx), %rcx
movq 0x40(%rbx), %rdx
movl (%rcx,%rax,4), %ecx
imulq $0x38, %rcx, %rcx
movq 0x48(%rbx), %rsi
movupd 0x18(%rsi,%rcx), %xmm0
movupd 0x28(%rsi,%rcx), %xmm1
movupd %xmm1, 0x28(%rdx,%rcx)
movupd %xmm0, 0x18(%rdx,%rcx)
incq %rax
movl 0xc(%rbx), %ecx
cmpq %rcx, %rax
jb 0x16df4b
movl 0x4(%rsp), %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq