// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movq %rdx, %r15
movq %rsi, 0x38(%rsp)
pushq $0x1
popq %rbx
xorl %ecx, %ecx
movq 0x38(%rsp), %rax
movq 0x28(%rax), %rdi
movl 0x38(%rdi), %eax
cmpl %eax, %ebx
ja 0x129c88
cmpl %eax, %ebx
je 0x129c67
movq 0x30(%rdi), %rax
movl %ecx, %edx
movl %ebx, %esi
movq (%rax,%rsi,8), %rsi
xorq (%rax,%rdx,8), %rsi
shrq $0x20, %rsi
je 0x129c84
movl %ecx, 0x8(%r14)
movl %ebx, %eax
subl %ecx, %eax
movl %eax, 0xc(%r14)
movl %ebx, %ecx
cmpl $0x2, %eax
jb 0x129c84
movq %r14, %rsi
callq 0x125a9f
movl %ebx, %ecx
incl %ebx
jmp 0x129c3d
testq %r15, %r15
je 0x12a066
movq %r15, 0x30(%rsp)
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0x2120
movq %rax, %r13
xorpd %xmm0, %xmm0
xorl %esi, %esi
xorl %r15d, %r15d
movq $0x0, 0x58(%rsp)
movl %esi, %eax
movq 0x30(%rsp), %rdi
movq (%rdi), %rcx
cmpq %rax, 0x30(%rcx)
jbe 0x129f70
movsd %xmm0, 0x10(%rsp)
movq %rsi, 0x18(%rsp)
leaq 0x20(%rsp), %rdx
movq %rsp, %rcx
xorl %r8d, %r8d
leaq 0x28(%rsp), %r9
pushq $0x0
leaq 0xc(%rsp), %rax
pushq %rax
callq 0x12e939
popq %rax
popq %rcx
movl (%rsp), %eax
movl 0x4(%rsp), %ecx
addl %eax, %ecx
cmpl $0x2, %ecx
jb 0x129f11
xorl %ebx, %ebx
xorl %r12d, %r12d
movq 0x58(%rsp), %rsi
movl %eax, %ecx
cmpq %rcx, %rbx
jae 0x129d77
movq 0x20(%rsp), %rcx
movl (%rcx,%rbx,4), %ecx
movq 0x50(%r14), %rdx
movb (%rdx,%rcx), %dl
testb %dl, %dl
je 0x129d72
leal -0x80000000(%rcx), %ebp
testb %dl, %dl
cmovnsl %ecx, %ebp
cmpq %rsi, %r12
jne 0x129d68
movq %r12, 0x8(%rsp)
leaq (%rsi,%rsi), %r12
testq %rsi, %rsi
pushq $0x2
popq %rax
cmoveq %rax, %r12
leaq (,%r12,4), %rsi
movq %r15, %rdi
callq 0x2370
movq %rax, %r15
movq %r12, %rsi
movq 0x8(%rsp), %r12
movl %ebp, (%r15,%r12,4)
incq %r12
movl (%rsp), %eax
incq %rbx
jmp 0x129d10
xorl %ebx, %ebx
movl 0x4(%rsp), %eax
cmpq %rax, %rbx
jae 0x129ddf
movq 0x28(%rsp), %rax
movl (%rax,%rbx,4), %eax
movq 0x50(%r14), %rcx
movb (%rcx,%rax), %cl
testb %cl, %cl
je 0x129dda
leal -0x80000000(%rax), %ebp
testb %cl, %cl
cmovlel %eax, %ebp
cmpq %rsi, %r12
jne 0x129dd3
movq %r12, 0x8(%rsp)
leaq (%rsi,%rsi), %r12
testq %rsi, %rsi
pushq $0x2
popq %rax
cmoveq %rax, %r12
leaq (,%r12,4), %rsi
movq %r15, %rdi
callq 0x2370
movq %rax, %r15
movq %r12, %rsi
movq 0x8(%rsp), %r12
movl %ebp, (%r15,%r12,4)
incq %r12
incq %rbx
jmp 0x129d79
movq %rsi, 0x58(%rsp)
testq %r12, %r12
je 0x129f11
leaq (%r15,%r12,4), %rsi
movq %r15, %rdi
callq 0x12268f
xorl %edx, %edx
pushq $0x1
popq %rsi
pushq $-0x1
popq %rax
movl %eax, %r8d
movl %eax, %ecx
xorl %edi, %edi
movsd 0x10(%rsp), %xmm0
movq %r12, 0x8(%rsp)
movl %esi, %ebp
cmpq %rbp, %r12
movsd %xmm0, 0x10(%rsp)
jb 0x129f23
movl %edx, %r12d
je 0x129e37
movl (%r15,%r12,4), %eax
xorl (%r15,%rbp,4), %eax
jns 0x129f05
movl %ecx, 0x54(%rsp)
movq %r12, %rax
cmpq %rbp, %rax
jae 0x129e58
movl (%r15,%rax,4), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movb $0x1, (%r13,%rcx)
incq %rax
jmp 0x129e3e
movl %r8d, 0x44(%rsp)
movl %edi, 0x48(%rsp)
movl %esi, 0x4c(%rsp)
movl %edx, 0x50(%rsp)
xorpd %xmm1, %xmm1
movq %r12, %rbx
cmpq %rbp, %rbx
jae 0x129eaf
movq 0x38(%rsp), %rax
movq 0x28(%rax), %rsi
movl (%r15,%rbx,4), %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %ecx
movq %r14, %rdi
movq %r13, %rdx
movsd %xmm1, 0x60(%rsp)
callq 0x126147
movsd 0x60(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
incq %rbx
jmp 0x129e70
movl 0x50(%rsp), %edx
movl 0x4c(%rsp), %esi
movl 0x48(%rsp), %edi
movl 0x44(%rsp), %r8d
cmpq %rbp, %r12
jae 0x129ed9
movl (%r15,%r12,4), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movb $0x0, (%r13,%rax)
incq %r12
jmp 0x129ec0
cmpl $-0x1, %r8d
sete %al
ucomisd %xmm0, %xmm1
seta %cl
orb %al, %cl
cmovel %r8d, %edx
movl 0x54(%rsp), %ecx
cmovnel %esi, %ecx
jne 0x129efa
movapd %xmm0, %xmm1
incl %edi
movapd %xmm1, %xmm0
movl %edx, %r8d
movl %esi, %edx
movq 0x8(%rsp), %r12
incl %esi
jmp 0x129e13
movq 0x18(%rsp), %rsi
movsd 0x10(%rsp), %xmm0
incl %esi
jmp 0x129cb6
cmpl $0x2, %edi
movq 0x18(%rsp), %rsi
jb 0x129f1c
movl %r8d, %ebx
movl %ecx, %ebp
cmpq %rbp, %rbx
jae 0x129f1c
movq 0x38(%rsp), %rax
movq 0x28(%rax), %rdi
movl (%r15,%rbx,4), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movq 0x50(%r14), %rax
movb (%rax,%rdx), %al
xorl %ecx, %ecx
subb %al, %cl
movsbl %cl, %ecx
movq %r14, %rsi
callq 0x125fc0
movsd 0x10(%rsp), %xmm0
movq 0x18(%rsp), %rsi
incq %rbx
jmp 0x129f32
movq %r15, %rdi
callq 0x2220
movq %r13, %rdi
callq 0x2220
xorl %ebx, %ebx
leaq 0x20(%rsp), %r15
movq %rsp, %r12
xorl %r13d, %r13d
movq 0x30(%rsp), %rbp
movl %r13d, %r13d
movq (%rbp), %rax
cmpq %r13, 0x30(%rax)
jbe 0x12a05e
movq %rbp, %rdi
movl %r13d, %esi
movq %r15, %rdx
movq %r12, %rcx
xorl %r8d, %r8d
leaq 0x28(%rsp), %r9
pushq $0x0
leaq 0xc(%rsp), %rax
pushq %rax
callq 0x12e939
popq %rax
popq %rcx
movq (%rbp), %rax
movq 0x40(%rax), %rcx
imulq $0x38, %r13, %rax
movb %bl, 0x11(%rcx,%rax)
movq (%rbp), %rcx
movq 0x40(%rcx), %rcx
movb %bl, 0x10(%rcx,%rax)
movq (%rbp), %rcx
movq 0x40(%rcx), %rcx
xorpd %xmm0, %xmm0
movupd %xmm0, 0x28(%rcx,%rax)
movupd %xmm0, 0x18(%rcx,%rax)
movl (%rsp), %edx
movq 0x20(%rsp), %rsi
xorl %edi, %edi
cmpq %rdi, %rdx
je 0x12a022
movl (%rsi,%rdi,4), %r8d
movq 0x50(%r14), %r9
movb (%r9,%r8), %r8b
incq %rdi
testb %r8b, %r8b
je 0x12a004
movb %r8b, 0x10(%rcx,%rax)
movl 0x4(%rsp), %ecx
movq 0x28(%rsp), %rdx
xorl %esi, %esi
cmpq %rsi, %rcx
je 0x12a056
movl (%rdx,%rsi,4), %edi
movq 0x50(%r14), %r8
movb (%r8,%rdi), %dil
incq %rsi
testb %dil, %dil
je 0x12a02d
movq 0x30(%rsp), %rcx
movq (%rcx), %rcx
movq 0x40(%rcx), %rcx
movb %dil, 0x11(%rcx,%rax)
incl %r13d
jmp 0x129f92
movq %rbp, %rdi
callq 0x12996e
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq