// File: /workspace/github_c_makefile_star2_O2/olcaytaner[P]Isell/clustering.c
// Function: k_means_clustering
Instanceptr k_means_clustering(Instanceptr data, int k)
{
	/*!Last Changed 22.03.2005*/
	Instanceptr result, tmp, tmp2, *b, minindex;
	int i, j, size = data_size(data), changed, dummy, *counts, *randomarray;
	result = empty_link_list_of_instances(current_dataset->multifeaturecount, k);
 b = safecalloc(size, sizeof(Instanceptr), "k_means_clustering", 4);
	counts = safemalloc(k * sizeof(int), "k_means_clustering", 5);
	/*Initialization means*/
	randomarray = random_array(size);
	tmp = result;
	i = 0;
	while (tmp)
	 {
		 tmp2 = data_x(data, randomarray[i]);
		 for (j = 0; j < current_dataset->multifeaturecount; j++)
				 if (j != current_dataset->classdefno)
						 tmp->values[j].floatvalue = tmp2->values[j].floatvalue;
			tmp = tmp->next;
			i++;
	 }
	safe_free(randomarray);
	changed = 1;
	while (changed)
	 {
		 changed = 0;
	  for (i = 0; i < k; i++)
		   counts[i] = 0;
			i = 0;
		 tmp = data;
			/*Finding b_i^t s*/
			while (tmp)
			 {
				 minindex = find_nearest_mean(result, tmp, &dummy);
					if (b[i] != minindex)
					 {
						 changed = 1;
					  b[i] = minindex;
					 }
					counts[dummy]++;
				 tmp = tmp->next;
					i++;
			 }
			/*Update m_i */
			if (changed)
			 {
				 /*Initialize m_i*/
				 tmp = result;
					while (tmp)
					 {
    		 for (j = 0; j < current_dataset->multifeaturecount; j++)
			    	 if (j != current_dataset->classdefno)
						     tmp->values[j].floatvalue = 0.0;
						 tmp = tmp->next;
					 }
					/*Sum up datapoints*/
					i = 0;
					tmp = data;
					while (tmp)
					 {
    		 for (j = 0; j < current_dataset->multifeaturecount; j++)
			    	 if (j != current_dataset->classdefno)
						     b[i]->values[j].floatvalue += tmp->values[j].floatvalue;						 
						 tmp = tmp->next;
							i++;
					 }
					/*Take average*/
				 tmp = result;
					i = 0;
					while (tmp)
					 {
						 if (counts[i] > 0)
      		 for (j = 0; j < current_dataset->multifeaturecount; j++)
	  		    	 if (j != current_dataset->classdefno)
		  				     tmp->values[j].floatvalue /= counts[i];
						 tmp = tmp->next;
							i++;
					 }
			 }
	 }
	safe_free(b);
	safe_free(counts);
	return result;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %ebx
movq %rdi, 0x18(%rsp)
callq 0x2c3cc
movl %eax, %r14d
leaq 0xc1287(%rip), %rbp     # 0xcd870
movq (%rbp), %rax
movl 0x1c0(%rax), %edi
movl %ebx, %esi
callq 0x109d3
movq %rax, %r15
movslq %r14d, %r14
leaq 0x89a13(%rip), %r13     # 0x9601a
pushq $0x8
popq %rsi
pushq $0x4
popq %rcx
movq %r14, %rdi
movq %r13, %rdx
callq 0x35067
movq %rax, %r12
movq %rbx, 0x10(%rsp)
movslq %ebx, %rdi
shlq $0x2, %rdi
pushq $0x5
popq %rdx
movq %r13, %rsi
callq 0x34f59
movq %rax, %r13
movl %r14d, %edi
callq 0x35cf7
movq %rax, %rbx
xorl %r14d, %r14d
movq %r15, 0x8(%rsp)
testq %r15, %r15
je 0xc69a
movl (%rbx,%r14,4), %esi
movq 0x18(%rsp), %rdi
callq 0x2d761
movq (%rbp), %rdx
xorl %ecx, %ecx
movslq 0x1c0(%rdx), %rsi
cmpq %rsi, %rcx
jge 0xc691
movl 0x1407128(%rdx), %esi
cmpq %rsi, %rcx
je 0xc68c
movq (%rax), %rdx
movsd (%rdx,%rcx,8), %xmm0
movq (%r15), %rdx
movsd %xmm0, (%rdx,%rcx,8)
movq (%rbp), %rdx
incq %rcx
jmp 0xc661
movq 0x10(%r15), %r15
incq %r14
jmp 0xc648
movq %rbx, %rdi
callq 0x34ff7
xorl %eax, %eax
movq 0x10(%rsp), %r8
testl %r8d, %r8d
cmovlel %eax, %r8d
movq %r8, 0x10(%rsp)
xorl %eax, %eax
cmpq %rax, %r8
je 0xc6c7
andl $0x0, (%r13,%rax,4)
incq %rax
jmp 0xc6b7
xorl %ebx, %ebx
movq %r12, %r15
movq 0x18(%rsp), %r14
testq %r14, %r14
je 0xc707
movq 0x8(%rsp), %rdi
movq %r14, %rsi
leaq 0x24(%rsp), %rdx
callq 0xc470
cmpq %rax, (%r15)
je 0xc6f3
movq %rax, (%r15)
pushq $0x1
popq %rbx
movslq 0x24(%rsp), %rax
incl (%r13,%rax,4)
movq 0x10(%r14), %r14
addq $0x8, %r15
jmp 0xc6d1
movq 0x8(%rsp), %rax
testl %ebx, %ebx
movq 0x10(%rsp), %r8
je 0xc809
testq %rax, %rax
je 0xc752
movq (%rbp), %rdx
xorl %ecx, %ecx
movslq 0x1c0(%rdx), %rsi
cmpq %rsi, %rcx
jge 0xc74c
movl 0x1407128(%rdx), %esi
cmpq %rsi, %rcx
je 0xc747
movq (%rax), %rdx
andq $0x0, (%rdx,%rcx,8)
movq (%rbp), %rdx
incq %rcx
jmp 0xc724
movq 0x10(%rax), %rax
jmp 0xc719
xorl %eax, %eax
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
je 0xc7a6
movq (%rbp), %rsi
xorl %edx, %edx
movslq 0x1c0(%rsi), %rdi
cmpq %rdi, %rdx
jge 0xc79d
movl 0x1407128(%rsi), %edi
cmpq %rdi, %rdx
je 0xc798
movq (%rcx), %rsi
movsd (%rsi,%rdx,8), %xmm0
movq (%r12,%rax,8), %rsi
movq (%rsi), %rsi
addsd (%rsi,%rdx,8), %xmm0
movsd %xmm0, (%rsi,%rdx,8)
movq (%rbp), %rsi
incq %rdx
jmp 0xc764
movq 0x10(%rcx), %rcx
incq %rax
jmp 0xc759
xorl %eax, %eax
movq 0x8(%rsp), %rcx
testq %rcx, %rcx
je 0xc801
cmpl $0x0, (%r13,%rax,4)
jle 0xc7f8
movq (%rbp), %rsi
xorl %edx, %edx
movslq 0x1c0(%rsi), %rdi
cmpq %rdi, %rdx
jge 0xc7f8
movl 0x1407128(%rsi), %edi
cmpq %rdi, %rdx
je 0xc7f3
cvtsi2sdl (%r13,%rax,4), %xmm0
movq (%rcx), %rsi
movsd (%rsi,%rdx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rsi,%rdx,8)
movq (%rbp), %rsi
incq %rdx
jmp 0xc7c0
movq 0x10(%rcx), %rcx
incq %rax
jmp 0xc7ad
testl %ebx, %ebx
jne 0xc6b5
movq %r12, %rdi
callq 0x34ff7
movq %r13, %rdi
callq 0x34ff7
movq 0x8(%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq