// File: /workspace/github_cpp_makefile_star2_O2/simonrharris[P]SKA/src/ska.cpp
// Function: mapSubcommand(int, char**)
int mapSubcommand(int argc, char *argv[]){

	std::string outprefix = "mappedkmers";
	std::string reference = "";
	long int kmersize=15;
	bool includeref=false;
	bool maprepeats=false;
	bool fillall=false;
	bool variantonly=false;
	bool circular=false;
	std::vector < std::string > sample;
	std::vector < std::string > args;

	for (int i=2; i<argc; ++i){

		std::string arg=(argv[i]);

		if (arg=="-h" || arg=="--help"){
			mapHelp();
			return 0;
		}
		else if (arg=="-c"){
			circular=true;
		}
		else if (arg == "-k"){
			i++;
			if (i<argc){
				try {
					kmersize = getint(argv[i]);
				}
				catch (const std::invalid_argument& e) {
					std::cerr << std::endl << "Expecting positive integer after -k flag" << std::endl << std::endl;
					return 1;
				}
			}
			else {
				std::cerr << std::endl << "Expecting positive integer after -k flag" << std::endl << std::endl;
				return 1;
			}
			if (kmersize < MinKmer || kmersize > MaxKmer || kmersize % 3 != 0){
				std::cerr << std::endl << "Kmer size must be a positive integer between " << MinKmer << " and " << MaxKmer << " and must be divisible by 3" << std::endl << std::endl;
				return 1;
			}
		}
		else if (arg == "-r"){
			i++;
			if (i<argc){
				reference = argv[i];
			}
			else {
				std::cerr << std::endl << "Expecting file name after -r flag" << std::endl << std::endl;
				return 1;
			}
		}
		else if (arg == "-i"){
			includeref = true;
		}
		else if (arg == "-m"){
			maprepeats = true;
		}
		else if (arg == "-a"){
			fillall = true;
		}
		else if (arg == "-v"){
			variantonly = true;
		}
		else if (arg == "-o"){
			i++;
			if (i<argc){
				outprefix = std::string(argv[i]);
			}
			else {
				std::cerr << std::endl << "Expecting file name after -o flag" << std::endl << std::endl;
				return 1;
			}
		}
		else if (arg == "-f"){
			i++;
			if (i<argc){
				fileToVector(argv[i], args);
			}
			else {
				std::cerr << std::endl << "Expecting file name after -f flag" << std::endl << std::endl;
				return 1;
			}
		}
		else if (arg == "-s"){
			i++;
			if (i<argc){
				fileToVector(argv[i], sample);
			}
			else {
				std::cerr << std::endl << "Expecting file name after -s flag" << std::endl << std::endl;
				return 1;
			}
		}
		else if (arg[0]=='-'){
			std::cerr << std::endl << "Unrecognised flag " << arg << std::endl << std::endl;
				return 1;
		}
		else {
			args.push_back(arg);
		}
	}

	if (args.size()<1){
		std::cerr << std::endl << "Too few arguments" << std::endl;
		mapHelp();
		return 1;
	}

	if (variantonly){
		outprefix+="_variants";
	}

	outprefix+=".aln";

	if (reference==""){
		std::cerr << std::endl << "Reference file is required" << std::endl;
		mapHelp();
		return 1;
	}

	skaVersion();

	std::cout << "Aligning ";
	if (args.size()>maxNamesToPrint){
		std::cout << args.size() << " files";
	}
	else {
		for (std::vector < std::string >::iterator it = args.begin(); it != args.end(); ++it){
			std::cout << *it << " ";
		}
	}
	std::cout << std::endl;
	std::cout << "Using split kmer size of " << kmersize << std::endl;
	std::cout << "Using " << reference << " as reference" << std::endl;
	if (includeref){
		std::cout << "Reference will be included in the alignment" << std::endl;
	}
	else{
		std::cout << "Reference will not be included in the alignment" << std::endl;
	}
	if (maprepeats){
		std::cout << "Repeats in the reference will be mapped rather than left as gaps" << std::endl;
	}
	else{
		std::cout << "Repeats in the reference will be output as gaps" << std::endl;
	}
	if (fillall){
		std::cout << "All bases in kmers will be mapped as lower case" << std::endl;
	}
	if (circular){
		std::cout << "Reference contigs will be treated as circular" << std::endl;
	}
	else {
		std::cout << "Reference contigs will be treated as linear" << std::endl;
	}
	std::cout << "Output will be written to " << outprefix << std::endl << std::endl;

	if (alignKmersToReference(reference, outprefix, args, kmersize, includeref, maprepeats, fillall, variantonly, sample, circular)){return 1;}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rsi, 0x10(%rsp)
movl %edi, %ebp
leaq 0x6c33(%rip), %rsi      # 0x28600
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x77c0
leaq 0x48dc(%rip), %rsi      # 0x262c2
leaq 0x60(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
callq 0x77c0
xorl %eax, %eax
movb %al, 0xa(%rsp)
movb %al, 0x9(%rsp)
movb %al, 0x8(%rsp)
movl $0x0, 0xc(%rsp)
movb %al, 0x7(%rsp)
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x20(%rsp), %rax
andq $0x0, 0x10(%rax)
movaps %xmm0, (%rax)
pushq $0x2
popq %rbx
pushq $0xf
popq %rax
movq %rax, 0x18(%rsp)
leaq 0xb8(%rsp), %r13
leaq 0x40(%rsp), %r15
cmpl %ebp, %ebx
jge 0x21ed7
movslq %ebx, %rax
movq 0x10(%rsp), %rcx
movq (%rcx,%rax,8), %rsi
movq %r13, %rdi
movq %r15, %rdx
callq 0x77c0
movq %r13, %rdi
leaq 0x5580(%rip), %rsi      # 0x26ff3
callq 0x132f3
testb %al, %al
jne 0x21a8f
movq %r13, %rdi
leaq 0x5570(%rip), %rsi      # 0x26ff6
callq 0x132f3
testb %al, %al
je 0x21aae
xorl %r14d, %r14d
callq 0x21848
xorl %r12d, %r12d
movq %r13, %rdi
callq 0x5778
incl %ebx
testb %r14b, %r14b
jne 0x21a4a
jmp 0x22162
movq %r13, %rdi
leaq 0x5ecb(%rip), %rsi      # 0x27983
callq 0x132f3
movb $0x1, %r14b
testb %al, %al
je 0x21acc
movb $0x1, %al
movl %eax, 0xc(%rsp)
jmp 0x21a9a
movq %r13, %rdi
leaq 0x5527(%rip), %rsi      # 0x26ffd
callq 0x132f3
testb %al, %al
je 0x21b38
incl %ebx
cmpl %ebp, %ebx
jge 0x21b6c
movslq %ebx, %rax
movq 0x10(%rsp), %rcx
movq (%rcx,%rax,8), %rdi
callq 0x1dca8
movq %rax, %rsi
cqto
pushq $0x3
popq %rcx
idivq %rcx
testq %rdx, %rdx
jne 0x21c0a
movslq 0x149db(%rip), %rax     # 0x364f0
cmpq %rax, %rsi
jl 0x21c0a
movslq 0x149cf(%rip), %rax     # 0x364f4
cmpq %rax, %rsi
jg 0x21c0a
movq %rsi, 0x18(%rsp)
jmp 0x21a9a
movq %r13, %rdi
leaq 0x5a28(%rip), %rsi      # 0x2756a
callq 0x132f3
testb %al, %al
je 0x21b99
incl %ebx
cmpl %ebp, %ebx
jge 0x21bb6
movslq %ebx, %rax
movq 0x10(%rsp), %rcx
movq (%rcx,%rax,8), %rsi
leaq 0x60(%rsp), %rdi
callq 0x5730
jmp 0x21a9a
movq 0x1447d(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x547e(%rip), %rsi      # 0x27000
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
jmp 0x21be1
movq %r13, %rdi
leaq 0x59a2(%rip), %rsi      # 0x27545
callq 0x132f3
testb %al, %al
je 0x21be9
movb $0x1, 0xa(%rsp)
jmp 0x21a9a
movq 0x14433(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x59a1(%rip), %rsi      # 0x2756d
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
xorl %r14d, %r14d
jmp 0x21c7a
movq %r13, %rdi
leaq 0x64ed(%rip), %rsi      # 0x280e0
callq 0x132f3
testb %al, %al
je 0x21c83
movb $0x1, 0x9(%rsp)
jmp 0x21a9a
movq %rsi, %r12
movq 0x143dc(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x5406(%rip), %rsi      # 0x27029
callq 0x53f0
movl 0x148c2(%rip), %esi     # 0x364f0
movq %rax, %rdi
callq 0x56a0
movq %rax, %rdi
leaq 0x4946(%rip), %rsi      # 0x26586
callq 0x53f0
movl 0x148a9(%rip), %esi     # 0x364f4
movq %rax, %rdi
callq 0x56a0
movq %rax, %rdi
leaq 0x53fa(%rip), %rsi      # 0x27057
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
xorl %r14d, %r14d
movq %r12, 0x18(%rsp)
pushq $0x1
popq %r12
jmp 0x21a9a
movq %r13, %rdi
leaq 0x636c(%rip), %rsi      # 0x27ff9
callq 0x132f3
testb %al, %al
je 0x21ca0
movb $0x1, 0x8(%rsp)
jmp 0x21a9a
movq %r13, %rdi
leaq 0x56f6(%rip), %rsi      # 0x273a0
callq 0x132f3
testb %al, %al
je 0x21cbd
movb $0x1, 0x7(%rsp)
jmp 0x21a9a
movq %r13, %rdi
leaq 0x5636(%rip), %rsi      # 0x272fd
callq 0x132f3
testb %al, %al
je 0x21d06
incl %ebx
cmpl %ebp, %ebx
jge 0x21d4d
movslq %ebx, %rax
movq 0x10(%rsp), %rcx
movq (%rcx,%rax,8), %rsi
movq %r15, %rdi
leaq 0xb(%rsp), %rdx
callq 0x77c0
leaq 0x80(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x54a0
jmp 0x21dc5
movq %r13, %rdi
leaq 0x5363(%rip), %rsi      # 0x27073
callq 0x132f3
testb %al, %al
je 0x21d7d
incl %ebx
cmpl %ebp, %ebx
jge 0x21dd4
movslq %ebx, %rax
movq 0x10(%rsp), %rcx
movq (%rcx,%rax,8), %rsi
movq %r15, %rdi
leaq 0xb(%rsp), %rdx
callq 0x77c0
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x7529
jmp 0x21dc5
movq 0x1429c(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x57e5(%rip), %rsi      # 0x27548
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
jmp 0x21be1
movq %r13, %rdi
leaq 0x55f4(%rip), %rsi      # 0x2737b
callq 0x132f3
testb %al, %al
je 0x21e04
incl %ebx
cmpl %ebp, %ebx
jge 0x21e4c
movslq %ebx, %rax
movq 0x10(%rsp), %rcx
movq (%rcx,%rax,8), %rsi
movq %r15, %rdi
leaq 0xb(%rsp), %rdx
callq 0x77c0
leaq 0x40(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x7529
leaq 0x40(%rsp), %rdi
callq 0x5778
jmp 0x21a9a
movq 0x14215(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x528c(%rip), %rsi      # 0x27076
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
jmp 0x21be1
movq 0xb8(%rsp), %rax
cmpb $0x2d, (%rax)
jne 0x21e7c
movq 0x141d8(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x5271(%rip), %rsi      # 0x27098
callq 0x53f0
movq %rax, %rdi
movq %r13, %rsi
callq 0x53a0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
jmp 0x21be1
movq 0x1419d(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x551c(%rip), %rsi      # 0x2737e
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
jmp 0x21be1
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
callq 0x77fc
jmp 0x21a9a
movq %rax, %r12
cmpl $0x1, %edx
jne 0x22220
movq %r12, %rdi
callq 0x5170
movq 0x14147(%rip), %rdi     # 0x35ff0
callq 0x51c0
movq %rax, %rdi
leaq 0x5148(%rip), %rsi      # 0x27000
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
callq 0x5640
jmp 0x21be1
movq 0x28(%rsp), %rax
cmpq 0x20(%rsp), %rax
je 0x21f3c
cmpb $0x1, 0x7(%rsp)
jne 0x21efe
leaq 0x54b2(%rip), %rsi      # 0x273a3
leaq 0x80(%rsp), %rdi
callq 0x5720
leaq 0x3888(%rip), %rsi      # 0x2578d
leaq 0x80(%rsp), %rdi
callq 0x5720
leaq 0x43a9(%rip), %rsi      # 0x262c2
leaq 0x60(%rsp), %rdi
callq 0x132f3
testb %al, %al
je 0x21f6d
movq 0x140c2(%rip), %rdi     # 0x35ff0
callq 0x51c0
leaq 0x66d2(%rip), %rsi      # 0x2860c
jmp 0x21f4f
movq 0x140ad(%rip), %rdi     # 0x35ff0
callq 0x51c0
leaq 0x515c(%rip), %rsi      # 0x270ab
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
callq 0x21848
pushq $0x1
popq %r12
jmp 0x22162
callq 0x1df8a
movq 0x1403f(%rip), %rdi     # 0x35fb8
leaq 0x66a7(%rip), %rsi      # 0x28627
callq 0x53f0
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %rax
movq %rax, %rsi
subq %rbx, %rsi
sarq $0x5, %rsi
movslq 0x14560(%rip), %rcx     # 0x36500
cmpq %rcx, %rsi
jbe 0x221a5
movq 0x14008(%rip), %rdi     # 0x35fb8
callq 0x5280
leaq 0x4b09(%rip), %rsi      # 0x26ac5
movq %rax, %rdi
callq 0x53f0
movq 0x13fed(%rip), %rdi     # 0x35fb8
callq 0x51c0
movq 0x13fe1(%rip), %rdi     # 0x35fb8
leaq 0x6653(%rip), %rsi      # 0x28631
callq 0x53f0
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0x53e0
movq %rax, %rdi
callq 0x51c0
movq 0x13fb9(%rip), %rdi     # 0x35fb8
leaq 0x6645(%rip), %rsi      # 0x2864b
callq 0x53f0
leaq 0x60(%rsp), %rsi
movq %rax, %rdi
callq 0x53a0
leaq 0x6633(%rip), %rsi      # 0x28652
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
leaq 0x662a(%rip), %rax      # 0x28660
leaq 0x664f(%rip), %rsi      # 0x2868c
cmpb $0x0, 0xa(%rsp)
cmovneq %rax, %rsi
movq 0x13f6b(%rip), %rdi     # 0x35fb8
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
leaq 0x665b(%rip), %rax      # 0x286bc
leaq 0x6695(%rip), %rsi      # 0x286fd
cmpb $0x0, 0x9(%rsp)
cmovneq %rax, %rsi
movq 0x13f40(%rip), %rdi     # 0x35fb8
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
cmpb $0x1, 0x8(%rsp)
jne 0x220a7
movq 0x13f25(%rip), %rdi     # 0x35fb8
leaq 0x6693(%rip), %rsi      # 0x2872d
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
leaq 0x66af(%rip), %rax      # 0x2875d
leaq 0x66d6(%rip), %rsi      # 0x2878b
testb $0x1, 0xc(%rsp)
cmovneq %rax, %rsi
movq 0x13ef3(%rip), %rdi     # 0x35fb8
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq 0x13edf(%rip), %rdi     # 0x35fb8
leaq 0x5338(%rip), %rsi      # 0x27418
callq 0x53f0
leaq 0x80(%rsp), %rsi
movq %rax, %rdi
callq 0x53a0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
leaq 0xb8(%rsp), %rcx
movq 0x18(%rsp), %rax
movl %eax, (%rcx)
movzbl 0xc(%rsp), %eax
andl $0x1, %eax
leaq 0x7(%rsp), %r10
leaq 0x8(%rsp), %r11
leaq 0x60(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0xa(%rsp), %r8
leaq 0x9(%rsp), %r9
pushq %rax
leaq 0xa8(%rsp), %rax
pushq %rax
pushq %r10
pushq %r11
callq 0x1626c
addq $0x20, %rsp
xorl %r12d, %r12d
testl %eax, %eax
setne %r12b
leaq 0x20(%rsp), %rdi
callq 0x911e
leaq 0xa0(%rsp), %rdi
callq 0x911e
leaq 0x60(%rsp), %rdi
callq 0x5778
leaq 0x80(%rsp), %rdi
callq 0x5778
movl %r12d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x13e0c(%rip), %r14     # 0x35fb8
leaq 0x41aa(%rip), %r15      # 0x2635d
cmpq %rax, %rbx
je 0x21fc4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x53a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x53f0
addq $0x20, %rbx
movq 0x28(%rsp), %rax
jmp 0x221b3
jmp 0x2221d
movq %rax, %r12
callq 0x5640
jmp 0x22220
movq %rax, %rdi
callq 0x7102
jmp 0x221f5
jmp 0x2221d
movq %rax, %r12
leaq 0x40(%rsp), %rdi
callq 0x5778
jmp 0x22220
jmp 0x2221d
jmp 0x2221d
jmp 0x22218
movq %rax, %r12
jmp 0x2224e
movq %rax, %r12
jmp 0x2225b
jmp 0x22218
jmp 0x22218
movq %rax, %r12
jmp 0x2222d
movq %rax, %r12
leaq 0xb8(%rsp), %rdi
callq 0x5778
leaq 0x20(%rsp), %rdi
callq 0x911e
leaq 0xa0(%rsp), %rdi
callq 0x911e
leaq 0x60(%rsp), %rdi
callq 0x5778
leaq 0x80(%rsp), %rdi
callq 0x5778
movq %r12, %rdi
callq 0x56c0