// File: /workspace/github_cpp_makefile_star2_O1/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 0x88(%rsp), %rax
movq 0x80(%rsp), %r10
testl %edx, %edx
movl %edx, 0xc(%rsp)
je 0x164fe9
movq 0x90(%rsp), %rbx
movb 0x70(%rsp), %bpl
movl %edx, %r14d
xorl %edx, %edx
movl (%rsi,%rdx,4), %r11d
movb %bpl, (%rcx,%r11)
incq %rdx
cmpq %rdx, %r14
jne 0x164e7f
movq 0x10(%rdi), %rdx
movq %rdx, 0x10(%rsp)
xorpd %xmm0, %xmm0
movq $-0x1, 0x28(%rsp)
movapd 0xcbbd3(%rip), %xmm1    # 0x230a80
xorl %r15d, %r15d
movq $-0x1, 0x20(%rsp)
movq $-0x1, 0x30(%rsp)
xorpd %xmm3, %xmm3
xorpd %xmm2, %xmm2
xorpd %xmm4, %xmm4
movq %r14, 0x18(%rsp)
xorl %edx, %edx
movq %rdx, (%rbx,%r15,8)
movq %rdx, (%rax,%r15,8)
movq %rdx, (%r10,%r15,8)
movl (%rsi,%r15,4), %edx
movq 0x10(%rsp), %r11
movq (%r11,%rdx,8), %rdx
testl %edx, %edx
je 0x164f6e
movq %rdx, %r11
shrq $0x20, %r11
movq 0x28(%rdi), %r13
movl %edx, %edx
movl %r11d, %r14d
shlq $0x4, %r14
leaq (%r14,%r13), %r12
movl (%r13,%r14), %r14d
cmpb %r8b, (%rcx,%r14)
jne 0x164f2c
movsd 0x8(%r12), %xmm5
andpd %xmm1, %xmm5
addsd (%r10,%r15,8), %xmm5
movsd %xmm5, (%r10,%r15,8)
cmpb %r9b, (%rcx,%r14)
jne 0x164f49
movsd 0x8(%r12), %xmm5
andpd %xmm1, %xmm5
addsd (%rax,%r15,8), %xmm5
movsd %xmm5, (%rax,%r15,8)
cmpb %bpl, (%rcx,%r14)
jne 0x164f66
movsd 0x8(%r12), %xmm5
andpd %xmm1, %xmm5
addsd (%rbx,%r15,8), %xmm5
movsd %xmm5, (%rbx,%r15,8)
incq %r11
decq %rdx
jne 0x164eff
movsd (%r10,%r15,8), %xmm5
ucomisd %xmm0, %xmm5
jbe 0x164f8c
ucomisd %xmm4, %xmm5
movq 0x18(%rsp), %r14
jbe 0x164fd3
movq %r15, 0x30(%rsp)
jmp 0x164fd7
movsd (%rax,%r15,8), %xmm6
ucomisd %xmm0, %xmm6
movq 0x18(%rsp), %r14
jbe 0x164fb2
ucomisd %xmm2, %xmm6
jbe 0x164fd3
movapd %xmm4, %xmm5
movapd %xmm6, %xmm2
movq %r15, 0x20(%rsp)
jmp 0x164fd7
movsd (%rbx,%r15,8), %xmm6
ucomisd %xmm0, %xmm6
jbe 0x164fd3
ucomisd %xmm3, %xmm6
jbe 0x164fd3
movapd %xmm4, %xmm5
movapd %xmm6, %xmm3
movq %r15, 0x28(%rsp)
jmp 0x164fd7
movapd %xmm4, %xmm5
incq %r15
movapd %xmm5, %xmm4
cmpq %r14, %r15
jne 0x164ed3
jmp 0x165004
movq $-0x1, 0x30(%rsp)
movq $-0x1, 0x20(%rsp)
movq $-0x1, 0x28(%rsp)
movq 0x30(%rsp), %r14
movq %r14, %rdx
movq 0x20(%rsp), %r15
andq %r15, %rdx
movq 0x28(%rsp), %r11
andq %r11, %rdx
cmpq $-0x1, %rdx
movl 0xc(%rsp), %r9d
je 0x1651e5
movq 0x98(%rsp), %rbx
cmpq $-0x1, %r15
cmovneq %r15, %r11
sete %dl
cmpq $-0x1, %r14
cmovneq %r14, %r11
setne %bpl
orb %dl, %bpl
cmovneq %r10, %rax
movb 0x78(%rsp), %r10b
movl (%rsi,%r11,4), %edx
movl %edx, (%rbx)
movl (%rsi,%r11,4), %edx
movb %r10b, (%rcx,%rdx)
movl $0x1, %r11d
cmpl $0x2, %r9d
setae %dl
jb 0x165198
movb %bpl, 0x10(%rsp)
movl %r9d, %r14d
movl $0x1, %r11d
xorpd %xmm0, %xmm0
movapd 0xcb9ef(%rip), %xmm1    # 0x230a80
movb %dl, 0x18(%rsp)
movq $-0x1, %r15
xorl %r12d, %r12d
xorpd %xmm2, %xmm2
movl (%rsi,%r12,4), %edx
cmpb %r10b, (%rcx,%rdx)
je 0x165114
movq 0x10(%rdi), %r9
movq (%r9,%rdx,8), %r9
testl %r9d, %r9d
je 0x1650fb
movq %r9, %rdx
shrq $0x20, %rdx
movq 0x28(%rdi), %r13
movl %r9d, %ebx
movl %edx, %r9d
shlq $0x4, %r9
movl (%r13,%r9), %ebp
cmpb %r10b, (%rcx,%rbp)
jne 0x1650f3
addq %r13, %r9
movsd 0x8(%r9), %xmm3
andpd %xmm1, %xmm3
addsd (%rax,%r12,8), %xmm3
movsd %xmm3, (%rax,%r12,8)
incq %rdx
decq %rbx
jne 0x1650c8
movsd (%rax,%r12,8), %xmm3
ucomisd %xmm0, %xmm3
jbe 0x165114
ucomisd %xmm2, %xmm3
jbe 0x165114
movapd %xmm3, %xmm2
movq %r12, %r15
incq %r12
cmpq %r14, %r12
jne 0x1650a3
cmpq $-0x1, %r15
je 0x165155
movl (%rsi,%r15,4), %edx
movq 0x98(%rsp), %rbx
movl %edx, (%rbx,%r11,4)
incq %r11
movl (%rsi,%r15,4), %edx
movb %r10b, (%rcx,%rdx)
cmpq %r14, %r11
setb %dl
jne 0x165091
movl 0xc(%rsp), %r9d
movb 0x10(%rsp), %bpl
jmp 0x165198
testb $0x1, 0x18(%rsp)
movq 0x98(%rsp), %rbx
movb 0x10(%rsp), %bpl
je 0x165193
xorl %eax, %eax
movl 0xc(%rsp), %r9d
movl (%rsi,%rax,4), %edx
cmpb %r10b, (%rcx,%rdx)
je 0x165189
movl %r11d, %edi
incl %r11d
movl %edx, (%rbx,%rdi,4)
movl (%rsi,%rax,4), %edx
movb %r10b, (%rcx,%rdx)
incq %rax
cmpq %rax, %r14
jne 0x165170
jmp 0x165198
movl 0xc(%rsp), %r9d
cmpl %r9d, %r11d
jne 0x1651f4
testb %bpl, %bpl
je 0x1651c0
testl %r9d, %r9d
je 0x1651e5
movl %r9d, %eax
xorl %edx, %edx
movl (%rbx,%rdx,4), %edi
movl %edi, (%rsi,%rdx,4)
movb %r8b, (%rcx,%rdi)
incq %rdx
cmpq %rdx, %rax
jne 0x1651ac
jmp 0x1651e5
testl %r9d, %r9d
je 0x1651e5
movl %r9d, %eax
decl %r9d
xorl %edx, %edx
movl %r9d, %edi
movl (%rbx,%rdi,4), %edi
movl %edi, (%rsi,%rdx,4)
movb %r8b, (%rcx,%rdi)
incq %rdx
decl %r9d
cmpq %rdx, %rax
jne 0x1651cd
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xcbc34(%rip), %rdi     # 0x230e2f
leaq 0xcbc36(%rip), %rsi     # 0x230e38
leaq 0xcbc38(%rip), %rcx     # 0x230e41
movl $0x90d, %edx            # imm = 0x90D
callq 0x2110