// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, 0x50(%rsp)
movq 0x28(%rsi), %rax
movl 0x38(%rax), %ecx
xorl %edi, %edi
testl %ecx, %ecx
je 0x16e269
movl $0x1, %ebx
cmpl %ecx, %ebx
je 0x16e234
movq 0x30(%rax), %rcx
movl %edi, %edx
movl %ebx, %esi
movq (%rcx,%rsi,8), %rsi
xorq (%rcx,%rdx,8), %rsi
shrq $0x20, %rsi
je 0x16e25d
movl %edi, 0x8(%r12)
movl %ebx, %ecx
subl %edi, %ecx
movl %ecx, 0xc(%r12)
cmpl $0x2, %ecx
jb 0x16e25b
movq %rax, %rdi
movq %r12, %rsi
callq 0x16a16a
movq 0x50(%rsp), %rax
movq 0x28(%rax), %rax
movl %ebx, %edi
incl %ebx
movl 0x38(%rax), %ecx
cmpl %ecx, %ebx
jbe 0x16e21a
movq %rcx, %rdi
testq %r14, %r14
je 0x16e60d
movl $0x1, %esi
callq 0x2120
movq %rax, 0x40(%rsp)
movq %r14, 0x68(%rsp)
movq (%r14), %rax
xorl %edi, %edi
cmpq $0x0, 0x30(%rax)
movq %r12, 0x10(%rsp)
je 0x16e61c
xorpd %xmm0, %xmm0
xorl %r13d, %r13d
xorl %r9d, %r9d
movq 0x40(%rax), %rcx
imulq $0x38, %rdi, %rdx
movl 0x8(%rcx,%rdx), %r11d
movl 0xc(%rcx,%rdx), %r10d
leal (%r10,%r11), %esi
cmpl $0x2, %esi
jb 0x16e5f7
movq %r10, 0x38(%rsp)
movq %rdi, 0x48(%rsp)
movq 0x10(%rax), %rsi
movl 0x4(%rcx,%rdx), %eax
movq %rax, 0x30(%rsp)
testq %r11, %r11
movsd %xmm0, 0x8(%rsp)
movq %rsi, 0x18(%rsp)
je 0x16e362
movl (%rcx,%rdx), %eax
leaq (%rsi,%rax,4), %rbx
xorl %r15d, %r15d
xorl %edx, %edx
movl (%rbx,%r15,4), %eax
movq 0x50(%r12), %rcx
movb (%rcx,%rax), %cl
testb %cl, %cl
je 0x16e358
leal -0x80000000(%rax), %ebp
testb %cl, %cl
cmovnsl %eax, %ebp
cmpq %r13, %rdx
jne 0x16e351
leaq (%rdx,%rdx), %r13
testq %rdx, %rdx
movl $0x2, %eax
cmoveq %rax, %r13
movq %rdx, %r14
leaq (,%r13,4), %rsi
movq %r9, %rdi
movq %r11, %r12
callq 0x2370
movq %r12, %r11
movq %r14, %rdx
movq 0x10(%rsp), %r12
movsd 0x8(%rsp), %xmm0
movq %rax, %r9
movl %ebp, (%r9,%rdx,4)
incq %rdx
incq %r15
cmpq %r15, %r11
jne 0x16e2f7
jmp 0x16e364
xorl %edx, %edx
movq 0x38(%rsp), %rsi
testq %rsi, %rsi
je 0x16e3e5
movq 0x18(%rsp), %rax
movq 0x30(%rsp), %rcx
leaq (%rax,%rcx,4), %rbx
xorl %r14d, %r14d
movl (%rbx,%r14,4), %eax
movq 0x50(%r12), %rcx
movb (%rcx,%rax), %cl
testb %cl, %cl
je 0x16e3dd
leal -0x80000000(%rax), %ebp
testb %cl, %cl
cmovlel %eax, %ebp
cmpq %r13, %rdx
jne 0x16e3d6
leaq (%rdx,%rdx), %r15
testq %rdx, %rdx
movl $0x2, %eax
cmoveq %rax, %r15
movq %rdx, %r13
leaq (,%r15,4), %rsi
movq %r9, %rdi
callq 0x2370
movq %r13, %rdx
movq %r15, %r13
movq 0x38(%rsp), %rsi
movsd 0x8(%rsp), %xmm0
movq %rax, %r9
movl %ebp, (%r9,%rdx,4)
incq %rdx
incq %r14
cmpq %r14, %rsi
jne 0x16e37f
testq %rdx, %rdx
je 0x16e5f2
movq %r13, 0x60(%rsp)
movq %rdx, 0x28(%rsp)
movq 0x28(%rsp), %rax
leaq (%r9,%rax,4), %rsi
movq %r9, %rdi
movq %r9, 0x18(%rsp)
callq 0x16707c
movq 0x28(%rsp), %rsi
movq 0x18(%rsp), %r9
movsd 0x8(%rsp), %xmm0
movl $0x1, %edi
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
xorl %r8d, %r8d
movl $0x1, %eax
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movq 0x40(%rsp), %rdx
cmpq %rax, %rsi
je 0x16e453
movl %ebx, %ecx
movl (%r9,%rcx,4), %ecx
xorl (%r9,%rax,4), %ecx
jns 0x16e57a
xorpd %xmm1, %xmm1
cmpl %edi, %ebx
jae 0x16e54e
movl %edi, %r14d
movl %ebx, %r12d
movq %r12, %rax
movl (%r9,%rax,4), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movb $0x1, (%rdx,%rcx)
incq %rax
cmpq %rax, %r14
jne 0x16e468
cmpl %edi, %ebx
jae 0x16e54e
movl %ebx, 0x5c(%rsp)
movl %r8d, 0x20(%rsp)
movl %r11d, 0x24(%rsp)
movq %rdi, 0x70(%rsp)
movl %r10d, 0x30(%rsp)
movsd %xmm0, 0x8(%rsp)
movq 0x50(%rsp), %rax
movq 0x28(%rax), %rbp
xorpd %xmm1, %xmm1
movq %r12, %r15
movq 0x10(%rsp), %r13
movl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
movsd %xmm1, 0x38(%rsp)
movl (%r9,%r15,4), %ecx
andl %ebx, %ecx
movq %r13, %rdi
movq %rbp, %rsi
movq 0x40(%rsp), %rdx
callq 0x16a869
movsd 0x38(%rsp), %xmm1
movq 0x18(%rsp), %r9
movq 0x40(%rsp), %rdx
addsd %xmm0, %xmm1
incq %r15
cmpq %r15, %r14
jne 0x16e4be
movq 0x70(%rsp), %rdi
movl 0x5c(%rsp), %ebx
cmpl %edi, %ebx
jae 0x16e534
movsd 0x8(%rsp), %xmm0
movq 0x28(%rsp), %rsi
movl 0x30(%rsp), %r10d
movl 0x24(%rsp), %r11d
movl 0x20(%rsp), %r8d
movl (%r9,%r12,4), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, (%rdx,%rax)
incq %r12
cmpq %r12, %r14
jne 0x16e51d
jmp 0x16e54e
movsd 0x8(%rsp), %xmm0
movq 0x28(%rsp), %rsi
movl 0x30(%rsp), %r10d
movl 0x24(%rsp), %r11d
movl 0x20(%rsp), %r8d
cmpl $-0x1, %r11d
sete %al
ucomisd %xmm0, %xmm1
seta %cl
orb %al, %cl
cmovnel %ebx, %r11d
cmovnel %edi, %r10d
jne 0x16e56c
movapd %xmm0, %xmm1
incl %r8d
movl %edi, %ebx
movapd %xmm1, %xmm0
movq 0x10(%rsp), %r12
incl %edi
movq %rdi, %rax
cmpq %rdi, %rsi
jae 0x16e43e
cmpl $0x2, %r8d
movq 0x48(%rsp), %rdi
movq 0x60(%rsp), %r13
jb 0x16e5f7
cmpl %r10d, %r11d
jae 0x16e5f7
movsd %xmm0, 0x8(%rsp)
movl %r11d, %ebx
movl %r10d, %r14d
movq 0x50(%rsp), %rax
movq 0x28(%rax), %rdi
movl (%r9,%rbx,4), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq 0x50(%r12), %rax
movb (%rax,%rdx), %al
xorl %ecx, %ecx
subb %al, %cl
movsbl %cl, %ecx
movq %r12, %rsi
callq 0x16a6bf
movq 0x18(%rsp), %r9
incq %rbx
cmpq %rbx, %r14
jne 0x16e5a9
movq 0x48(%rsp), %rdi
movsd 0x8(%rsp), %xmm0
movq 0x60(%rsp), %r13
jmp 0x16e5f7
movq 0x48(%rsp), %rdi
incl %edi
movq 0x68(%rsp), %rax
movq (%rax), %rax
cmpq %rdi, 0x30(%rax)
ja 0x16e2a5
jmp 0x16e61f
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r9d, %r9d
movq %r9, %rdi
callq 0x2220
movq 0x40(%rsp), %rdi
callq 0x2220
movq 0x68(%rsp), %rdi
movq (%rdi), %rdx
cmpq $0x0, 0x30(%rdx)
je 0x16e70e
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 0x16e6c4
leaq (%r12,%r11,4), %r11
movq 0x10(%rsp), %rbx
movq 0x50(%rbx), %rbx
xorl %r14d, %r14d
movl (%r11,%r14,4), %r15d
movb (%rbx,%r15), %bpl
testb %bpl, %bpl
jne 0x16e6bf
incq %r14
cmpq %r14, %r9
jne 0x16e6a8
jmp 0x16e6c4
movb %bpl, 0x10(%r10,%rdx)
testq %rsi, %rsi
je 0x16e6fc
leaq (%r12,%r8,4), %r11
movq 0x10(%rsp), %r8
movq 0x50(%r8), %r8
xorl %r9d, %r9d
movl (%r11,%r9,4), %r10d
movb (%r8,%r10), %r10b
testb %r10b, %r10b
jne 0x16e6f0
incq %r9
cmpq %r9, %rsi
jne 0x16e6d9
jmp 0x16e6fc
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 0x16e64e
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x16e0b5