// File: /workspace/github_cpp_makefile_star2_O2/prophyle[P]prophasm/src/prophasm.cpp
// Function: main
int main (int argc, char* argv[])
{
	int32_t k=-1;

	std::string intersection_fn;
	std::vector<std::string> in_fns;
	std::vector<std::string> out_fns;
	std::string stats_fn;
	FILE *fstats=nullptr;

	if (argc<2){
		print_help();
		exit(1);
	}

	bool compute_intersection=false;
	bool compute_output=false;
	bool verbose=true;
	int32_t no_sets=0;

	int c;
	while ((c = getopt(argc, (char *const *)argv, "hSi:o:x:s:k:")) >= 0) {
		switch (c) {
			case 'h': {
				print_help();
				exit(0);
				break;
			}
			case 'i': {
				in_fns.push_back(std::string(optarg));
				no_sets+=1;
				break;
			}
			case 'o': {
				out_fns.push_back(std::string(optarg));
				compute_output=true;
				break;
			}
			case 'x': {
				intersection_fn=std::string(optarg);
				compute_intersection=true;
				break;
			}
			case 's': {
				stats_fn=std::string(optarg);
				if(stats_fn=="-"){
					fstats = stdin;
				}
				else {
					fstats = fopen(stats_fn.c_str(), "w+");
					test_file(fstats, stats_fn);
				}

				break;
			}
			case 'S': {
				verbose=false;

				break;
			}
			case 'k': {
				k = atoi(optarg);
				break;
			}
			case '?': {
				std::cerr<<"Unknown error"<<std::endl;
				exit(1);
				break;
			}
		}
	}

	if (k == -1){
		print_help();
		std::cerr << "K-mer size (-k) is required." << std::endl;
		return EXIT_FAILURE;
	}


	if (k <= 0 || max_allowed_kmer_length<k){
		std::cerr << "K-mer size must satisfy 1 <= k <= " << max_allowed_kmer_length << "." << std::endl;
		return EXIT_FAILURE;
	}

	if (compute_output && (static_cast<int32_t>(out_fns.size())!=no_sets)){
		std::cerr << "If -o is used, it must be used as many times as -i (" << no_sets << "!=" << out_fns.size() << ")." << std::endl;
		return EXIT_FAILURE;
	}

	if(fstats){
		fprintf(fstats,"# cmd: %s",argv[0]);

		for (int32_t i=1;i<argc;i++){
			fprintf(fstats," %s",argv[i]);
		}
		fprintf(fstats,"\n");
	}

	std::vector< std::unordered_set<nkmer_t> > full_sets(no_sets);

	if(verbose){
		std::cerr << "=====================" << std::endl;
		std::cerr << "1) Loading references" << std::endl;
		std::cerr << "=====================" << std::endl;
	}


	std::vector<int32_t> in_sizes;
	std::vector<int32_t> out_sizes;

	for(int32_t i=0;i<no_sets;i++){
		kmers_from_fasta(in_fns[i],full_sets[i],k,fstats,verbose);
		//debug_print_kmer_set(full_sets[i],k);
		in_sizes.insert(in_sizes.end(),full_sets[i].size());
	}

	if(verbose){
		std::cerr << "===============" << std::endl;
		std::cerr << "2) Intersecting" << std::endl;
		std::cerr << "===============" << std::endl;
	}


	std::unordered_set<nkmer_t> intersection;

	int32_t intersection_size = 0;

	if(compute_intersection){
		if (verbose){
			std::cerr << "2.1) Computing intersection" << std::endl;
		}

		find_intersection(full_sets, intersection);
		intersection_size  = intersection.size();
		if (verbose){
			std::cerr << "   intersection size: " <<  intersection_size << std::endl;
		}
		if(compute_output){
			if (verbose){
				std::cerr << "2.2) Removing this intersection from all kmer sets" << std::endl;
			}
			remove_subset(full_sets, intersection);
		}
	}

	if(compute_output){
		for (int32_t i=0;i<no_sets;i++){
			out_sizes.insert(out_sizes.end(),full_sets[i].size());
			assert(in_sizes[i]==out_sizes[i]+intersection_size);
			if (verbose){
				std::cerr << in_sizes[i] << " " << out_sizes[i] << " ...inter:" << intersection_size << std::endl;
			}
		}
	}

	if(verbose){
		std::cerr << "=============" << std::endl;
		std::cerr << "3) Assembling" << std::endl;
		std::cerr << "=============" << std::endl;
	}

	if(compute_output){
		for(int32_t i=0;i<static_cast<int32_t>(in_fns.size());i++){
			assemble(out_fns[i], full_sets[i], k, fstats, verbose);
		}
	}
	if(compute_intersection){
		assemble(intersection_fn, intersection, k, fstats, verbose);
	}

	if (fstats){
		fclose(fstats);
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
leaq 0x138(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rsi, 0x50(%rsp)
xorl %ecx, %ecx
movb %cl, (%rax)
xorps %xmm0, %xmm0
andq $0x0, 0xb0(%rsp)
andq $0x0, 0x70(%rsp)
leaq 0x118(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x60(%rsp)
movq $0x0, 0x8(%rsp)
movb %cl, (%rax)
movl %edi, 0x2c(%rsp)
cmpl $0x2, %edi
jl 0x2f9c
pushq $-0x1
popq %rax
movq %rax, 0x20(%rsp)
movb $0x1, %al
movl %eax, 0x4(%rsp)
leaq 0x2d32(%rip), %r14      # 0x54d0
leaq 0xb8(%rsp), %rbp
leaq 0x2863(%rip), %rbx      # 0x5010
leaq 0x148(%rsp), %r12
movq $0x0, 0x48(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
movl 0x2c(%rsp), %edi
movq 0x50(%rsp), %rsi
movq %r14, %rdx
callq 0x2060
testl %eax, %eax
js 0x2991
leal -0x68(%rax), %ecx
cmpl $0xb, %ecx
ja 0x2837
movslq (%rbx,%rcx,4), %rax
addq %rbx, %rax
jmpq *%rax
movq 0x57bf(%rip), %rax      # 0x7fc0
movq (%rax), %rsi
movq %rbp, %rdi
leaq 0x30(%rsp), %rdx
callq 0x3314
leaq 0xa0(%rsp), %rdi
movq %rbp, %rsi
callq 0x3ec6
movq %rbp, %rdi
callq 0x20d0
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
jmp 0x27d0
cmpl $0x53, %eax
je 0x2942
cmpl $0x78, %eax
je 0x2907
cmpl $0x3f, %eax
jne 0x27d0
jmp 0x2f60
movq 0x5766(%rip), %rax      # 0x7fc0
movq (%rax), %rdi
callq 0x22f0
movq %rax, 0x20(%rsp)
jmp 0x27d0
movq %r14, %r15
movq 0x574a(%rip), %rax      # 0x7fc0
movq (%rax), %rsi
movq %rbp, %rdi
leaq 0x30(%rsp), %rdx
callq 0x3314
leaq 0x108(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2240
movq %rbp, %rdi
callq 0x20d0
movq %r13, %rdi
leaq 0x2c32(%rip), %rsi      # 0x54dd
callq 0x334f
testb %al, %al
je 0x294f
movq 0x5711(%rip), %rax      # 0x7fd0
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movq %r15, %r14
jmp 0x27d0
movq 0x56ea(%rip), %rax      # 0x7fc0
movq (%rax), %rsi
movq %rbp, %rdi
leaq 0x30(%rsp), %rdx
callq 0x3314
leaq 0x60(%rsp), %rdi
movq %rbp, %rsi
callq 0x3ec6
movq %rbp, %rdi
callq 0x20d0
movb $0x1, %al
movq %rax, 0x18(%rsp)
jmp 0x27d0
movq 0x56b2(%rip), %rax      # 0x7fc0
movq (%rax), %rsi
movq %rbp, %rdi
leaq 0x30(%rsp), %rdx
callq 0x3314
leaq 0x128(%rsp), %rdi
movq %rbp, %rsi
callq 0x2240
movq %rbp, %rdi
callq 0x20d0
movb $0x1, %al
movq %rax, 0x48(%rsp)
jmp 0x27d0
movl $0x0, 0x4(%rsp)
jmp 0x27d0
movq 0x108(%rsp), %rdi
leaq 0x2b81(%rip), %rsi      # 0x54df
callq 0x2210
movq %rax, %r14
movq %r12, %rdi
movq %r13, %rsi
callq 0x22a0
movq %r14, 0x10(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x2626
movq %r15, %r14
movq %r12, %rdi
callq 0x20d0
jmp 0x27d0
movq 0x20(%rsp), %rax
cmpl $-0x1, %eax
je 0x29cd
addl $-0x21, %eax
cmpl $-0x21, %eax
ja 0x29f5
movq 0x5646(%rip), %rdi      # 0x7ff0
leaq 0x2b5c(%rip), %rsi      # 0x550d
callq 0x21c0
pushq $0x20
popq %rsi
movq %rax, %rdi
callq 0x2350
movq %rax, %rdi
leaq 0x2afd(%rip), %rsi      # 0x54c8
jmp 0x29e0
callq 0x24cc
leaq 0x2b17(%rip), %rsi      # 0x54f0
movq 0x5610(%rip), %rdi      # 0x7ff0
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
pushq $0x1
popq %rbx
jmp 0x2f0f
testb $0x1, 0x18(%rsp)
je 0x2a14
movq 0x68(%rsp), %rax
subq 0x60(%rsp), %rax
shrq $0x5, %rax
cmpl %eax, 0x8(%rsp)
jne 0x2d76
movq 0x10(%rsp), %r13
testq %r13, %r13
je 0x2a72
movq 0x50(%rsp), %rax
movq (%rax), %rdx
leaq 0x2b3b(%rip), %rsi      # 0x5568
movq %r13, %rdi
xorl %eax, %eax
callq 0x22c0
movl 0x2c(%rsp), %r14d
pushq $0x1
popq %r15
leaq 0x2b27(%rip), %rbx      # 0x556e
cmpq %r14, %r15
jae 0x2a67
movq 0x50(%rsp), %rax
movq (%rax,%r15,8), %rdx
movq %r13, %rdi
movq %rbx, %rsi
xorl %eax, %eax
callq 0x22c0
incq %r15
jmp 0x2a47
pushq $0xa
popq %rdi
movq %r13, %rsi
callq 0x2250
movslq 0x8(%rsp), %rsi
leaq 0x30(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
callq 0x335c
testb $0x1, 0x4(%rsp)
je 0x2ae1
movq 0x5559(%rip), %rdi      # 0x7ff0
leaq 0x2ad4(%rip), %rsi      # 0x5572
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
movq 0x553e(%rip), %rdi      # 0x7ff0
leaq 0x2acf(%rip), %rsi      # 0x5588
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
movq 0x5523(%rip), %rdi      # 0x7ff0
leaq 0x2a9e(%rip), %rsi      # 0x5572
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
xorps %xmm0, %xmm0
leaq 0xf0(%rsp), %rbx
movaps %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
movaps %xmm0, 0x80(%rsp)
andq $0x0, 0x90(%rsp)
xorl %r14d, %r14d
movq 0x8(%rsp), %rax
testl %eax, %eax
cmovlel %r14d, %eax
movq %rax, 0x8(%rsp)
imulq $0x38, %rax, %r12
movzbl 0x4(%rsp), %ebp
andl $0x1, %ebp
leaq 0xb8(%rsp), %r15
xorl %r13d, %r13d
cmpq %r13, %r12
je 0x2b87
movq 0xa0(%rsp), %rdi
addq %r14, %rdi
movq 0x30(%rsp), %rsi
addq %r13, %rsi
movq 0x20(%rsp), %rdx
movq 0x10(%rsp), %rcx
movl %ebp, %r8d
callq 0x33ad
movq 0xf8(%rsp), %rsi
movq 0x30(%rsp), %rax
movl 0x18(%rax,%r13), %eax
movl %eax, 0xb8(%rsp)
movq %rbx, %rdi
movq %r15, %rdx
callq 0x4674
addq $0x38, %r13
addq $0x20, %r14
jmp 0x2b2f
testb $0x1, 0x4(%rsp)
je 0x2bdf
movq 0x545b(%rip), %rdi      # 0x7ff0
leaq 0x29dc(%rip), %rsi      # 0x5578
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
movq 0x5440(%rip), %rdi      # 0x7ff0
leaq 0x29e7(%rip), %rsi      # 0x559e
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
movq 0x5425(%rip), %rdi      # 0x7ff0
leaq 0x29a6(%rip), %rsi      # 0x5578
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
leaq 0xe8(%rsp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
movups %xmm0, -0x20(%rax)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
testb $0x1, 0x48(%rsp)
je 0x2ca2
testb $0x1, 0x4(%rsp)
je 0x2c32
movq 0x53d2(%rip), %rdi      # 0x7ff0
leaq 0x2989(%rip), %rsi      # 0x55ae
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
leaq 0x30(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x3777
movl 0xd0(%rsp), %ebx
testb $0x1, 0x4(%rsp)
je 0x2dca
movq 0x5393(%rip), %rdi      # 0x7ff0
leaq 0x2966(%rip), %rsi      # 0x55ca
callq 0x21c0
movq %rax, %rdi
movl %ebx, %esi
callq 0x2350
movq %rax, %rdi
callq 0x20e0
testb $0x1, 0x18(%rsp)
je 0x2cb6
movq 0x5367(%rip), %rdi      # 0x7ff0
leaq 0x2951(%rip), %rsi      # 0x55e1
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
jmp 0x2dd5
xorl %ebx, %ebx
testb $0x1, 0x18(%rsp)
jne 0x2de7
testb $0x1, 0x4(%rsp)
je 0x2d07
movq 0x5333(%rip), %rdi      # 0x7ff0
leaq 0x28b6(%rip), %rsi      # 0x557a
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
movq 0x5318(%rip), %rdi      # 0x7ff0
leaq 0x2990(%rip), %rsi      # 0x566f
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
movq 0x52fd(%rip), %rdi      # 0x7ff0
leaq 0x2880(%rip), %rsi      # 0x557a
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
testb $0x1, 0x18(%rsp)
movq 0x20(%rsp), %r12
movq 0x10(%rsp), %r13
je 0x2d66
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0xa8(%rsp), %rax
subq 0xa0(%rsp), %rax
shrq $0x5, %rax
cltq
cmpq %rax, %r15
jge 0x2d66
movq 0x60(%rsp), %rdi
addq %r14, %rdi
movq 0x30(%rsp), %rsi
addq %rbx, %rsi
movl %r12d, %edx
movq %r13, %rcx
movl %ebp, %r8d
callq 0x38ea
incq %r15
addq $0x20, %r14
addq $0x38, %rbx
jmp 0x2d20
testb $0x1, 0x48(%rsp)
je 0x2ecd
jmp 0x2eab
movq 0x5273(%rip), %rdi      # 0x7ff0
leaq 0x27ac(%rip), %rsi      # 0x5530
callq 0x21c0
movq %rax, %rdi
movq 0x8(%rsp), %rsi
callq 0x2350
leaq 0x27c8(%rip), %rsi      # 0x5565
movq %rax, %rdi
callq 0x21c0
movq 0x68(%rsp), %rsi
subq 0x60(%rsp), %rsi
sarq $0x5, %rsi
movq %rax, %rdi
callq 0x2140
movq %rax, %rdi
leaq 0x2702(%rip), %rsi      # 0x54c7
jmp 0x29e0
testb $0x1, 0x18(%rsp)
je 0x2eab
leaq 0x30(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x3881
pushq $0x18
popq %r14
xorl %r13d, %r13d
leaq 0x80(%rsp), %r15
leaq 0x5c(%rsp), %r12
cmpq %r13, 0x8(%rsp)
je 0x2caf
movq 0x88(%rsp), %rsi
movq 0x30(%rsp), %rax
movl (%rax,%r14), %eax
movl %eax, 0x5c(%rsp)
movq %r15, %rdi
movq %r12, %rdx
callq 0x4674
movq 0xf0(%rsp), %rax
movl (%rax,%r13,4), %esi
movq 0x80(%rsp), %rax
movl (%rax,%r13,4), %eax
addl %ebx, %eax
cmpl %eax, %esi
jne 0x2f7d
testb $0x1, 0x4(%rsp)
je 0x2e9f
movq 0x519a(%rip), %rdi      # 0x7ff0
callq 0x2350
movq %rax, %rdi
leaq 0x2660(%rip), %rsi      # 0x54c5
callq 0x21c0
movq 0x80(%rsp), %rcx
movl (%rcx,%r13,4), %esi
movq %rax, %rdi
callq 0x2350
movq %rax, %rdi
leaq 0x27dc(%rip), %rsi      # 0x5664
callq 0x21c0
movq %rax, %rdi
movl %ebx, %esi
callq 0x2350
movq %rax, %rdi
callq 0x20e0
incq %r13
addq $0x38, %r14
jmp 0x2dfb
leaq 0x128(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x10(%rsp), %rcx
movl %ebp, %r8d
callq 0x38ea
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x2edc
callq 0x21d0
leaq 0xb8(%rsp), %rdi
callq 0x3d08
leaq 0x80(%rsp), %rdi
callq 0x41ca
leaq 0xf0(%rsp), %rdi
callq 0x41ca
leaq 0x30(%rsp), %rdi
callq 0x3cc4
xorl %ebx, %ebx
leaq 0x108(%rsp), %rdi
callq 0x20d0
leaq 0x60(%rsp), %rdi
callq 0x3ce6
leaq 0xa0(%rsp), %rdi
callq 0x3ce6
leaq 0x128(%rsp), %rdi
callq 0x20d0
movl %ebx, %eax
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x24cc
xorl %edi, %edi
callq 0x2230
movq 0x5089(%rip), %rdi      # 0x7ff0
leaq 0x2574(%rip), %rsi      # 0x54e2
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
jmp 0x2fa1
leaq 0x2690(%rip), %rdi      # 0x5614
leaq 0x26b5(%rip), %rsi      # 0x5640
leaq 0x26bb(%rip), %rcx      # 0x564d
movl $0x2b7, %edx            # imm = 0x2B7
callq 0x2120
callq 0x24cc
pushq $0x1
popq %rdi
callq 0x2230
jmp 0x2fec
jmp 0x2fec
jmp 0x2fe7
movq %rax, %rbx
jmp 0x301b
jmp 0x2ff1
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
jmp 0x2fda
jmp 0x2fec
jmp 0x2ff1
jmp 0x2ff1
jmp 0x2fec
jmp 0x2fec
jmp 0x2fcf
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x20d0
jmp 0x3025
jmp 0x2fec
jmp 0x2fec
jmp 0x2fe7
movq %rax, %rbx
jmp 0x3001
movq %rax, %rbx
jmp 0x3025
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x3d08
leaq 0x80(%rsp), %rdi
callq 0x41ca
leaq 0xf0(%rsp), %rdi
callq 0x41ca
leaq 0x30(%rsp), %rdi
callq 0x3cc4
leaq 0x108(%rsp), %rdi
callq 0x20d0
leaq 0x60(%rsp), %rdi
callq 0x3ce6
leaq 0xa0(%rsp), %rdi
callq 0x3ce6
leaq 0x128(%rsp), %rdi
callq 0x20d0
movq %rbx, %rdi
callq 0x2360