// File: /workspace/github_cpp_makefile_star2_O3/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 0x2d25
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 0x2e9e
movq (%rbx), %rsi
leaq 0x1d8(%rsp), %rdi
leaq 0x1f8(%rsp), %rdx
callq 0x20e0
leaq 0xe8(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
callq 0x26ec
leaq 0x1e8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3aee
callq 0x2260
leaq 0x1f8(%rsp), %rdi
callq 0x2420
movq 0x40(%rsp), %rsi
leaq 0x1f8(%rsp), %rdi
movl $0x10, %edx
callq 0x2040
leaq 0x773f(%rip), %rsi      # 0xb258
leaq 0x1f8(%rsp), %rbx
movl $0xb, %edx
movq %rbx, %rdi
callq 0x22b0
movq 0x38(%rsp), %r14
testq %r14, %r14
je 0x3b55
movq %r14, %rdi
callq 0x2100
leaq 0x1f8(%rsp), %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x22b0
jmp 0x3b77
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 0x7686(%rip), %rsi      # 0xb264
movq %rbx, %rdi
callq 0x2320
testl %eax, %eax
je 0x3cb0
leaq 0x767d(%rip), %rsi      # 0xb272
movq %rbx, %rdi
callq 0x2320
testl %eax, %eax
je 0x3d33
leaq 0x7683(%rip), %rsi      # 0xb28f
movq %rbx, %rdi
callq 0x2320
testl %eax, %eax
jne 0x4404
movl $0xa0, %edi
callq 0x2290
movq %rax, 0x18(%rsp)
leaq 0x188(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x7668(%rip), %rsi      # 0xb2a6
leaq 0x7672(%rip), %rdx      # 0xb2b7
leaq 0x178(%rsp), %rdi
callq 0x56cc
leaq 0x110(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x4594
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 0x4a4a
leaq 0x110(%rsp), %rdi
callq 0x3656
movq 0x178(%rsp), %rdi
jmp 0x3dbb
movl $0xa0, %edi
callq 0x2290
movq %rax, 0x18(%rsp)
leaq 0x1c8(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x7599(%rip), %rsi      # 0xb26b
leaq 0x7598(%rip), %rdx      # 0xb271
leaq 0x1b8(%rsp), %rdi
callq 0x56cc
leaq 0x140(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x4594
movl 0x10(%rsp), %ecx
leaq 0x1b8(%rsp), %rsi
leaq 0x140(%rsp), %rdx
movq 0x18(%rsp), %rdi
callq 0x4626
leaq 0x140(%rsp), %rdi
callq 0x3656
movq 0x1b8(%rsp), %rdi
jmp 0x3dbb
movl $0xa0, %edi
callq 0x2290
movq %rax, 0x18(%rsp)
leaq 0x1a8(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x752e(%rip), %rsi      # 0xb283
leaq 0x7532(%rip), %rdx      # 0xb28e
leaq 0x198(%rsp), %rdi
callq 0x56cc
leaq 0x128(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x4594
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 0x47d6
leaq 0x128(%rsp), %rdi
callq 0x3656
movq 0x198(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3dc5
callq 0x2260
callq 0x2050
movq %rax, 0x80(%rsp)
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x4cc2
callq 0x2050
movq %rax, 0x78(%rsp)
leaq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0x4d2c
leaq 0x30(%rbx), %rsi
leaq 0x48(%rsp), %rdi
callq 0x4594
cmpl $0x0, 0x10(%rsp)
jle 0x3f84
xorl %r15d, %r15d
leaq 0x1f8(%rsp), %r13
leaq 0x74fb(%rip), %r12      # 0xb31c
leaq 0x74d0(%rip), %rbp      # 0xb2f8
movl $0x8, %edx
movq %r13, %rdi
leaq 0x74a2(%rip), %rsi      # 0xb2d9
callq 0x22b0
movq %r13, %rdi
movl %r15d, %esi
callq 0x2440
movq %rax, %r14
movl $0x8, %edx
movq %rax, %rdi
leaq 0x7489(%rip), %rsi      # 0xb2e2
callq 0x22b0
movq 0x60(%rsp), %rax
movq 0x68(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %r15, %rdx
jbe 0x43d1
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 0x743d(%rip), %rsi      # 0xb2eb
callq 0x22b0
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %r15, %rdx
jbe 0x43d1
leaq (%rax,%rbx,8), %rsi
leaq 0xd0(%rsp), %rdi
callq 0x393a
movq 0xd0(%rsp), %rdi
movq 0xd8(%rsp), %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x3f59
xorl %r14d, %r14d
cmpq %r14, %rdx
jbe 0x439e
movsd (%rdi,%r14,8), %xmm0
movq %r13, %rdi
callq 0x23a0
movl $0x2, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x22b0
incq %r14
movq 0xd0(%rsp), %rdi
movq 0xd8(%rsp), %rax
subq %rdi, %rax
movq %rax, %rdx
sarq $0x3, %rdx
shrq $0x3, %rax
cltq
cmpq %rax, %r14
jl 0x3f0a
testq %rdi, %rdi
je 0x3f63
callq 0x2260
movl $0x2, %edx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x22b0
incq %r15
movslq 0x10(%rsp), %rax
cmpq %rax, %r15
jl 0x3e28
leaq 0x7370(%rip), %rsi      # 0xb2fb
leaq 0x1f8(%rsp), %rdi
movl $0x11, %edx
callq 0x22b0
movq 0x78(%rsp), %rax
subq 0x80(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
mulsd 0x7206(%rip), %xmm0     # 0xb1c0
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 0x4dfc
leaq 0x7303(%rip), %rsi      # 0xb30d
leaq 0x1f8(%rsp), %rdi
movl $0xe, %edx
callq 0x22b0
leaq 0xb8(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x393a
movq 0xb8(%rsp), %rdi
movq 0xc0(%rsp), %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x40ab
xorl %ebx, %ebx
leaq 0x1f8(%rsp), %r14
leaq 0x72bf(%rip), %r15      # 0xb31c
cmpq %rbx, %rdx
jbe 0x43f3
movsd (%rdi,%rbx,8), %xmm0
movq %r14, %rdi
callq 0x23a0
movl $0x2, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x22b0
incq %rbx
movq 0xb8(%rsp), %rdi
movq 0xc0(%rsp), %rax
subq %rdi, %rax
movq %rax, %rdx
sarq $0x3, %rdx
shrq $0x3, %rax
cltq
cmpq %rax, %rbx
jl 0x405d
testq %rdi, %rdi
je 0x40b5
callq 0x2260
leaq 0x7260(%rip), %rsi      # 0xb31c
leaq 0x1f8(%rsp), %rdi
movl $0x2, %edx
callq 0x22b0
movsd 0x170(%rsp), %xmm0
leaq 0x1f8(%rsp), %rdi
callq 0x23a0
leaq 0x7234(%rip), %rsi      # 0xb31f
movl $0x3, %edx
movq %rax, %rdi
callq 0x22b0
cmpb $0x1, 0x17(%rsp)
jne 0x42d9
cmpl $0x0, 0x10(%rsp)
jle 0x42d9
xorl %r14d, %r14d
leaq 0x1f8(%rsp), %r12
leaq 0x71fc(%rip), %rbx      # 0xb31c
leaq 0x71d1(%rip), %r13      # 0xb2f8
movl $0x8, %edx
movq %r12, %rdi
leaq 0x71a3(%rip), %rsi      # 0xb2d9
callq 0x22b0
movq %r12, %rdi
movl %r14d, %esi
callq 0x2440
movl $0x1, %edx
movq %rax, %rdi
leaq 0x71ce(%rip), %rsi      # 0xb323
callq 0x22b0
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %r14, %rdx
jbe 0x43e2
leaq (%r14,%r14,2), %r15
leaq (%rax,%r15,8), %rsi
leaq 0xa0(%rsp), %rdi
callq 0x393a
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rdx
subq %rdi, %rdx
sarq $0x3, %rdx
testl %edx, %edx
jle 0x4202
xorl %ebp, %ebp
cmpq %rbp, %rdx
jbe 0x43af
movsd (%rdi,%rbp,8), %xmm0
movq %r12, %rdi
callq 0x23a0
movl $0x2, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x22b0
incq %rbp
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rax
subq %rdi, %rax
movq %rax, %rdx
sarq $0x3, %rdx
shrq $0x3, %rax
cltq
cmpq %rax, %rbp
jl 0x41b4
testq %rdi, %rdi
je 0x420c
callq 0x2260
movq 0x60(%rsp), %rax
movq 0x68(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rdx
cmpq %r14, %rdx
jbe 0x43e2
leaq (%rax,%r15,8), %rsi
leaq 0x88(%rsp), %rdi
callq 0x54c2
movq 0x88(%rsp), %rdi
movq 0x90(%rsp), %rdx
subq %rdi, %rdx
sarq $0x2, %rdx
testl %edx, %edx
jle 0x42ae
xorl %ebp, %ebp
cmpq %rbp, %rdx
jbe 0x43c0
movl (%rdi,%rbp,4), %esi
movq %r12, %rdi
callq 0x2440
movl $0x2, %edx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x22b0
incq %rbp
movq 0x88(%rsp), %rdi
movq 0x90(%rsp), %rax
subq %rdi, %rax
movq %rax, %rdx
sarq $0x2, %rdx
shrq $0x2, %rax
cltq
cmpq %rax, %rbp
jl 0x4262
testq %rdi, %rdi
je 0x42b8
callq 0x2260
movl $0x2, %edx
movq %r12, %rdi
movq %r13, %rsi
callq 0x22b0
incq %r14
movslq 0x10(%rsp), %rax
cmpq %rax, %r14
jl 0x4127
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
callq 0x5542
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 0x4331
callq 0x2260
leaq 0x48(%rsp), %rdi
callq 0x3656
leaq 0x60(%rsp), %rdi
callq 0x55fc
movq 0xbc84(%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 0x3656
xorl %eax, %eax
addq $0x3f8, %rsp            # imm = 0x3F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6f80(%rip), %rdi      # 0xb325
movq %r14, %rsi
xorl %eax, %eax
callq 0x2370
leaq 0x6f6f(%rip), %rdi      # 0xb325
movq %rbp, %rsi
xorl %eax, %eax
callq 0x2370
leaq 0x6f5e(%rip), %rdi      # 0xb325
movq %rbp, %rsi
xorl %eax, %eax
callq 0x2370
leaq 0x6f4d(%rip), %rdi      # 0xb325
movq %r15, %rsi
xorl %eax, %eax
callq 0x2370
leaq 0x6f3c(%rip), %rdi      # 0xb325
movq %r14, %rsi
xorl %eax, %eax
callq 0x2370
leaq 0x6f2b(%rip), %rdi      # 0xb325
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2370
movq 0xbbcd(%rip), %rdi      # 0xffd8
leaq 0x6ea6(%rip), %rsi      # 0xb2b8
movl $0x20, %edx
callq 0x22b0
movq 0xbbb5(%rip), %rdi      # 0xffd8
callq 0x2110
movl $0x1, %edi
callq 0x22e0
movq %rax, %r14
leaq 0x128(%rsp), %rdi
callq 0x3656
jmp 0x4447
movq %rax, %r14
movq 0x198(%rsp), %rdi
jmp 0x4491
jmp 0x449d
movq %rax, %r14
leaq 0x140(%rsp), %rdi
callq 0x3656
jmp 0x4468
movq %rax, %r14
movq 0x1b8(%rsp), %rdi
jmp 0x4491
jmp 0x449d
movq %rax, %r14
leaq 0x110(%rsp), %rdi
callq 0x3656
jmp 0x4489
movq %rax, %r14
movq 0x178(%rsp), %rdi
cmpq %rbx, %rdi
je 0x44a0
callq 0x2260
jmp 0x44a0
movq %rax, %r14
movq 0x18(%rsp), %rdi
callq 0x2260
jmp 0x4572
jmp 0x4512
movq %rax, %r14
jmp 0x4568
jmp 0x44f5
jmp 0x44f5
movq %rax, %r14
jmp 0x457f
movq %rax, %r14
leaq 0x1e8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x458c
callq 0x2260
jmp 0x458c
movq %rax, %r14
jmp 0x458c
jmp 0x44f5
jmp 0x4512
jmp 0x450d
movq %rax, %r14
jmp 0x4572
jmp 0x4500
jmp 0x450d
jmp 0x4512
movq %rax, %r14
movq 0xb8(%rsp), %rdi
jmp 0x4535
movq %rax, %r14
jmp 0x453f
movq %rax, %r14
jmp 0x455e
jmp 0x451d
jmp 0x452a
jmp 0x4549
movq %rax, %r14
movq 0x88(%rsp), %rdi
jmp 0x4535
movq %rax, %r14
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x453f
callq 0x2260
movq 0x158(%rsp), %rdi
jmp 0x4554
movq %rax, %r14
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x455e
callq 0x2260
leaq 0x48(%rsp), %rdi
callq 0x3656
leaq 0x60(%rsp), %rdi
callq 0x55fc
leaq 0x1f8(%rsp), %rdi
callq 0x2360
leaq 0xe8(%rsp), %rdi
callq 0x3656
movq %r14, %rdi
callq 0x2450