// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movl $0x0, -0x34(%rbp)
movq -0x10(%rbp), %rax
imull $0x32, (%rax), %eax
movl %eax, -0x38(%rbp)
movsd 0x163e54(%rip), %xmm0   # 0x38d8c8
movsd %xmm0, -0x40(%rbp)
movl -0x24(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0x28(%rbp), %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x20(%rbp), %rax
cmpl $0x2, 0xc(%rax)
jae 0x229aa3
movl $0x0, -0x4(%rbp)
jmp 0x229de3
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x228fc0
movsd %xmm0, -0x40(%rbp)
movq -0x20(%rbp), %rax
cmpl $0x2, 0xc(%rax)
jne 0x229acf
movl $0x0, -0x4(%rbp)
jmp 0x229de3
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x229b09
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %esi
movl $0x1, %edx
callq 0x22d360
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x229ad6
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x20(%rbp), %rdx
leaq -0x34(%rbp), %rcx
callq 0x229df0
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
ucomisd -0x40(%rbp), %xmm0
jbe 0x229b79
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x229b6d
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %esi
movl $0x1, %edx
callq 0x22d360
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x229b3a
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
jmp 0x229bb2
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x229bb0
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %esi
xorl %edx, %edx
callq 0x22d360
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x229b80
jmp 0x229bb2
movq 0x19a427(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x228e90
movq -0x50(%rbp), %rdi
leaq 0x164447(%rip), %rsi    # 0x38e023
leaq 0x164459(%rip), %rdx    # 0x38e03c
movb $0x1, %al
callq 0x22a0
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jae 0x229d6e
movl -0x30(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x229c23
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x20(%rbp), %rdx
callq 0x229f00
jmp 0x229c3d
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x20(%rbp), %rdx
movl $0x3, %ecx
callq 0x229fc0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x20(%rbp), %rdx
leaq -0x34(%rbp), %rcx
callq 0x229df0
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
ucomisd -0x40(%rbp), %xmm0
jbe 0x229cad
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x229ca1
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %esi
movl $0x1, %edx
callq 0x22d360
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x229c6e
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
jmp 0x229ce6
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x229ce4
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %esi
xorl %edx, %edx
callq 0x22d360
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x229cb4
jmp 0x229ce6
movl -0x34(%rbp), %eax
xorl %edx, %edx
divl -0x38(%rbp)
cmpl $0x0, %edx
jne 0x229d5e
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x228d30
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x228e90
movsd %xmm0, -0x48(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x229d54
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %esi
movl $0x1, %edx
callq 0x22d360
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x229d21
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
jmp 0x229d60
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x229bf1
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x229da5
movq -0x20(%rbp), %rdi
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x2c(%rbp), %ecx
movl (%rax,%rcx,4), %esi
xorl %edx, %edx
callq 0x22d360
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x229d75
movq 0x19a234(%rip), %rax    # 0x3c3fe0
movq (%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x228e90
movq -0x58(%rbp), %rdi
leaq 0x16427b(%rip), %rsi    # 0x38e04a
leaq 0x164266(%rip), %rdx    # 0x38e03c
movb $0x1, %al
callq 0x22a0
movl -0x34(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)