// File: ivanzamanov[P]kmeans/src/kmeans/kmeans.cpp
// Function: clusterize(int, list<article_entry*>&)
void clusterize(int k, entryList &l) {
	printf("Clusterizing in %d clusters\n", k);
	cluster *clusters = new cluster[k];
	selectSeeds(clusters, l, k);
	double RSS = printRSS(clusters, k);
	double prevRSS = 1e+20;

	int iterations = 0;
	while(iterations < MAX_ITERATIONS && (prevRSS - RSS) > THRESHOLD) {
		iterations++;
		printf("Iteration %d\n", iterations);
		// Clear cluster members.
		for(int i=0; i<k; i++)
			clusters[i].members.clear();

		// Find the best cluster for each entry
		// and add as a member
		for(int i=0; i<l.size(); i++) {
			article_entry* entry = l.get(i);
			cluster& cl = findBestCluster(clusters, k, entry->v);
			cl.members.add(entry);
		}

		// Recompute centroids
		printf("Computing centroids ");
		for(int i=0; i<k; i++) {
			printf("%d ", i);
			recomputeCentroid(clusters[i]);
		}
		printf("\n");

		prevRSS = RSS;
		RSS = printRSS(clusters, k);
	}

	delete[] clusters;
}

// Assembly:
subq $0xd8, %rsp
movl %edi, 0xd4(%rsp)
movq %rsi, 0xc8(%rsp)
movl 0xd4(%rsp), %esi
leaq 0x2fc8(%rip), %rdi      # 0x506c
movb $0x0, %al
callq 0x1040
movslq 0xd4(%rsp), %rax
movq %rax, 0x60(%rsp)
movl $0x28, %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
callq 0x1030
movq %rax, %rcx
movq 0x60(%rsp), %rax
movq %rcx, 0x68(%rsp)
movq %rax, (%rcx)
addq $0x8, %rcx
movq %rcx, 0x70(%rsp)
cmpq $0x0, %rax
je 0x2148
movq 0x70(%rsp), %rax
movq 0x60(%rsp), %rcx
imulq $0x28, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0x50(%rsp)
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x2ab0
jmp 0x2130
movq 0x50(%rsp), %rcx
movq 0x48(%rsp), %rax
addq $0x28, %rax
cmpq %rcx, %rax
movq %rax, 0x58(%rsp)
jne 0x211f
movq 0x70(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
movl 0xd4(%rsp), %edx
callq 0x1d60
movq 0xc0(%rsp), %rdi
movl 0xd4(%rsp), %esi
callq 0x1fa0
movsd %xmm0, 0xa8(%rsp)
movsd 0x2e7a(%rip), %xmm0     # 0x5010
movsd %xmm0, 0xa0(%rsp)
movl $0x0, 0x9c(%rsp)
xorl %eax, %eax
cmpl $0xa, 0x9c(%rsp)
movb %al, 0x47(%rsp)
jge 0x21df
movsd 0xa0(%rsp), %xmm0
subsd 0xa8(%rsp), %xmm0
movsd 0x2e44(%rip), %xmm1     # 0x5018
ucomisd %xmm1, %xmm0
seta %al
movb %al, 0x47(%rsp)
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0x21ec
jmp 0x23fd
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
movl 0x9c(%rsp), %esi
leaq 0x2e7e(%rip), %rdi      # 0x5089
movb $0x0, %al
callq 0x1040
movl $0x0, 0x98(%rsp)
movl 0x98(%rsp), %eax
cmpl 0xd4(%rsp), %eax
jge 0x22c0
movq 0xc0(%rsp), %rdi
movslq 0x98(%rsp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rdi
addq $0x18, %rdi
callq 0x2b00
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0x221d
movq 0x70(%rsp), %rcx
movq %rax, %rsi
movq 0x48(%rsp), %rax
movq %rsi, 0xb8(%rsp)
movl %edx, 0xb4(%rsp)
cmpq %rax, %rcx
movq %rax, 0x38(%rsp)
je 0x22b1
movq 0x38(%rsp), %rdi
addq $-0x28, %rdi
movq %rdi, 0x30(%rsp)
callq 0x2ae0
movq 0x70(%rsp), %rcx
movq 0x30(%rsp), %rax
cmpq %rcx, %rax
movq %rax, 0x38(%rsp)
jne 0x228a
movq 0x68(%rsp), %rdi
callq 0x10d0
jmp 0x246f
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0xc8(%rsp), %rdi
callq 0x2850
movl %eax, %ecx
movl 0x2c(%rsp), %eax
cmpl %ecx, %eax
jge 0x2360
movq 0xc8(%rsp), %rdi
movl 0x94(%rsp), %esi
callq 0x2960
movq %rax, 0x88(%rsp)
movq 0xc0(%rsp), %rdi
movl 0xd4(%rsp), %esi
movq 0x88(%rsp), %rdx
addq $0x8, %rdx
callq 0x1e60
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rdi
addq $0x18, %rdi
leaq 0x88(%rsp), %rsi
callq 0x29b0
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x22cb
leaq 0x2cb9(%rip), %rdi      # 0x5020
movb $0x0, %al
callq 0x1040
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
cmpl 0xd4(%rsp), %eax
jge 0x23bb
movl 0x7c(%rsp), %esi
leaq 0x2ca7(%rip), %rdi      # 0x5035
movb $0x0, %al
callq 0x1040
movq 0xc0(%rsp), %rdi
movslq 0x7c(%rsp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rdi
callq 0x1c70
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x2376
leaq 0x2d13(%rip), %rdi      # 0x50d5
movb $0x0, %al
callq 0x1040
movsd 0xa8(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
movq 0xc0(%rsp), %rdi
movl 0xd4(%rsp), %esi
callq 0x1fa0
movsd %xmm0, 0xa8(%rsp)
jmp 0x21aa
movq 0xc0(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0x2467
movq 0x20(%rsp), %rcx
movq %rcx, %rax
addq $-0x8, %rax
movq %rax, 0x10(%rsp)
imulq $0x28, -0x8(%rcx), %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, 0x18(%rsp)
je 0x245d
movq 0x18(%rsp), %rdi
addq $-0x28, %rdi
movq %rdi, 0x8(%rsp)
callq 0x2ae0
movq 0x20(%rsp), %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
movq %rax, 0x18(%rsp)
jne 0x2436
movq 0x10(%rsp), %rdi
callq 0x10d0
addq $0xd8, %rsp
retq
movq 0xb8(%rsp), %rdi
callq 0x1130
nopl (%rax)