// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/rcut.cpp
// Function: mc_init_spin_all(mc_opt_t const*, mc_g_t*, mb_g_t*, mc_svaux_t*)
void mc_init_spin_all(const mc_opt_t *opt, mc_g_t *mg, mb_g_t *mbg, mc_svaux_t *b)
{
	uint32_t st, i, k, m, a_n[2], *a[2], qn;
	for (st = 0, i = 1; i <= mg->e->n_seq; ++i) {
		if (i == mg->e->n_seq || mg->e->cc[st]>>32 != mg->e->cc[i]>>32) {
			b->cc_off = st, b->cc_size = i - st;
			if (b->cc_size >= 2)
			{
				mc_init_spin(mg->e, b);
			}
			st = i;
		}
	}

	if(!mbg) return;
	// memcpy(b->s_opt, b->s, sizeof(int8_t)*mg->e->n_seq);

	///adjust by block
	kvec_t(uint32_t) s; kv_init(s);
	uint32_t ws, we, n_flip;
	uint8_t *vis = NULL; CALLOC(vis, mg->e->n_seq);
	t_w_t w = 0, max_w = 0;
	for (i = 0; i < mbg->u->u.n; i++)///each block
	{
		decode_mb_node(mbg, i, &(a[0]), &(a_n[0]), NULL, &(a[1]), &(a_n[1]), NULL);
		///if(a_n[0] == 0 || a_n[1] == 0) continue;
		if(a_n[0] + a_n[1] <= 1) continue;
		s.n = 0;
		for (k = 0; k < a_n[0]; k++)
        {
            qn = a[0][k];
			if(b->s[qn] == 0) continue;
			// qn <<= 1;
			if(b->s[qn] < 0) qn += ((uint32_t)1<<31);			
			kv_push(uint32_t, s, qn);
		}

		for (k = 0; k < a_n[1]; k++)
        {
            qn = a[1][k];
			if(b->s[qn] == 0) continue;
			// qn <<= 1; qn++;
			if(b->s[qn] > 0) qn += ((uint32_t)1<<31);
			kv_push(uint32_t, s, qn);
		}

		if(s.n == 0) continue;
		radix_sort_mc32(s.a, s.a + s.n);

		ws = we = (uint32_t)-1; n_flip = 0;
		for (st = 0, k = 1; k <= s.n; k++)
		{
			if(k == s.n || (s.a[k]>>31) != (s.a[st]>>31))
			{
				w = 0;
				for (m = st; m < k; m++) vis[(s.a[m]<<1)>>1] = 1;
				for (m = st; m < k; m++) w += incre_weight(b, mg->e, vis, (s.a[m]<<1)>>1);
				for (m = st; m < k; m++) vis[(s.a[m]<<1)>>1] = 0;
				if(ws == (uint32_t)-1 || max_w < w) max_w = w, ws = st, we = k;
				st = k;
				n_flip++;
			}
		}

		if(n_flip <= 1) continue;

		/*******************************for debug************************************/
		// t_w_t sc_opt = mc_score_all(mg->e, b);
		/*******************************for debug************************************/
		for (m = ws; m < we; m++) mc_set_spin(mg->e, b, (s.a[m]<<1)>>1, -b->s[(s.a[m]<<1)>>1]);
		/*******************************for debug************************************/
		// t_w_t sc_cur = mc_score_all(mg->e, b);
		// if(sc_cur - sc_opt != max_w*2)
		// {
		// 	fprintf(stderr, "ERROR-0-::%s\n", __func__);
		// 	// fprintf(stderr, "sc_opt: %ld, sc_cur: %ld, max_w: %ld\n", sc_opt, sc_cur, max_w);
		// 	fprintf(stderr, "sc_opt: %f, sc_cur: %f, max_w: %f\n", sc_opt, sc_cur, max_w);
		// }
		/*******************************for debug************************************/
	}
	kv_destroy(s); free(vis);

	for (i = 0; i < mbg->u->u.n; i++)///each block
	{
		decode_mb_node(mbg, i, &(a[0]), &(a_n[0]), NULL, &(a[1]), &(a_n[1]), NULL);
		mbg->u->u.a[i].s[0] = mbg->u->u.a[i].s[1] = 0;
		mbg->u->u.a[i].z[0] = mbg->u->u.a[i].z[1] = 0;
		mbg->u->u.a[i].z[2] = mbg->u->u.a[i].z[3] = 0;
		for (k = 0; k < a_n[0]; k++)
        {
            qn = a[0][k];
			if(b->s[qn] == 0) continue;
			mbg->u->u.a[i].s[0] = b->s[qn];
			break;
		}

		for (k = 0; k < a_n[1]; k++)
        {
            qn = a[1][k];
			if(b->s[qn] == 0) continue;
			mbg->u->u.a[i].s[1] = b->s[qn];
			break;
		}
	}

	reset_mb_g_t_z(mbg);

	/*******************************for debug************************************/
	// debug_mbg(mbg, mg, b);
	/*******************************for debug************************************/
	// memcpy(b->s, b->s_opt, sizeof(int8_t)*mg->e->n_seq);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, %r8
movq %rdx, %r14
movq %rsi, %r15
movq 0x28(%rsi), %rdi
movl 0x38(%rdi), %ecx
testl %ecx, %ecx
movq %r8, (%rsp)
je 0x16784a
xorl %eax, %eax
movl $0x1, %ebx
cmpl %ecx, %ebx
je 0x16781c
movq 0x30(%rdi), %rcx
movl %eax, %edx
movl %ebx, %esi
movq (%rcx,%rsi,8), %rsi
xorq (%rcx,%rdx,8), %rsi
shrq $0x20, %rsi
je 0x16783d
movl %eax, 0x8(%r8)
movl %ebx, %ecx
subl %eax, %ecx
movl %ecx, 0xc(%r8)
movl %ebx, %eax
cmpl $0x2, %ecx
jb 0x16783d
movq %r8, %rsi
callq 0x1637b8
movq (%rsp), %r8
movl %ebx, %eax
incl %ebx
movq 0x28(%r15), %rdi
movl 0x38(%rdi), %ecx
cmpl %ecx, %ebx
jbe 0x167802
testq %r14, %r14
je 0x167bab
movl %ecx, %edi
movl $0x1, %esi
callq 0x2120
movq %rax, 0x20(%rsp)
movq %r14, 0x40(%rsp)
movq (%r14), %rax
cmpq $0x0, 0x30(%rax)
je 0x167bba
movq %r15, 0x48(%rsp)
xorpd %xmm0, %xmm0
xorl %r8d, %r8d
xorl %r13d, %r13d
xorl %edx, %edx
movq (%rsp), %rsi
movq 0x40(%rax), %rcx
movq %rdx, 0x50(%rsp)
imulq $0x38, %rdx, %rdx
movl 0x8(%rcx,%rdx), %r15d
movl 0xc(%rcx,%rdx), %edi
movq %rdi, 0x18(%rsp)
addl %r15d, %edi
cmpl $0x2, %edi
jb 0x167b90
movq 0x10(%rax), %rdi
movl 0x4(%rcx,%rdx), %eax
movq %rax, 0x28(%rsp)
testq %r15, %r15
movsd %xmm0, 0x8(%rsp)
movq %rdi, 0x10(%rsp)
je 0x167942
movl (%rcx,%rdx), %eax
leaq (%rdi,%rax,4), %rdx
xorl %ebx, %ebx
xorl %r12d, %r12d
movl (%rdx,%rbx,4), %eax
movq 0x50(%rsi), %rcx
movb (%rcx,%rax), %cl
testb %cl, %cl
je 0x167938
leal -0x80000000(%rax), %ebp
testb %cl, %cl
cmovnsl %eax, %ebp
cmpq %r8, %r12
jne 0x167930
leaq (%r8,%r8), %r14
testq %r8, %r8
movl $0x2, %eax
cmoveq %rax, %r14
leaq (,%r14,4), %rsi
movq %r13, %rdi
movq %rdx, %r13
callq 0x2370
movq %r13, %rdx
movsd 0x8(%rsp), %xmm0
movq (%rsp), %rsi
movq %rax, %r13
movq %r14, %r8
movl %ebp, (%r13,%r12,4)
incq %r12
incq %rbx
cmpq %rbx, %r15
jne 0x1678dc
jmp 0x167945
xorl %r12d, %r12d
movq 0x18(%rsp), %rdx
testq %rdx, %rdx
je 0x1679c4
movq 0x10(%rsp), %rax
movq 0x28(%rsp), %rcx
leaq (%rax,%rcx,4), %rbx
xorl %r14d, %r14d
movl (%rbx,%r14,4), %eax
movq 0x50(%rsi), %rcx
movb (%rcx,%rax), %cl
testb %cl, %cl
je 0x1679bc
leal -0x80000000(%rax), %ebp
testb %cl, %cl
cmovlel %eax, %ebp
cmpq %r8, %r12
jne 0x1679b4
leaq (%r8,%r8), %r15
testq %r8, %r8
movl $0x2, %eax
cmoveq %rax, %r15
leaq (,%r15,4), %rsi
movq %r13, %rdi
callq 0x2370
movsd 0x8(%rsp), %xmm0
movq (%rsp), %rsi
movq %rax, %r13
movq %r15, %r8
movq 0x18(%rsp), %rdx
movl %ebp, (%r13,%r12,4)
incq %r12
incq %r14
cmpq %r14, %rdx
jne 0x167960
testq %r12, %r12
je 0x167b90
movq %r8, 0x38(%rsp)
leaq (,%r12,4), %rsi
addq %r13, %rsi
movq %r13, %rdi
callq 0x160753
movsd 0x8(%rsp), %xmm0
movl $0x1, %esi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
movl $0x1, %eax
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %r10d, %r10d
movq 0x20(%rsp), %rdx
movq %r12, 0x58(%rsp)
cmpq %rax, %r12
je 0x167a28
movl %r10d, %ecx
movl (%r13,%rcx,4), %ecx
xorl (%r13,%rax,4), %ecx
jns 0x167b24
movl %r8d, 0x30(%rsp)
movl %r9d, 0x34(%rsp)
movl %edi, 0x28(%rsp)
movsd %xmm0, 0x8(%rsp)
movl %esi, %r14d
cmpl %esi, %r10d
jae 0x167a5e
movl %r10d, %eax
movl (%r13,%rax,4), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movb $0x1, (%rdx,%rcx)
incq %rax
cmpq %rax, %r14
jne 0x167a47
xorpd %xmm1, %xmm1
movq %rsi, 0x60(%rsp)
movl %r10d, 0x10(%rsp)
cmpl %esi, %r10d
movq (%rsp), %rbx
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
jae 0x167abe
movq 0x48(%rsp), %rax
movq 0x28(%rax), %rbp
movl 0x10(%rsp), %r15d
movsd %xmm1, 0x18(%rsp)
movl (%r13,%r15,4), %ecx
andl %r12d, %ecx
movq %rbx, %rdi
movq %rbp, %rsi
movq 0x20(%rsp), %rdx
callq 0x163ed3
movsd 0x18(%rsp), %xmm1
movq 0x20(%rsp), %rdx
addsd %xmm0, %xmm1
incq %r15
cmpq %r15, %r14
jne 0x167a89
movq 0x60(%rsp), %rsi
movl 0x10(%rsp), %edi
cmpl %esi, %edi
jae 0x167ae4
movl %edi, %eax
movl (%r13,%rax,4), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movb $0x0, (%rdx,%rcx)
incq %rax
cmpq %rax, %r14
jne 0x167acd
movl 0x34(%rsp), %r9d
cmpl $-0x1, %r9d
sete %al
movsd 0x8(%rsp), %xmm0
ucomisd %xmm0, %xmm1
seta %cl
orb %al, %cl
cmovnel %edi, %r9d
movl 0x28(%rsp), %edi
cmovnel %esi, %edi
movq 0x58(%rsp), %r12
movl 0x30(%rsp), %r8d
jne 0x167b1a
movapd %xmm0, %xmm1
incl %r8d
movl %esi, %r10d
movapd %xmm1, %xmm0
incl %esi
movq %rsi, %rax
cmpq %rsi, %r12
jae 0x167a10
cmpl $0x2, %r8d
movq (%rsp), %rsi
movq 0x38(%rsp), %r8
jb 0x167b90
cmpl %edi, %r9d
jae 0x167b90
movsd %xmm0, 0x8(%rsp)
movl %r9d, %ebx
movl %edi, %r14d
movq 0x48(%rsp), %r15
movq 0x28(%r15), %rdi
movl (%r13,%rbx,4), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq 0x50(%rsi), %rax
movb (%rax,%rdx), %al
xorl %ecx, %ecx
subb %al, %cl
movsbl %cl, %ecx
callq 0x163d25
movq (%rsp), %rsi
incq %rbx
cmpq %rbx, %r14
jne 0x167b57
movsd 0x8(%rsp), %xmm0
movq 0x38(%rsp), %r8
movq 0x50(%rsp), %rdx
incl %edx
movq 0x40(%rsp), %rax
movq (%rax), %rax
cmpq %rdx, 0x30(%rax)
ja 0x16788c
jmp 0x167bbd
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r13d, %r13d
movq %r13, %rdi
callq 0x2220
movq 0x20(%rsp), %rdi
callq 0x2220
movq 0x40(%rsp), %rdi
movq (%rdi), %rdx
cmpq $0x0, 0x30(%rdx)
je 0x167caa
xorl %eax, %eax
xorpd %xmm0, %xmm0
xorl %esi, %esi
xorl %ecx, %ecx
movq 0x10(%rdx), %r12
movq 0x40(%rdx), %r10
imulq $0x38, %rsi, %rdx
movl (%r10,%rdx), %r11d
movl 0x4(%r10,%rdx), %r8d
movl 0x8(%r10,%rdx), %r9d
movl 0xc(%r10,%rdx), %esi
movb %al, 0x11(%r10,%rdx)
movq (%rdi), %r10
movq 0x40(%r10), %r10
movb %al, 0x10(%r10,%rdx)
movq (%rdi), %r10
movq 0x40(%r10), %r10
movupd %xmm0, 0x28(%r10,%rdx)
movupd %xmm0, 0x18(%r10,%rdx)
testq %r9, %r9
je 0x167c61
leaq (%r12,%r11,4), %r11
movq (%rsp), %rbx
movq 0x50(%rbx), %rbx
xorl %r14d, %r14d
movl (%r11,%r14,4), %r15d
movb (%rbx,%r15), %bpl
testb %bpl, %bpl
jne 0x167c5c
incq %r14
cmpl %r14d, %r9d
jne 0x167c45
jmp 0x167c61
movb %bpl, 0x10(%r10,%rdx)
testq %rsi, %rsi
je 0x167c98
leaq (%r12,%r8,4), %r11
movq (%rsp), %r8
movq 0x50(%r8), %r8
xorl %r9d, %r9d
movl (%r11,%r9,4), %r10d
movb (%r8,%r10), %r10b
testb %r10b, %r10b
jne 0x167c8c
incq %r9
cmpl %r9d, %esi
jne 0x167c75
jmp 0x167c98
movq (%rdi), %rsi
movq 0x40(%rsi), %rsi
movb %r10b, 0x11(%rsi,%rdx)
incl %ecx
movq (%rdi), %rdx
movq %rcx, %rsi
cmpq %rcx, 0x30(%rdx)
ja 0x167bec
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x167675