// File: /workspace/github_cpp_makefile_star2_O1/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 0xe770
negq %r12
xorl %ebx, %ebx
movq %rbp, %r13
movq %r13, %rdi
callq 0x3a20
addq $-0x10, %rbx
addq $0x10, %r13
cmpq %rbx, %r12
jne 0xe75b
movslq 0x4(%r15), %rax
testq %rax, %rax
movq 0x8(%rsp), %r12
jle 0xe7cd
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 0xe7c5
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 0xe7ab
incq %rsi
cmpq %rax, %rsi
jne 0xe787
movslq 0x8(%r15), %rax
testq %rax, %rax
jle 0xe84c
movl (%r15), %ecx
movq 0x18(%r15), %rdx
xorl %esi, %esi
movl (%r14,%rsi,4), %edi
testl %edi, %edi
jle 0xe844
testl %ecx, %ecx
jle 0xe819
xorps %xmm0, %xmm0
cvtsi2sd %edi, %xmm0
movq %rsi, %rdi
shlq $0x4, %rdi
movq (%rbp,%rdi), %rdi
xorl %r8d, %r8d
movsd (%rdi,%r8,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rdi,%r8,8)
incq %r8
cmpq %r8, %rcx
jne 0xe801
testl %ecx, %ecx
jle 0xe844
movq %rsi, %r8
shlq $0x4, %r8
movq (%rbp,%r8), %rdi
movq (%rdx,%r8), %r8
xorl %r9d, %r9d
movsd (%rdi,%r9,8), %xmm0
movsd %xmm0, (%r8,%r9,8)
incq %r9
cmpq %r9, %rcx
jne 0xe830
incq %rsi
cmpq %rax, %rsi
jne 0xe7df
movq %r14, %rdi
callq 0x3360
movq (%r12), %rax
movq %rax, %rbx
shlq $0x4, %rbx
leaq 0x8(%rbx), %r14
testq %rax, %rax
je 0xe87e
movq %r12, %r15
addq $-0x8, %r15
leaq (%r15,%rbx), %rdi
callq 0x3a6e
addq $-0x10, %rbx
jne 0xe86f
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 0xe8ba
negq %rbx
movq 0x8(%rsp), %r15
addq $-0x8, %r15
leaq (%r15,%rbx), %rdi
callq 0x3a6e
addq $-0x10, %rbx
jne 0xe8ab
movq 0x8(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x3500
movq %r14, %rdi
callq 0x34a0
nop