// File: /workspace/github_cpp_makefile_star2_O2/simonrharris[P]SKA/src/sk_refalign.cpp
// Function: alignKmersToReference(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>> const&, int const&, bool const&, bool const&, bool const&, bool const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>> const&, bool)
int alignKmersToReference(const std::string & reference, const std::string & outputfile, const std::vector < std::string > & kmerfiles, const int & kmerlen, const bool & includeref, const bool & maprepeats, const bool & fillall, const bool & variantonly, const std::vector <std::string> & sample, const bool circularContigs)
{

	const std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
	
	// Create the kmer map
	std::unordered_map < std::string, std::vector <int> > kmerMap;
	std::set < int > revSet;
	int substringlength=(kmerlen*2)+1;
	std::string filename=splitFileName(reference);
	std::cout << "Reading " << filename << std::endl;

	int basenum=0;
	std::string refseq;
	char base;
	std::vector < int > referenceContigLengths;
	
	igzstream gzfileStream;

	if(openGzFileStream(reference, gzfileStream)){return 1;}//open the fasta file. May be gzipped.

	while (gzfileStream.peek() != EOF){
		std::string sequence;
		std::string name;

		if(readNextFastaSequence(gzfileStream, reference, name, sequence)){return 1;}//read the next sequence from the file
		int seqlen=sequence.length();
		referenceContigLengths.push_back(seqlen);
		refseq.append(sequence);
		if (seqlen<substringlength){
			basenum+=seqlen;
			continue;
		}

		int baseposition=basenum;

		if (circularContigs){
			if(circulariseSequence(sequence, kmerlen)){return 1;}//if the contigs are circular, add an extra kmer length of sequence from the opposite end to each end
		}
		else {
			baseposition+=kmerlen;
		}
		
		int i=0;
		
		
		for (std::string::iterator iti = sequence.begin(), end = sequence.end()-(substringlength-1); iti != end; ++iti, ++i, ++baseposition){
			std::string kmer=sequence.substr(i,substringlength);
			
			if (reverseComplementIfMin(kmer)){
				revSet.insert(baseposition);
			}
			extractMiddleBase(kmer, base);
			if(ascii_codons(kmer)){return 1;}
			
			addKmerToBasePositionMap(kmerMap, kmer, baseposition);

		}
		basenum+=seqlen;
			
    }
    gzfileStream.close();


	
	std::cout << kmerMap.size() << " kmers read" << std::endl;
	
	std::vector < std::string > sampleNames;
	if (collectSampleNames(kmerfiles, sampleNames)!=0){return 1;}//get sample names from all kmerfiles

	std::vector < bool > include (sampleNames.size());
	getSubsample(sample, sampleNames, include);//get a vector of bools representing which samples to include based on a provided sample file. This also removed duplicate samples in the input files

	std::vector < std::string > includedSampleNames;

	if (includeref){
		includedSampleNames.push_back(filename);
	}

	for (std::vector < bool >::iterator it=include.begin(); it!=include.end(); ++it){//make a vector of all included sample names to help printing output files later
		if (*it){
			includedSampleNames.push_back(sampleNames[distance(include.begin(), it)]);
		}
	}

	std::cout << includedSampleNames.size()-int(includeref) << " samples will be aligned to " << reference << std::endl;

	std::vector < std::string > sequences(includedSampleNames.size(), std::string (basenum , '-'));//create a vector to store the new sequences;

	std::ifstream fileStream;

	char kmerbuffer[kmerlen*2/3];
	int sampleNum=0;
	int includedSampleNum=0;
	if (includeref){
		sequences[0]=refseq;
		includedSampleNum=1;
	}
	
	std::vector < std::string > names;
	
	for (int s = 0; s < kmerfiles.size(); ++s){

		std::string filename=splitFileName(kmerfiles[s]);
		if (openFileStream(kmerfiles[s], fileStream)){return 1;};

		int kmersize;
		
		readKmerHeader(fileStream, kmersize, names);//read the header from the kmer file to get the kmer size and sample names
		
		if (kmersize!=kmerlen){
			std::cerr << std::endl << "kmer size in " << filename << " is not " << kmerlen << std::endl << std::endl;
			return 1;
		}

		std::vector < int > fileInclude;

		for (int i=0; i<names.size(); ++i){ //put the index of all sample names that are to be included into a vector
			if (include[sampleNum+i]){
				fileInclude.push_back(i);
			}
		}

		if (fileInclude.size()==0){ // if no sample names in the file are going to be included then don't read the file
			fileStream.close();
			std::cerr << "Nothing to align" << std::endl;
			sampleNum+=int(names.size());
			names.clear();
			continue;
		}

		char asciibuffer[int(ceil(float(names.size())/6))];

		while (fileStream.read(asciibuffer, sizeof(asciibuffer))){
			std::string asciibits (asciibuffer, sizeof(asciibuffer));
			std::vector < bool > mybits;
			vectorbool_from_ascii(asciibits, mybits);//read the ascii representation of the taxa to a vector of bools
			
			while (fileStream.peek()!='\n' && fileStream.get(base)){
				//base=toupper(base);
				fileStream.read(kmerbuffer, sizeof(kmerbuffer));
				std::string kmer (kmerbuffer, kmerlen*2/3);
				std::unordered_map < std::string, std::vector <int> >::iterator it = kmerMap.find(kmer);//check if the kmer is in the hash
				if ( it != kmerMap.end() ){//if the kmer is in the hash
					if (it->second.size()==1 || maprepeats){//if the match is unique or the user has chosen to map repeats
						
						for (std::vector < int >::iterator itb = it->second.begin(); itb != it->second.end(); ++itb) {//iterate each match of the kmer in the reference
							char mybase=base;
							std::set < int >::iterator itc = revSet.find(*itb);//for each match see if the kmer in the reference is on the reverse strand
							if ( itc != revSet.end() ){//if so, complement the base
								mybase=complement(base);
							}

							for (int j=0; j<fileInclude.size(); ++j){ //add the base to all samples that are true in the bitset
								if (mybits[fileInclude[j]]){
									sequences[includedSampleNum+j][*itb]=mybase;//set the base of the sequence at the site matching the reference
								}
							}
						}
					}
				}
			}
			fileStream.ignore(256,'\n');//skip the end ofline character
    	}

    	for (int i=0; i<fileInclude.size(); ++i){//print the percentage of mapped bases for each sample in a file

    		int contigStart=0;
    		int filledto=0;

    		for (std::vector <int >::iterator rclit=referenceContigLengths.begin(); rclit!=referenceContigLengths.end(); ++rclit){//Fill bases around mapped bases or SNPs
    			filledto=contigStart;
    			for (int j=kmerlen; j<(*rclit-kmerlen); ++j){
    				if ((base_score[sequences[includedSampleNum+i][contigStart+j]]<4 && std::isupper(sequences[includedSampleNum+i][contigStart+j])) && ( fillall || sequences[includedSampleNum+i][contigStart+j] != refseq[contigStart+j])){
    					int fillfrom=std::max(std::max(contigStart+j-kmerlen, contigStart), filledto);
		    			filledto=std::min(contigStart+j+kmerlen+1, contigStart+*rclit);
		    			for (int k=fillfrom; k<filledto; ++k){
		    				if (sequences[includedSampleNum+i][k]=='-'){//if the base is a gap in the sample set it to the reference base in lower case
								sequences[includedSampleNum+i][k]=std::tolower(refseq[k]);//this needs fixing when the contigs are circular to stop it running into the next contig
							}
		    			}
    				}
    			}
    			contigStart+=*rclit;
    		}

	    	int mappedbases=basenum;
	    	int mappedNs=0;
	    	int snps=0;
	    	for (int j=0; j<basenum; ++j){
	    		if (sequences[includedSampleNum+i][j]=='-'){
	    			mappedbases--;
	    		}
	    		if (base_score[sequences[includedSampleNum+i][j]]<4 && base_score[refseq[j]]<4 && sequences[includedSampleNum+i][j]!=refseq[j] && ::isupper(sequences[includedSampleNum+i][j]) ) { 
	    			snps++;
	    		}
	    	}
			std::cout << includedSampleNames[i] << ": " << float(mappedbases)/(basenum)*100 << "% of reference bases mapped. " << snps << " SNPs found vs reference sequence. ";
			if(calculateMissedSNPs(basenum, snps, kmersize)){return 1;};
    	}
    	sampleNum+=int(names.size()); //add the number of samples in the file to the count of total samples
    	includedSampleNum+=int(fileInclude.size());
    	names.clear();
		fileStream.close();
	}


	std::ofstream alignfile(outputfile);
	if (alignfile.fail()){
		std::cerr << std::endl << "Error: Failed to open " << outputfile << std::endl << std::endl;
		return 1;
	}
	
	if (variantonly){
		printVariantSites(alignfile, basenum, includedSampleNames, sequences);
	}
	else{
		std::cout << "Printing alignment" << std::endl;
		for (int i=0; i<includedSampleNames.size(); ++i){
			alignfile << ">" << includedSampleNames[i] << std::endl << sequences[i] << std::endl;
		}
	}

	alignfile.close();
	
	printDuration(start);

	return 0;
	
	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x958, %rsp            # imm = 0x958
movq %r9, -0x1f0(%rbp)
movq %r8, %r14
movq %rcx, %rbx
movq %rdx, -0xd8(%rbp)
movq %rsi, -0xf8(%rbp)
movq %rdi, %r13
callq 0x5050
movq %rax, -0x1d8(%rbp)
leaq -0x2a0(%rbp), %rax
movq %rax, -0x30(%rax)
movq $0x1, -0x28(%rax)
xorps %xmm0, %xmm0
leaq -0x290(%rbp), %rcx
andl $0x0, (%rcx)
movups %xmm0, -0x20(%rax)
andq $0x0, 0x8(%rcx)
movl $0x3f800000, -0x10(%rax) # imm = 0x3F800000
movups %xmm0, -0x8(%rax)
movq %rcx, 0x10(%rcx)
movq %rcx, 0x18(%rcx)
andq $0x0, 0x20(%rcx)
movq %rbx, -0x50(%rbp)
movl (%rbx), %ebx
leaq -0x268(%rbp), %rdi
movq %r13, %rsi
callq 0x7320
movq 0x1fcb1(%rip), %rdi     # 0x35fb8
leaq 0xef6d(%rip), %rsi      # 0x2527b
callq 0x53f0
leaq -0x268(%rbp), %rsi
movq %rax, %rdi
callq 0x53a0
movq %rax, %rdi
callq 0x51c0
leaq -0x218(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
xorps %xmm0, %xmm0
movaps %xmm0, -0x190(%rbp)
andq $0x0, -0x180(%rbp)
leaq -0x978(%rbp), %rdi
callq 0x6d0a
leaq -0x978(%rbp), %rsi
pushq $0x1
popq %rdx
movq %r13, %rdi
movl %edx, -0x34(%rbp)
callq 0x74af
testl %eax, %eax
jne 0x170e2
movq %r14, -0x68(%rbp)
movl %ebx, %eax
addl %ebx, %ebx
leal 0x1(,%rax,2), %eax
leaq -0x4c8(%rbp), %r14
movl %ebx, -0x48(%rbp)
movslq %ebx, %rcx
negq %rcx
movq %rcx, -0x58(%rbp)
movslq %eax, %r12
leaq -0x1b8(%rbp), %r15
xorl %ebx, %ebx
movq %r13, -0x40(%rbp)
leaq -0x7f0(%rbp), %rdi
callq 0x5140
cmpl $-0x1, %eax
movl %ebx, -0x30(%rbp)
je 0x1659d
movq %r14, -0x4d8(%rbp)
andq $0x0, -0x4d0(%rbp)
xorl %ecx, %ecx
movb %cl, -0x4c8(%rbp)
leaq -0x6c8(%rbp), %rax
movq %rax, -0x6d8(%rbp)
andq $0x0, -0x6d0(%rbp)
movb %cl, -0x6c8(%rbp)
leaq -0x978(%rbp), %rdi
movq %r13, %rsi
leaq -0x6d8(%rbp), %rdx
leaq -0x4d8(%rbp), %rbx
movq %rbx, %rcx
callq 0x67b1
testl %eax, %eax
jne 0x16580
movl -0x4d0(%rbp), %eax
movl %eax, -0x130(%rbp)
leaq -0x190(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0xbe78
leaq -0x228(%rbp), %rdi
movq %rbx, %rsi
callq 0x5750
movl -0x130(%rbp), %ebx
cmpl -0x48(%rbp), %ebx
jle 0x16484
movl -0x30(%rbp), %ecx
movl %ecx, -0x150(%rbp)
movq -0x50(%rbp), %rax
movl (%rax), %esi
cmpb $0x0, 0x28(%rbp)
je 0x164a1
leaq -0x4d8(%rbp), %rbx
movq %rbx, %rdi
callq 0x6c50
testl %eax, %eax
je 0x164b0
jmp 0x16580
leaq -0x6d8(%rbp), %rdi
callq 0x5778
leaq -0x4d8(%rbp), %rdi
callq 0x5778
jmp 0x16574
addl %ecx, %esi
movl %esi, -0x150(%rbp)
leaq -0x4d8(%rbp), %rbx
movq -0x4d0(%rbp), %r14
addq -0x58(%rbp), %r14
xorl %r13d, %r13d
cmpq %r13, %r14
je 0x1653b
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq %r12, %rcx
callq 0x5420
movq %r15, %rdi
callq 0x6578
testb %al, %al
je 0x164f3
leaq -0x298(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x174fa
movq %r15, %rdi
leaq -0x29(%rbp), %rsi
callq 0x7c51
movq %r15, %rdi
callq 0x6674
testl %eax, %eax
jne 0x16533
movl -0x150(%rbp), %edx
leaq -0x2d0(%rbp), %rdi
movq %r15, %rsi
callq 0x15e80
movq %r15, %rdi
callq 0x5778
incq %r13
incl -0x150(%rbp)
jmp 0x164be
movq %r15, %rdi
callq 0x5778
xorl %ebx, %ebx
cmpq %r13, %r14
jne 0x16548
movl -0x130(%rbp), %ebx
leaq -0x6d8(%rbp), %rdi
callq 0x5778
leaq -0x4d8(%rbp), %rdi
callq 0x5778
cmpq %r13, %r14
movq -0x40(%rbp), %r13
leaq -0x4c8(%rbp), %r14
jne 0x170e2
movl -0x30(%rbp), %eax
addl %ebx, %eax
movl %eax, %ebx
jmp 0x163ab
leaq -0x6d8(%rbp), %rdi
callq 0x5778
leaq -0x4d8(%rbp), %rdi
callq 0x5778
jmp 0x170e2
leaq -0x978(%rbp), %rdi
callq 0x6db4
movq -0x68(%rbp), %rbx
movq -0x2b8(%rbp), %rsi
movq 0x1f9fd(%rip), %rdi     # 0x35fb8
callq 0x5280
leaq 0xfdfa(%rip), %rsi      # 0x263c1
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
xorps %xmm0, %xmm0
leaq -0x170(%rbp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
pushq $0x1
popq %rdx
movq -0xd8(%rbp), %rdi
movl %edx, -0x34(%rbp)
callq 0x8436
testl %eax, %eax
jne 0x170d6
movq -0x168(%rbp), %rsi
subq -0x170(%rbp), %rsi
sarq $0x5, %rsi
leaq -0x1b8(%rbp), %rdi
leaq -0x4d8(%rbp), %rdx
callq 0x9076
leaq -0x170(%rbp), %rsi
leaq -0x1b8(%rbp), %rdx
movq 0x20(%rbp), %rdi
callq 0x8605
andq $0x0, -0x80(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x90(%rbp)
cmpb $0x1, (%rbx)
jne 0x16666
leaq -0x90(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x77fc
xorl %r14d, %r14d
movq -0x1b8(%rbp), %r15
leaq -0x90(%rbp), %rbx
xorl %r12d, %r12d
cmpq -0x1a8(%rbp), %r15
jne 0x1668c
cmpl -0x1a0(%rbp), %r12d
je 0x166d7
movq (%r15), %rax
btq %r12, %rax
jae 0x166bc
movl %r12d, %esi
movq %r15, %rax
subq -0x1b8(%rbp), %rax
shlq $0x8, %rax
addq -0x170(%rbp), %rax
shlq $0x5, %rsi
addq %rax, %rsi
movq %rbx, %rdi
callq 0x77fc
xorl %eax, %eax
cmpl $0x3f, %r12d
leal 0x1(%r12), %ecx
sete %al
leaq (%r15,%rax,8), %r15
cmovel %r14d, %ecx
movl %ecx, %r12d
jmp 0x1667a
movq -0x88(%rbp), %rsi
subq -0x90(%rbp), %rsi
sarq $0x5, %rsi
movq -0x68(%rbp), %r15
movzbl (%r15), %eax
subq %rax, %rsi
movq 0x1f8bd(%rip), %rdi     # 0x35fb8
callq 0x5280
leaq 0xfcc6(%rip), %rsi      # 0x263cd
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
movq %r13, %rsi
callq 0x53a0
movq %rax, %rdi
callq 0x51c0
movq -0x90(%rbp), %r14
movq -0x88(%rbp), %rbx
movslq -0x30(%rbp), %rsi
leaq -0x4d8(%rbp), %rdi
leaq -0x4c8(%rbp), %rax
movq %rax, (%rdi)
pushq $0x2d
popq %rdx
callq 0x5470
subq %r14, %rbx
sarq $0x5, %rbx
leaq -0xf0(%rbp), %rdi
leaq -0x4d8(%rbp), %rdx
leaq -0x6d8(%rbp), %rcx
movq %rbx, %rsi
callq 0x173be
leaq -0x4d8(%rbp), %rdi
callq 0x5778
leaq -0x4d8(%rbp), %rdi
callq 0x5450
movl -0x30(%rbp), %ebx
movq %rsp, -0x1c0(%rbp)
movq -0x50(%rbp), %rax
movl (%rax), %eax
addl %eax, %eax
pushq $0x3
popq %rcx
cltd
idivl %ecx
movq %rax, -0x1f8(%rbp)
addq $0xf, %rax
andq $-0x10, %rax
movq %rsp, %rcx
subq %rax, %rcx
movq %rcx, -0x108(%rbp)
movq %rcx, %rsp
movq $0x0, -0x70(%rbp)
cmpb $0x1, (%r15)
movl $0x0, -0x5c(%rbp)
jne 0x167ee
movq -0xf0(%rbp), %rdi
leaq -0x228(%rbp), %rsi
callq 0x5200
pushq $0x1
popq %rax
movl %eax, -0x5c(%rbp)
xorps %xmm0, %xmm0
leaq -0xd0(%rbp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
cvtsi2ss %ebx, %xmm0
movss %xmm0, -0x74(%rbp)
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
movq %rax, -0x200(%rbp)
pushq $0x1
popq %rax
movl %eax, -0x34(%rbp)
leaq -0x248(%rbp), %r12
xorl %r14d, %r14d
movq -0xd8(%rbp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq %r14, %rax
jbe 0x16f66
movq %r14, -0x1d0(%rbp)
shlq $0x5, %r14
addq %r14, %rsi
leaq -0x130(%rbp), %rdi
callq 0x7320
movq -0xd8(%rbp), %rax
addq (%rax), %r14
movq %r14, %rdi
leaq -0x4d8(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x73fe
leaq -0xb0(%rbp), %rbx
testl %eax, %eax
jne 0x17031
leaq -0x4d8(%rbp), %rdi
leaq -0x78(%rbp), %rsi
leaq -0xd0(%rbp), %rdx
callq 0x832b
movl -0x78(%rbp), %eax
movq -0x50(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x16fda
xorps %xmm0, %xmm0
movaps %xmm0, -0xb0(%rbp)
andq $0x0, -0xa0(%rbp)
xorl %ecx, %ecx
movl %ecx, -0x6d8(%rbp)
movslq %ecx, %rdx
movq -0xc8(%rbp), %rax
subq -0xd0(%rbp), %rax
sarq $0x5, %rax
cmpq %rdx, %rax
jbe 0x16917
addl -0x70(%rbp), %ecx
movslq %ecx, %rsi
leaq -0x1b8(%rbp), %rdi
callq 0x903c
testq %rdx, (%rax)
je 0x1690d
movq %rbx, %rdi
leaq -0x6d8(%rbp), %rsi
callq 0xbe78
movl -0x6d8(%rbp), %ecx
incl %ecx
jmp 0x168c7
movq -0xa8(%rbp), %rcx
cmpq -0xb0(%rbp), %rcx
je 0x16936
testq %rax, %rax
js 0x169a5
cvtsi2ss %rax, %xmm0
jmp 0x169ba
leaq -0x4d8(%rbp), %rdi
callq 0x50a0
movq 0x1f6a7(%rip), %rdi     # 0x35ff0
leaq 0xfab1(%rip), %rsi      # 0x26401
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
movq -0xc8(%rbp), %rax
subq -0xd0(%rbp), %rax
shrq $0x5, %rax
movq -0x70(%rbp), %rcx
addl %eax, %ecx
movq %rcx, -0x70(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0xbe96
leaq -0xb0(%rbp), %rdi
callq 0xc052
leaq -0x130(%rbp), %rdi
callq 0x5778
movl -0x30(%rbp), %ebx
jmp 0x16f57
movq %rax, %rcx
shrq %rcx
andl $0x1, %eax
orq %rcx, %rax
cvtsi2ss %rax, %xmm0
addss %xmm0, %xmm0
divss 0xecae(%rip), %xmm0     # 0x25670
callq 0x5220
movq %rsp, -0x1c8(%rbp)
cvttss2si %xmm0, %ecx
leaq 0xf(%rcx), %rax
andq $-0x10, %rax
movq %rsp, %rdx
subq %rax, %rdx
movq %rdx, %rsp
movq %rcx, -0x40(%rbp)
movq %rdx, -0x58(%rbp)
leaq (%rdx,%rcx), %rax
movq %rax, -0x68(%rbp)
movslq -0x5c(%rbp), %rcx
movq %rcx, -0x1e0(%rbp)
shlq $0x5, %rcx
movq %rcx, -0x48(%rbp)
leaq -0x4d8(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x54c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x16c22
leaq -0x140(%rbp), %rax
movq %rax, -0x150(%rbp)
leaq -0x150(%rbp), %rbx
movq %rbx, %rdi
movq -0x58(%rbp), %rsi
movq -0x68(%rbp), %rdx
callq 0x7828
andq $0x0, -0x6d8(%rbp)
andl $0x0, -0x6d0(%rbp)
andq $0x0, -0x6c8(%rbp)
andl $0x0, -0x6c0(%rbp)
andq $0x0, -0x6b8(%rbp)
movq %rbx, %rdi
leaq -0x6d8(%rbp), %rsi
callq 0x7bb6
leaq -0x4d8(%rbp), %rdi
callq 0x5140
cmpl $0xa, %eax
je 0x16bf1
leaq -0x4d8(%rbp), %rdi
leaq -0x29(%rbp), %rsi
callq 0x5550
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x16bf1
leaq -0x4d8(%rbp), %rdi
movq -0x108(%rbp), %rsi
movq -0x1f8(%rbp), %rdx
callq 0x54c0
movq -0x50(%rbp), %rax
movl (%rax), %eax
addl %eax, %eax
cltd
pushq $0x3
popq %rcx
idivl %ecx
movslq %eax, %rdx
leaq -0x238(%rbp), %rax
movq %rax, -0x248(%rbp)
movq -0x108(%rbp), %rsi
addq %rsi, %rdx
movq %r12, %rdi
callq 0x7828
leaq -0x2d0(%rbp), %rdi
movq %r12, %rsi
callq 0x13d4e
movq %rax, %r15
testq %rax, %rax
je 0x16bdd
movq 0x28(%r15), %r13
movq 0x30(%r15), %rax
movq %rax, %rcx
subq %r13, %rcx
cmpq $0x4, %rcx
je 0x16bd4
movq -0x1f0(%rbp), %rcx
cmpb $0x1, (%rcx)
je 0x16bd4
jmp 0x16bdd
movb -0x29(%rbp), %r14b
leaq -0x298(%rbp), %rdi
movq %r13, %rsi
callq 0x176c8
leaq -0x290(%rbp), %rcx
cmpq %rcx, %rax
je 0x16b7b
leaq -0x29(%rbp), %rdi
callq 0x64ec
movl %eax, %r14d
movq -0x48(%rbp), %r12
xorl %ebx, %ebx
movq -0xb0(%rbp), %rax
movq -0xa8(%rbp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rbx, %rcx
jbe 0x16bcc
movslq (%rax,%rbx,4), %rsi
leaq -0x6d8(%rbp), %rdi
callq 0x903c
testq %rdx, (%rax)
je 0x16bc3
movq -0xf0(%rbp), %rax
movslq (%r13), %rcx
movq (%rax,%r12), %rax
movb %r14b, (%rax,%rcx)
incq %rbx
addq $0x20, %r12
jmp 0x16b81
addq $0x4, %r13
movq 0x30(%r15), %rax
cmpq %rax, %r13
jne 0x16b50
leaq -0x248(%rbp), %r12
movq %r12, %rdi
callq 0x5778
jmp 0x16a86
movl $0x100, %esi            # imm = 0x100
leaq -0x4d8(%rbp), %rdi
pushq $0xa
popq %rdx
callq 0x5260
leaq -0x6d8(%rbp), %rdi
callq 0x908a
leaq -0x150(%rbp), %rdi
callq 0x5778
jmp 0x16a06
xorl %ecx, %ecx
movq -0xa8(%rbp), %rbx
subq -0xb0(%rbp), %rbx
movq %rbx, %rax
sarq $0x2, %rax
movq %rax, -0x1e8(%rbp)
cmpq %rcx, %rax
movq %rcx, -0x100(%rbp)
jbe 0x16ee2
movq -0x190(%rbp), %rdx
movq -0x1e0(%rbp), %rax
addq %rcx, %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x98(%rbp)
cmpq -0x188(%rbp), %rdx
je 0x16dc1
movq -0x50(%rbp), %rax
movl (%rax), %edi
movq -0x98(%rbp), %rax
movslq %eax, %rcx
movq %rcx, -0x208(%rbp)
leal (%rax,%rdi), %ecx
movl %ecx, -0x58(%rbp)
movslq %edi, %rsi
movl %eax, %r12d
movq %rdx, -0x68(%rbp)
movl (%rdx), %ebx
movl %ebx, %eax
subl %edi, %eax
cltq
cmpq %rax, %rsi
jge 0x16da9
movq -0xf0(%rbp), %rax
movq -0x40(%rbp), %r13
shlq $0x5, %r13
movq -0x208(%rbp), %rcx
movq %rsi, -0x48(%rbp)
leaq (%rsi,%rcx), %r15
movq (%rax,%r13), %rax
movsbq (%rax,%r15), %r14
leaq 0x1f76d(%rip), %rax     # 0x36450
cmpb $0x3, (%rax,%r14)
jg 0x16d1e
movq %rdi, -0x110(%rbp)
movl %r14d, %edi
callq 0x5310
testl %eax, %eax
je 0x16d89
movq 0x10(%rbp), %rax
cmpb $0x0, (%rax)
movq -0x110(%rbp), %rdi
jne 0x16d23
movq -0x228(%rbp), %rax
cmpb (%rax,%r15), %r14b
jne 0x16d23
movl %r12d, %ebx
jmp 0x16d93
leal (%r15,%rdi), %eax
incl %eax
movq -0x98(%rbp), %rcx
addl %ecx, %ebx
cmpl %eax, %ebx
cmovgel %eax, %ebx
cmpl %ecx, %r12d
cmovlel %ecx, %r12d
movl -0x58(%rbp), %eax
subl %edi, %eax
cmpl %eax, %r12d
cmovgl %r12d, %eax
movslq %eax, %r14
movslq %ebx, %r12
cmpq %r12, %r14
jge 0x16d81
movq -0xf0(%rbp), %rax
movq (%rax,%r13), %r15
cmpb $0x2d, (%r15,%r14)
jne 0x16d7c
movq -0x228(%rbp), %rax
movsbl (%rax,%r14), %edi
callq 0x5580
movb %al, (%r15,%r14)
incq %r14
jmp 0x16d50
movq -0x50(%rbp), %rax
movl (%rax), %edi
jmp 0x16d93
movl %r12d, %ebx
movq -0x110(%rbp), %rdi
movq -0x48(%rbp), %rsi
incq %rsi
incl -0x58(%rbp)
movl %ebx, %r12d
movq -0x68(%rbp), %rdx
jmp 0x16ca4
addl -0x98(%rbp), %ebx
addq $0x4, %rdx
movl %ebx, %eax
movq %rax, -0x98(%rbp)
jmp 0x16c70
movq -0x40(%rbp), %rax
shlq $0x5, %rax
addq -0xf0(%rbp), %rax
movq %rax, -0x40(%rbp)
xorl %r14d, %r14d
movq -0x228(%rbp), %rbx
movl -0x30(%rbp), %r12d
movl $0x0, -0x48(%rbp)
cmpq %r14, -0x200(%rbp)
je 0x16e44
movq -0x40(%rbp), %rax
movq (%rax), %rax
movsbq (%rax,%r14), %rdi
xorl %r13d, %r13d
cmpq $0x2d, %rdi
sete %r15b
leaq 0x1f640(%rip), %rax     # 0x36450
cmpb $0x3, (%rax,%rdi)
jg 0x16e39
movsbq (%rbx,%r14), %rax
cmpb %al, %dil
je 0x16e39
leaq 0x1f629(%rip), %rcx     # 0x36450
cmpb $0x3, (%rcx,%rax)
jg 0x16e39
callq 0x5310
cmpl $0x1, %eax
sbbl $-0x1, -0x48(%rbp)
movb %r15b, %r13b
subl %r13d, %r12d
incq %r14
jmp 0x16de9
movq -0x100(%rbp), %r14
movq %r14, %rsi
shlq $0x5, %rsi
addq -0x90(%rbp), %rsi
movq 0x1f158(%rip), %rdi     # 0x35fb8
callq 0x53a0
movl -0x48(%rbp), %r15d
movq %rax, %rdi
leaq 0xe450(%rip), %rsi      # 0x252c3
callq 0x53f0
cvtsi2ss %r12d, %xmm0
divss -0x74(%rbp), %xmm0
mulss 0xe7e2(%rip), %xmm0     # 0x2566c
movq %rax, %rdi
callq 0x5300
movq %rax, %rdi
leaq 0xf576(%rip), %rsi      # 0x26412
callq 0x53f0
movq %rax, %rdi
movl %r15d, %esi
callq 0x56a0
movq %rax, %rdi
leaq 0xf57a(%rip), %rsi      # 0x26430
callq 0x53f0
cvtsi2ss %r15d, %xmm1
movl -0x78(%rbp), %edi
movss -0x74(%rbp), %xmm0
callq 0x76d1
leaq 0x1(%r14), %rcx
testl %eax, %eax
leaq -0x248(%rbp), %r12
je 0x16c24
jmp 0x16f21
movq -0xd0(%rbp), %r15
movq -0xc8(%rbp), %r14
leaq -0xd0(%rbp), %rdi
callq 0xbe96
leaq -0x4d8(%rbp), %rdi
callq 0x50a0
shrq $0x2, %rbx
subq %r15, %r14
shrq $0x5, %r14
addl %ebx, -0x5c(%rbp)
movq -0x70(%rbp), %rax
addl %r14d, %eax
movq %rax, -0x70(%rbp)
movl -0x30(%rbp), %ebx
movq -0x1c8(%rbp), %rsp
leaq -0xb0(%rbp), %rdi
callq 0xc052
leaq -0x130(%rbp), %rdi
callq 0x5778
movq -0x1e8(%rbp), %rax
cmpq -0x100(%rbp), %rax
ja 0x17093
movq -0x1d0(%rbp), %r14
incq %r14
jmp 0x1682a
leaq -0x6d8(%rbp), %rdi
pushq $0x10
popq %rdx
movq -0xf8(%rbp), %rsi
callq 0x5740
movq -0x6d8(%rbp), %rax
movq -0x18(%rax), %rax
testb $0x5, -0x6b8(%rbp,%rax)
je 0x1703f
movq 0x1f054(%rip), %rdi     # 0x35ff0
callq 0x51c0
leaq 0xe478(%rip), %rsi      # 0x25420
movq %rax, %rdi
callq 0x53f0
movq %rax, %rdi
movq -0xf8(%rbp), %rsi
callq 0x53a0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
pushq $0x1
popq %rax
movl %eax, -0x34(%rbp)
jmp 0x17087
movq 0x1f00f(%rip), %rdi     # 0x35ff0
callq 0x51c0
leaq 0xf3fd(%rip), %rsi      # 0x263ea
movq %rax, %rdi
callq 0x53f0
leaq -0x130(%rbp), %rsi
movq %rax, %rdi
callq 0x53a0
leaq 0xf3ed(%rip), %rsi      # 0x263f8
movq %rax, %rdi
callq 0x53f0
movq -0x50(%rbp), %rcx
movl (%rcx), %esi
movq %rax, %rdi
callq 0x56a0
movq %rax, %rdi
callq 0x51c0
movq %rax, %rdi
callq 0x51c0
leaq -0x130(%rbp), %rdi
callq 0x5778
jmp 0x17093
movq 0x18(%rbp), %rax
cmpb $0x1, (%rax)
jne 0x1713c
leaq -0x6d8(%rbp), %rdi
leaq -0x90(%rbp), %rdx
leaq -0xf0(%rbp), %rcx
movl %ebx, %esi
callq 0x15f2f
leaq -0x6d8(%rbp), %rdi
callq 0x5560
movl $0x0, -0x34(%rbp)
movq -0x1d8(%rbp), %rdi
callq 0x7657
leaq -0x6d8(%rbp), %rdi
callq 0x55c0
leaq -0xd0(%rbp), %rdi
callq 0x911e
movq -0x1c0(%rbp), %rsp
leaq -0x4d8(%rbp), %rdi
callq 0x50e0
leaq -0xf0(%rbp), %rdi
callq 0x911e
leaq -0x90(%rbp), %rdi
callq 0x911e
leaq -0x1b8(%rbp), %rdi
callq 0x908a
leaq -0x170(%rbp), %rdi
callq 0x911e
leaq -0x978(%rbp), %rdi
callq 0x6dea
leaq -0x190(%rbp), %rdi
callq 0xc052
leaq -0x228(%rbp), %rdi
callq 0x5778
leaq -0x268(%rbp), %rdi
callq 0x5778
leaq -0x298(%rbp), %rdi
callq 0x17446
leaq -0x2d0(%rbp), %rdi
callq 0x13878
movl -0x34(%rbp), %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1ee75(%rip), %rdi     # 0x35fb8
leaq 0xf30a(%rip), %rsi      # 0x26454
callq 0x53f0
movq %rax, %rdi
callq 0x51c0
xorl %r15d, %r15d
leaq 0xef36(%rip), %rbx      # 0x26097
leaq -0x6d8(%rbp), %r14
xorl %r12d, %r12d
movq -0x88(%rbp), %rax
subq -0x90(%rbp), %rax
sarq $0x5, %rax
cmpq %r12, %rax
jbe 0x17068
movq %r14, %rdi
movq %rbx, %rsi
callq 0x53f0
movq -0x90(%rbp), %rsi
addq %r15, %rsi
movq %rax, %rdi
callq 0x53a0
movq %rax, %rdi
callq 0x51c0
movq -0xf0(%rbp), %rsi
addq %r15, %rsi
movq %rax, %rdi
callq 0x53a0
movq %rax, %rdi
callq 0x51c0
incq %r12
addq $0x20, %r15
jmp 0x1716b
jmp 0x1722c
movq %rax, %rbx
jmp 0x172fc
jmp 0x1720c
jmp 0x17224
movq %rax, %rbx
jmp 0x17308
movq %rax, %rbx
leaq -0x4d8(%rbp), %rdi
callq 0x5778
jmp 0x17314
jmp 0x17246
movq %rax, %rbx
jmp 0x17320
jmp 0x17204
movq %rax, %rbx
jmp 0x1732c
movq %rax, %rbx
leaq -0x6d8(%rbp), %rdi
callq 0x55c0
jmp 0x172f0
jmp 0x17246
jmp 0x17224
movq %rax, %rbx
jmp 0x172e4
movq %rax, %rbx
jmp 0x172f0
jmp 0x17270
movq %rax, %rbx
jmp 0x17344
movq %rax, %rbx
jmp 0x17368
movq %rax, %rbx
jmp 0x17314
jmp 0x17275
jmp 0x17260
movq %rax, %rbx
jmp 0x1735c
jmp 0x17270
jmp 0x17270
jmp 0x17275
movq %rax, %rbx
jmp 0x17338
jmp 0x17270
jmp 0x17270
jmp 0x172aa
jmp 0x17275
movq %rax, %rbx
jmp 0x172d8
movq %rax, %rbx
jmp 0x1728d
jmp 0x172b1
jmp 0x172aa
movq %rax, %rbx
leaq -0x1b8(%rbp), %rdi
callq 0x5778
leaq -0x6d8(%rbp), %rdi
callq 0x5778
leaq -0x4d8(%rbp), %rdi
callq 0x5778
jmp 0x17338
movq %rax, %rbx
jmp 0x172c0
jmp 0x172b1
movq %rax, %rbx
leaq -0x248(%rbp), %rdi
callq 0x5778
leaq -0x6d8(%rbp), %rdi
callq 0x908a
leaq -0x150(%rbp), %rdi
callq 0x5778
leaq -0xb0(%rbp), %rdi
callq 0xc052
leaq -0x130(%rbp), %rdi
callq 0x5778
leaq -0xd0(%rbp), %rdi
callq 0x911e
leaq -0x4d8(%rbp), %rdi
callq 0x50e0
leaq -0xf0(%rbp), %rdi
callq 0x911e
leaq -0x90(%rbp), %rdi
callq 0x911e
leaq -0x1b8(%rbp), %rdi
callq 0x908a
leaq -0x170(%rbp), %rdi
callq 0x911e
leaq -0x978(%rbp), %rdi
callq 0x6dea
leaq -0x190(%rbp), %rdi
callq 0xc052
leaq -0x228(%rbp), %rdi
callq 0x5778
leaq -0x268(%rbp), %rdi
callq 0x5778
leaq -0x298(%rbp), %rdi
callq 0x17446
leaq -0x2d0(%rbp), %rdi
callq 0x13878
movq %rbx, %rdi
callq 0x56c0