// File: /workspace/github_c_makefile_star2_O3/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 $0x38, %rsp
movl %esi, %ebx
movq %rdi, %r12
callq 0x30309
movl %eax, %r14d
leaq 0xc8a4f(%rip), %rbp     # 0xd5870
movq (%rbp), %rax
movl 0x1c0(%rax), %edi
movl %ebx, %esi
callq 0x116c9
movq %rax, %r13
movslq %r14d, %r14
leaq 0x991d3(%rip), %r15     # 0xa6012
movl $0x8, %esi
movq %r14, %rdi
movq %r15, %rdx
movl $0x4, %ecx
callq 0x3a1fc
movq %rax, 0x18(%rsp)
movl %ebx, (%rsp)
movslq %ebx, %rdi
shlq $0x2, %rdi
movq %r15, %rsi
movl $0x5, %edx
callq 0x3a0e1
movq %rax, 0x10(%rsp)
movl %r14d, %edi
callq 0x3afe4
movq %rax, %r14
movq %r13, 0x8(%rsp)
testq %r13, %r13
je 0xcee9
movq %rbp, %r13
xorl %ebx, %ebx
movq 0x8(%rsp), %r15
movl (%r14,%rbx,4), %esi
movq %r12, %rdi
callq 0x31851
movq (%r13), %rcx
cmpl $0x0, 0x1c0(%rcx)
jle 0xcedd
xorl %edx, %edx
movl 0x1407128(%rcx), %esi
cmpq %rsi, %rdx
je 0xcece
movq (%rax), %rcx
movsd (%rcx,%rdx,8), %xmm0
movq (%r15), %rcx
movsd %xmm0, (%rcx,%rdx,8)
movq (%r13), %rcx
incq %rdx
movslq 0x1c0(%rcx), %rsi
cmpq %rsi, %rdx
jl 0xceaf
movq 0x10(%r15), %r15
incq %rbx
testq %r15, %r15
jne 0xce94
movq %r14, %rdi
callq 0x3a180
movl (%rsp), %eax
shlq $0x2, %rax
movq %rax, 0x20(%rsp)
movq 0x8(%rsp), %r15
movq 0x18(%rsp), %rbp
movq 0x10(%rsp), %r9
movq %r12, 0x28(%rsp)
cmpl $0x0, (%rsp)
jle 0xcf2b
movq %r9, %rdi
xorl %esi, %esi
movq 0x20(%rsp), %rdx
callq 0x4050
movq 0x10(%rsp), %r9
testq %r12, %r12
je 0xd0e4
xorl %r13d, %r13d
movq %r12, %r14
movl $0x0, 0x4(%rsp)
testq %r15, %r15
je 0xcf87
xorl %ebp, %ebp
xorl %r12d, %r12d
movsd 0x985c4(%rip), %xmm0    # 0xa5518
movsd %xmm0, 0x30(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x2fe29
movsd 0x30(%rsp), %xmm1
ucomisd %xmm0, %xmm1
cmoval %ebp, %ebx
minsd %xmm1, %xmm0
cmovaq %r15, %r12
movq 0x10(%r15), %r15
incl %ebp
testq %r15, %r15
jne 0xcf54
jmp 0xcf8a
xorl %r12d, %r12d
movq 0x18(%rsp), %rbp
cmpq %r12, (%rbp,%r13,8)
je 0xcfa3
movq %r12, (%rbp,%r13,8)
movl $0x1, 0x4(%rsp)
movslq %ebx, %rax
movq 0x10(%rsp), %r9
incl (%r9,%rax,4)
movq 0x10(%r14), %r14
incq %r13
testq %r14, %r14
movq 0x8(%rsp), %r15
jne 0xcf42
cmpl $0x0, 0x4(%rsp)
movq 0x28(%rsp), %r12
je 0xd0e4
testq %r15, %r15
leaq 0xc8896(%rip), %r8      # 0xd5870
je 0xd01e
movq (%r8), %rcx
movq %r15, %rax
cmpl $0x0, 0x1c0(%rcx)
jle 0xd015
xorl %edx, %edx
movl 0x1407128(%rcx), %esi
cmpq %rsi, %rdx
je 0xd006
movq (%rax), %rcx
movq $0x0, (%rcx,%rdx,8)
movq (%r8), %rcx
incq %rdx
movslq 0x1c0(%rcx), %rsi
cmpq %rsi, %rdx
jl 0xcfed
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0xcfe2
testq %r12, %r12
je 0xd079
movq (%r8), %rdx
xorl %eax, %eax
movq %r12, %rcx
cmpl $0x0, 0x1c0(%rdx)
jle 0xd06d
xorl %esi, %esi
movl 0x1407128(%rdx), %edi
cmpq %rdi, %rsi
je 0xd05e
movq (%rcx), %rdx
movsd (%rdx,%rsi,8), %xmm0
movq (%rbp,%rax,8), %rdx
movq (%rdx), %rdx
addsd (%rdx,%rsi,8), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
movq (%r8), %rdx
incq %rsi
movslq 0x1c0(%rdx), %rdi
cmpq %rdi, %rsi
jl 0xd036
movq 0x10(%rcx), %rcx
incq %rax
testq %rcx, %rcx
jne 0xd02b
testq %r15, %r15
je 0xcf11
xorl %eax, %eax
movq %r15, %rcx
cmpl $0x0, (%r9,%rax,4)
jle 0xd0d3
movq (%r8), %rsi
cmpl $0x0, 0x1c0(%rsi)
jle 0xd0d3
xorl %edx, %edx
movl 0x1407128(%rsi), %edi
cmpq %rdi, %rdx
je 0xd0c4
xorps %xmm0, %xmm0
cvtsi2sdl (%r9,%rax,4), %xmm0
movq (%rcx), %rsi
movsd (%rsi,%rdx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rsi,%rdx,8)
movq (%r8), %rsi
incq %rdx
movslq 0x1c0(%rsi), %rdi
cmpq %rdi, %rdx
jl 0xd09c
movq 0x10(%rcx), %rcx
incq %rax
testq %rcx, %rcx
jne 0xd087
jmp 0xcf11
movq %rbp, %rdi
movq %r9, %rbx
callq 0x3a180
movq %rbx, %rdi
callq 0x3a180
movq %r15, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq