// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rdi
callq 0x449f0
movl %eax, -0x44(%rbp)
leaq 0xf3d4e(%rip), %rax     # 0x105840
movq (%rax), %rax
movl 0x1c0(%rax), %edi
movl -0xc(%rbp), %esi
callq 0x18580
movq %rax, -0x18(%rbp)
movslq -0x44(%rbp), %rdi
movl $0x8, %esi
leaq 0xd11a1(%rip), %rdx     # 0xe2cb8
movl $0x4, %ecx
callq 0x52f40
movq %rax, -0x30(%rbp)
movslq -0xc(%rbp), %rdi
shlq $0x2, %rdi
leaq 0xd1184(%rip), %rsi     # 0xe2cb8
movl $0x5, %edx
callq 0x52d70
movq %rax, -0x58(%rbp)
movl -0x44(%rbp), %edi
callq 0x54600
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x3c(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x11bf8
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movl (%rax,%rcx,4), %esi
callq 0x46c40
movq %rax, -0x28(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
leaq 0xf3caf(%rip), %rcx     # 0x105840
movq (%rcx), %rcx
cmpl 0x1c0(%rcx), %eax
jge 0x11bde
movl -0x40(%rbp), %eax
leaq 0xf3c9a(%rip), %rcx     # 0x105840
movq (%rcx), %rcx
cmpl 0x1407128(%rcx), %eax
je 0x11bd1
movq -0x28(%rbp), %rax
movq (%rax), %rax
movslq -0x40(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x20(%rbp), %rax
movq (%rax), %rax
movslq -0x40(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x11bd3
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x11b87
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x11b5d
movq -0x60(%rbp), %rdi
callq 0x52e80
movl $0x1, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
je 0x11e84
movl $0x0, -0x48(%rbp)
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x11c42
movq -0x58(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x11c20
movl $0x0, -0x3c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x11cbe
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq -0x4c(%rbp), %rdx
callq 0x11900
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
cmpq -0x38(%rbp), %rax
je 0x11c96
movl $0x1, -0x48(%rbp)
movq -0x38(%rbp), %rdx
movq -0x30(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x58(%rbp), %rax
movslq -0x4c(%rbp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x11c51
cmpl $0x0, -0x48(%rbp)
je 0x11e7f
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x11d36
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
leaq 0xf3b58(%rip), %rcx     # 0x105840
movq (%rcx), %rcx
cmpl 0x1c0(%rcx), %eax
jge 0x11d28
movl -0x40(%rbp), %eax
leaq 0xf3b43(%rip), %rcx     # 0x105840
movq (%rcx), %rcx
cmpl 0x1407128(%rcx), %eax
je 0x11d1b
movq -0x20(%rbp), %rax
movq (%rax), %rax
movslq -0x40(%rbp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x11d1d
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x11cde
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
jmp 0x11cd0
movl $0x0, -0x3c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x11dd5
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
leaq 0xf3adf(%rip), %rcx     # 0x105840
movq (%rcx), %rcx
cmpl 0x1c0(%rcx), %eax
jge 0x11dbb
movl -0x40(%rbp), %eax
leaq 0xf3aca(%rip), %rcx     # 0x105840
movq (%rcx), %rcx
cmpl 0x1407128(%rcx), %eax
je 0x11dae
movq -0x20(%rbp), %rax
movq (%rax), %rax
movslq -0x40(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x30(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq (%rax), %rax
movslq -0x40(%rbp), %rcx
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x11db0
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x11d57
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x11d45
movq -0x18(%rbp), %rax
movq %rax, -0x20(%rbp)
movl $0x0, -0x3c(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x11e7d
movq -0x58(%rbp), %rax
movslq -0x3c(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
jle 0x11e63
movl $0x0, -0x40(%rbp)
movl -0x40(%rbp), %eax
leaq 0xf3a32(%rip), %rcx     # 0x105840
movq (%rcx), %rcx
cmpl 0x1c0(%rcx), %eax
jge 0x11e61
movl -0x40(%rbp), %eax
leaq 0xf3a1d(%rip), %rcx     # 0x105840
movq (%rcx), %rcx
cmpl 0x1407128(%rcx), %eax
je 0x11e54
movq -0x58(%rbp), %rax
movslq -0x3c(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm1
movq -0x20(%rbp), %rax
movq (%rax), %rax
movslq -0x40(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
jmp 0x11e56
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x11e04
jmp 0x11e63
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x20(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x11de4
jmp 0x11e7f
jmp 0x11c08
movq -0x30(%rbp), %rdi
callq 0x52e80
movq -0x58(%rbp), %rdi
callq 0x52e80
movq -0x18(%rbp), %rax
addq $0x60, %rsp
popq %rbp
retq