// File: /workspace/github_cpp_makefile_star2_O1/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)
cmpl $0x2, 0xc(%rsi)
ja 0x1637fd
cmpl $0x0, 0xc(%rbx)
je 0x1637fd
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
cmpl 0xc(%rbx), %eax
jb 0x1637e2
cmpl $0x0, 0xc(%rbx)
je 0x163834
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)
je 0x163836
incq %rax
movl 0xc(%rbx), %edi
cmpq %rdi, %rax
jb 0x163811
jmp 0x163836
xorl %eax, %eax
cmpl 0xc(%rbx), %eax
jae 0x163a6b
cmpl $0x0, 0xc(%rbx)
je 0x163923
xorl %r15d, %r15d
movapd 0xcb76c(%rip), %xmm1    # 0x22efc0
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 0x163914
movq %rax, %r13
shrq $0x20, %r13
movl %eax, %ebp
movq 0x28(%r14), %rax
movl %r13d, %r12d
movq %r12, %rcx
shlq $0x4, %rcx
movsd 0x8(%rax,%rcx), %xmm2
movq 0x18(%rbx), %rax
cmpq %rax, 0x10(%rbx)
jne 0x1638da
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x18(%rbx)
movq 0x20(%rbx), %rdi
shlq $0x3, %rsi
movapd %xmm2, (%rsp)
callq 0x2370
movapd (%rsp), %xmm2
movapd 0xcb6ea(%rip), %xmm1    # 0x22efc0
movq %rax, 0x20(%rbx)
movapd %xmm2, %xmm0
xorpd %xmm1, %xmm0
maxsd %xmm0, %xmm2
cvttsd2si %xmm2, %rax
notl %eax
shlq $0x20, %rax
orq %r12, %rax
movq 0x10(%rbx), %rcx
movq 0x20(%rbx), %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x10(%rbx)
movq %rax, (%rdx,%rcx,8)
incq %r13
decq %rbp
jne 0x163885
incq %r15
movl 0xc(%rbx), %eax
cmpq %rax, %r15
jb 0x163854
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x1604f0
cmpq $0x0, 0x10(%rbx)
je 0x163a6b
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), %r15
movq %r15, %r8
shrq $0x20, %r8
movb (%rdi,%r8), %r11b
testb %r11b, %r11b
je 0x1639ba
movl %r15d, %r8d
cmpb $0x0, (%rdi,%r8)
jne 0x163a5c
movsd 0x8(%r9), %xmm1
movzbl %r11b, %r9d
negb %r11b
ucomisd %xmm0, %xmm1
movzbl %r11b, %r10d
jmp 0x1639e5
movl %r15d, %r10d
cmpb $0x0, (%rdi,%r10)
je 0x1639ef
testb %r11b, %r11b
jne 0x163995
movsd 0x8(%r9), %xmm1
movl %r15d, %r9d
movzbl (%rdi,%r9), %r9d
movl %r9d, %r10d
negb %r10b
ucomisd %xmm0, %xmm1
movzbl %r10b, %r10d
cmovbel %r9d, %r10d
movb %r10b, (%rdi,%r8)
jmp 0x163a5c
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,%r8)
movsd 0x8(%r9), %xmm1
movq 0x50(%rbx), %rdi
movzbl (%rdi,%r8), %r8d
movl %r8d, %r9d
negb %r9b
ucomisd %xmm0, %xmm1
movzbl %r9b, %r9d
cmovbel %r8d, %r9d
movb %r9b, (%rdi,%r10)
movl %eax, %r8d
incl %eax
cmpq %r8, 0x10(%rbx)
ja 0x163969
movq %r14, %rdi
movq %rbx, %rsi
callq 0x16360e
movq %r14, %rdi
movq %rbx, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x163573