// File: /workspace/github_cpp_makefile_star2_O1/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 0x16755b
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %rbp
cmpl $0x0, 0xc(%rdx)
je 0x167018
movq 0x8(%r13), %rax
movq 0x30(%rax), %rax
movq 0x28(%rbx), %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), %esi
cmpq %rsi, %rdx
jb 0x167002
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1634fc
movsd %xmm0, 0x8(%rsp)
cmpl $0x0, 0xc(%rbx)
je 0x167084
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 0x167031
leaq 0x4(%rsp), %rcx
movq %rbp, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x16756a
ucomisd 0x8(%rsp), %xmm0
movl 0xc(%rbx), %eax
jbe 0x167101
testl %eax, %eax
je 0x1670f9
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
movups 0x28(%rsi,%rdx), %xmm2
movups %xmm2, 0x28(%rcx,%rdx)
movupd %xmm1, 0x18(%rcx,%rdx)
incq %rax
movl 0xc(%rbx), %ecx
cmpq %rcx, %rax
jb 0x1670a8
movsd %xmm0, 0x8(%rsp)
jmp 0x16715a
testl %eax, %eax
je 0x16715a
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 0x167107
cmpl $0x0, 0x4(%rbp)
je 0x1674fc
xorl %eax, %eax
movq %rbx, 0x28(%rsp)
movq %r13, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movq %rax, 0x30(%rsp)
testb $0x1, %al
jne 0x167307
movq (%rbx), %rax
movabsq $-0x61c8864680b583eb, %rcx # imm = 0x9E3779B97F4A7C15
addq %rcx, %rax
movq %rax, %rcx
shrq $0x1e, %rcx
xorq %rax, %rcx
movabsq $-0x40a7b892e31b1a47, %rax # imm = 0xBF58476D1CE4E5B9
imulq %rax, %rcx
movq %rcx, %rax
shrq $0x1b, %rax
xorq %rcx, %rax
movabsq $-0x6b2fb644ecceee15, %rcx # imm = 0x94D049BB133111EB
imulq %rcx, %rax
movq %rax, %rcx
shrq $0x1f, %rcx
xorq %rax, %rcx
movq %rcx, (%rbx)
shrq $0xc, %rcx
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
orq %rax, %rcx
movq %rcx, %xmm0
addsd 0xbe34b(%rip), %xmm0    # 0x225538
movl 0xc(%rbx), %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd %xmm1, %xmm0
addsd 0xbae0c(%rip), %xmm0    # 0x222010
cvttsd2si %xmm0, %rcx
leal -0x1(%rax), %edx
cmpl %ecx, %eax
cmoval %ecx, %edx
movq 0x8(%r13), %rax
movq 0x30(%rax), %rcx
addl 0x8(%rbx), %edx
movq (%rcx,%rdx,8), %rcx
mulsd 0x10(%rbp), %xmm1
cvttsd2si %xmm1, %edx
movl %edx, 0x14(%rsp)
movl %ecx, %edx
movq 0x30(%rbx), %rsi
movl %ecx, (%rsi)
movq 0x38(%rbx), %rdi
movl %ecx, (%rdi,%rdx,4)
xorl %edx, %edx
movl $0x1, %r8d
xorl %r9d, %r9d
movl %r8d, %r10d
cmpl %r8d, %edx
jae 0x1672a5
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 0x16729d
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 0x167295
movl %r8d, %ebx
incl %r8d
movl %ebp, (%rsi,%rbx,4)
movl %ecx, (%rdi,%rbp,4)
incq %r15
decq %r14
jne 0x167279
incq %r12
cmpq %r13, %r12
jne 0x16725c
movl 0x14(%rsp), %r11d
testl %r11d, %r11d
setne %dl
cmpl %r11d, %r8d
seta %r11b
testb %r11b, %dl
jne 0x1672cc
incl %r9d
movl %r10d, %edx
cmpl $0x3, %r9d
jne 0x167247
testl %r8d, %r8d
movq 0x28(%rsp), %rbx
movq 0x20(%rsp), %r13
movq 0x18(%rsp), %rbp
je 0x1673b1
movl %r8d, %r14d
xorl %r15d, %r15d
movq 0x30(%rbx), %rax
movl (%rax,%r15,4), %esi
movq %r13, %rdi
callq 0x16bb18
incq %r15
cmpq %r15, %r14
jne 0x1672ea
jmp 0x1673b1
cmpl $0x0, 0xc(%rbx)
je 0x1673b1
xorl %r12d, %r12d
movq 0x8(%r13), %rax
movq 0x30(%rax), %rax
movl 0x8(%rbx), %ecx
addl %r12d, %ecx
movq (%rax,%rcx,8), %rsi
movq (%rbx), %rax
movabsq $-0x61c8864680b583eb, %rcx # imm = 0x9E3779B97F4A7C15
addq %rcx, %rax
movq %rax, %rcx
shrq $0x1e, %rcx
xorq %rax, %rcx
movabsq $-0x40a7b892e31b1a47, %rax # imm = 0xBF58476D1CE4E5B9
imulq %rax, %rcx
movq %rcx, %rax
shrq $0x1b, %rax
xorq %rcx, %rax
movabsq $-0x6b2fb644ecceee15, %rcx # imm = 0x94D049BB133111EB
imulq %rcx, %rax
movq %rax, %rcx
shrq $0x1f, %rcx
xorq %rax, %rcx
movq %rcx, (%rbx)
shrq $0xc, %rcx
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
orq %rax, %rcx
movq %rcx, %xmm0
addsd 0xbe1a7(%rip), %xmm0    # 0x225538
movsd 0x10(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1673a4
movq %r13, %rdi
callq 0x16bb18
incl %r12d
cmpl 0xc(%rbx), %r12d
jb 0x167314
movq %rbp, %rdi
movq %r13, %rsi
movq %rbx, %rdx
leaq 0x4(%rsp), %rcx
callq 0x16756a
ucomisd 0x8(%rsp), %xmm0
movl 0xc(%rbx), %eax
jbe 0x16742e
testl %eax, %eax
je 0x167426
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
movups 0x28(%rsi,%rdx), %xmm2
movups %xmm2, 0x28(%rcx,%rdx)
movupd %xmm1, 0x18(%rcx,%rdx)
incq %rax
movl 0xc(%rbx), %ecx
cmpq %rcx, %rax
jb 0x1673d5
movsd %xmm0, 0x8(%rsp)
jmp 0x167487
testl %eax, %eax
je 0x167487
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 0x167434
movl 0x4(%rsp), %eax
xorl %edx, %edx
divl 0x10(%rsp)
testl %edx, %edx
jne 0x1674ec
movq %r13, %rdi
callq 0x167675
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1634fc
movsd %xmm0, 0x8(%rsp)
cmpl $0x0, 0xc(%rbx)
je 0x1674ec
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 0x1674b6
movq 0x30(%rsp), %rax
incl %eax
cmpl 0x4(%rbp), %eax
jb 0x167175
cmpl $0x0, 0xc(%rbx)
je 0x167557
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 0x167504
movl 0x4(%rsp), %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq