// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %r9, %r15
movl %r8d, 0xc(%rsp)
movq %rcx, 0x10(%rsp)
movq %rsi, 0x48(%rsp)
movq %rdi, 0x40(%rsp)
movq 0xa0(%rsp), %rax
movl 0x98(%rsp), %ebx
movl 0x90(%rsp), %r14d
movq (%rax), %rdx
movq 0x10(%rax), %rdi
xorl %esi, %esi
callq 0x20e0
movl %ebx, %eax
subl %ebx, %r14d
movq %rax, 0x38(%rsp)
jbe 0x166684
leaq (%r15,%rax,4), %r9
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
xorpd %xmm0, %xmm0
movsd 0xbefb1(%rip), %xmm1    # 0x225538
xorl %ebx, %ebx
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movq 0xa0(%rsp), %rdx
movq %r15, %rsi
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %r8
movq %r15, 0x30(%rsp)
movq %r9, 0x28(%rsp)
movl (%r9,%rbx,4), %eax
cmpq %r10, %rax
je 0x16669d
cmpb %dil, (%r8,%rax)
jae 0x166676
movq 0x10(%rdx), %rbp
movl %eax, %r15d
shrl $0x3, %r15d
movl %eax, %ecx
andb $0x7, %cl
movl $0x1, %r12d
shll %cl, %r12d
movzbl (%rbp,%r15), %r13d
movl %eax, %ecx
andl $0x7, %ecx
btl %ecx, %r13d
jb 0x166676
movl %r11d, 0x24(%rsp)
movq 0x48(%rsp), %rdi
movq 0x40(%rsp), %rsi
movl %eax, %ecx
movsd %xmm1, 0x50(%rsp)
movsd %xmm0, 0x18(%rsp)
callq 0x16644f
movsd 0x50(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x18(%rsp)
movsd 0x18(%rsp), %xmm0
orb %r13b, %r12b
movb %r12b, (%rbp,%r15)
ucomisd %xmm1, %xmm0
jbe 0x166650
leal 0x1(%rbx), %r11d
movapd %xmm0, %xmm1
movq 0xa0(%rsp), %rdx
jmp 0x16665d
movq 0xa0(%rsp), %rdx
movl 0x24(%rsp), %r11d
movq 0x30(%rsp), %rsi
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %r8
movq 0x28(%rsp), %r9
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
incq %rbx
cmpl %ebx, %r14d
jne 0x1665ad
jmp 0x16669d
movsd 0xbeeac(%rip), %xmm1    # 0x225538
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movq 0xa0(%rsp), %rdx
movq %r15, %rsi
movl 0x98(%rsp), %r9d
movl 0x90(%rsp), %r10d
subl %r9d, %r10d
jbe 0x1666de
movq 0x38(%rsp), %rax
leaq (%rsi,%rax,4), %rax
xorl %ecx, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
movl (%rax,%rcx,4), %esi
cmpq %r8, %rsi
je 0x1666de
movq 0x10(%rdx), %rdi
shrl $0x3, %esi
movb $0x0, (%rdi,%rsi)
incq %rcx
cmpl %ecx, %r10d
jne 0x1666c3
movsd 0xbee1a(%rip), %xmm0    # 0x225500
ucomisd %xmm1, %xmm0
jae 0x166710
cmpl $-0x1, %r11d
je 0x166710
ucomisd 0x18(%rdx), %xmm1
ja 0x166703
jne 0x166710
jp 0x166710
cmpl %r9d, 0x20(%rdx)
jbe 0x166710
movsd %xmm1, 0x18(%rdx)
movl %r9d, 0x20(%rdx)
movl %r11d, 0x24(%rdx)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq