// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/rcut.cpp
// Function: mcgg_init_spin(mc_match_t const*, mcgg_svaux_t*)
t_w_t mcgg_init_spin(const mc_match_t *ma, mcgg_svaux_t *b)
{
	uint32_t i;
	b->cc_edge.n = 0;
	for (i = 0; i < b->cc_size; ++i) {///how many nodes
		uint32_t k = (uint32_t)ma->cc[b->cc_off + i];///node id
		b->cc_node[i] = k;
		if(b->s[k].s == 0) break;
	}
	if(i >= b->cc_size) goto passed;

	for (i = 0; i < b->cc_size; ++i) {///how many nodes
		uint32_t k = (uint32_t)ma->cc[b->cc_off + i];///node id
		uint32_t o = ma->idx.a[k] >> 32;///cc group id
		uint32_t n = (uint32_t)ma->idx.a[k], j;
		b->cc_node[i] = k;
		for (j = 0; j < n; ++j) {
			w_t w = ma->ma.a[o + j].w;
			w = w > 0? w : -w;
			kv_push(uint64_t, b->cc_edge, (uint64_t)((uint32_t)-1 - ((uint32_t)w)) << 32 | (o + j));
		}
	}
	radix_sort_mc64(b->cc_edge.a, b->cc_edge.a + b->cc_edge.n);
	for (i = 0; i < b->cc_edge.n; ++i) { // from the strongest edge to the weakest
		const mc_edge_t *e = &ma->ma.a[(uint32_t)b->cc_edge.a[i]];
		uint32_t n1 = ma_x(*e), n2 = ma_y(*e);
		if (b->s[n1].s == 0 && b->s[n2].s == 0) {
			b->s[n1].s = kr_drand_node(n1, b, &(b->s[n1].hc));
			// debug_hapM(&(b->s[n1]), "s0");
			b->s[n2].s = kr_drand_node_ref(n2, b, &(b->s[n2].hc), b->s[n1].s, e->w>0?1:0);
			// debug_hapM(&(b->s[n2]), "s1");
		}
		else if(b->s[n1].s == 0)
		{
			b->s[n1].s = kr_drand_node_ref(n1, b, &(b->s[n1].hc), b->s[n2].s, e->w>0?1:0);	
			// debug_hapM(&(b->s[n1]), "s2");	
		}
		else if(b->s[n2].s == 0)
		{
			b->s[n2].s = kr_drand_node_ref(n2, b, &(b->s[n2].hc), b->s[n1].s, e->w>0?1:0);
			// debug_hapM(&(b->s[n2]), "s3");
		}
	}

	passed:
	mcgg_reset_z(ma, b);
	return mcgg_score(ma, b);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movq %rdi, %r14
andq $0x0, 0x10(%rsi)
xorl %ecx, %ecx
movq %rcx, %rax
movl 0xc(%rbx), %ecx
cmpq %rcx, %rax
jae 0x12c564
movq 0x30(%r14), %rcx
movl 0x8(%rbx), %edx
addl %eax, %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %edx
movq 0x28(%rbx), %rsi
movl %ecx, (%rsi,%rax,4)
movq 0x50(%rbx), %rsi
shlq $0x5, %rdx
leaq 0x1(%rax), %rcx
cmpl $0x0, (%rsi,%rdx)
jne 0x12c52e
movl 0xc(%rbx), %ecx
cmpl %eax, %ecx
jbe 0x12c7e4
xorl %esi, %esi
movapd 0xc0b4a(%rip), %xmm1    # 0x1ed0c0
pushq $0x2
popq %r12
movl %ecx, %eax
cmpq %rax, %rsi
jae 0x12c650
movq 0x10(%r14), %rax
movq 0x30(%r14), %rcx
movl 0x8(%rbx), %edx
addl %esi, %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %edx
movq (%rax,%rdx,8), %rbp
movl %ebp, %r13d
shrq $0x20, %rbp
movq 0x28(%rbx), %rax
movq %rsi, 0x8(%rsp)
movl %ecx, (%rax,%rsi,4)
subq $0x1, %r13
jb 0x12c640
movq 0x28(%r14), %rax
movl %ebp, %r15d
movq %r15, %rcx
shlq $0x4, %rcx
movsd 0x8(%rax,%rcx), %xmm2
movq 0x10(%rbx), %rcx
movq 0x20(%rbx), %rax
cmpq 0x18(%rbx), %rcx
jne 0x12c612
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %r12, %rsi
movq %rsi, 0x18(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
movapd %xmm2, 0x10(%rsp)
callq 0x2370
movapd 0x10(%rsp), %xmm2
movapd 0xc0ab6(%rip), %xmm1    # 0x1ed0c0
movq %rax, 0x20(%rbx)
movq 0x10(%rbx), %rcx
movapd %xmm2, %xmm0
xorpd %xmm1, %xmm0
maxsd %xmm0, %xmm2
cvttsd2si %xmm2, %rdx
notl %edx
shlq $0x20, %rdx
orq %r15, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x10(%rbx)
movq %rdx, (%rax,%rcx,8)
incq %rbp
jmp 0x12c5af
movq 0x8(%rsp), %rsi
incq %rsi
movl 0xc(%rbx), %ecx
jmp 0x12c57a
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1224a9
xorl %r13d, %r13d
movl %r13d, %r13d
cmpq %r13, 0x10(%rbx)
jbe 0x12c7e4
movq 0x28(%r14), %rax
movq 0x20(%rbx), %rcx
movq 0x50(%rbx), %r12
movl (%rcx,%r13,8), %ecx
shlq $0x4, %rcx
leaq (%rax,%rcx), %rdi
movq (%rax,%rcx), %rbp
movl %ebp, %esi
shrq $0x20, %rbp
movq %rbp, %r8
shlq $0x5, %r8
movl (%r12,%r8), %ecx
movq %rsi, %r15
shlq $0x5, %r15
movl (%r12,%r15), %eax
testl %ecx, %ecx
je 0x12c6e1
testl %eax, %eax
jne 0x12c7dc
addq %r15, %r12
addq $0x8, %r12
movsd 0x8(%rdi), %xmm0
xorl %r8d, %r8d
ucomisd 0xb6e9c(%rip), %xmm0    # 0x1e3568
seta %r8b
movq %rsi, %rbp
movq %rsi, %rdi
movq %rbx, %rsi
movq %r12, %rdx
jmp 0x12c7cc
leaq (%r12,%r8), %rcx
leaq 0x8(%rcx), %rdx
testl %eax, %eax
je 0x12c70e
movsd 0x8(%rdi), %xmm0
xorl %r8d, %r8d
ucomisd 0xb6e6b(%rip), %xmm0    # 0x1e3568
seta %r8b
movq %rbp, %rdi
movq %rbx, %rsi
movl %eax, %ecx
jmp 0x12c7cc
movq %rdx, 0x20(%rsp)
movq %rdi, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
movzwl 0x4(%r12,%r8), %eax
movl %eax, 0x4(%rsp)
andw $0x0, 0x8(%rcx)
movq (%rbx), %rdi
movq %rcx, %rbp
callq 0x125d08
movq %rax, (%rbx)
movzwl 0x6(%rbp), %ecx
cmpw $-0x1, %cx
je 0x12c77e
movq %rax, %rdx
shrq $0xc, %rdx
movabsq $0x3ff0000000000000, %rsi # imm = 0x3FF0000000000000
orq %rsi, %rdx
movq %rdx, %xmm0
addsd 0xb6e1b(%rip), %xmm0    # 0x1e3580
ucomisd 0x10(%rbp), %xmm0
movl 0x4(%rsp), %edx
jbe 0x12c782
movq 0x20(%rsp), %rdx
movw $0x1, (%rdx)
movl %ecx, %edx
jmp 0x12c782
movl 0x4(%rsp), %edx
decl %edx
movq 0x60(%rbx), %rcx
movzwl %dx, %edx
imulq $0x18, %rdx, %rsi
movq (%rcx,%rsi), %rdi
xorl %edx, %edx
divq 0x8(%rcx,%rsi)
movl (%rdi,%rdx,4), %ecx
movl %ecx, (%rbp)
leaq (%r12,%r15), %rdx
addq $0x8, %rdx
movq 0x8(%rsp), %rax
movsd 0x8(%rax), %xmm0
xorl %r8d, %r8d
ucomisd 0xb6dab(%rip), %xmm0    # 0x1e3568
seta %r8b
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x12c808
movq 0x50(%rbx), %rcx
shlq $0x5, %rbp
movl %eax, (%rcx,%rbp)
incl %r13d
jmp 0x12c664
movq %r14, %rdi
movq %rbx, %rsi
callq 0x12c3f3
movq %r14, %rdi
movq %rbx, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x12c49c