// File: /workspace/github_cpp_makefile_star2_O2/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, %r14
movslq 0x8(%rdi), %rbx
leaq (,%rbx,4), %r13
movq %rbx, %rcx
shlq $0x4, %rcx
testq %rbx, %rbx
pushq $-0x1
popq %rax
cmovsq %rax, %r13
movq %rcx, 0x8(%rsp)
leaq 0x8(%rcx), %rbp
cmovsq %rax, %rbp
movq %r13, %rdi
callq 0x3030
movq %rax, %r15
xorl %r12d, %r12d
movq %rax, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x3170
movq %rbp, 0x10(%rsp)
movq %rbp, %rdi
callq 0x3030
leaq 0x8(%rax), %rbp
movq %rax, (%rsp)
movq %rbx, (%rax)
testq %rbx, %rbx
je 0xcbd6
negq 0x8(%rsp)
xorl %ebx, %ebx
movq %rbp, %r13
movq %r13, %rdi
callq 0x3860
addq $-0x10, %rbx
addq $0x10, %r13
cmpq %rbx, 0x8(%rsp)
jne 0xcbbf
movl (%r14), %eax
movl 0x4(%r14), %ecx
testl %eax, %eax
cmovlel %r12d, %eax
movq 0x10(%r14), %rdx
testl %ecx, %ecx
cmovlel %r12d, %ecx
cmpq %rcx, %r12
je 0xcc36
movq %r12, %rdi
shlq $0x4, %rdi
movslq 0x8(%rdx,%rdi), %rsi
incl (%r15,%rsi,4)
addq %rdx, %rdi
shlq $0x4, %rsi
addq %rbp, %rsi
xorl %r8d, %r8d
cmpq %r8, %rax
je 0xcc31
movq (%rdi), %r9
movsd (%r9,%r8,8), %xmm0
movq (%rsi), %r9
addsd (%r9,%r8,8), %xmm0
movsd %xmm0, (%r9,%r8,8)
incq %r8
jmp 0xcc0f
incq %r12
jmp 0xcbed
xorl %ebx, %ebx
xorl %r12d, %r12d
movslq 0x8(%r14), %rax
cmpq %rax, %r12
jge 0xcc8f
movl (%r15,%r12,4), %eax
testl %eax, %eax
jle 0xcc8a
cvtsi2sd %eax, %xmm0
movl (%r14), %eax
movq %r12, %rsi
shlq $0x4, %rsi
addq %rbp, %rsi
testl %eax, %eax
cmovlel %ebx, %eax
xorl %ecx, %ecx
cmpq %rcx, %rax
je 0xcc7f
movq (%rsi), %rdx
movsd (%rdx,%rcx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rdx,%rcx,8)
incq %rcx
jmp 0xcc64
movq %r14, %rdi
movl %r12d, %edx
callq 0xc9c4
incq %r12
jmp 0xcc3b
movq %r15, %rdi
callq 0x3370
movq (%rsp), %r12
movq (%r12), %rax
movq %rax, %rbx
shlq $0x4, %rbx
leaq 0x8(%rbx), %r14
testq %rax, %rax
je 0xccc5
movq %r12, %r15
addq $-0x8, %r15
leaq (%r15,%rbx), %rdi
callq 0x38a8
addq $-0x10, %rbx
jne 0xccb6
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 0xcd00
negq %rbx
movq (%rsp), %r15
addq $-0x8, %r15
leaq (%r15,%rbx), %rdi
callq 0x38a8
addq $-0x10, %rbx
jne 0xccf1
movq (%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x3500
movq %r14, %rdi
callq 0x34b0