// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/rcut.cpp
// Function: mcgg_solve_cc(mc_opt_t const*, mc_gg_t const*, mcgg_svaux_t*, unsigned int, unsigned int)
uint32_t mcgg_solve_cc(const mc_opt_t *opt, const mc_gg_t *mg, mcgg_svaux_t *b, uint32_t cc_off, uint32_t cc_size)
{
	// double t0, t1, tt0, tt1;
	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;
	sc_opt = mcgg_init_spin(mg->e, b);
	if (b->cc_size == 2) return 0;
	for (j = 0; j < b->cc_size; ++j) back_status(b, b->cc_node[j], 1);

	sc = mcgg_optimize_local(opt, mg->e, b, &n_iter);
	if (sc > sc_opt)
	{
		for (j = 0; j < b->cc_size; ++j) back_status(b, b->cc_node[j], 1);
		sc_opt = sc;
	}
	else
	{
		for (j = 0; j < b->cc_size; ++j) back_status(b, b->cc_node[j], 0);
	}
	fprintf(stderr, "\nBeg-[M::%s::score->%f]\n", __func__, mcgg_score(mg->e, b));
	// print_mcgg_node(mg->e, b, 3838);
	// print_mcgg_node(mg->e, b, 36880);

	// tt0 = tt1 = 0;
	for (k = 0; k < (uint32_t)opt->n_perturb; ++k) {
		// t0 = yak_realtime();
        if (k&1) mcgg_perturb(opt, mg->e, b);
        else mcgg_perturb_node(opt, mg->e, b, 3);
		// tt0 += yak_realtime()-t0;

		// t1 = yak_realtime();
        sc = mcgg_optimize_local(opt, mg->e, b, &n_iter);
		// tt1 += yak_realtime()-t1;
		// fprintf(stderr, "++(%u) sc_after: %f\n", k, mcgg_score(mg->e, b));

        if (sc > sc_opt) {
            for (j = 0; j < b->cc_size; ++j) back_status(b, b->cc_node[j], 1);
			sc_opt = sc;
        } else {
            for (j = 0; j < b->cc_size; ++j) back_status(b, b->cc_node[j], 0);
        }

		
        if((n_iter%flush) == 0)
        {
            mcgg_reset_z(mg->e, b);
            sc = mcgg_score(mg->e, b);
            for (j = 0; j < b->cc_size; ++j) back_status(b, b->cc_node[j], 1);
			sc_opt = sc;
        }
		// fprintf(stderr, "\n");
		// print_mcgg_node(mg->e, b, 3838);
		// print_mcgg_node(mg->e, b, 36880);
		// if((k&31)==0)fprintf(stderr, "+++(%u) sc: %f, sc_opt: %f, tt0: %.3f, tt1: %.3f\n", k, sc, sc_opt, tt0, tt1);
    }

	for (j = 0; j < b->cc_size; ++j) back_status(b, b->cc_node[j], 0);
	fprintf(stderr, "End-[M::%s::score->%f]\n", __func__, mcgg_score(mg->e, b));
	return n_iter;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
andl $0x0, 0x4(%rsp)
movq %rdi, 0x10(%rsp)
imull $0x32, (%rdi), %eax
movl %eax, 0x24(%rsp)
movl %ecx, 0x8(%rdx)
movl %r8d, 0xc(%rdx)
xorl %ebp, %ebp
cmpl $0x2, %r8d
jb 0x12ca8d
movq %rdx, %rbx
movq %rsi, %r13
movq 0x10(%rsi), %rdi
movq %rdx, %rsi
callq 0x12c513
movl 0xc(%rbx), %eax
cmpl $0x2, %eax
jne 0x12ca9e
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd %xmm0, 0x8(%rsp)
xorl %r14d, %r14d
pushq $0x1
popq %rbp
movl %eax, %eax
cmpq %rax, %r14
jae 0x12cacb
movq 0x28(%rbx), %rax
movl (%rax,%r14,4), %esi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x12eaa5
incq %r14
movl 0xc(%rbx), %eax
jmp 0x12caaa
movq 0x10(%r13), %rsi
leaq 0x4(%rsp), %rcx
movq 0x10(%rsp), %rdi
movq %rbx, %rdx
callq 0x12ce3e
movsd %xmm0, 0x18(%rsp)
ucomisd 0x8(%rsp), %xmm0
jbe 0x12cb16
xorl %r14d, %r14d
pushq $0x1
popq %r12
movl 0xc(%rbx), %eax
cmpq %rax, %r14
jae 0x12cb38
movq 0x28(%rbx), %rax
movl (%rax,%r14,4), %esi
movq %rbx, %rdi
movl %r12d, %edx
callq 0x12eaa5
incq %r14
jmp 0x12caf6
xorl %r14d, %r14d
movl 0xc(%rbx), %eax
cmpq %rax, %r14
jae 0x12cb44
movq 0x28(%rbx), %rax
movl (%rax,%r14,4), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x12eaa5
incq %r14
jmp 0x12cb19
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x126495(%rip), %rax    # 0x252fe0
movq (%rax), %r12
movq 0x10(%r13), %rdi
movq %rbx, %rsi
callq 0x12c49c
leaq 0xc2762(%rip), %rsi     # 0x1ef2c3
leaq 0xc2774(%rip), %rdx     # 0x1ef2dc
movq %r12, %rdi
movb $0x1, %al
callq 0x22b0
xorl %ecx, %ecx
pushq $0x1
popq %rbp
movsd 0x8(%rsp), %xmm0
movq %r13, 0x28(%rsp)
movq 0x10(%rsp), %rax
cmpl 0x4(%rax), %ecx
jae 0x12cde5
movsd %xmm0, 0x8(%rsp)
movq 0x10(%r13), %r13
movq %rcx, 0x18(%rsp)
testb $0x1, %cl
jne 0x12cc7d
movq %rbx, %rdi
callq 0x12e635
movl 0xc(%rbx), %eax
cvtsi2sd %rax, %xmm1
mulsd %xmm1, %xmm0
addsd 0xb344c(%rip), %xmm0    # 0x1e0010
cvttsd2si %xmm0, %rcx
leal -0x1(%rax), %edx
cmpl %ecx, %eax
cmoval %ecx, %edx
movq 0x30(%r13), %rax
addl 0x8(%rbx), %edx
movq (%rax,%rdx,8), %rax
movq 0x10(%rsp), %rcx
mulsd 0x10(%rcx), %xmm1
cvttsd2si %xmm1, %ecx
movl %eax, %edi
movq 0x30(%rbx), %rdx
movl %eax, (%rdx)
movq 0x38(%rbx), %rsi
movl %eax, (%rsi,%rdi,4)
xorl %edi, %edi
xorl %r10d, %r10d
movl %ebp, %r8d
cmpl $0x3, %edi
je 0x12cccc
movl %r10d, %r10d
movl %r8d, %r11d
movl %r8d, %r9d
cmpq %r11, %r10
jae 0x12cc5d
movl (%rdx,%r10,4), %r14d
movq 0x10(%r13), %r12
movq (%r12,%r14,8), %r14
movl %r14d, %r12d
shrq $0x20, %r14
subq $0x1, %r12
jb 0x12cc58
movq 0x28(%r13), %rbp
movl %r14d, %r15d
shlq $0x4, %r15
movl (%rbp,%r15), %ebp
cmpl %eax, (%rsi,%rbp,4)
je 0x12cc53
movl %r9d, %r15d
incl %r9d
movl %ebp, (%rdx,%r15,4)
movl %eax, (%rsi,%rbp,4)
incq %r14
jmp 0x12cc2b
incq %r10
jmp 0x12cc13
testl %ecx, %ecx
setne %r10b
cmpl %ecx, %r9d
seta %r11b
andb %r10b, %r11b
incl %edi
movl %r8d, %r10d
movl %r9d, %r8d
cmpb $0x1, %r11b
jne 0x12cc01
jmp 0x12cccf
xorl %r14d, %r14d
cmpl 0xc(%rbx), %r14d
jae 0x12cd01
movq 0x30(%r13), %rax
movl 0x8(%rbx), %ecx
addl %r14d, %ecx
movl (%rax,%rcx,8), %r12d
movq %rbx, %rdi
callq 0x12e635
movq 0x10(%rsp), %rax
movsd 0x10(%rax), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x12ccc7
movq %r12, %rdi
movq %rbx, %rsi
callq 0x12e893
movq %r13, %rdi
movq %rbx, %rsi
movl %r12d, %edx
movl %eax, %ecx
callq 0x12e808
incl %r14d
jmp 0x12cc80
movl %r8d, %r9d
movl %r9d, %r14d
xorl %ebp, %ebp
cmpq %rbp, %r14
je 0x12cd01
movq 0x30(%rbx), %rax
movl (%rax,%rbp,4), %r12d
movq %r12, %rdi
movq %rbx, %rsi
callq 0x12e893
movq %r13, %rdi
movq %rbx, %rsi
movl %r12d, %edx
movl %eax, %ecx
callq 0x12e808
incq %rbp
jmp 0x12ccd4
movq 0x28(%rsp), %r13
movq 0x10(%r13), %rsi
movq 0x10(%rsp), %rdi
movq %rbx, %rdx
leaq 0x4(%rsp), %rcx
callq 0x12ce3e
movapd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
movsd %xmm1, 0x30(%rsp)
ucomisd %xmm0, %xmm1
pushq $0x1
popq %rbp
jbe 0x12cd57
xorl %r14d, %r14d
movl 0xc(%rbx), %eax
cmpq %rax, %r14
jae 0x12cd7f
movq 0x28(%rbx), %rax
movl (%rax,%r14,4), %esi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x12eaa5
incq %r14
jmp 0x12cd38
xorl %r14d, %r14d
movl 0xc(%rbx), %eax
cmpq %rax, %r14
jae 0x12cd85
movq 0x28(%rbx), %rax
movl (%rax,%r14,4), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x12eaa5
movsd 0x8(%rsp), %xmm0
incq %r14
jmp 0x12cd5a
movsd 0x30(%rsp), %xmm0
movl 0x4(%rsp), %eax
xorl %edx, %edx
divl 0x24(%rsp)
testl %edx, %edx
je 0x12cd9f
movq 0x18(%rsp), %rcx
incl %ecx
jmp 0x12cb82
movq 0x10(%r13), %rdi
movq %rbx, %rsi
callq 0x12c3f3
movq 0x10(%r13), %rdi
movq %rbx, %rsi
callq 0x12c49c
xorl %r14d, %r14d
movsd %xmm0, 0x8(%rsp)
movl 0xc(%rbx), %eax
cmpq %rax, %r14
jae 0x12cd93
movq 0x28(%rbx), %rax
movl (%rax,%r14,4), %esi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x12eaa5
movsd 0x8(%rsp), %xmm0
incq %r14
jmp 0x12cdc0
xorl %r14d, %r14d
movl 0xc(%rbx), %eax
cmpq %rax, %r14
jae 0x12ce07
movq 0x28(%rbx), %rax
movl (%rax,%r14,4), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0x12eaa5
incq %r14
jmp 0x12cde8
movq 0x1261d2(%rip), %rax    # 0x252fe0
movq (%rax), %r15
movq 0x10(%r13), %rdi
movq %rbx, %rsi
callq 0x12c49c
leaq 0xc24c6(%rip), %rsi     # 0x1ef2ea
leaq 0xc24b1(%rip), %rdx     # 0x1ef2dc
movq %r15, %rdi
movb $0x1, %al
callq 0x22b0
movl 0x4(%rsp), %ebp
jmp 0x12ca8d