// File: /workspace/github_cpp_makefile_star2_O3/sebastienmeyer2[P]mst-clustering/src/kmeans.cpp
// Function: Cloud::set_centroid_centers()
void Cloud::set_centroid_centers()
{
    /*!
     * @brief M-step in the kmeans algorithm
     */

	int* clusterSizes = new int [this->k] ();

	// we start with "new" points
	Point *newCenters = new Point [this->k] ();

	// we count the number of data points in each cluster 
	// and accumulate their sum
	for (int i = 0; i < this->n; i ++){

		int pointLabel = points[i].label;
		clusterSizes[pointLabel] += 1;

		for (int j = 0; j < this->d; j ++){
			newCenters[pointLabel].coords[j] += points[i].coords[j];
		}
	}

	// we compute the average per cluster
	for (int i = 0; i < this->k; i ++){
		if (clusterSizes[i] > 0){
			for (int j = 0; j < this->d; j ++){
				newCenters[i].coords[j] /= clusterSizes[i];
			}
			
			this->set_center(newCenters[i], i);
		}
	}

	delete[] clusterSizes;
	clusterSizes = NULL;
	delete[] newCenters;
	newCenters = NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %r15
movslq 0x8(%rdi), %r13
leaq (,%r13,4), %rbx
movq %r13, %r12
shlq $0x4, %r12
testq %r13, %r13
movq $-0x1, %rax
cmovsq %rax, %rbx
leaq 0x8(%r12), %rbp
cmovsq %rax, %rbp
movq %rbx, %rdi
callq 0x3030
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x3130
movq %rbp, 0x10(%rsp)
movq %rbp, %rdi
callq 0x3030
leaq 0x8(%rax), %rbp
movq %rax, 0x8(%rsp)
movq %r13, (%rax)
testq %r13, %r13
je 0xeaf0
negq %r12
xorl %ebx, %ebx
movq %rbp, %r13
movq %r13, %rdi
callq 0x3a20
addq $-0x10, %rbx
addq $0x10, %r13
cmpq %rbx, %r12
jne 0xeadb
movslq 0x4(%r15), %rax
testq %rax, %rax
movq 0x8(%rsp), %r12
jle 0xeb4d
movq 0x10(%r15), %rcx
movl (%r15), %edx
xorl %esi, %esi
movq %rsi, %rdi
shlq $0x4, %rdi
movslq 0x8(%rcx,%rdi), %r8
incl (%r14,%r8,4)
testl %edx, %edx
jle 0xeb45
movq (%rcx,%rdi), %rdi
shlq $0x4, %r8
movq (%rbp,%r8), %r8
xorl %r9d, %r9d
movsd (%rdi,%r9,8), %xmm0
addsd (%r8,%r9,8), %xmm0
movsd %xmm0, (%r8,%r9,8)
incq %r9
cmpq %r9, %rdx
jne 0xeb2b
incq %rsi
cmpq %rax, %rsi
jne 0xeb07
movslq 0x8(%r15), %rax
testq %rax, %rax
jle 0xebce
movl (%r15), %ecx
movq 0x18(%r15), %rdx
xorl %esi, %esi
testl %ecx, %ecx
setle %r8b
movl (%r14,%rsi,4), %edi
testl %edi, %edi
setle %r9b
orb %r8b, %r9b
jne 0xebc6
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
movq %rsi, %rdi
shlq $0x4, %rdi
movq (%rbp,%rdi), %r8
xorl %r9d, %r9d
movsd (%r8,%r9,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%r8,%r9,8)
incq %r9
cmpq %r9, %rcx
jne 0xeb8a
testl %ecx, %ecx
jle 0xebc6
movq (%rbp,%rdi), %r8
movq (%rdx,%rdi), %rdi
xorl %r9d, %r9d
movsd (%r8,%r9,8), %xmm0
movsd %xmm0, (%rdi,%r9,8)
incq %r9
cmpq %r9, %rcx
jne 0xebb2
incq %rsi
cmpq %rax, %rsi
jne 0xeb5f
movq %r14, %rdi
callq 0x3360
movq (%r12), %rax
movq %rax, %rbx
shlq $0x4, %rbx
leaq 0x8(%rbx), %r14
testq %rax, %rax
je 0xec00
movq %r12, %r15
addq $-0x8, %r15
leaq (%r15,%rbx), %rdi
callq 0x3a6e
addq $-0x10, %rbx
jne 0xebf1
movq %r12, %rdi
movq %r14, %rsi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3500
movq %rax, %r14
testq %rbx, %rbx
je 0xec3c
negq %rbx
movq 0x8(%rsp), %r15
addq $-0x8, %r15
leaq (%r15,%rbx), %rdi
callq 0x3a6e
addq $-0x10, %rbx
jne 0xec2d
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x3500
movq %r14, %rdi
callq 0x34a0
nop