// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movl %r8d, 0x1c(%rsp)
movq %rcx, 0x40(%rsp)
movq %rsi, 0x30(%rsp)
movq %rdi, 0x28(%rsp)
movl 0x80(%rsp), %r15d
movq 0x90(%rsp), %rbx
movl 0x88(%rsp), %ebp
movq (%rbx), %rdx
movq 0x10(%rbx), %rdi
xorl %esi, %esi
callq 0x20e0
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %r12d
movsd 0xbafda(%rip), %xmm1    # 0x1e3580
xorpd %xmm0, %xmm0
pushq $-0x1
popq %rax
movl %eax, 0x8(%rsp)
pushq $0x1
popq %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl %r8d, %r10d
movq %r12, %r13
movq %r14, 0x20(%rsp)
cmpq %rdi, %r13
jae 0x1286e3
movl (%rsi,%r13,4), %eax
cmpq %r9, %rax
je 0x1286e3
movl 0x1c(%rsp), %ecx
movq 0x40(%rsp), %rdx
cmpb %cl, (%rdx,%rax)
jae 0x1286bc
movq 0x10(%rbx), %rbp
movl %eax, %r15d
shrl $0x3, %r15d
movl %eax, %ecx
andb $0x7, %cl
movl %r8d, %r14d
shll %cl, %r14d
movq %rbx, %rdx
movzbl (%rbp,%r15), %ebx
movl %eax, %ecx
andl $0x7, %ecx
btl %ecx, %ebx
jae 0x12861f
movq %rdx, %rbx
jmp 0x1286bc
movl %r10d, 0xc(%rsp)
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rsi
movl %eax, %ecx
movsd %xmm1, 0x38(%rsp)
movsd %xmm0, 0x10(%rsp)
callq 0x128489
movsd 0x38(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
orb %bl, %r14b
movb %r14b, (%rbp,%r15)
ucomisd %xmm1, %xmm0
jbe 0x128699
movl 0xc(%rsp), %r10d
movl %r10d, 0x8(%rsp)
movapd %xmm0, %xmm1
movq 0x90(%rsp), %rbx
movq 0x20(%rsp), %rsi
movl 0x80(%rsp), %edi
pushq $0x1
popq %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
jmp 0x1286bc
movq 0x90(%rsp), %rbx
movq 0x20(%rsp), %rsi
movl 0x80(%rsp), %edi
pushq $0x1
popq %r8
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movl 0xc(%rsp), %r10d
incq %r13
incl %r10d
jmp 0x1285c6
movl (%rsi,%r12,4), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %rax
je 0x1286e8
movq 0x10(%rbx), %rcx
shrl $0x3, %eax
movb $0x0, (%rcx,%rax)
incq %r12
cmpq %rdi, %r12
jb 0x1286c7
movsd 0xbae60(%rip), %xmm0    # 0x1e3550
ucomisd %xmm1, %xmm0
setae %al
movl 0x8(%rsp), %edx
cmpl $-0x1, %edx
sete %cl
orb %al, %cl
jne 0x12872e
ucomisd 0x18(%rbx), %xmm1
ja 0x12871c
jne 0x12872e
jp 0x12872e
movl 0x88(%rsp), %eax
cmpl %eax, 0x20(%rbx)
jbe 0x12872e
movsd %xmm1, 0x18(%rbx)
movl 0x88(%rsp), %eax
movl %eax, 0x20(%rbx)
movl %edx, 0x24(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq