// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %r15
movl %r8d, (%rsp)
movq %rcx, 0x8(%rsp)
movq %rsi, 0x38(%rsp)
movq %rdi, 0x30(%rsp)
movq 0x90(%rsp), %rbx
movl 0x88(%rsp), %ebp
movl 0x80(%rsp), %r14d
movq (%rbx), %rdx
movq 0x10(%rbx), %rdi
xorl %esi, %esi
callq 0x20e0
subl %ebp, %r14d
jbe 0x16d0bf
movq %rbx, %rdx
movl %ebp, %eax
movq %rax, 0x28(%rsp)
leaq (%r15,%rax,4), %r9
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
xorpd %xmm0, %xmm0
movsd 0xc46c6(%rip), %xmm1    # 0x231638
xorl %ebx, %ebx
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movq %r15, %rsi
movl (%rsp), %edi
movq 0x8(%rsp), %r8
movq %r15, 0x20(%rsp)
movq %r9, 0x18(%rsp)
movl (%r9,%rbx,4), %eax
cmpq %r10, %rax
je 0x16d04a
cmpb %dil, (%r8,%rax)
jae 0x16d03e
movq 0x10(%rdx), %r12
movl %eax, %ebp
shrl $0x3, %ebp
movl %eax, %ecx
andb $0x7, %cl
movl $0x1, %r15d
shll %cl, %r15d
movzbl (%r12,%rbp), %r13d
movl %eax, %ecx
andl $0x7, %ecx
btl %ecx, %r13d
jb 0x16d03e
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl %eax, %ecx
movsd %xmm1, 0x40(%rsp)
movsd %xmm0, 0x10(%rsp)
callq 0x16ce36
movsd 0x40(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
orb %r13b, %r15b
movb %r15b, (%r12,%rbp)
ucomisd %xmm1, %xmm0
jbe 0x16d01e
leal 0x1(%rbx), %eax
movl %eax, 0x4(%rsp)
movapd %xmm0, %xmm1
movq 0x90(%rsp), %rdx
movq 0x20(%rsp), %rsi
movl (%rsp), %edi
movq 0x8(%rsp), %r8
movq 0x18(%rsp), %r9
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
incq %rbx
cmpl %ebx, %r14d
jne 0x16cf8f
movl 0x88(%rsp), %r9d
movl 0x80(%rsp), %r10d
subl %r9d, %r10d
jbe 0x16d08b
movq 0x28(%rsp), %rax
leaq (%rsi,%rax,4), %rax
xorl %ecx, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl (%rax,%rcx,4), %esi
cmpq %r8, %rsi
je 0x16d08b
movq 0x10(%rdx), %rdi
shrl $0x3, %esi
movb $0x0, (%rdi,%rsi)
incq %rcx
cmpl %ecx, %r10d
jne 0x16d070
movsd 0xc456d(%rip), %xmm0    # 0x231600
ucomisd %xmm1, %xmm0
movl 0x4(%rsp), %eax
jae 0x16d0bf
cmpl $-0x1, %eax
je 0x16d0bf
ucomisd 0x18(%rdx), %xmm1
ja 0x16d0b3
jne 0x16d0bf
jp 0x16d0bf
cmpl %r9d, 0x20(%rdx)
jbe 0x16d0bf
movsd %xmm1, 0x18(%rdx)
movl %r9d, 0x20(%rdx)
movl %eax, 0x24(%rdx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq