// File: /workspace/github_c_makefile_star2_O1/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 0x2fa14
movl %eax, %r14d
leaq 0xc5af3(%rip), %rbp     # 0xd2870
movq (%rbp), %rax
movl 0x1c0(%rax), %edi
movl %ebx, %esi
callq 0x11704
movq %rax, %r13
movslq %r14d, %r14
leaq 0x96277(%rip), %r15     # 0xa3012
movl $0x8, %esi
movq %r14, %rdi
movq %r15, %rdx
movl $0x4, %ecx
callq 0x39210
movq %rax, 0x18(%rsp)
movl %ebx, (%rsp)
movslq %ebx, %rdi
shlq $0x2, %rdi
movq %r15, %rsi
movl $0x5, %edx
callq 0x390f5
movq %rax, 0x10(%rsp)
movl %r14d, %edi
callq 0x39fdc
movq %rax, %r14
movq %r13, 0x8(%rsp)
testq %r13, %r13
movq %rbp, %r13
je 0xce45
xorl %ebx, %ebx
movq 0x8(%rsp), %r15
movl (%r14,%rbx,4), %esi
movq %r12, %rdi
callq 0x30fe5
movq (%r13), %rdx
cmpl $0x0, 0x1c0(%rdx)
jle 0xce39
xorl %ecx, %ecx
movl 0x1407128(%rdx), %edx
cmpq %rdx, %rcx
je 0xce26
movq (%rax), %rdx
movsd (%rdx,%rcx,8), %xmm0
movq (%r15), %rdx
movsd %xmm0, (%rdx,%rcx,8)
incq %rcx
movq (%r13), %rdx
movslq 0x1c0(%rdx), %rsi
cmpq %rsi, %rcx
jl 0xce0b
movq 0x10(%r15), %r15
incq %rbx
testq %r15, %r15
jne 0xcdf0
movq %r14, %rdi
callq 0x39194
movl (%rsp), %eax
shlq $0x2, %rax
movq %rax, 0x20(%rsp)
movq 0x8(%rsp), %r15
movq 0x18(%rsp), %rbp
movq 0x10(%rsp), %r14
movq %r12, 0x28(%rsp)
cmpl $0x0, (%rsp)
jle 0xce82
movq %r14, %rdi
xorl %esi, %esi
movq 0x20(%rsp), %rdx
callq 0x4050
testq %r12, %r12
je 0xcf31
xorl %r13d, %r13d
movq %r12, %r14
movl $0x0, 0x4(%rsp)
testq %r15, %r15
je 0xcede
xorl %ebp, %ebp
xorl %r12d, %r12d
movsd 0x9566d(%rip), %xmm0    # 0xa2518
movsd %xmm0, 0x30(%rsp)
movq %r15, %rdi
movq %r14, %rsi
callq 0x2f4ea
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 0xceab
jmp 0xcee1
xorl %r12d, %r12d
movq 0x18(%rsp), %rbp
cmpq %r12, (%rbp,%r13,8)
je 0xcefa
movq %r12, (%rbp,%r13,8)
movl $0x1, 0x4(%rsp)
movslq %ebx, %rax
movq 0x10(%rsp), %rcx
incl (%rcx,%rax,4)
movq 0x10(%r14), %r14
incq %r13
testq %r14, %r14
movq 0x8(%rsp), %r15
jne 0xce99
cmpl $0x0, 0x4(%rsp)
sete %al
movq 0x28(%rsp), %r12
leaq 0xc5946(%rip), %r13     # 0xd2870
movq 0x10(%rsp), %r14
jmp 0xcf33
movb $0x1, %al
testb %al, %al
jne 0xd04e
movq %r15, %rax
testq %r15, %r15
je 0xcf84
movq (%r13), %rdx
cmpl $0x0, 0x1c0(%rdx)
jle 0xcf7b
xorl %ecx, %ecx
movl 0x1407128(%rdx), %edx
cmpq %rdx, %rcx
je 0xcf68
movq (%rax), %rdx
movq $0x0, (%rdx,%rcx,8)
incq %rcx
movq (%r13), %rdx
movslq 0x1c0(%rdx), %rsi
cmpq %rsi, %rcx
jl 0xcf52
movq 0x10(%rax), %rax
testq %rax, %rax
jne 0xcf43
testq %r12, %r12
je 0xcfe1
xorl %eax, %eax
movq %r12, %rcx
movq (%r13), %rsi
cmpl $0x0, 0x1c0(%rsi)
jle 0xcfd5
xorl %edx, %edx
movl 0x1407128(%rsi), %esi
cmpq %rsi, %rdx
je 0xcfc2
movq (%rcx), %rsi
movsd (%rsi,%rdx,8), %xmm0
movq (%rbp,%rax,8), %rsi
movq (%rsi), %rsi
addsd (%rsi,%rdx,8), %xmm0
movsd %xmm0, (%rsi,%rdx,8)
incq %rdx
movq (%r13), %rsi
movslq 0x1c0(%rsi), %rdi
cmpq %rdi, %rdx
jl 0xcf9d
movq 0x10(%rcx), %rcx
incq %rax
testq %rcx, %rcx
jne 0xcf8e
testq %r15, %r15
je 0xce6d
xorl %eax, %eax
movq %r15, %rcx
cmpl $0x0, (%r14,%rax,4)
jle 0xd03d
movq (%r13), %rsi
cmpl $0x0, 0x1c0(%rsi)
jle 0xd03d
xorl %edx, %edx
movl 0x1407128(%rsi), %esi
cmpq %rsi, %rdx
je 0xd02a
xorps %xmm0, %xmm0
cvtsi2sdl (%r14,%rax,4), %xmm0
movq (%rcx), %rsi
movsd (%rsi,%rdx,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, (%rsi,%rdx,8)
incq %rdx
movq (%r13), %rsi
movslq 0x1c0(%rsi), %rdi
cmpq %rdi, %rdx
jl 0xd005
movq 0x10(%rcx), %rcx
incq %rax
testq %rcx, %rcx
jne 0xcfef
jmp 0xce6d
movq %rbp, %rdi
callq 0x39194
movq %r14, %rdi
callq 0x39194
movq %r15, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq