// File: /workspace/github_cpp_makefile_star2_O3/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, 0x8(%rsp)
movq $0x0, 0x10(%rsi)
cmpl $0x0, 0xc(%rsi)
je 0x170ba1
movq 0x8(%rsp), %rax
movq 0x30(%rax), %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)
shlq $0x5, %r8
cmpl $0x0, (%rsi,%r8)
movl 0xc(%rbx), %edi
je 0x17058b
incq %rax
cmpq %rdi, %rax
jb 0x170566
cmpl %edi, %eax
jae 0x170ba1
xorl %r15d, %r15d
movapd 0xcac32(%rip), %xmm1    # 0x23b1d0
movl $0x2, %r12d
movq 0x8(%rsp), %rdi
movq 0x10(%rdi), %rax
movq 0x30(%rdi), %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 0x17066a
movq %rax, %r13
shrq $0x20, %r13
movl %eax, %ebp
movq 0x20(%rbx), %rax
movq 0x28(%rdi), %rcx
movl %r13d, %r14d
movq %r14, %rdx
shlq $0x4, %rdx
movsd 0x8(%rcx,%rdx), %xmm2
movq 0x10(%rbx), %rcx
cmpq 0x18(%rbx), %rcx
jne 0x170638
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 0xcaba5(%rip), %xmm1    # 0x23b1d0
movq 0x8(%rsp), %rdi
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 %r14, %rdx
leaq 0x1(%rcx), %rsi
movq %rsi, 0x10(%rbx)
movq %rdx, (%rax,%rcx,8)
incq %r13
decq %rbp
jne 0x1705de
incq %r15
movl 0xc(%rbx), %eax
cmpq %rax, %r15
jb 0x1705a9
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rdi
leaq (%rdi,%rax,8), %rsi
callq 0x166e19
movq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x170ba1
movq 0x8(%rsp), %rax
movq 0x28(%rax), %r11
movq 0x20(%rbx), %rax
movq %rax, 0x20(%rsp)
movq 0x50(%rbx), %r12
xorl %r13d, %r13d
xorpd %xmm0, %xmm0
movsd 0xc0f77(%rip), %xmm1    # 0x231638
movq 0x20(%rsp), %rax
movl (%rax,%r13,8), %eax
shlq $0x4, %rax
leaq (%r11,%rax), %r9
movq (%r11,%rax), %rax
movl %eax, %r8d
shrq $0x20, %rax
shlq $0x5, %rax
movl (%r12,%rax), %r10d
shlq $0x5, %r8
leaq (%r12,%r8), %rbp
movl (%r12,%r8), %edi
testl %r10d, %r10d
je 0x1707cf
testl %edi, %edi
jne 0x170b93
movsd 0x8(%r9), %xmm2
movzwl 0x4(%r12,%r8), %eax
movw $0x0, 0x8(%rbp)
movq (%rbx), %rcx
movabsq $-0x61c8864680b583eb, %rdx # imm = 0x9E3779B97F4A7C15
addq %rdx, %rcx
movq %rcx, %rdx
shrq $0x1e, %rdx
xorq %rcx, %rdx
movabsq $-0x40a7b892e31b1a47, %rcx # imm = 0xBF58476D1CE4E5B9
imulq %rcx, %rdx
movq %rdx, %rsi
shrq $0x1b, %rsi
xorq %rdx, %rsi
movabsq $-0x6b2fb644ecceee15, %rcx # imm = 0x94D049BB133111EB
imulq %rcx, %rsi
movq %rsi, %rcx
shrq $0x1f, %rcx
xorq %rsi, %rcx
movq %rcx, (%rbx)
movzwl 0x6(%rbp), %edx
cmpw $-0x1, %dx
je 0x170797
movq %rcx, %rsi
shrq $0xc, %rsi
movabsq $0x3ff0000000000000, %rdi # imm = 0x3FF0000000000000
orq %rdi, %rsi
movq %rsi, %xmm3
addsd %xmm1, %xmm3
ucomisd 0x10(%rbp), %xmm3
jbe 0x170797
movw $0x1, 0x8(%rbp)
movl %edx, %eax
xorl %edx, %edx
ucomisd %xmm0, %xmm2
seta %dl
negl %edx
xorl %edx, %r10d
andl 0x68(%rbx), %r10d
je 0x170a2a
movl %r10d, %edx
xorl %edi, %edi
movl %r10d, %esi
movl %edx, %r8d
andl $0x1, %r8d
addq %r8, %rdi
shrl %esi
cmpl $0x1, %edx
movl %esi, %edx
ja 0x1707b7
jmp 0x170a2c
leaq (%r12,%rax), %r10
testl %edi, %edi
je 0x1708a8
movsd 0x8(%r9), %xmm2
movzwl 0x4(%r12,%rax), %eax
movw $0x0, 0x8(%r10)
movq (%rbx), %rcx
movabsq $-0x61c8864680b583eb, %rdx # imm = 0x9E3779B97F4A7C15
addq %rdx, %rcx
movq %rcx, %rdx
shrq $0x1e, %rdx
xorq %rcx, %rdx
movabsq $-0x40a7b892e31b1a47, %rcx # imm = 0xBF58476D1CE4E5B9
imulq %rcx, %rdx
movq %rdx, %rsi
shrq $0x1b, %rsi
xorq %rdx, %rsi
movabsq $-0x6b2fb644ecceee15, %rcx # imm = 0x94D049BB133111EB
imulq %rcx, %rsi
movq %rsi, %rcx
shrq $0x1f, %rcx
xorq %rsi, %rcx
movq %rcx, (%rbx)
movzwl 0x6(%r10), %edx
cmpw $-0x1, %dx
je 0x170874
movq %rcx, %rsi
shrq $0xc, %rsi
movabsq $0x3ff0000000000000, %r8 # imm = 0x3FF0000000000000
orq %r8, %rsi
movq %rsi, %xmm3
addsd %xmm1, %xmm3
ucomisd 0x10(%r10), %xmm3
jbe 0x170874
movw $0x1, 0x8(%r10)
movl %edx, %eax
xorl %edx, %edx
ucomisd %xmm0, %xmm2
seta %dl
negl %edx
xorl %edx, %edi
andl 0x68(%rbx), %edi
je 0x170a51
movl %edi, %edx
xorl %ebp, %ebp
movl %edi, %esi
movl %edx, %r8d
andl $0x1, %r8d
addq %r8, %rbp
shrl %esi
cmpl $0x1, %edx
movl %esi, %edx
ja 0x170890
jmp 0x170a53
movzwl 0x4(%r12,%rax), %eax
movw $0x0, 0x8(%r10)
movq (%rbx), %rcx
movabsq $-0x61c8864680b583eb, %rdx # imm = 0x9E3779B97F4A7C15
addq %rdx, %rcx
movq %rcx, %rdx
shrq $0x1e, %rdx
xorq %rcx, %rdx
movabsq $-0x40a7b892e31b1a47, %rcx # imm = 0xBF58476D1CE4E5B9
imulq %rcx, %rdx
movq %rdx, %rsi
shrq $0x1b, %rsi
xorq %rdx, %rsi
movabsq $-0x6b2fb644ecceee15, %rcx # imm = 0x94D049BB133111EB
imulq %rcx, %rsi
movq %rsi, %rcx
shrq $0x1f, %rcx
xorq %rsi, %rcx
movzwl 0x6(%r10), %edx
cmpw $-0x1, %dx
je 0x170938
movq %rcx, %rsi
shrq $0xc, %rsi
movabsq $0x3ff0000000000000, %rdi # imm = 0x3FF0000000000000
orq %rdi, %rsi
movq %rsi, %xmm2
addsd %xmm1, %xmm2
ucomisd 0x10(%r10), %xmm2
jbe 0x170938
movw $0x1, 0x8(%r10)
movl %edx, %eax
decl %eax
movq 0x60(%rbx), %rsi
movzwl %ax, %eax
leaq (%rax,%rax,2), %rdi
movq (%rsi,%rdi,8), %r14
movq %rcx, %rax
xorl %edx, %edx
divq 0x8(%rsi,%rdi,8)
movl (%r14,%rdx,4), %edi
movl %edi, (%r10)
movsd 0x8(%r9), %xmm2
movzwl 0x4(%r12,%r8), %eax
movw $0x0, 0x8(%r12,%r8)
movabsq $-0x61c8864680b583eb, %rdx # imm = 0x9E3779B97F4A7C15
addq %rdx, %rcx
movq %rcx, %rdx
shrq $0x1e, %rdx
xorq %rcx, %rdx
movabsq $-0x40a7b892e31b1a47, %rcx # imm = 0xBF58476D1CE4E5B9
imulq %rcx, %rdx
movq %rdx, %rsi
shrq $0x1b, %rsi
xorq %rdx, %rsi
movabsq $-0x6b2fb644ecceee15, %rcx # imm = 0x94D049BB133111EB
imulq %rcx, %rsi
movq %rsi, %rcx
shrq $0x1f, %rcx
xorq %rsi, %rcx
movq %rcx, (%rbx)
movzwl 0x6(%rbp), %edx
cmpw $-0x1, %dx
je 0x1709f5
movq %rcx, %rsi
shrq $0xc, %rsi
movabsq $0x3ff0000000000000, %r9 # imm = 0x3FF0000000000000
orq %r9, %rsi
movq %rsi, %xmm3
addsd %xmm1, %xmm3
ucomisd 0x10(%rbp), %xmm3
jbe 0x1709f5
leaq (%r12,%r8), %rax
addq $0x8, %rax
movw $0x1, (%rax)
movl %edx, %eax
xorl %edx, %edx
ucomisd %xmm0, %xmm2
seta %dl
negl %edx
xorl %edx, %edi
andl 0x68(%rbx), %edi
je 0x170b20
movl %edi, %edx
xorl %r10d, %r10d
movl %edi, %esi
movl %edx, %r8d
andl $0x1, %r8d
addq %r8, %r10
shrl %esi
cmpl $0x1, %edx
movl %esi, %edx
ja 0x170a12
jmp 0x170b23
xorl %edi, %edi
movzwl %ax, %eax
movq %rdi, %r8
subq %rax, %r8
je 0x170ac6
jae 0x170a78
movl 0x6c(%rbx), %edx
subq %rdi, %rdx
subq %rdi, %rax
xorl %r9d, %r9d
movq %rdx, %rdi
movq %rax, %r8
jmp 0x170a7e
xorl %ebp, %ebp
movzwl %ax, %eax
movq %rbp, %r8
subq %rax, %r8
je 0x170b1b
jae 0x170acf
movl 0x6c(%rbx), %edx
subq %rbp, %rdx
subq %rbp, %rax
xorl %r9d, %r9d
movq %rdx, %rbp
movq %rax, %r8
jmp 0x170ad5
movl $0x1, %r9d
testq %r8, %r8
je 0x170ac6
movl 0x6c(%rbx), %r15d
xorl %esi, %esi
testq %r15, %r15
je 0x170abb
movq %rcx, %rax
xorl %edx, %edx
divq %rdi
xorl %eax, %eax
btl %eax, %r10d
movl %r9d, %r14d
sbbl $0x0, %r14d
jne 0x170aad
testq %rdx, %rdx
je 0x170ab7
decq %rdx
incq %rax
cmpq %rax, %r15
jne 0x170a98
jmp 0x170abb
btcl %eax, %r10d
decq %rdi
incq %rsi
cmpq %r8, %rsi
jne 0x170a89
movl %r10d, (%rbp)
jmp 0x170b93
movl $0x1, %r9d
testq %r8, %r8
je 0x170b1b
movl 0x6c(%rbx), %r15d
xorl %esi, %esi
testq %r15, %r15
je 0x170b10
movq %rcx, %rax
xorl %edx, %edx
divq %rbp
xorl %eax, %eax
btl %eax, %edi
movl %r9d, %r14d
sbbl $0x0, %r14d
jne 0x170b03
testq %rdx, %rdx
je 0x170b0d
decq %rdx
incq %rax
cmpq %rax, %r15
jne 0x170aef
jmp 0x170b10
btcl %eax, %edi
decq %rbp
incq %rsi
cmpq %r8, %rsi
jne 0x170ae0
movl %edi, (%r10)
jmp 0x170b93
xorl %r10d, %r10d
movzwl %ax, %eax
movq %r10, %r8
subq %rax, %r8
je 0x170b90
jae 0x170b44
movl 0x6c(%rbx), %edx
subq %r10, %rdx
subq %r10, %rax
xorl %r9d, %r9d
movq %rdx, %r10
movq %rax, %r8
jmp 0x170b4a
movl $0x1, %r9d
testq %r8, %r8
je 0x170b90
movl 0x6c(%rbx), %r15d
xorl %esi, %esi
testq %r15, %r15
je 0x170b85
movq %rcx, %rax
xorl %edx, %edx
divq %r10
xorl %eax, %eax
btl %eax, %edi
movl %r9d, %r14d
sbbl $0x0, %r14d
jne 0x170b78
testq %rdx, %rdx
je 0x170b82
decq %rdx
incq %rax
cmpq %rax, %r15
jne 0x170b64
jmp 0x170b85
btcl %eax, %edi
decq %r10
incq %rsi
cmpq %r8, %rsi
jne 0x170b55
movl %edi, (%rbp)
incl %r13d
cmpq %r13, 0x10(%rsp)
ja 0x1706c1
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1703e0
movq %r14, %rdi
movq %rbx, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1704b0