// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq $0x0, 0x10(%rax)
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x229041
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x8(%rcx), %ecx
addl -0x14(%rbp), %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x14(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl -0x18(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, (%rax)
jne 0x229034
jmp 0x229041
jmp 0x229036
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x228fe3
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jb 0x229052
jmp 0x22947d
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jae 0x229200
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movq -0x10(%rbp), %rcx
movl 0x8(%rcx), %ecx
addl -0x14(%rbp), %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x1c(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x1c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
movl %eax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x1c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x24(%rbp)
movl -0x1c(%rbp), %edx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x14(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0x2291f0
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x20(%rbp), %ecx
addl -0x28(%rbp), %ecx
movl %ecx, %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x30(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x22910b
movsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
jmp 0x22912c
movsd -0x30(%rbp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x18(%rcx), %rax
jne 0x22919f
movq -0x10(%rbp), %rax
cmpq $0x0, 0x18(%rax)
je 0x229164
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
shlq %rax
movq %rax, -0x50(%rbp)
jmp 0x22916f
movl $0x2, %eax
movq %rax, -0x50(%rbp)
jmp 0x22916f
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
cvttsd2si -0x30(%rbp), %rax
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
subl %ecx, %eax
movl %eax, %eax
movl %eax, %edx
shlq $0x20, %rdx
movl -0x20(%rbp), %eax
addl -0x28(%rbp), %eax
movl %eax, %eax
orq %rax, %rdx
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rsi
movq 0x10(%rsi), %rcx
movq %rcx, %rdi
addq $0x1, %rdi
movq %rdi, 0x10(%rsi)
movq %rdx, (%rax,%rcx,8)
jmp 0x2291e2
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x2290c4
jmp 0x2291f2
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x229059
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x216280
movl $0x0, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x22947b
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl -0x14(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x3c(%rbp)
movq -0x38(%rbp), %rax
movq (%rax), %rax
movl %eax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl -0x3c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, (%rax)
jne 0x22935a
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl -0x40(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, (%rax)
jne 0x22935a
movl -0x3c(%rbp), %eax
movl %eax, %edi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdx
movl -0x3c(%rbp), %eax
shlq $0x5, %rax
addq %rax, %rdx
addq $0x8, %rdx
callq 0x2294a0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl -0x3c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
movl -0x40(%rbp), %eax
movl %eax, %edi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdx
movl -0x40(%rbp), %eax
shlq $0x5, %rax
addq %rax, %rdx
addq $0x8, %rdx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl -0x3c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %ecx
movq -0x38(%rbp), %rax
movsd 0x8(%rax), %xmm0
xorps %xmm1, %xmm1
xorl %eax, %eax
movl $0x1, %r8d
ucomisd %xmm1, %xmm0
cmoval %r8d, %eax
movslq %eax, %r8
callq 0x2295e0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl -0x40(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
jmp 0x22946b
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl -0x3c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, (%rax)
jne 0x2293e3
movl -0x3c(%rbp), %eax
movl %eax, %edi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdx
movl -0x3c(%rbp), %eax
shlq $0x5, %rax
addq %rax, %rdx
addq $0x8, %rdx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl -0x40(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %ecx
movq -0x38(%rbp), %rax
movsd 0x8(%rax), %xmm0
xorps %xmm1, %xmm1
xorl %eax, %eax
movl $0x1, %r8d
ucomisd %xmm1, %xmm0
cmoval %r8d, %eax
movslq %eax, %r8
callq 0x2295e0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl -0x3c(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
jmp 0x229469
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl -0x40(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, (%rax)
jne 0x229467
movl -0x40(%rbp), %eax
movl %eax, %edi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rdx
movl -0x40(%rbp), %eax
shlq $0x5, %rax
addq %rax, %rdx
addq $0x8, %rdx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl -0x3c(%rbp), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %ecx
movq -0x38(%rbp), %rax
movsd 0x8(%rax), %xmm0
xorps %xmm1, %xmm1
xorl %eax, %eax
movl $0x1, %r8d
ucomisd %xmm1, %xmm0
cmoval %r8d, %eax
movslq %eax, %r8
callq 0x2295e0
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl -0x40(%rbp), %edx
shlq $0x5, %rdx
addq %rdx, %rax
movl %ecx, (%rax)
jmp 0x229469
jmp 0x22946b
jmp 0x22946d
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x22922b
jmp 0x22947d
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x228d30
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x228e90
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)