// File: /workspace/github_cpp_makefile_star2_O2/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
andl $0x0, 0x14(%rsp)
movq %rsi, 0x8(%rsp)
imull $0x32, (%rdi), %eax
movl %eax, 0x24(%rsp)
movl %ecx, 0x8(%rdx)
movl %r8d, 0xc(%rdx)
xorl %eax, %eax
cmpl $0x3, %r8d
jb 0x129869
movl %r8d, %ebp
movq %rdx, %rbx
movq %rdi, %r15
xorl %eax, %eax
movl %ebp, %ecx
cmpq %rcx, %rax
jae 0x12940f
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq 0x30(%rcx), %rcx
movl 0x8(%rbx), %edx
addl %eax, %edx
movl (%rcx,%rdx,8), %ecx
movq 0x28(%rbx), %rdx
movl %ecx, (%rdx,%rax,4)
incq %rax
movl 0xc(%rbx), %ebp
jmp 0x1293e4
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1257dd
xorl %eax, %eax
movl %ebp, %ecx
cmpq %rcx, %rax
jae 0x129475
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), %xmm2
movupd 0x28(%rsi,%rdx), %xmm1
movupd %xmm1, 0x28(%rcx,%rdx)
movupd %xmm2, 0x18(%rcx,%rdx)
incq %rax
movl 0xc(%rbx), %ebp
jmp 0x12941e
leaq 0x14(%rsp), %rcx
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
movsd %xmm0, 0x18(%rsp)
callq 0x129878
movsd 0x18(%rsp), %xmm2
xorl %eax, %eax
ucomisd %xmm2, %xmm0
jbe 0x1294f7
movl 0xc(%rbx), %ecx
cmpq %rcx, %rax
jae 0x129550
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), %xmm2
movupd 0x28(%rsi,%rdx), %xmm1
movupd %xmm1, 0x28(%rcx,%rdx)
movupd %xmm2, 0x18(%rcx,%rdx)
incq %rax
jmp 0x12949e
movl 0xc(%rbx), %ecx
cmpq %rcx, %rax
jae 0x12954c
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
jmp 0x1294f7
movapd %xmm2, %xmm0
xorl %eax, %eax
cmpl 0x4(%r15), %eax
jae 0x12980c
movsd %xmm0, 0x28(%rsp)
movq %rax, 0x30(%rsp)
testb $0x1, %al
jne 0x129662
movq %rbx, %rdi
callq 0x12e635
movl 0xc(%rbx), %eax
cvtsi2sd %rax, %xmm1
mulsd %xmm1, %xmm0
addsd 0xb6a85(%rip), %xmm0    # 0x1e0010
cvttsd2si %xmm0, %rcx
leal -0x1(%rax), %edx
cmpl %ecx, %eax
cmoval %ecx, %edx
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x30(%rax), %rcx
addl 0x8(%rbx), %edx
movq (%rcx,%rdx,8), %rcx
mulsd 0x10(%r15), %xmm1
cvttsd2si %xmm1, %edx
movl %edx, 0x18(%rsp)
movl %ecx, %r8d
movq 0x30(%rbx), %rsi
movl %ecx, (%rsi)
movq 0x38(%rbx), %rdi
movl %ecx, (%rdi,%r8,4)
xorl %r8d, %r8d
xorl %r11d, %r11d
pushq $0x1
popq %r9
cmpl $0x3, %r8d
je 0x1296a6
movq %r15, %rdx
movl %r11d, %r11d
movl %r9d, %r13d
movl %r9d, %r10d
cmpq %r13, %r11
jae 0x129636
movl (%rsi,%r11,4), %r15d
movq 0x10(%rax), %r12
movq (%r12,%r15,8), %rbp
movl %ebp, %r12d
shrq $0x20, %rbp
subq $0x1, %r12
jb 0x129631
movq 0x28(%rax), %r15
movl %ebp, %r14d
imulq $0x28, %r14, %r14
movl (%r15,%r14), %r15d
cmpl %ecx, (%rdi,%r15,4)
je 0x12962c
movl %r10d, %r14d
incl %r10d
movl %r15d, (%rsi,%r14,4)
movl %ecx, (%rdi,%r15,4)
incq %rbp
jmp 0x129603
incq %r11
jmp 0x1295eb
movl 0x18(%rsp), %ebp
testl %ebp, %ebp
setne %r11b
cmpl %ebp, %r10d
seta %bpl
andb %r11b, %bpl
incl %r8d
movl %r9d, %r11d
movl %r10d, %r9d
cmpb $0x1, %bpl
movq %rdx, %r15
jne 0x1295d5
jmp 0x1296a9
xorl %ebp, %ebp
cmpl %ecx, %ebp
jae 0x1296cb
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x30(%rax), %rax
movl 0x8(%rbx), %ecx
addl %ebp, %ecx
movq (%rax,%rcx,8), %r13
movq %rbx, %rdi
callq 0x12e635
movsd 0x10(%r15), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x12969f
movq 0x8(%rsp), %rdi
movl %r13d, %esi
callq 0x12e664
incl %ebp
movl 0xc(%rbx), %ecx
jmp 0x129664
movl %r9d, %r10d
movl %r10d, %r12d
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x1296cb
movq 0x30(%rbx), %rax
movl (%rax,%r13,4), %esi
movq 0x8(%rsp), %rdi
callq 0x12e664
incq %r13
jmp 0x1296af
movq %r15, %rdi
movq 0x8(%rsp), %rsi
movq %rbx, %rdx
leaq 0x14(%rsp), %rcx
callq 0x129878
movsd 0x28(%rsp), %xmm2
xorl %eax, %eax
ucomisd %xmm2, %xmm0
jbe 0x129747
movl 0xc(%rbx), %ecx
cmpq %rcx, %rax
jae 0x1297a0
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
jmp 0x1296ee
movl 0xc(%rbx), %ecx
cmpq %rcx, %rax
jae 0x12979c
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
jmp 0x129747
movapd %xmm2, %xmm0
movl 0x14(%rsp), %eax
xorl %edx, %edx
divl 0x24(%rsp)
testl %edx, %edx
je 0x1297ba
movq 0x30(%rsp), %rax
incl %eax
jmp 0x129552
movq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x12996e
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1257dd
xorl %eax, %eax
movl 0xc(%rbx), %ecx
cmpq %rcx, %rax
jae 0x1297ae
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
jmp 0x1297d4
xorl %eax, %eax
movl %ecx, %ecx
cmpq %rcx, %rax
jae 0x129865
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
jmp 0x12980e
movl 0x14(%rsp), %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq