// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/rcut.cpp
// Function: reorder_bub(mc_match_t const*, unsigned int*, unsigned int, unsigned char*, unsigned char, unsigned char, unsigned char, unsigned char, double*, double*, double*, unsigned int*)
void reorder_bub(const mc_match_t *ma, uint32_t *a, uint32_t a_n, uint8_t *ff, uint8_t lf, uint8_t rf, uint8_t cf,
uint8_t cuf, double *sc_l, double *sc_r, double *sc_m, uint32_t *res)
{
	uint32_t k, o, j, n, t, z; mc_edge_t *e; 
	double mm_l, mm_r, m_inner, mm; int64_t mmlk, mmrk, rev, m_inner_k, mmk;
	for (k = 0; k < a_n; k++) ff[a[k]] = cf;
	m_inner = 0; m_inner_k = -1; mm_l = mm_r = 0; mmlk = mmrk = -1;
	for (k = 0, rev = -1; k < a_n; k++) {
		sc_l[k] = sc_r[k] = sc_m[k] = 0;
		o = ma->idx.a[a[k]] >> 32; n = (uint32_t)ma->idx.a[a[k]];
		for (j = 0; j < n; ++j) {
			e = &ma->ma.a[o + j];
			t = ma_y(*e);
			if(ff[t] == lf) sc_l[k] += fabs(e->w);
			if(ff[t] == rf) sc_r[k] += fabs(e->w);
			if(ff[t] == cf) sc_m[k] += fabs(e->w);
		}
		if(sc_l[k] > 0) {
			if(sc_l[k] > mm_l) {
				mm_l = sc_l[k]; mmlk = k; 
			}
		} else if(sc_r[k] > 0) {
			if(sc_r[k] > mm_r) {
				mm_r = sc_r[k]; mmrk = k; 
			}
		} else if(sc_m[k] > 0 && sc_m[k] > m_inner) {
			m_inner = sc_m[k]; m_inner_k = k;
		}
	}
	if(mmlk == -1 && mmrk == -1 && m_inner_k == -1) return;
	if(mmlk != -1) {
		mm = mm_l; mmk = mmlk; rev = 0;
	} else if(mmrk != -1) {
		mm = mm_r; mmk = mmrk; rev = 1;
	} else {
		mm = m_inner; mmk = m_inner_k; rev = 0;
	}

	double *sc = (!rev)?sc_l:sc_r;
	// for (k = 0; k < a_n; k++) {
	// 	// sc_m[k] = 0;
	// 	fprintf(stderr, "+[M::%s] a_n::%u, a[%u]::%u\n", __func__, a_n, k, a[k]);
	// }
	
	z = 0; res[z++] = a[mmk]; ff[a[mmk]] = cuf; 
	for (; z < a_n; ) {
		for (k = 0, mm = 0, mmk = -1; k < a_n; k++) {
			if(ff[a[k]] == cuf) continue;
			o = ma->idx.a[a[k]] >> 32; n = (uint32_t)ma->idx.a[a[k]];
			for (j = 0; j < n; ++j) {
				e = &ma->ma.a[o + j];
				t = ma_y(*e);
				if(ff[t] == cuf) sc[k] += fabs(e->w);
				// if(ff[t] == cuf) sc_m[k] += fabs(e->w);
			}

			if(sc[k] > 0) {
				if(sc[k] > mm) {
					mm = sc[k]; mmk = k; 
				}
			}
		}
		if(mmk == -1) break;
		// fprintf(stderr, "-[M::%s] mmk::%ld, a[%ld]::%u, z::%u\n", __func__, mmk, mmk, a[mmk], z);
		res[z++] = a[mmk]; ff[a[mmk]] = cuf; 
	}
	// fprintf(stderr, "[M::%s] a_n::%u, z::%u\n", __func__, a_n, k, z);
	if(z < a_n) {
		for (k = 0; k < a_n; k++) {
			if(ff[a[k]] == cuf) continue;
			res[z++] = a[k]; ff[a[k]] = cuf;
		}
	}
	assert(z == a_n);
	if(!rev) {
		for (k = 0; k < a_n; k++) {
			a[k] = res[k]; 
			// fprintf(stderr, "[M::%s] a_n::%u, a[k]::%u, res[k]::%u\n", __func__, a_n, a[k], res[k]);
			ff[a[k]] = lf;
		}
	} else {
		for (k = 0; k < a_n; k++) {
			a[k] = res[a_n-k-1]; ff[a[k]] = lf;
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq 0x90(%rsp), %r15
movq 0x88(%rsp), %rbx
movq 0x80(%rsp), %r14
movb 0x78(%rsp), %r11b
movb 0x70(%rsp), %bpl
movl %edx, 0xc(%rsp)
movl %edx, %r13d
xorl %eax, %eax
cmpq %rax, %r13
je 0x126fdf
movl (%rsi,%rax,4), %edx
movb %bpl, (%rcx,%rdx)
incq %rax
jmp 0x126fce
pushq $-0x1
popq %rax
xorpd %xmm1, %xmm1
xorl %r12d, %r12d
movapd 0xc7b9f(%rip), %xmm0    # 0x1eeb90
xorpd %xmm4, %xmm4
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
movq %rax, 0x20(%rsp)
movq %rax, 0x30(%rsp)
movq %rax, 0x28(%rsp)
movq %rsi, 0x18(%rsp)
movq %r13, 0x10(%rsp)
cmpq %r13, %r12
je 0x12713a
andq $0x0, (%r15,%r12,8)
andq $0x0, (%rbx,%r12,8)
andq $0x0, (%r14,%r12,8)
movq 0x10(%rdi), %rax
movl (%rsi,%r12,4), %edx
movq (%rax,%rdx,8), %r13
movl %r13d, %eax
shrq $0x20, %r13
subq $0x1, %rax
jb 0x1270bb
movq 0x28(%rdi), %rdx
movl %r13d, %esi
shlq $0x4, %rsi
leaq (%rdx,%rsi), %r10
movl (%rdx,%rsi), %edx
movb (%rcx,%rdx), %sil
cmpb %r8b, %sil
jne 0x12707c
movsd 0x8(%r10), %xmm5
andpd %xmm0, %xmm5
addsd (%r14,%r12,8), %xmm5
movsd %xmm5, (%r14,%r12,8)
movb (%rcx,%rdx), %sil
cmpb %r9b, %sil
jne 0x12709b
movsd 0x8(%r10), %xmm5
andpd %xmm0, %xmm5
addsd (%rbx,%r12,8), %xmm5
movsd %xmm5, (%rbx,%r12,8)
movb (%rcx,%rdx), %sil
cmpb %bpl, %sil
jne 0x1270b6
movsd 0x8(%r10), %xmm5
andpd %xmm0, %xmm5
addsd (%r15,%r12,8), %xmm5
movsd %xmm5, (%r15,%r12,8)
incq %r13
jmp 0x127041
movsd (%r14,%r12,8), %xmm5
ucomisd %xmm1, %xmm5
jbe 0x1270de
ucomisd %xmm4, %xmm5
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %r13
jbe 0x12712a
movq %r12, 0x30(%rsp)
jmp 0x12712e
movsd (%rbx,%r12,8), %xmm6
ucomisd %xmm1, %xmm6
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %r13
jbe 0x127109
ucomisd %xmm2, %xmm6
jbe 0x12712a
movapd %xmm4, %xmm5
movapd %xmm6, %xmm2
movq %r12, 0x20(%rsp)
jmp 0x12712e
movsd (%r15,%r12,8), %xmm6
ucomisd %xmm1, %xmm6
jbe 0x12712a
ucomisd %xmm3, %xmm6
jbe 0x12712a
movapd %xmm4, %xmm5
movapd %xmm6, %xmm3
movq %r12, 0x28(%rsp)
jmp 0x12712e
movapd %xmm4, %xmm5
incq %r12
movapd %xmm5, %xmm4
jmp 0x127016
movq 0x30(%rsp), %rdx
movq %rdx, %rax
movq 0x20(%rsp), %r9
andq %r9, %rax
movq 0x28(%rsp), %r10
andq %r10, %rax
cmpq $-0x1, %rax
movq 0x98(%rsp), %r15
jne 0x12716f
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $-0x1, %r9
sete %al
cmovneq %r9, %r10
cmoveq %r14, %rbx
cmpq $-0x1, %rdx
setne %r9b
cmovneq %rdx, %r10
cmovneq %r14, %rbx
orb %al, %r9b
movl (%rsi,%r10,4), %edx
movl %edx, (%r15)
pushq $0x1
popq %rax
pushq $-0x1
popq %r14
xorpd %xmm1, %xmm1
movl %edx, %edx
movb %r11b, (%rcx,%rdx)
cmpq %r13, %rax
jae 0x127297
xorpd %xmm2, %xmm2
xorl %r12d, %r12d
movq %r14, %r15
cmpq %r13, %r12
je 0x12724c
movl (%rsi,%r12,4), %edx
cmpb %r11b, (%rcx,%rdx)
je 0x127244
movq 0x10(%rdi), %rsi
movq (%rsi,%rdx,8), %r10
movl %r10d, %r13d
shrq $0x20, %r10
subq $0x1, %r13
jb 0x127215
movq 0x28(%rdi), %rdx
movl %r10d, %esi
shlq $0x4, %rsi
movl (%rdx,%rsi), %ebp
cmpb %r11b, (%rcx,%rbp)
jne 0x127210
addq %rsi, %rdx
movsd 0x8(%rdx), %xmm3
andpd %xmm0, %xmm3
addsd (%rbx,%r12,8), %xmm3
movsd %xmm3, (%rbx,%r12,8)
incq %r10
jmp 0x1271de
movsd (%rbx,%r12,8), %xmm3
ucomisd %xmm1, %xmm3
jbe 0x12723a
ucomisd %xmm2, %xmm3
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %r13
jbe 0x127244
movapd %xmm3, %xmm2
movq %r12, %r15
jmp 0x127244
movq 0x18(%rsp), %rsi
movq 0x10(%rsp), %r13
incq %r12
jmp 0x1271bc
cmpq $-0x1, %r15
je 0x12726a
movl (%rsi,%r15,4), %edx
movq 0x98(%rsp), %r15
movl %edx, (%r15,%rax,4)
incq %rax
jmp 0x1271a3
xorl %edi, %edi
movq 0x98(%rsp), %r15
cmpq %rdi, %r13
je 0x127297
movl (%rsi,%rdi,4), %edx
cmpb %r11b, (%rcx,%rdx)
je 0x127292
movl %eax, %r10d
incl %eax
movl %edx, (%r15,%r10,4)
movl (%rsi,%rdi,4), %edx
movb %r11b, (%rcx,%rdx)
incq %rdi
jmp 0x127274
cmpl 0xc(%rsp), %eax
jne 0x1272e7
testb %r9b, %r9b
je 0x1272bd
xorl %eax, %eax
cmpq %rax, %r13
je 0x127160
movl (%r15,%rax,4), %edx
movl %edx, (%rsi,%rax,4)
movb %r8b, (%rcx,%rdx)
incq %rax
jmp 0x1272a4
shlq $0x2, %r13
movl 0xc(%rsp), %edi
decl %edi
xorl %eax, %eax
cmpq %rax, %r13
je 0x127160
movl %edi, %edx
movl (%r15,%rdx,4), %edx
movl %edx, (%rsi,%rax)
movb %r8b, (%rcx,%rdx)
addq $0x4, %rax
decl %edi
jmp 0x1272c9
leaq 0xc7c51(%rip), %rdi     # 0x1eef3f
leaq 0xc7c53(%rip), %rsi     # 0x1eef48
leaq 0xc7c55(%rip), %rcx     # 0x1eef51
movl $0x90d, %edx            # imm = 0x90D
callq 0x2110