// File: /workspace/github_cpp_makefile_star2_O3/simonrharris[P]SKA/src/kmers.cpp
// Function: printKmerFile(std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::array<int, 8ul>, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, std::array<int, 8ul>>>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, float)
int printKmerFile(const std::unordered_map < std::string, std::array < int, 8 > > & mymap, const std::string outputfile, const int kmersize, float userminmaf){
	
	std::cout << "Writing split kmers to " << outputfile << std::endl;

	std::ofstream kmerfile(outputfile);
	if (kmerfile.fail()){
		std::cerr << std::endl << "Error: Failed to open " << outputfile << std::endl << std::endl;
		return 1;
	}
	kmerfile << "SKA v" << versionNumber << std::endl;
	kmerfile << kmersize << std::endl;
	std:: string samplename=splitFileName(outputfile);
	kmerfile << samplename.substr(0, samplename.find_last_of(".")) << std::endl;
	kmerfile << '"';
	for (std::unordered_map < std::string, std::array < int, 8 > >::const_iterator it=mymap.begin(); it!=mymap.end(); ++it){
		std::string kmer=it->first;
		char base;
		bool basefound=false;
		int i=0;

		int filebasecoverage=0;
		int maxfilebasecoverage=0;
		char maxbase;
		for (std::array < int, 8 >::const_iterator it2=it->second.begin(); it2!=it->second.begin()+4; ++it2, ++i){
			filebasecoverage+=*it2;
			if (*it2>maxfilebasecoverage){
				maxfilebasecoverage=*it2;
				base=bases[i];
			}
		}

		float covcutoff=(1.0-userminmaf)*filebasecoverage;

		if (ascii_codons(kmer)){return 1;}
		if (maxfilebasecoverage>=covcutoff){	
			kmerfile << base << kmer;
		}
		else {
			kmerfile << "N" << kmer;
		}
	}
	kmerfile.close();
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movss %xmm0, 0x30(%rsp)
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x38469(%rip), %r15     # 0x40fc0
leaq 0x2a8ba(%rip), %rsi     # 0x33418
movl $0x17, %edx
movq %r15, %rdi
callq 0x5420
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %r15, %rdi
callq 0x5420
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %r15, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5250
leaq 0x58(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl $0x10, %edx
callq 0x5720
movq (%r15), %rax
movq -0x18(%rax), %rax
testb $0x5, 0x78(%rsp,%rax)
je 0x8c7e
movq 0x3841e(%rip), %rdi     # 0x40ff0
movq (%rdi), %rax
addq -0x18(%rax), %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq 0x38403(%rip), %rdi     # 0x40ff0
callq 0x5030
movq %rax, %rdi
callq 0x5250
movq %rax, %rbx
leaq 0x2a82c(%rip), %rsi     # 0x33430
movl $0x16, %edx
movq %rax, %rdi
callq 0x5420
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %rbx, %rdi
callq 0x5420
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5250
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x5030
movl $0x1, %ebx
movq %rax, %rdi
callq 0x5250
jmp 0x8f13
leaq 0x2b0e5(%rip), %rsi     # 0x33d6a
leaq 0x58(%rsp), %rdi
movl $0x5, %edx
callq 0x5420
leaq 0x38875(%rip), %rax     # 0x41510
movq (%rax), %rsi
movq 0x8(%rax), %rdx
leaq 0x58(%rsp), %rdi
callq 0x5420
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %r15, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5250
leaq 0x58(%rsp), %rdi
movl %ebp, %esi
callq 0x5690
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %r15, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5250
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x7dec
leaq 0x2d6d6(%rip), %rsi     # 0x363f6
leaq 0x38(%rsp), %r14
movl $0x1, %ecx
movq %r14, %rdi
movq $-0x1, %rdx
callq 0x5740
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x53e0
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
leaq 0x58(%rsp), %rdi
callq 0x5420
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x53a0
movsbl %al, %esi
movq %r14, %rdi
callq 0x5030
movq %rax, %rdi
callq 0x5250
leaq 0x20(%rsp), %r13
movq -0x10(%r13), %rdi
cmpq %r13, %rdi
je 0x8da4
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x53c0
leaq 0x10(%rsp), %rsi
movb $0x22, (%rsi)
leaq 0x58(%rsp), %rdi
movl $0x1, %edx
callq 0x5420
movq 0x10(%rbx), %rbp
testq %rbp, %rbp
movss 0x30(%rsp), %xmm0
je 0x8ece
cvtss2sd %xmm0, %xmm0
movsd 0x2a48e(%rip), %xmm1    # 0x33268
subsd %xmm0, %xmm1
movsd %xmm1, 0x30(%rsp)
leaq 0x10(%rsp), %rbx
movq %r13, 0x10(%rsp)
movq 0x8(%rbp), %rsi
movq 0x10(%rbp), %rdx
addq %rsi, %rdx
movq %rbx, %rdi
callq 0x8566
xorl %r15d, %r15d
movl $0x28, %eax
leaq 0x386b5(%rip), %rcx     # 0x414c5
xorl %r14d, %r14d
movl (%rbp,%rax), %edx
cmpl %r15d, %edx
jle 0x8e22
movb (%rcx), %r12b
movl %edx, %r15d
addl %edx, %r14d
addq $0x4, %rax
incq %rcx
cmpq $0x38, %rax
jne 0x8e13
movq %rbx, %rdi
callq 0x6a7b
testl %eax, %eax
jne 0x8edc
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
mulsd 0x30(%rsp), %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %r15d, %xmm1
cvtsd2ss %xmm0, %xmm0
ucomiss %xmm0, %xmm1
jae 0x8e7c
movl $0x1, %edx
leaq 0x58(%rsp), %r14
movq %r14, %rdi
leaq 0x2a5d2(%rip), %rsi     # 0x33447
callq 0x5420
jmp 0x8e98
movb %r12b, 0xf(%rsp)
movl $0x1, %edx
leaq 0x58(%rsp), %rdi
leaq 0xf(%rsp), %rsi
callq 0x5420
movq %rax, %r14
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r14, %rdi
callq 0x5420
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x8ec1
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x53c0
movq (%rbp), %rbp
testq %rbp, %rbp
jne 0x8de9
xorl %ebx, %ebx
leaq 0x58(%rsp), %rdi
callq 0x5530
jmp 0x8ef8
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x8ef3
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x53c0
movl $0x1, %ebx
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8f13
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x53c0
movq 0x38096(%rip), %rax     # 0x40fb0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x60(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x58(%rsp,%rcx)
callq 0x5330
leaq 0x150(%rsp), %rdi
callq 0x5120
movl %ebx, %eax
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8f72
jmp 0x8f77
jmp 0x8f72
movq %rax, %rbx
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x8f89
jmp 0x8f96
movq %rax, %rbx
jmp 0x8f96
movq %rax, %rbx
jmp 0x8fb1
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x8f96
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x53c0
leaq 0x48(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x8fb1
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x53c0
leaq 0x58(%rsp), %rdi
callq 0x55a0
movq %rbx, %rdi
callq 0x56a0