// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/rcut.cpp
// Function: mc_init_spin(mc_match_t const*, mc_svaux_t*)
t_w_t mc_init_spin(const mc_match_t *ma, mc_svaux_t *b)
{
	uint32_t i;
	b->cc_edge.n = 0;
	if(b->cc_size <= 2)//mannually clear
	{
		for (i = 0; i < b->cc_size; ++i) {///how many nodes
			b->s[(uint32_t)ma->cc[b->cc_off + i]] = 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] == 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] == 0 && b->s[n2] == 0) {
			b->x = kr_splitmix64(b->x);
			b->s[n1] = b->x&1? 1 : -1;
			b->s[n2] = e->w > 0? -b->s[n1] : b->s[n1];
		}/****************************may have bugs********************************/
		else if(b->s[n1] == 0)
		{
			b->s[n1] = e->w > 0? -b->s[n2] : b->s[n2];
		}
		else if(b->s[n2] == 0)
		{
			b->s[n2] = e->w > 0? -b->s[n1] : b->s[n1];
		}
		/****************************may have bugs********************************/
	}

	passed:
	mc_reset_z(ma, b);
	return mc_score(ma, b);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movq $0x0, 0x10(%rsi)
movl 0xc(%rsi), %eax
cmpl $0x2, %eax
ja 0x16a1bd
testl %eax, %eax
je 0x16a406
xorl %eax, %eax
movq 0x50(%rbx), %rcx
movq 0x30(%r14), %rdx
movl 0x8(%rbx), %esi
addl %eax, %esi
movl (%rdx,%rsi,8), %edx
movb $0x0, (%rcx,%rdx)
incl %eax
movl 0xc(%rbx), %ecx
cmpl %ecx, %eax
jb 0x16a198
testl %ecx, %ecx
je 0x16a406
movq 0x30(%r14), %rcx
movq 0x28(%rbx), %rdx
movq 0x50(%rbx), %rsi
xorl %eax, %eax
movl 0x8(%rbx), %edi
addl %eax, %edi
movq (%rcx,%rdi,8), %rdi
movl %edi, %r8d
movl %edi, (%rdx,%rax,4)
cmpb $0x0, (%rsi,%r8)
movl 0xc(%rbx), %edi
je 0x16a1ec
incq %rax
cmpq %rdi, %rax
jb 0x16a1cb
cmpl %edi, %eax
jae 0x16a406
xorl %r15d, %r15d
movapd 0xd0fd1(%rip), %xmm1    # 0x23b1d0
movq 0x10(%r14), %rax
movq 0x30(%r14), %rcx
movl 0x8(%rbx), %edx
addl %r15d, %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %edx
movq (%rax,%rdx,8), %rax
movq 0x28(%rbx), %rdx
movl %ecx, (%rdx,%r15,4)
testl %eax, %eax
je 0x16a2be
movq %rax, %r13
shrq $0x20, %r13
movl %eax, %ebp
movq 0x20(%rbx), %rax
movq 0x28(%r14), %rcx
movl %r13d, %r12d
movq %r12, %rdx
shlq $0x4, %rdx
movsd 0x8(%rcx,%rdx), %xmm2
movq 0x10(%rbx), %rcx
cmpq 0x18(%rbx), %rcx
jne 0x16a28c
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x18(%rbx)
shlq $0x3, %rsi
movq %rax, %rdi
movapd %xmm2, (%rsp)
callq 0x2370
movapd (%rsp), %xmm2
movapd 0xd0f4c(%rip), %xmm1    # 0x23b1d0
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 %r12, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x10(%rbx)
movq %rdx, (%rax,%rcx,8)
incq %r13
decq %rbp
jne 0x16a234
incq %r15
movl 0xc(%rbx), %eax
cmpq %rax, %r15
jb 0x16a1ff
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x166e19
cmpq $0x0, 0x10(%rbx)
je 0x16a406
movl $0x1, %eax
xorl %r8d, %r8d
movabsq $-0x61c8864680b583eb, %rcx # imm = 0x9E3779B97F4A7C15
movabsq $-0x40a7b892e31b1a47, %rdx # imm = 0xBF58476D1CE4E5B9
movabsq $-0x6b2fb644ecceee15, %rsi # imm = 0x94D049BB133111EB
xorpd %xmm0, %xmm0
movq 0x28(%r14), %r10
movq 0x20(%rbx), %r9
movq 0x50(%rbx), %rdi
movl (%r9,%r8,8), %r8d
shlq $0x4, %r8
leaq (%r10,%r8), %r9
movq (%r10,%r8), %r10
movl %r10d, %r8d
shrq $0x20, %r10
movb (%rdi,%r10), %r11b
movb (%rdi,%r8), %bpl
testb %r11b, %r11b
je 0x16a366
testb %bpl, %bpl
jne 0x16a3f7
movsd 0x8(%r9), %xmm1
movzbl %r11b, %r9d
negb %r11b
ucomisd %xmm0, %xmm1
movzbl %r11b, %r10d
jmp 0x16a3ef
testb %bpl, %bpl
je 0x16a38a
movsd 0x8(%r9), %xmm1
movzbl %bpl, %r8d
negb %bpl
ucomisd %xmm0, %xmm1
movzbl %bpl, %r9d
cmovbel %r8d, %r9d
movb %r9b, (%rdi,%r10)
jmp 0x16a3f7
movq (%rbx), %r11
addq %rcx, %r11
movq %r11, %r15
shrq $0x1e, %r15
xorq %r11, %r15
imulq %rdx, %r15
movq %r15, %r11
shrq $0x1b, %r11
xorq %r15, %r11
imulq %rsi, %r11
movq %r11, %r15
shrq $0x1f, %r15
xorq %r11, %r15
movq %r15, (%rbx)
andl $0x1, %r15d
cmpq $0x1, %r15
movl $0x0, %r11d
sbbl %r11d, %r11d
orb $0x1, %r11b
movb %r11b, (%rdi,%r10)
movsd 0x8(%r9), %xmm1
movq 0x50(%rbx), %rdi
movzbl (%rdi,%r10), %r9d
movl %r9d, %r10d
negb %r10b
ucomisd %xmm0, %xmm1
movzbl %r10b, %r10d
cmovbel %r9d, %r10d
movb %r10b, (%rdi,%r8)
movl %eax, %r8d
incl %eax
cmpq %r8, 0x10(%rbx)
ja 0x16a313
movq %r14, %rdi
movq %rbx, %rsi
callq 0x169f7d
movq %r14, %rdi
movq %rbx, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x169ee2