// File: chhylp123[P]hifiasm/rcut.cpp
// Function: cal_clus_sc0(mc_match_t*, mc_svaux_t*, mc_clus_t*, unsigned char*, unsigned char, unsigned int*, unsigned int, unsigned int, clus_flip_aux*, unsigned int)
void cal_clus_sc0(mc_match_t *ma, mc_svaux_t *b_aux, mc_clus_t* bc, uint8_t *lock, uint8_t lock_max,
uint32_t *a, uint32_t a_n, uint32_t id, clus_flip_aux *r, uint32_t tid)
{
	uint32_t i, max_occ = (uint32_t)-1;//, len, mm0 = (uint32_t)-1, mm1 = 0; 
	bits_p *vis = &(r->vis); t_w_t w = 0, max_w = -1;

	memset(vis->a, 0, sizeof(*(vis->a))*vis->n);
	// if(bc->dbg) {
	// 	if(a[id] == 487 || a[id] == 47) {
	// 		fprintf(stderr, "[M::%s::] id::%u, a[id]::%u, s::%d\n", __func__, id, a[id], b_aux->s[a[id]]);
	// 	}
	// }
	for (i = id; i < a_n && a[i] != (uint32_t)-1; i++) {
		if(lock[a[i]] >= lock_max) continue;
		if(is_set_bits_p((*vis), a[i])) continue;
		w += clus_weight(b_aux, ma, vis, a[i]);
		// if(bc->dbg) {
		// 	if(a[id] == 487 || a[id] == 47) {
		// 		fprintf(stderr, "[M::%s::id->%u] a[%u]::%u, s::%d, lock::%u, lock_max::%u, is_set::%u, w::%f\n", 
		// 		__func__, id, i, a[i], b_aux->s[a[i]], lock[a[i]], lock_max, is_set_bits_p((*vis), a[i]), w);
		// 	}
		// }
		set_bits_p((*vis), a[i]); 
		// mm1 = a[i]; if(a[i] < mm0) mm0 = a[i];
		///update max_w
        if(max_w < w) {
            max_w = w; max_occ = i + 1 - id;
        }
	}
	// if(mm0 != (uint32_t)-1 && mm1 != (uint32_t)-1) {
	// 	len = MIN(((mm1>>3)+1), vis->n) - (mm0>>3);
	// 	memset(vis->a+(mm0>>3), 0, sizeof(*(vis->a))*len);
	// }
	for (i = id; i < a_n && a[i] != (uint32_t)-1; i++) vis->a[a[i]>>3] = 0;///reset
	if(max_w <= 0.000001 || max_occ == (uint32_t)-1) return;
	if((max_w > r->w) || (max_w == r->w && id < r->off)) {
        r->w = max_w; r->off = id; r->occ = max_occ;
    }
	// if(bc->dbg) {
	// 	if(a[id] == 487 || a[id] == 47) {
	// 		fprintf(stderr, "[M::%s::] id::%u, a[id]::%u, w::%f, off::%u, occ::%u\n", __func__, id, a[id], r->w, r->off, r->occ);
	// 	}
	// }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movb %r8b, %al
movl 0x28(%rbp), %r8d
movq 0x20(%rbp), %r8
movl 0x18(%rbp), %r8d
movl 0x10(%rbp), %r8d
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movb %al, -0x21(%rbp)
movq %r9, -0x30(%rbp)
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movq 0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
movsd 0x160437(%rip), %xmm0   # 0x382278
movsd %xmm0, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rdi
movq -0x40(%rbp), %rax
movq (%rax), %rdx
shlq $0x0, %rdx
xorl %esi, %esi
callq 0x2100
movl 0x18(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %ecx
xorl %eax, %eax
cmpl 0x10(%rbp), %ecx
movb %al, -0x51(%rbp)
jae 0x221e84
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
cmpl $-0x1, (%rax,%rcx,4)
setne %al
movb %al, -0x51(%rbp)
movb -0x51(%rbp), %al
testb $0x1, %al
jne 0x221e90
jmp 0x221f75
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
movl -0x34(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movzbl (%rax,%rcx), %eax
movzbl -0x21(%rbp), %ecx
cmpl %ecx, %eax
jl 0x221eaf
jmp 0x221f67
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movl -0x34(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl $0x3, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
movq -0x30(%rbp), %rcx
movl -0x34(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
andl $0x7, %ecx
sarl %cl, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x221ee6
jmp 0x221f67
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %ecx
callq 0x221c50
addsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x48(%rbp)
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
movl (%rax,%rcx,4), %ecx
andl $0x7, %ecx
movl $0x1, %esi
shll %cl, %esi
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movl -0x34(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl $0x3, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %edx
orl %esi, %edx
movb %dl, (%rax,%rcx)
movsd -0x50(%rbp), %xmm1
movsd -0x48(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x221f65
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
subl 0x18(%rbp), %eax
movl %eax, -0x38(%rbp)
jmp 0x221f67
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x221e66
movl 0x18(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %ecx
xorl %eax, %eax
cmpl 0x10(%rbp), %ecx
movb %al, -0x52(%rbp)
jae 0x221f99
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
cmpl $-0x1, (%rax,%rcx,4)
setne %al
movb %al, -0x52(%rbp)
movb -0x52(%rbp), %al
testb $0x1, %al
jne 0x221fa2
jmp 0x221fc8
movq -0x40(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x30(%rbp), %rcx
movl -0x34(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl $0x3, %ecx
movl %ecx, %ecx
movb $0x0, (%rax,%rcx)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x221f7b
movsd 0x1602b0(%rip), %xmm0   # 0x382280
ucomisd -0x50(%rbp), %xmm0
jae 0x221fdd
cmpl $-0x1, -0x38(%rbp)
jne 0x221fdf
jmp 0x22202f
movsd -0x50(%rbp), %xmm0
movq 0x20(%rbp), %rax
ucomisd 0x18(%rax), %xmm0
ja 0x22200d
movsd -0x50(%rbp), %xmm0
movq 0x20(%rbp), %rax
ucomisd 0x18(%rax), %xmm0
jne 0x22202f
jp 0x22202f
movl 0x18(%rbp), %eax
movq 0x20(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jae 0x22202f
movsd -0x50(%rbp), %xmm0
movq 0x20(%rbp), %rax
movsd %xmm0, 0x18(%rax)
movl 0x18(%rbp), %ecx
movq 0x20(%rbp), %rax
movl %ecx, 0x20(%rax)
movl -0x38(%rbp), %ecx
movq 0x20(%rbp), %rax
movl %ecx, 0x24(%rax)
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)