// File: /workspace/github_cpp_makefile_star2_O2/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 $0x3e8, %rsp            # imm = 0x3E8
leaq 0x88(%rsp), %rbx
andq $0x0, (%rbx)
leaq 0x80(%rsp), %r14
andq $0x0, (%r14)
leaq 0x38(%rsp), %rcx
andq $0x0, (%rcx)
leaq 0x30(%rsp), %rax
andq $0x0, (%rax)
movq %rax, (%rsp)
leaq 0xf(%rsp), %r9
movq %rbx, %rdx
movq %r14, %r8
callq 0x2afc
movq (%r14), %rdi
leaq 0x1c(%rsp), %rax
movq %rax, (%rsp)
leaq 0x18(%rsp), %rsi
leaq 0x2c(%rsp), %rdx
leaq 0x28(%rsp), %rcx
leaq 0x24(%rsp), %r8
leaq 0x20(%rsp), %r9
callq 0x2c6d
movq (%rbx), %rsi
leaq 0x180(%rsp), %rdi
leaq 0x1e8(%rsp), %rdx
callq 0x20b0
leaq 0x68(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0x252c
leaq 0x180(%rsp), %rdi
callq 0x20d0
leaq 0x1e8(%rsp), %rdi
callq 0x2380
movq 0x38(%rsp), %rsi
leaq 0x1e8(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x2030
leaq 0x6e13(%rip), %rsi      # 0xa228
leaq 0x1e8(%rsp), %rdi
callq 0x2200
movq 0x30(%rsp), %rsi
movq %rax, %rdi
callq 0x2200
movq %rax, %rdi
callq 0x20e0
movq %rax, %rdi
callq 0x20e0
movq 0x30(%rsp), %rbx
leaq 0x6de9(%rip), %rsi      # 0xa234
movq %rbx, %rdi
callq 0x2290
testl %eax, %eax
je 0x350c
leaq 0x6de0(%rip), %rsi      # 0xa242
movq %rbx, %rdi
callq 0x2290
testl %eax, %eax
je 0x357b
leaq 0x6de6(%rip), %rsi      # 0xa25f
movq %rbx, %rdi
callq 0x2290
testl %eax, %eax
jne 0x3932
movl $0xa0, %edi
callq 0x2210
movq %rax, 0x10(%rsp)
leaq 0x6dd7(%rip), %rsi      # 0xa276
leaq 0x120(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x20b0
leaq 0xd8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x3a92
movl 0x18(%rsp), %ecx
movl 0x24(%rsp), %r8d
movl 0x20(%rsp), %r9d
movl 0x1c(%rsp), %eax
movl %eax, (%rsp)
leaq 0x120(%rsp), %rsi
leaq 0xd8(%rsp), %rdx
movq 0x10(%rsp), %rdi
callq 0x3e76
leaq 0xd8(%rsp), %rdi
callq 0x2f0e
leaq 0x120(%rsp), %rdi
jmp 0x35f2
movl $0xa0, %edi
callq 0x2210
movq %rax, 0x10(%rsp)
leaq 0x6d19(%rip), %rsi      # 0xa23b
leaq 0x160(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x20b0
leaq 0x108(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x3a92
movl 0x18(%rsp), %ecx
leaq 0x160(%rsp), %rsi
leaq 0x108(%rsp), %rdx
movq 0x10(%rsp), %rdi
callq 0x3aec
leaq 0x108(%rsp), %rdi
callq 0x2f0e
leaq 0x160(%rsp), %rdi
jmp 0x35f2
movl $0xa0, %edi
callq 0x2210
movq %rax, 0x10(%rsp)
leaq 0x6cc2(%rip), %rsi      # 0xa253
leaq 0x140(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x20b0
leaq 0xf0(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x3a92
movl 0x18(%rsp), %ecx
movl 0x2c(%rsp), %r8d
movl 0x28(%rsp), %r9d
leaq 0x140(%rsp), %rsi
leaq 0xf0(%rsp), %rdx
movq 0x10(%rsp), %rdi
callq 0x3c52
leaq 0xf0(%rsp), %rdi
callq 0x2f0e
leaq 0x140(%rsp), %rdi
callq 0x20d0
callq 0x2040
movq %rax, %rbx
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x40a0
callq 0x2040
movq %rax, %r14
leaq 0xa8(%rsp), %rdi
movq %r15, %rsi
callq 0x4108
leaq 0x30(%r15), %rsi
leaq 0x90(%rsp), %rdi
callq 0x3a92
subq %rbx, %r14
cvtsi2sd %r14, %xmm0
mulsd 0x6b7b(%rip), %xmm0     # 0xa1c0
movsd %xmm0, 0x40(%rsp)
xorl %r14d, %r14d
leaq 0x1e8(%rsp), %r12
leaq 0x90(%rsp), %r15
leaq 0x1d0(%rsp), %r13
leaq 0x6c5b(%rip), %rbp      # 0xa2c8
movslq 0x18(%rsp), %rax
cmpq %rax, %r14
jge 0x3715
movq %r12, %rdi
leaq 0x6c24(%rip), %rsi      # 0xa2a9
callq 0x2200
movq %rax, %rdi
movl %r14d, %esi
callq 0x23a0
movq %rax, %rdi
leaq 0x6c13(%rip), %rsi      # 0xa2b2
callq 0x2200
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
movq %r14, %rsi
callq 0x4184
movq 0x8(%rax), %rsi
subq (%rax), %rsi
sarq $0x2, %rsi
movq %rbx, %rdi
callq 0x2180
movq %rax, %rdi
leaq 0x6be7(%rip), %rsi      # 0xa2bb
callq 0x2200
movq %r15, %rdi
movq %r14, %rsi
callq 0x4200
movq %r13, %rdi
movq %rax, %rsi
callq 0x31d2
movq %r13, %rdi
movq %r12, %rsi
callq 0x41a2
movq %r13, %rdi
callq 0x2f86
movq %r12, %rdi
movq %rbp, %rsi
callq 0x2200
incq %r14
jmp 0x366d
leaq 0x6baf(%rip), %rsi      # 0xa2cb
leaq 0x1e8(%rsp), %rdi
callq 0x2200
movq %rax, %rdi
movsd 0x40(%rsp), %xmm0
callq 0x2310
movq %rax, %rdi
callq 0x20e0
leaq 0x48(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x421e
leaq 0x6b88(%rip), %rsi      # 0xa2dd
leaq 0x1e8(%rsp), %rdi
callq 0x2200
leaq 0xc0(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x31d2
leaq 0xc0(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
callq 0x41a2
leaq 0xc0(%rsp), %rdi
callq 0x2f86
leaq 0x6b4f(%rip), %rsi      # 0xa2ec
leaq 0x1e8(%rsp), %rdi
callq 0x2200
movsd 0x60(%rsp), %xmm0
movq %rax, %rdi
callq 0x2310
leaq 0x6b30(%rip), %rsi      # 0xa2ef
movq %rax, %rdi
callq 0x2200
cmpb $0x1, 0xf(%rsp)
jne 0x389d
xorl %r14d, %r14d
leaq 0x1e8(%rsp), %r15
leaq 0x1b8(%rsp), %rbp
leaq 0xa8(%rsp), %rbx
leaq 0x1a0(%rsp), %r12
leaq 0x6acc(%rip), %r13      # 0xa2c8
movslq 0x18(%rsp), %rax
cmpq %rax, %r14
jge 0x389d
movq %r15, %rdi
leaq 0x6a95(%rip), %rsi      # 0xa2a9
callq 0x2200
movq %rax, %rdi
movl %r14d, %esi
callq 0x23a0
movq %rax, %rdi
leaq 0x6ac5(%rip), %rsi      # 0xa2f3
callq 0x2200
leaq 0x90(%rsp), %rdi
movq %r14, %rsi
callq 0x4200
movq %rbp, %rdi
movq %rax, %rsi
callq 0x31d2
movq %rbp, %rdi
movq %r15, %rsi
callq 0x41a2
movq %rbp, %rdi
callq 0x2f86
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4184
movq %r12, %rdi
movq %rax, %rsi
callq 0x46c2
movq %r12, %rdi
movq %r15, %rsi
callq 0x4666
movq %r12, %rdi
callq 0x4888
movq %r15, %rdi
movq %r13, %rsi
callq 0x2200
incq %r14
jmp 0x37fc
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
callq 0x4710
movq %rbx, %rdi
callq 0x21f0
leaq 0x1e8(%rsp), %rdi
callq 0x22a0
movq 0x88(%rsp), %rdi
callq 0x2240
movq 0x80(%rsp), %rdi
callq 0x2240
movq 0x38(%rsp), %rdi
callq 0x2240
leaq 0x48(%rsp), %rdi
callq 0x2f86
leaq 0x90(%rsp), %rdi
callq 0x2f0e
leaq 0xa8(%rsp), %rdi
callq 0x47a6
leaq 0x1e8(%rsp), %rdi
callq 0x22d0
leaq 0x68(%rsp), %rdi
callq 0x2f0e
xorl %eax, %eax
addq $0x3e8, %rsp            # imm = 0x3E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xc69f(%rip), %rdi      # 0xffd8
leaq 0x6948(%rip), %rsi      # 0xa288
callq 0x2200
movq %rax, %rdi
callq 0x20e0
pushq $0x1
popq %rdi
callq 0x2250
movq %rax, %r14
leaq 0xf0(%rsp), %rdi
callq 0x2f0e
jmp 0x396a
movq %rax, %r14
leaq 0x140(%rsp), %rdi
jmp 0x39b4
jmp 0x39bb
movq %rax, %r14
leaq 0x108(%rsp), %rdi
callq 0x2f0e
jmp 0x398b
movq %rax, %r14
leaq 0x160(%rsp), %rdi
jmp 0x39b4
jmp 0x39bb
movq %rax, %r14
leaq 0xd8(%rsp), %rdi
callq 0x2f0e
jmp 0x39ac
movq %rax, %r14
leaq 0x120(%rsp), %rdi
callq 0x20d0
jmp 0x39be
movq %rax, %r14
movq 0x10(%rsp), %rdi
callq 0x21f0
jmp 0x3a72
movq %rax, %r14
leaq 0xc0(%rsp), %rdi
jmp 0x3a32
jmp 0x3a55
movq %rax, %r14
jmp 0x3a65
jmp 0x3a0e
jmp 0x3a0e
movq %rax, %r14
jmp 0x3a7f
movq %rax, %r14
leaq 0x180(%rsp), %rdi
callq 0x20d0
jmp 0x3a89
movq %rax, %r14
jmp 0x3a89
jmp 0x3a0e
jmp 0x3a55
movq %rax, %r14
jmp 0x3a72
jmp 0x3a46
movq %rax, %r14
leaq 0x1a0(%rsp), %rdi
callq 0x4888
jmp 0x3a49
movq %rax, %r14
leaq 0x1b8(%rsp), %rdi
callq 0x2f86
jmp 0x3a49
movq %rax, %r14
leaq 0x1d0(%rsp), %rdi
jmp 0x3a4e
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x2f86
jmp 0x3a58
movq %rax, %r14
leaq 0x90(%rsp), %rdi
callq 0x2f0e
leaq 0xa8(%rsp), %rdi
callq 0x47a6
leaq 0x1e8(%rsp), %rdi
callq 0x22d0
leaq 0x68(%rsp), %rdi
callq 0x2f0e
movq %r14, %rdi
callq 0x23b0
nop