// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movslq 0x8(%rax), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, -0x70(%rbp)
callq 0xa030
movq -0x70(%rbp), %rdx
movq %rax, %rdi
movq %rdi, -0x68(%rbp)
xorl %esi, %esi
callq 0xa3a0
movq -0x68(%rbp), %rcx
movq -0x60(%rbp), %rax
movq %rcx, -0x10(%rbp)
movslq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movl $0x10, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
addq $0x8, %rdi
setb %al
orb %al, %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, -0x58(%rbp)
callq 0xa030
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, -0x48(%rbp)
movq %rax, (%rcx)
addq $0x8, %rcx
movq %rcx, -0x40(%rbp)
cmpq $0x0, %rax
je 0x32d94
movq -0x40(%rbp), %rax
movq -0x50(%rbp), %rdx
shlq $0x4, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0x80(%rbp)
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rdi
movq %rdi, -0x88(%rbp)
callq 0xb520
jmp 0x32d7c
movq -0x80(%rbp), %rcx
movq -0x88(%rbp), %rax
addq $0x10, %rax
cmpq %rcx, %rax
movq %rax, -0x78(%rbp)
jne 0x32d6a
movq -0x40(%rbp), %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x28(%rbp)
movq -0x60(%rbp), %rcx
movl -0x28(%rbp), %eax
cmpl 0x4(%rcx), %eax
jge 0x32eae
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl $0x0, -0x30(%rbp)
movq -0x60(%rbp), %rcx
movl -0x30(%rbp), %eax
cmpl (%rcx), %eax
jge 0x32e9e
movq -0x60(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x28(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq -0x30(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x18(%rbp), %rax
movslq -0x2c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq -0x30(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x32de4
movq -0x40(%rbp), %rcx
movq %rax, %rsi
movq -0x88(%rbp), %rax
movq %rsi, -0x20(%rbp)
movl %edx, -0x24(%rbp)
cmpq %rax, %rcx
movq %rax, -0x90(%rbp)
je 0x32e8c
movq -0x90(%rbp), %rdi
addq $-0x10, %rdi
movq %rdi, -0x98(%rbp)
callq 0xb590
movq -0x40(%rbp), %rcx
movq -0x98(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0x90(%rbp)
jne 0x32e5e
movq -0x58(%rbp), %rsi
movq -0x48(%rbp), %rdi
callq 0xaf60
jmp 0x33019
jmp 0x32ea0
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x32da3
movl $0x0, -0x34(%rbp)
movq -0x60(%rbp), %rcx
movl -0x34(%rbp), %eax
cmpl 0x8(%rcx), %eax
jge 0x32f4c
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0x32f3c
movl $0x0, -0x38(%rbp)
movq -0x60(%rbp), %rcx
movl -0x38(%rbp), %eax
cmpl (%rcx), %eax
jge 0x32f21
movq -0x10(%rbp), %rax
movslq -0x34(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm1
movq -0x18(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movslq -0x38(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x32eda
movq -0x60(%rbp), %rdi
movq -0x18(%rbp), %rsi
movslq -0x34(%rbp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movl -0x34(%rbp), %edx
callq 0x32a30
jmp 0x32f3e
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x32eb5
movq -0x10(%rbp), %rax
movq %rax, -0xa0(%rbp)
cmpq $0x0, %rax
je 0x32f69
movq -0xa0(%rbp), %rdi
callq 0xaa00
movq $0x0, -0x10(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $0x0, %rax
je 0x33008
movq -0xa8(%rbp), %rcx
movq %rcx, %rax
addq $-0x8, %rax
movq %rax, -0xc0(%rbp)
movq -0x8(%rcx), %rdx
movq %rdx, -0xb8(%rbp)
shlq $0x4, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, -0xb0(%rbp)
je 0x32fed
movq -0xb0(%rbp), %rdi
addq $-0x10, %rdi
movq %rdi, -0xc8(%rbp)
callq 0xb590
movq -0xa8(%rbp), %rcx
movq -0xc8(%rbp), %rax
cmpq %rcx, %rax
movq %rax, -0xb0(%rbp)
jne 0x32fbc
movq -0xc0(%rbp), %rdi
movq -0xb8(%rbp), %rsi
shlq $0x4, %rsi
addq $0x8, %rsi
callq 0xaf60
movq $0x0, -0x18(%rbp)
addq $0xd0, %rsp
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0xade0
nopw %cs:(%rax,%rax)