// File: /workspace/github_cpp_makefile_star2_O3/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 %rdi, 0x20(%rsp)
movq 0x88(%rsp), %rax
movq 0x80(%rsp), %rdi
testl %edx, %edx
movl %edx, 0x4(%rsp)
je 0x16b9d5
movq $-0x1, 0x8(%rsp)
movq 0x90(%rsp), %rbx
movb 0x70(%rsp), %bpl
movl %edx, %r14d
xorl %r11d, %r11d
movl (%rsi,%r11,4), %r10d
movb %bpl, (%rcx,%r10)
incq %r11
cmpq %r11, %r14
jne 0x16b844
movq $-0x1, 0x10(%rsp)
movq $-0x1, 0x18(%rsp)
testl %edx, %edx
je 0x16b9f0
movq 0x20(%rsp), %rdx
movq 0x10(%rdx), %rdx
movq %rdx, 0x30(%rsp)
xorpd %xmm0, %xmm0
movq $-0x1, 0x18(%rsp)
movapd 0xd13ff(%rip), %xmm1    # 0x23cc90
xorl %r12d, %r12d
movq $-0x1, 0x10(%rsp)
movq $-0x1, 0x8(%rsp)
xorpd %xmm3, %xmm3
xorpd %xmm2, %xmm2
xorpd %xmm4, %xmm4
movq %r14, 0x28(%rsp)
xorl %edx, %edx
movq %rdx, (%rbx,%r12,8)
movq %rdx, (%rax,%r12,8)
movq %rdx, (%rdi,%r12,8)
movl (%rsi,%r12,4), %edx
movq 0x30(%rsp), %r10
movq (%r10,%rdx,8), %rdx
testl %edx, %edx
je 0x16b97d
movq %rdx, %r15
shrq $0x20, %r15
movq 0x20(%rsp), %r10
movq 0x28(%r10), %r13
movl %edx, %r11d
movl %r15d, %r10d
shlq $0x4, %r10
leaq (%r10,%r13), %rdx
movl (%r13,%r10), %r14d
movb (%rcx,%r14), %r10b
cmpb %r8b, %r10b
jne 0x16b91f
movsd 0x8(%rdx), %xmm5
andpd %xmm1, %xmm5
addsd (%rdi,%r12,8), %xmm5
movsd %xmm5, (%rdi,%r12,8)
movb (%rcx,%r14), %r10b
cmpb %r9b, %r10b
jne 0x16b93d
movsd 0x8(%rdx), %xmm5
andpd %xmm1, %xmm5
addsd (%rax,%r12,8), %xmm5
movsd %xmm5, (%rax,%r12,8)
movb (%rcx,%r14), %r10b
cmpb %bpl, %r10b
jne 0x16b957
movsd 0x8(%rdx), %xmm5
andpd %xmm1, %xmm5
addsd (%rbx,%r12,8), %xmm5
movsd %xmm5, (%rbx,%r12,8)
incq %r15
decq %r11
jne 0x16b8ed
movsd (%rdi,%r12,8), %xmm5
ucomisd %xmm0, %xmm5
movq 0x28(%rsp), %r14
jbe 0x16b97d
ucomisd %xmm4, %xmm5
jbe 0x16b9bf
movq %r12, 0x8(%rsp)
jmp 0x16b9c3
movsd (%rax,%r12,8), %xmm6
ucomisd %xmm0, %xmm6
jbe 0x16b99e
ucomisd %xmm2, %xmm6
jbe 0x16b9bf
movapd %xmm4, %xmm5
movapd %xmm6, %xmm2
movq %r12, 0x10(%rsp)
jmp 0x16b9c3
movsd (%rbx,%r12,8), %xmm6
ucomisd %xmm0, %xmm6
jbe 0x16b9bf
ucomisd %xmm3, %xmm6
jbe 0x16b9bf
movapd %xmm4, %xmm5
movapd %xmm6, %xmm3
movq %r12, 0x18(%rsp)
jmp 0x16b9c3
movapd %xmm4, %xmm5
incq %r12
movapd %xmm5, %xmm4
cmpq %r14, %r12
jne 0x16b8b7
jmp 0x16b9f0
movq $-0x1, 0x8(%rsp)
movq $-0x1, 0x10(%rsp)
movq $-0x1, 0x18(%rsp)
movq 0x8(%rsp), %r10
movq %r10, %rdx
movq 0x10(%rsp), %r15
andq %r15, %rdx
movq 0x18(%rsp), %r14
andq %r14, %rdx
cmpq $-0x1, %rdx
movl 0x4(%rsp), %r11d
je 0x16bba1
movq 0x98(%rsp), %r9
movb 0x78(%rsp), %bl
cmpq $-0x1, %r15
cmovneq %r15, %r14
cmoveq %rdi, %rax
sete 0x30(%rsp)
cmpq $-0x1, %r10
cmovneq %r10, %r14
setne 0x28(%rsp)
cmovneq %rdi, %rax
movl (%rsi,%r14,4), %edx
movl %edx, (%r9)
movb %bl, (%rcx,%rdx)
movl $0x1, %r10d
cmpl $0x2, %r11d
jb 0x16bb46
movl %r11d, %r14d
movl $0x1, %r10d
xorpd %xmm0, %xmm0
movapd 0xd121c(%rip), %xmm1    # 0x23cc90
movq $-0x1, %r15
xorl %r12d, %r12d
xorpd %xmm2, %xmm2
movl (%rsi,%r12,4), %edx
cmpb %bl, (%rcx,%rdx)
je 0x16baf8
movq 0x20(%rsp), %rdi
movq 0x10(%rdi), %rdi
movq (%rdi,%rdx,8), %rdi
testl %edi, %edi
je 0x16badf
movq %rdi, %rdx
shrq $0x20, %rdx
movq 0x20(%rsp), %r11
movq 0x28(%r11), %r13
movl %edi, %r11d
movl %edx, %edi
shlq $0x4, %rdi
movl (%r13,%rdi), %ebp
cmpb %bl, (%rcx,%rbp)
jne 0x16bad7
addq %r13, %rdi
movsd 0x8(%rdi), %xmm3
andpd %xmm1, %xmm3
addsd (%rax,%r12,8), %xmm3
movsd %xmm3, (%rax,%r12,8)
incq %rdx
decq %r11
jne 0x16baaf
movsd (%rax,%r12,8), %xmm3
ucomisd %xmm0, %xmm3
jbe 0x16baf8
ucomisd %xmm2, %xmm3
jbe 0x16baf8
movapd %xmm3, %xmm2
movq %r12, %r15
incq %r12
cmpq %r14, %r12
jne 0x16ba82
cmpq $-0x1, %r15
je 0x16bb1f
movl (%rsi,%r15,4), %edx
movl %edx, (%r9,%r10,4)
incq %r10
movb %bl, (%rcx,%rdx)
cmpq %r14, %r10
jne 0x16ba74
jmp 0x16bb4b
xorl %eax, %eax
movl 0x4(%rsp), %r11d
movl (%rsi,%rax,4), %edx
cmpb %bl, (%rcx,%rdx)
je 0x16bb3e
movl %r10d, %edi
incl %r10d
movl %edx, (%r9,%rdi,4)
movl (%rsi,%rax,4), %edx
movb %bl, (%rcx,%rdx)
incq %rax
cmpq %rax, %r14
jne 0x16bb26
cmpl %r11d, %r10d
jne 0x16bbb0
movb 0x28(%rsp), %al
orb 0x30(%rsp), %al
je 0x16bb76
movl 0x4(%rsp), %eax
testl %eax, %eax
je 0x16bba1
movl %eax, %eax
xorl %edx, %edx
movl (%r9,%rdx,4), %edi
movl %edi, (%rsi,%rdx,4)
movb %r8b, (%rcx,%rdi)
incq %rdx
cmpq %rdx, %rax
jne 0x16bb61
jmp 0x16bba1
movl 0x4(%rsp), %r10d
testl %r10d, %r10d
je 0x16bba1
movl %r10d, %eax
decl %r10d
xorl %edx, %edx
movl %r10d, %edi
movl (%r9,%rdi,4), %edi
movl %edi, (%rsi,%rdx,4)
movb %r8b, (%rcx,%rdi)
incq %rdx
decl %r10d
cmpq %rdx, %rax
jne 0x16bb88
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xd14c8(%rip), %rdi     # 0x23d07f
leaq 0xd14ca(%rip), %rsi     # 0x23d088
leaq 0xd14cc(%rip), %rcx     # 0x23d091
movl $0x90d, %edx            # imm = 0x90D
callq 0x2110