// File: ivanzamanov[P]kmeans/src/kmeans/kmeans_main.cpp
// Function: processDirectory(char const*)
void processDirectory(const char* inputDirPath) {
	DIR* dirHandle = opendir(inputDirPath);
	struct dirent* entry = readdir(dirHandle);

	list<article_entry*> entries;
	char path[4096];
	ptree<int> p;
	double docCount = 0;
	while(entry != NULL) {
		sprintf(path, "%s/%s", inputDirPath, entry->d_name);
		processFile(path, entry->d_name, p, entries);
		entry = readdir(dirHandle);
		docCount++;
	}
	printf("Unique tokens: %d in %d documents\n", tokenCount, (int)docCount);
	// Document frequencies
	double* dfs = new double[tokenCount];
	for (int i=0; i<tokenCount; i++)
		dfs[i] = 0;
	for(int i=0; i<entries.size(); i++) {
		vector& v = entries.get(i)->v;
		for(int j=0; j<v.size; j++)
			dfs[ v.keys[j] ]++;
	}
	for(int i=0; i<entries.size(); i++) {
		vector& v = entries.get(i)->v;
		for(int j=0; j<v.size; j++) {
			int tf = v.values[j];
			double idf = log( docCount / dfs[ v.keys[j] ]);
			v.values[j] = ((double) tf) * idf;
		}
	}
	delete dfs;
	
	kmeans(entries);
	
	closedir(dirHandle);
}

// Assembly:
subq $0x10d8, %rsp           # imm = 0x10D8
movq %rdi, 0x10d0(%rsp)
movq 0x10d0(%rsp), %rdi
callq 0x1100
movq %rax, 0x10c8(%rsp)
movq 0x10c8(%rsp), %rdi
callq 0x1110
movq %rax, 0x10c0(%rsp)
leaq 0x10b0(%rsp), %rdi
callq 0x2bb0
leaq 0x90(%rsp), %rdi
callq 0x3900
jmp 0x3055
xorps %xmm0, %xmm0
movsd %xmm0, 0x78(%rsp)
cmpq $0x0, 0x10c0(%rsp)
je 0x313d
movq 0x10d0(%rsp), %rdx
movq 0x10c0(%rsp), %rcx
addq $0x13, %rcx
leaq 0x204f(%rip), %rsi      # 0x50d7
xorl %eax, %eax
leaq 0xb0(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x1050
movq 0x38(%rsp), %rdi
movq 0x10c0(%rsp), %rsi
addq $0x13, %rsi
leaq 0x90(%rsp), %rdx
leaq 0x10b0(%rsp), %rcx
callq 0x2e00
jmp 0x30c4
movq 0x10c8(%rsp), %rdi
callq 0x1110
movq %rax, 0x30(%rsp)
jmp 0x30d8
movq 0x30(%rsp), %rax
movq %rax, 0x10c0(%rsp)
movsd 0x1fc3(%rip), %xmm0     # 0x50b0
addsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
jmp 0x305e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
jmp 0x33bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x3a30
jmp 0x33bd
movl 0x3f85(%rip), %esi      # 0x70c8
cvttsd2si 0x78(%rsp), %edx
leaq 0x1f8d(%rip), %rdi      # 0x50dd
xorl %eax, %eax
callq 0x1040
jmp 0x3159
movslq 0x3f68(%rip), %rax      # 0x70c8
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x1030
movq %rax, 0x28(%rsp)
jmp 0x3182
movq 0x28(%rsp), %rax
movq %rax, 0x70(%rsp)
movl $0x0, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
cmpl 0x3f2a(%rip), %eax      # 0x70c8
jge 0x31bf
movq 0x70(%rsp), %rax
movslq 0x6c(%rsp), %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x3194
movl $0x0, 0x68(%rsp)
movl 0x68(%rsp), %eax
movl %eax, 0x20(%rsp)
leaq 0x10b0(%rsp), %rdi
callq 0x2850
movl %eax, 0x24(%rsp)
jmp 0x31e2
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3275
movl 0x68(%rsp), %esi
leaq 0x10b0(%rsp), %rdi
callq 0x2960
movq %rax, 0x18(%rsp)
jmp 0x320a
movq 0x18(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x60(%rsp)
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
movq 0x60(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x3263
movq 0x70(%rsp), %rax
movq 0x60(%rsp), %rcx
movq 0x8(%rcx), %rcx
movslq 0x5c(%rsp), %rdx
movslq (%rcx,%rdx,4), %rcx
movsd 0x1e64(%rip), %xmm0     # 0x50b0
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x3220
jmp 0x3265
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x31c7
movl $0x0, 0x58(%rsp)
movl 0x58(%rsp), %eax
movl %eax, 0x10(%rsp)
leaq 0x10b0(%rsp), %rdi
callq 0x2850
movl %eax, 0x14(%rsp)
jmp 0x3298
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3365
movl 0x58(%rsp), %esi
leaq 0x10b0(%rsp), %rdi
callq 0x2960
movq %rax, 0x8(%rsp)
jmp 0x32c0
movq 0x8(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x50(%rsp)
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movq 0x50(%rsp), %rcx
cmpl (%rcx), %eax
jge 0x3353
movq 0x50(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x4c(%rsp), %rcx
cvttsd2si (%rax,%rcx,8), %eax
movl %eax, 0x48(%rsp)
movsd 0x78(%rsp), %xmm0
movq 0x70(%rsp), %rax
movq 0x50(%rsp), %rcx
movq 0x8(%rcx), %rcx
movslq 0x4c(%rsp), %rdx
movslq (%rcx,%rdx,4), %rcx
divsd (%rax,%rcx,8), %xmm0
callq 0x10a0
movsd %xmm0, 0x40(%rsp)
cvtsi2sdl 0x48(%rsp), %xmm0
mulsd 0x40(%rsp), %xmm0
movq 0x50(%rsp), %rax
movq 0x10(%rax), %rax
movslq 0x4c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x32d6
jmp 0x3355
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x327d
movq 0x70(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x337d
movq (%rsp), %rdi
callq 0x10b0
leaq 0x10b0(%rsp), %rdi
callq 0x2560
jmp 0x338c
movq 0x10c8(%rsp), %rdi
callq 0x1120
jmp 0x339b
leaq 0x90(%rsp), %rdi
callq 0x3a30
leaq 0x10b0(%rsp), %rdi
callq 0x2c10
addq $0x10d8, %rsp           # imm = 0x10D8
retq
leaq 0x10b0(%rsp), %rdi
callq 0x2c10
movq 0x88(%rsp), %rdi
callq 0x1130
nopw (%rax,%rax)