// File: /workspace/github_cpp_makefile_star2_O1/nikosgalanis[P]Vectors-Similarity-Search-and-Clustering/programs/Clustering/cluster_main.cc
// Function: main
int main(int argc, char* argv[]) {
	// parse the arguments
	char* input_file = NULL, *config_file = NULL, *output_file = NULL, *method = NULL;
	bool complete;

	parse_clustering_args(argc, argv, &input_file, &output_file, &config_file, &complete, &method);

	// get the clustering parameters from the config file
	int k, l_lsh, k_lsh, M_hc, k_hc, probes_hc; 

	parse_clustering_config(config_file, &k, &l_lsh, &k_lsh, &M_hc, &k_hc, &probes_hc);

	// parse the dataset in order to get a vector of our feature vector
	vector<vector<double>> feature_vectors = parse_input(input_file);

	// create the output file that we are going to use to print the clustering results
	ofstream output; 
	output.open(output_file);

	output << "Algorithm: " << method << endl << endl;
	Clustering<double>* instant;
	// construct the cluster class depending on which algorithm the user has chosen
	if (!strcmp(method, "Lloyds")) {
		instant = new Clustering<double>("lloyds", feature_vectors, k);
	}
	else if (!strcmp(method, "Range_Search_LSH")) {
		instant = new Clustering<double>("reverse_LSH", feature_vectors, k, l_lsh, k_lsh);

	}
	else if (!strcmp(method, "Range_Search_Hypercube")) {
		instant = new Clustering<double>("reverse_Hypercube", feature_vectors, k, M_hc, k_hc, probes_hc);

	} else {
		cout << "Clustering method not supported\n" << endl;
		exit(EXIT_FAILURE);
	}

	auto c_start = std::chrono::high_resolution_clock::now();
	// run the clustering algorithm
	instant->run_clustering();
	auto c_end = std::chrono::high_resolution_clock::now();

	duration<double> cluster_time = duration_cast<duration<double>>(c_end - c_start);  
	
	// get the map of the centroids
	vector<vector<int>> centroids_map = instant->get_centroids_map();
	// get the actual centroids
	vector<vector<double>> centroids = instant->get_centroids();
	
	// print stats for each cluster
	for (int i = 0; i < k; i++) {
		output << "CLUSTER-" << i << " {size: " << centroids_map.at(i).size() << ", centroid: ";
		print::vector_print_infile(centroids.at(i), &output);
		output << "}\n";
	}

	// print the clustering time
	output << "clustering_time: " <<  cluster_time.count() << endl;
	
	// compute the silhouette
	auto pair = instant->compute_silhouette();
	// print the silhouette results
	output << "\nSilhouette: [";
	print::vector_print_infile(pair.first, &output);
	output << ", " << pair.second << "]\n\n";

	// if the user wants to print the images of each cluster
	if (complete) {
		for (int i = 0; i < k; i++) {
			output << "CLUSTER-" << i << "{";
			print::vector_print_infile(centroids.at(i), &output);
			print::vector_print_infile(centroids_map.at(i), &output);
			output << "}\n";
		}
	}
	// free the class pointer to wrap it up
	
	delete instant;

	output.close();

	free(input_file);
	free(config_file);
	free(output_file);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3f8, %rsp            # imm = 0x3F8
xorl %eax, %eax
leaq 0x108(%rsp), %rbx
movq %rax, (%rbx)
leaq 0x100(%rsp), %r14
movq %rax, (%r14)
leaq 0x40(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x38(%rsp), %rdx
movq %rax, (%rdx)
movq %rdx, (%rsp)
leaq 0x17(%rsp), %r9
movq %rbx, %rdx
movq %r14, %r8
callq 0x2cf8
movq (%r14), %rdi
leaq 0x24(%rsp), %rax
movq %rax, (%rsp)
leaq 0x10(%rsp), %rsi
leaq 0x34(%rsp), %rdx
leaq 0x30(%rsp), %rcx
leaq 0x2c(%rsp), %r8
leaq 0x28(%rsp), %r9
callq 0x2e76
movq (%rbx), %rsi
leaq 0x1d8(%rsp), %rdi
leaq 0x1f8(%rsp), %rdx
callq 0x20e0
leaq 0xe8(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
callq 0x26cc
leaq 0x1e8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3ab2
callq 0x2260
leaq 0x1f8(%rsp), %rdi
callq 0x2420
movq 0x40(%rsp), %rsi
leaq 0x1f8(%rsp), %rdi
movl $0x10, %edx
callq 0x2040
leaq 0x779b(%rip), %rsi      # 0xb278
leaq 0x1f8(%rsp), %rbx
movl $0xb, %edx
movq %rbx, %rdi
callq 0x22b0
movq 0x38(%rsp), %r14
testq %r14, %r14
je 0x3b19
movq %r14, %rdi
callq 0x2100
leaq 0x1f8(%rsp), %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x22b0
jmp 0x3b3b
movq 0x1f8(%rsp), %rax
movq -0x18(%rax), %rax
leaq (%rsp,%rax), %rdi
addq $0x1f8, %rdi            # imm = 0x1F8
movl 0x20(%rdi), %esi
orl $0x1, %esi
callq 0x2410
movq 0x1f8(%rsp), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2270
movsbl %al, %esi
leaq 0x1f8(%rsp), %rdi
callq 0x2030
movq %rax, %rdi
callq 0x21c0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x21c0
movq 0x38(%rsp), %rbx
leaq 0x76e2(%rip), %rsi      # 0xb284
movq %rbx, %rdi
callq 0x2320
testl %eax, %eax
je 0x3c74
leaq 0x76d9(%rip), %rsi      # 0xb292
movq %rbx, %rdi
callq 0x2320
testl %eax, %eax
je 0x3cf7
leaq 0x76df(%rip), %rsi      # 0xb2af
movq %rbx, %rdi
callq 0x2320
testl %eax, %eax
jne 0x43e6
movl $0xa0, %edi
callq 0x2290
movq %rax, 0x18(%rsp)
leaq 0x188(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x76c4(%rip), %rsi      # 0xb2c6
leaq 0x76ce(%rip), %rdx      # 0xb2d7
leaq 0x178(%rsp), %rdi
callq 0x56fa
leaq 0x110(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x4574
movl 0x10(%rsp), %ecx
movl 0x2c(%rsp), %r8d
movl 0x28(%rsp), %r9d
movl 0x24(%rsp), %eax
movl %eax, (%rsp)
leaq 0x178(%rsp), %rsi
leaq 0x110(%rsp), %rdx
movq 0x18(%rsp), %rdi
callq 0x4a2a
leaq 0x110(%rsp), %rdi
callq 0x3618
movq 0x178(%rsp), %rdi
jmp 0x3d7f
movl $0xa0, %edi
callq 0x2290
movq %rax, 0x18(%rsp)
leaq 0x1c8(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x75f5(%rip), %rsi      # 0xb28b
leaq 0x75f4(%rip), %rdx      # 0xb291
leaq 0x1b8(%rsp), %rdi
callq 0x56fa
leaq 0x140(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x4574
movl 0x10(%rsp), %ecx
leaq 0x1b8(%rsp), %rsi
leaq 0x140(%rsp), %rdx
movq 0x18(%rsp), %rdi
callq 0x4606
leaq 0x140(%rsp), %rdi
callq 0x3618
movq 0x1b8(%rsp), %rdi
jmp 0x3d7f
movl $0xa0, %edi
callq 0x2290
movq %rax, 0x18(%rsp)
leaq 0x1a8(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x758a(%rip), %rsi      # 0xb2a3
leaq 0x758e(%rip), %rdx      # 0xb2ae
leaq 0x198(%rsp), %rdi
callq 0x56fa
leaq 0x128(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x4574
movl 0x10(%rsp), %ecx
movl 0x34(%rsp), %r8d
movl 0x30(%rsp), %r9d
leaq 0x198(%rsp), %rsi
leaq 0x128(%rsp), %rdx
movq 0x18(%rsp), %rdi
callq 0x47b6
leaq 0x128(%rsp), %rdi
callq 0x3618
movq 0x198(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3d89
callq 0x2260
callq 0x2050
movq %rax, 0xe0(%rsp)
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x4ca2
callq 0x2050
movq %rax, 0xd8(%rsp)
leaq 0xc0(%rsp), %rdi
movq %rbx, %rsi
callq 0x4d0c
leaq 0x30(%rbx), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x4574
cmpl $0x0, 0x10(%rsp)
jle 0x3f66
xorl %r15d, %r15d
leaq 0x1f8(%rsp), %r13
leaq 0x754e(%rip), %r12      # 0xb33c
leaq 0x7523(%rip), %rbp      # 0xb318
movl $0x8, %edx
movq %r13, %rdi
leaq 0x74f5(%rip), %rsi      # 0xb2f9
callq 0x22b0
movq %r13, %rdi
movl %r15d, %esi
callq 0x2440
movq %rax, %r14
movl $0x8, %edx
movq %rax, %rdi
leaq 0x74dc(%rip), %rsi      # 0xb302
callq 0x22b0
movq 0xc0(%rsp), %rax
movq 0xc8(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %r15, %rdx
jbe 0x43b3
leaq (%r15,%r15,2), %rbx
movq 0x8(%rax,%rbx,8), %rsi
subq (%rax,%rbx,8), %rsi
sarq $0x2, %rsi
movq %r14, %rdi
callq 0x21b0
movl $0xc, %edx
movq %rax, %rdi
leaq 0x748a(%rip), %rsi      # 0xb30b
callq 0x22b0
movq 0xa8(%rsp), %rax
movq 0xb0(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %r15, %rdx
jbe 0x43b3
leaq (%rax,%rbx,8), %rsi
leaq 0x90(%rsp), %rdi
callq 0x3900
movq 0x90(%rsp), %rax
movq 0x98(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x3f33
xorl %r14d, %r14d
cmpq %r14, %rdx
jbe 0x4380
movsd (%rax,%r14,8), %xmm0
movq %r13, %rdi
callq 0x23a0
movl $0x2, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x22b0
incq %r14
movq 0x90(%rsp), %rax
movq 0x98(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %r14
jl 0x3ee3
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x3f45
callq 0x2260
movl $0x2, %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x22b0
incq %r15
movslq 0x10(%rsp), %rax
cmpq %rax, %r15
jl 0x3df5
leaq 0x73ae(%rip), %rsi      # 0xb31b
leaq 0x1f8(%rsp), %rdi
movl $0x11, %edx
callq 0x22b0
movq 0xd8(%rsp), %rax
subq 0xe0(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x7241(%rip), %xmm0     # 0xb1e0
leaq 0x1f8(%rsp), %rdi
callq 0x23a0
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x21c0
leaq 0x158(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x4ddc
leaq 0x733e(%rip), %rsi      # 0xb32d
leaq 0x1f8(%rsp), %rdi
movl $0xe, %edx
callq 0x22b0
leaq 0x78(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x3900
movq 0x78(%rsp), %rax
movq 0x80(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x4088
xorl %ebx, %ebx
leaq 0x1f8(%rsp), %r14
leaq 0x7300(%rip), %r15      # 0xb33c
cmpq %rbx, %rdx
jbe 0x43d5
movsd (%rax,%rbx,8), %xmm0
movq %r14, %rdi
callq 0x23a0
movl $0x2, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x22b0
incq %rbx
movq 0x78(%rsp), %rax
movq 0x80(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbx
jl 0x403c
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x4097
callq 0x2260
leaq 0x729e(%rip), %rsi      # 0xb33c
leaq 0x1f8(%rsp), %rdi
movl $0x2, %edx
callq 0x22b0
movsd 0x170(%rsp), %xmm0
leaq 0x1f8(%rsp), %rdi
callq 0x23a0
leaq 0x7272(%rip), %rsi      # 0xb33f
movl $0x3, %edx
movq %rax, %rdi
callq 0x22b0
cmpb $0x1, 0x17(%rsp)
jne 0x42b5
cmpl $0x0, 0x10(%rsp)
jle 0x42b5
xorl %r14d, %r14d
leaq 0x1f8(%rsp), %r12
leaq 0x723a(%rip), %rbx      # 0xb33c
leaq 0x720f(%rip), %r13      # 0xb318
movl $0x8, %edx
movq %r12, %rdi
leaq 0x71e1(%rip), %rsi      # 0xb2f9
callq 0x22b0
movq %r12, %rdi
movl %r14d, %esi
callq 0x2440
movl $0x1, %edx
movq %rax, %rdi
leaq 0x720c(%rip), %rsi      # 0xb343
callq 0x22b0
movq 0xa8(%rsp), %rax
movq 0xb0(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %r14, %rdx
jbe 0x43c4
leaq (%r14,%r14,2), %r15
leaq (%rax,%r15,8), %rsi
leaq 0x60(%rsp), %rdi
callq 0x3900
movq 0x60(%rsp), %rax
movq 0x68(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x41dc
xorl %ebp, %ebp
cmpq %rbp, %rdx
jbe 0x4391
movsd (%rax,%rbp,8), %xmm0
movq %r12, %rdi
callq 0x23a0
movl $0x2, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x22b0
incq %rbp
movq 0x60(%rsp), %rax
movq 0x68(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x3, %rdx
shrq $0x3, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbp
jl 0x4193
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x41eb
callq 0x2260
movq 0xc0(%rsp), %rax
movq 0xc8(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %r14, %rdx
jbe 0x43c4
leaq (%rax,%r15,8), %rsi
leaq 0x48(%rsp), %rdi
callq 0x54f6
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
testl %edx, %edx
jle 0x4285
xorl %ebp, %ebp
cmpq %rbp, %rdx
jbe 0x43a2
movl (%rax,%rbp,4), %esi
movq %r12, %rdi
callq 0x2440
movl $0x2, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x22b0
incq %rbp
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rcx
subq %rax, %rcx
movq %rcx, %rdx
sarq $0x2, %rdx
shrq $0x2, %rcx
movslq %ecx, %rcx
cmpq %rcx, %rbp
jl 0x423e
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x4294
callq 0x2260
movl $0x2, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x22b0
incq %r14
movslq 0x10(%rsp), %rax
cmpq %rax, %r14
jl 0x4109
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x5576
movq %rbx, %rdi
callq 0x2260
leaq 0x1f8(%rsp), %rdi
callq 0x2330
movq 0x108(%rsp), %rdi
callq 0x22d0
movq 0x100(%rsp), %rdi
callq 0x22d0
movq 0x40(%rsp), %rdi
callq 0x22d0
movq 0x158(%rsp), %rdi
testq %rdi, %rdi
je 0x430d
callq 0x2260
leaq 0xa8(%rsp), %rdi
callq 0x3618
leaq 0xc0(%rsp), %rdi
callq 0x5630
movq 0xbca2(%rip), %rax      # 0xffd0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x200(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x1f8(%rsp,%rcx)
callq 0x2210
leaq 0x2f0(%rsp), %rdi
callq 0x20c0
leaq 0xe8(%rsp), %rdi
callq 0x3618
xorl %eax, %eax
addq $0x3f8, %rsp            # imm = 0x3F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6fbe(%rip), %rdi      # 0xb345
movq %r14, %rsi
xorl %eax, %eax
callq 0x2370
leaq 0x6fad(%rip), %rdi      # 0xb345
movq %rbp, %rsi
xorl %eax, %eax
callq 0x2370
leaq 0x6f9c(%rip), %rdi      # 0xb345
movq %rbp, %rsi
xorl %eax, %eax
callq 0x2370
leaq 0x6f8b(%rip), %rdi      # 0xb345
movq %r15, %rsi
xorl %eax, %eax
callq 0x2370
leaq 0x6f7a(%rip), %rdi      # 0xb345
movq %r14, %rsi
xorl %eax, %eax
callq 0x2370
leaq 0x6f69(%rip), %rdi      # 0xb345
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2370
movq 0xbbeb(%rip), %rdi      # 0xffd8
leaq 0x6ee4(%rip), %rsi      # 0xb2d8
movl $0x20, %edx
callq 0x22b0
movq 0xbbd3(%rip), %rdi      # 0xffd8
callq 0x2110
movl $0x1, %edi
callq 0x22e0
movq %rax, %r14
leaq 0x128(%rsp), %rdi
callq 0x3618
jmp 0x4429
movq %rax, %r14
movq 0x198(%rsp), %rdi
jmp 0x4473
jmp 0x447f
movq %rax, %r14
leaq 0x140(%rsp), %rdi
callq 0x3618
jmp 0x444a
movq %rax, %r14
movq 0x1b8(%rsp), %rdi
jmp 0x4473
jmp 0x447f
movq %rax, %r14
leaq 0x110(%rsp), %rdi
callq 0x3618
jmp 0x446b
movq %rax, %r14
movq 0x178(%rsp), %rdi
cmpq %rbx, %rdi
je 0x4482
callq 0x2260
jmp 0x4482
movq %rax, %r14
movq 0x18(%rsp), %rdi
callq 0x2260
jmp 0x4551
jmp 0x44f1
movq %rax, %r14
jmp 0x4544
jmp 0x44d7
jmp 0x44d7
movq %rax, %r14
jmp 0x455e
movq %rax, %r14
leaq 0x1e8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x456b
callq 0x2260
jmp 0x456b
movq %rax, %r14
jmp 0x456b
jmp 0x44d7
jmp 0x44f1
jmp 0x44ec
movq %rax, %r14
jmp 0x4551
jmp 0x44e2
jmp 0x44ec
jmp 0x44f1
movq %rax, %r14
movq 0x78(%rsp), %rdi
jmp 0x450e
movq %rax, %r14
jmp 0x4518
movq %rax, %r14
jmp 0x4537
jmp 0x44fc
jmp 0x4506
jmp 0x4522
movq %rax, %r14
movq 0x48(%rsp), %rdi
jmp 0x450e
movq %rax, %r14
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x4518
callq 0x2260
movq 0x158(%rsp), %rdi
jmp 0x452d
movq %rax, %r14
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x4537
callq 0x2260
leaq 0xa8(%rsp), %rdi
callq 0x3618
leaq 0xc0(%rsp), %rdi
callq 0x5630
leaq 0x1f8(%rsp), %rdi
callq 0x2360
leaq 0xe8(%rsp), %rdi
callq 0x3618
movq %r14, %rdi
callq 0x2450
nop