// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/rcut.cpp
// Function: renew_mc_clus_t(mc_clus_t*, unsigned int*, unsigned int)
void renew_mc_clus_t(mc_clus_t *bc, uint32_t *a, uint32_t a_n)
{
	if(!bc) return;
	// fprintf(stderr, "[M::%s] a_n::%u\n", __func__, a_n);
	uint32_t k, i, *ba, bn, m, cocc, iin, /**bub_occ = 0,**/ bbn = 0; uint64_t *p; ma_utg_t *u = NULL;
	kvec_t(double) sc_l; kvec_t(double) sc_r; kvec_t(double) sc_m; kvec_t(uint32_t) tmp;
	kv_init(sc_l); kv_init(sc_r); kv_init(sc_m); kv_init(tmp);
	
	bc->cc.ng.n = bc->cc.nn.n = 0;
	memset(bc->lock, 0, sizeof((*(bc->lock)))*bc->n);///bc->n: number of all nodes
	for (k = 0; k < a_n; k++) bc->lock[a[k]] = 1;///a_n: number of nodes within the cluster

	kv_resize(uint32_t, bc->cc.nn, a_n);
	// for (i = 0; i < bc->bub->chain_weight.n; i++) {
    //     if(bc->bub->chain_weight.a[i].del) continue;
    //     u = &(bc->bub->b_ug->u.a[bc->bub->chain_weight.a[i].id]);///list of bubbles
	for (i = 0; i < bc->bub->b_ug->u.n; i++) {
        u = &(bc->bub->b_ug->u.a[i]);
        if(u->n == 0) continue;
		// bub_occ += u->n;
		// fprintf(stderr, "[M::%s] i::%u, u->n::%u\n", __func__, i, (uint32_t)u->n);
        for (k = cocc = 0, iin = bc->cc.nn.n; k < u->n; k++) {
            get_bubbles(bc->bub, u->a[k]>>33, NULL, NULL, &ba, &bn, NULL);
			kv_pushp(uint64_t, bc->cc.ng, &p); bbn += bn;
			*p = bc->cc.nn.n;///a bubble
			for (m = 0; m < bn; m++) {
				if(!(bc->lock[ba[m]>>1])) continue;///if the node of bubble is not at this cluster
				kv_push(uint32_t, bc->cc.nn, (ba[m]>>1));
				bc->lock[ba[m]>>1] = 2;
            }
			if(bc->cc.nn.n <= (*p)) {///no node in this bubble
				bc->cc.ng.n--;
				continue;
			}
			*p <<= 32; *p |= (bc->cc.nn.n-((*p)>>32)); cocc++;
        }
		//split the chain if there is multipe bubbles
		if(cocc > 0) {//cocc: # of bubbles in this chain
			for (k = bc->cc.ng.n - cocc; k < bc->cc.ng.n; k++) {
				kv_resize(double, sc_l, clus_n((*bc), k)); 
				kv_resize(double, sc_r, clus_n((*bc), k)); 
				kv_resize(double, sc_m, clus_n((*bc), k)); 
				kv_resize(uint32_t, tmp, clus_n((*bc), k)); 
				// prt_bub(clus_a((*bc), k), clus_n((*bc), k), "-0-"); 
				reorder_bub(bc->mg->e, clus_a((*bc), k), clus_n((*bc), k), bc->lock, 3, 2, 4, 5, 
				sc_l.a, sc_r.a, sc_m.a, tmp.a);
				// prt_bub(clus_a((*bc), k), clus_n((*bc), k), "-1-"); 
			}
			for (k = iin; k < bc->cc.nn.n; k++) bc->lock[bc->cc.nn.a[k]] = 1;//reset
			kv_pushp(uint64_t, bc->cc.ng, &p); *p = (uint64_t)-1; ///cluster
			kv_push(uint32_t, bc->cc.nn, ((uint32_t)-1)); ///split, node id
		}
    }

	for (k = 0; k < a_n; k++) bc->lock[a[k]] = 0;
	kv_destroy(sc_l); kv_destroy(sc_r); kv_destroy(sc_m); kv_destroy(tmp);
	// fprintf(stderr, "[M::%s] a_n::%u, bc->cc.nn.n::%u, bc->cc.ng.n::%u, bbn::%u, bub_occ::%u, bc->bub->b_ug->u.n::%u\n", __func__, 
	// a_n, (uint32_t)bc->cc.nn.n, (uint32_t)bc->cc.ng.n, bbn, bub_occ, (uint32_t)bc->bub->b_ug->u.n);
}

// Assembly:
testq %rdi, %rdi
je 0x16c1cd
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movl %edx, %eax
movq %rsi, %rbx
movq %rdi, %r14
xorl %r15d, %r15d
movq %r15, (%rdi)
movq %r15, 0x18(%rdi)
movq 0x48(%rdi), %rdi
movl 0x54(%r14), %edx
xorl %esi, %esi
movl %eax, %ebp
callq 0x20e0
movl %ebp, 0x1c(%rsp)
testl %ebp, %ebp
je 0x16bc2e
movl 0x1c(%rsp), %r15d
xorl %eax, %eax
movq 0x48(%r14), %rcx
movl (%rbx,%rax,4), %edx
movb $0x1, (%rcx,%rdx)
incq %rax
cmpq %rax, %r15
jne 0x16bc1b
cmpq %r15, 0x8(%r14)
jae 0x16bc86
leaq -0x1(%r15), %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x1(%rcx), %rax
movq %rax, 0x8(%r14)
movq 0x10(%r14), %rdi
leaq 0x4(,%rcx,4), %rsi
callq 0x2370
movq %rax, 0x10(%r14)
movq 0x30(%r14), %rax
movq 0xc0(%rax), %rax
xorl %edx, %edx
cmpq $0x0, (%rax)
je 0x16c165
movq $0x0, 0x68(%rsp)
xorl %r13d, %r13d
movq $0x0, 0x60(%rsp)
xorl %ebp, %ebp
movq $0x0, 0x58(%rsp)
movq $0x0, 0x10(%rsp)
movq $0x0, 0x50(%rsp)
movq $0x0, 0x8(%rsp)
movq %rbx, 0x30(%rsp)
movq %r15, 0x28(%rsp)
movq 0x10(%rax), %rax
leaq (%rdx,%rdx,4), %rcx
cmpl $0x0, 0x10(%rax,%rcx,8)
je 0x16c14d
movq %rdx, 0x38(%rsp)
movq %rbp, 0x40(%rsp)
movq %r13, 0x48(%rsp)
leaq (%rax,%rcx,8), %r12
movq (%r14), %rax
movq %rax, 0x78(%rsp)
movl %eax, %eax
movq %rax, 0x80(%rsp)
xorl %ebp, %ebp
movl $0x0, 0x20(%rsp)
movq 0x30(%r14), %rdi
movq 0x18(%r12), %rax
movq (%rax,%rbp,8), %rsi
shrq $0x21, %rsi
subq $0x8, %rsp
xorl %edx, %edx
xorl %ecx, %ecx
leaq 0x78(%rsp), %r8
leaq 0x2c(%rsp), %r9
pushq $0x0
callq 0x13df01
addq $0x10, %rsp
movq 0x18(%r14), %r13
movq 0x28(%r14), %rbx
cmpq 0x20(%r14), %r13
jne 0x16bd8f
leaq (,%r13,2), %rsi
testq %r13, %r13
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x20(%r14)
shlq $0x3, %rsi
movq %rbx, %rdi
callq 0x2370
movq %rax, %rbx
movq %rax, 0x28(%r14)
movq 0x18(%r14), %r13
leaq 0x1(%r13), %rax
movq %rax, 0x18(%r14)
movl 0x24(%rsp), %eax
movq (%r14), %rcx
movq %rcx, (%rbx,%r13,8)
testl %eax, %eax
je 0x16be50
xorl %r15d, %r15d
movq 0x48(%r14), %rcx
movq 0x70(%rsp), %rdx
movl (%rdx,%r15,4), %esi
shrl %esi
cmpb $0x0, (%rcx,%rsi)
je 0x16be1d
movq (%r14), %rdi
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rdi
jne 0x16be05
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq 0x70(%rsp), %rdx
movl (%rdx,%r15,4), %esi
movq (%r14), %rdi
movq 0x48(%r14), %rcx
shrl %esi
leaq 0x1(%rdi), %r8
movq %r8, (%r14)
movl %esi, (%rax,%rdi,4)
movl (%rdx,%r15,4), %eax
shrl %eax
movb $0x2, (%rcx,%rax)
movl 0x24(%rsp), %eax
incq %r15
movl %eax, %ecx
cmpq %rcx, %r15
jb 0x16bdad
movq (%rbx,%r13,8), %rax
cmpq %rax, (%r14)
jbe 0x16be50
movq %rax, %rcx
shlq $0x20, %rcx
movq %rcx, (%rbx,%r13,8)
movq (%r14), %rdx
movl %eax, %eax
subq %rax, %rdx
orq %rcx, %rdx
movq %rdx, (%rbx,%r13,8)
incl 0x20(%rsp)
jmp 0x16be54
decq 0x18(%r14)
incq %rbp
movl 0x10(%r12), %eax
cmpq %rax, %rbp
jb 0x16bd24
movl 0x20(%rsp), %eax
testl %eax, %eax
je 0x16c134
movq 0x18(%r14), %rcx
movl %ecx, %r12d
subl %eax, %r12d
cmpq %r12, %rcx
movq 0x48(%rsp), %r13
movq 0x40(%rsp), %rbp
jbe 0x16c074
movl %r12d, %ebx
incl %ebx
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %rdx
movl %edx, %eax
cmpq %rax, 0x50(%rsp)
jae 0x16bf01
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x8(,%rcx,8), %rsi
incq %rcx
movq %rcx, 0x50(%rsp)
movq 0x8(%rsp), %rdi
callq 0x2370
movq %rax, 0x8(%rsp)
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %rdx
movl %edx, %eax
cmpq %rax, 0x58(%rsp)
jae 0x16bf65
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x8(,%rcx,8), %rsi
incq %rcx
movq %rcx, 0x58(%rsp)
movq 0x10(%rsp), %rdi
callq 0x2370
movq %rax, 0x10(%rsp)
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %rdx
movl %edx, %eax
cmpq %rax, 0x60(%rsp)
jae 0x16bfc5
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x8(,%rcx,8), %rsi
incq %rcx
movq %rcx, 0x60(%rsp)
movq %rbp, %rdi
callq 0x2370
movq %rax, %rbp
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %rdx
movl %edx, %eax
cmpq %rax, 0x68(%rsp)
jae 0x16c023
decq %rax
movq %rax, %rcx
shrq %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x2, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x4, %rcx
orq %rax, %rcx
movq %rcx, %rax
shrq $0x8, %rax
orq %rcx, %rax
movq %rax, %rcx
shrq $0x10, %rcx
orq %rax, %rcx
leaq 0x4(,%rcx,4), %rsi
incq %rcx
movq %rcx, 0x68(%rsp)
movq %r13, %rdi
callq 0x2370
movq %rax, %r13
movq 0x28(%r14), %rax
movq (%rax,%r12,8), %rdx
movq 0x40(%r14), %rax
movq 0x48(%r14), %rcx
movq 0x28(%rax), %rdi
movq %rdx, %rsi
shrq $0x1e, %rsi
andq $-0x4, %rsi
addq 0x10(%r14), %rsi
movl $0x3, %r8d
movl $0x2, %r9d
pushq %r13
pushq %rbp
pushq 0x20(%rsp)
pushq 0x20(%rsp)
pushq $0x5
pushq $0x4
callq 0x16b7f9
addq $0x30, %rsp
movl %ebx, %r12d
movq 0x18(%r14), %rcx
incl %ebx
cmpq %r12, %rcx
ja 0x16be93
movq 0x80(%rsp), %rsi
cmpq %rsi, (%r14)
movq 0x30(%rsp), %rbx
movq 0x28(%rsp), %r15
jbe 0x16c0ae
movq 0x78(%rsp), %rdx
incl %edx
movq 0x10(%r14), %rax
movq 0x48(%r14), %rcx
movl (%rax,%rsi,4), %eax
movb $0x1, (%rcx,%rax)
movl %edx, %esi
incl %edx
cmpq %rsi, (%r14)
ja 0x16c092
movq 0x18(%r14), %rcx
movq 0x28(%r14), %rax
cmpq 0x20(%r14), %rcx
jne 0x16c0e0
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x20(%r14)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x28(%r14)
movq 0x18(%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x18(%r14)
movq $-0x1, (%rax,%rcx,8)
movq (%r14), %rcx
movq 0x10(%r14), %rax
cmpq 0x8(%r14), %rcx
jne 0x16c124
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r14)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, 0x10(%r14)
movq (%r14), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r14)
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
jmp 0x16c148
movq 0x30(%rsp), %rbx
movq 0x28(%rsp), %r15
movq 0x48(%rsp), %r13
movq 0x40(%rsp), %rbp
movq 0x38(%rsp), %rdx
incl %edx
movq 0x30(%r14), %rax
movq 0xc0(%rax), %rax
cmpq %rdx, (%rax)
ja 0x16bce2
jmp 0x16c17c
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
xorl %ebp, %ebp
xorl %r13d, %r13d
cmpl $0x0, 0x1c(%rsp)
je 0x16c198
xorl %eax, %eax
movq 0x48(%r14), %rcx
movl (%rbx,%rax,4), %edx
movb $0x0, (%rcx,%rdx)
incq %rax
cmpq %rax, %r15
jne 0x16c185
movq 0x8(%rsp), %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
callq 0x2220
movq %rbp, %rdi
callq 0x2220
movq %r13, %rdi
callq 0x2220
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq