// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %r14
movl %edi, %ebp
movslq %edi, %rbx
pushq $0x28
popq %rcx
movq %rbx, %rax
mulq %rcx
seto %cl
addq $0x8, %rax
pushq $-0x1
popq %r15
cmovbq %r15, %rax
leaq 0x1773(%rip), %rdi      # 0x306c
testb %cl, %cl
cmoveq %rax, %r15
movl %ebx, %esi
xorl %eax, %eax
callq 0x1040
movq %r15, %rdi
callq 0x1030
movq %rbx, (%rax)
movq %rax, (%rsp)
leaq 0x8(%rax), %r12
testl %ebx, %ebx
je 0x193e
imulq $-0x28, %rbx, %r13
xorl %r15d, %r15d
movq %r12, %rbx
movq %rbx, %rdi
callq 0x1d02
addq $-0x28, %r15
addq $0x28, %rbx
cmpq %r15, %r13
jne 0x1929
movq %r12, %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0x16c2
movq %r12, %rdi
movl %ebp, %esi
callq 0x1841
testl %ebp, %ebp
movl $0x0, %ebx
cmovgl %ebp, %ebx
movq (%rsp), %rax
addq $0x20, %rax
movq %rax, 0x10(%rsp)
movsd 0x169c(%rip), %xmm1     # 0x3010
xorl %esi, %esi
cmpl $0x9, %esi
ja 0x1a5f
subsd %xmm0, %xmm1
ucomisd 0x168d(%rip), %xmm1     # 0x3018
jbe 0x1a5f
movsd %xmm0, 0x18(%rsp)
incl %esi
leaq 0x16e9(%rip), %rdi      # 0x3089
movl %esi, 0xc(%rsp)
xorl %eax, %eax
callq 0x1040
movq 0x10(%rsp), %r15
movq %rbx, %r13
subq $0x1, %r13
jb 0x19c7
movq %r15, %rdi
callq 0x1d16
addq $0x28, %r15
jmp 0x19b3
xorl %r15d, %r15d
leaq 0x20(%rsp), %r13
movslq (%r14), %rax
cmpq %rax, %r15
jge 0x1a03
movq 0x8(%r14), %rax
movq (%rax,%r15,8), %rdx
movq %rdx, 0x20(%rsp)
addq $0x8, %rdx
movq %r12, %rdi
movl %ebp, %esi
callq 0x176d
leaq 0x18(%rax), %rdi
movq %r13, %rsi
callq 0x1c80
incq %r15
jmp 0x19cf
leaq 0x1616(%rip), %rdi      # 0x3020
xorl %eax, %eax
callq 0x1040
movq %r12, %r15
xorl %r13d, %r13d
cmpq %r13, %rbx
je 0x1a3e
leaq 0x1612(%rip), %rdi      # 0x3035
movl %r13d, %esi
xorl %eax, %eax
callq 0x1040
movq %r15, %rdi
callq 0x162a
incq %r13
addq $0x28, %r15
jmp 0x1a17
pushq $0xa
popq %rdi
callq 0x10e0
movq %r12, %rdi
movl %ebp, %esi
callq 0x1841
movsd 0x18(%rsp), %xmm1
movl 0xc(%rsp), %esi
jmp 0x1976
movq (%rsp), %r15
movq (%r15), %rax
testq %rax, %rax
je 0x1a85
imulq $0x28, %rax, %rbx
movq %r15, %r14
addq $-0x8, %r14
leaq (%r14,%rbx), %rdi
callq 0x1d72
addq $-0x28, %rbx
jne 0x1a76
movq %r15, %rdi
callq 0x10d0
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
testq %r15, %r15
je 0x1abe
negq %r15
movq (%rsp), %r14
addq $-0x8, %r14
leaq (%r14,%r15), %rdi
callq 0x1d72
addq $-0x28, %r15
jne 0x1aaf
movq (%rsp), %rdi
callq 0x10d0
movq %rbx, %rdi
callq 0x1150