// File: ivanzamanov[P]kmeans/src/kmeans/kmeans.cpp
// Function: computeCentroid(vector&, list<vector*> const&)
void computeCentroid(vector& centroid, const list<vector*> &vectors) {
	if(vectors.size() == 0)
		return;
	list<iter*> iters;
	for(int i=0; i<vectors.size(); i++) {
		iter *it = new iter;
		it->size = vectors.get(i)->size;
		iters.add(it);
	}

	int stop = 0;
	while(!stop) {
		int min_key = 1 << 30;
		stop = 1;
		for(int i=0; i<iters.size(); i++) {
			iter& it = *(iters.get(i));
			int key = vectors.get(i)->keys[it.index];
			if(key < min_key && it.index < it.size) {
				stop = 0;
				min_key = key;
			}
		}
		if(stop)
			break;

		expandVector(centroid);
		centroid.keys[centroid.size - 1] = min_key;

		double value = 0;
		for(int i=0; i<iters.size(); i++) {
			iter& it = *(iters.get(i));
			int key = vectors.get(i)->keys[it.index];
			double val = vectors.get(i)->values[it.index];
			if(key == min_key && it.index < it.size) {
				value += val;
				it.index++;
			}
		}

		centroid.values[centroid.size - 1] = value;
	}

	for(int i=0; i<centroid.size; i++)
		centroid.values[i] /= vectors.size();
	for(int i=0; i<iters.size(); i++)
		delete iters.get(i);
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xf0(%rsp)
movq %rsi, 0xe8(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x25e0
cmpl $0x0, %eax
jne 0x17ae
jmp 0x1c52
leaq 0xd8(%rsp), %rdi
callq 0x25f0
movl $0x0, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
movl %eax, 0x60(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x25e0
movl %eax, 0x64(%rsp)
jmp 0x17e4
movl 0x60(%rsp), %eax
movl 0x64(%rsp), %ecx
cmpl %ecx, %eax
jge 0x189d
movl $0x8, %edi
callq 0x10c0
movq %rax, 0x58(%rsp)
jmp 0x1805
movq 0x58(%rsp), %rdi
callq 0x2650
movq 0x58(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xe8(%rsp), %rdi
movl 0xd4(%rsp), %esi
callq 0x2670
movq %rax, 0x50(%rsp)
jmp 0x1837
movq 0x50(%rsp), %rax
movl (%rax), %ecx
movq 0xb8(%rsp), %rax
movl %ecx, (%rax)
leaq 0xd8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x2690
jmp 0x185f
jmp 0x1861
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x17c6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x27c0
jmp 0x1c5a
movl $0x0, 0xb4(%rsp)
cmpl $0x0, 0xb4(%rsp)
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x18be
jmp 0x1b7e
movl $0x40000000, 0xb0(%rsp) # imm = 0x40000000
movl $0x1, 0xb4(%rsp)
movl $0x0, 0xac(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0x48(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x2790
movl %eax, 0x4c(%rsp)
jmp 0x18fd
movl 0x48(%rsp), %eax
movl 0x4c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x19c7
movl 0xac(%rsp), %esi
leaq 0xd8(%rsp), %rdi
callq 0x27a0
movq %rax, 0x40(%rsp)
jmp 0x1928
movq 0x40(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0xe8(%rsp), %rdi
movl 0xac(%rsp), %esi
callq 0x2670
movq %rax, 0x38(%rsp)
jmp 0x1950
movq 0x38(%rsp), %rax
movq 0x8(%rax), %rax
movq 0xa0(%rsp), %rcx
movslq 0x4(%rcx), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
cmpl 0xb0(%rsp), %eax
jge 0x19af
movq 0xa0(%rsp), %rax
movl 0x4(%rax), %eax
movq 0xa0(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x19af
movl $0x0, 0xb4(%rsp)
movl 0x9c(%rsp), %eax
movl %eax, 0xb0(%rsp)
jmp 0x19b1
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x18df
cmpl $0x0, 0xb4(%rsp)
je 0x19d6
jmp 0x1b7e
movq 0xf0(%rsp), %rdi
callq 0x1250
jmp 0x19e5
movl 0xb0(%rsp), %edx
movq 0xf0(%rsp), %rax
movq 0x8(%rax), %rax
movq 0xf0(%rsp), %rcx
movl (%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, (%rax,%rcx,4)
xorps %xmm0, %xmm0
movsd %xmm0, 0x90(%rsp)
movl $0x0, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
movl %eax, 0x30(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x2790
movl %eax, 0x34(%rsp)
jmp 0x1a40
movl 0x30(%rsp), %eax
movl 0x34(%rsp), %ecx
cmpl %ecx, %eax
jge 0x1b4f
movl 0x8c(%rsp), %esi
leaq 0xd8(%rsp), %rdi
callq 0x27a0
movq %rax, 0x28(%rsp)
jmp 0x1a6b
movq 0x28(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0xe8(%rsp), %rdi
movl 0x8c(%rsp), %esi
callq 0x2670
movq %rax, 0x20(%rsp)
jmp 0x1a93
movq 0x20(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x80(%rsp), %rcx
movslq 0x4(%rcx), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, 0x7c(%rsp)
movq 0xe8(%rsp), %rdi
movl 0x8c(%rsp), %esi
callq 0x2670
movq %rax, 0x18(%rsp)
jmp 0x1aca
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x80(%rsp), %rcx
movslq 0x4(%rcx), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x70(%rsp)
movl 0x7c(%rsp), %eax
cmpl 0xb0(%rsp), %eax
jne 0x1b37
movq 0x80(%rsp), %rax
movl 0x4(%rax), %eax
movq 0x80(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x1b37
movsd 0x70(%rsp), %xmm0
addsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movq 0x80(%rsp), %rax
movl 0x4(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x4(%rax)
jmp 0x1b39
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x1a22
movsd 0x90(%rsp), %xmm0
movq 0xf0(%rsp), %rax
movq 0x10(%rax), %rax
movq 0xf0(%rsp), %rcx
movl (%rcx), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x18a8
movl $0x0, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
movq 0xf0(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x1bdd
movq 0xe8(%rsp), %rdi
callq 0x25e0
movl %eax, 0x14(%rsp)
jmp 0x1ba9
movl 0x14(%rsp), %eax
cvtsi2sd %eax, %xmm1
movq 0xf0(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x6c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x1b86
movl $0x0, 0x68(%rsp)
movl 0x68(%rsp), %eax
movl %eax, 0xc(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x2790
movl %eax, 0x10(%rsp)
jmp 0x1c00
movl 0xc(%rsp), %eax
movl 0x10(%rsp), %ecx
cmpl %ecx, %eax
jge 0x1c45
movl 0x68(%rsp), %esi
leaq 0xd8(%rsp), %rdi
callq 0x27a0
movq %rax, (%rsp)
jmp 0x1c23
movq (%rsp), %rax
cmpq $0x0, %rax
je 0x1c36
movq (%rsp), %rdi
callq 0x10b0
jmp 0x1c38
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x1be5
leaq 0xd8(%rsp), %rdi
callq 0x27c0
addq $0xf8, %rsp
retq
movq 0xc8(%rsp), %rdi
callq 0x1130
nopw (%rax,%rax)