// File: /workspace/github_cpp_makefile_star2_O3/prophyle[P]prophasm/src/prophasm.cpp
// Function: int assemble<std::unordered_set<unsigned long, std::hash<unsigned long>, std::equal_to<unsigned long>, std::allocator<unsigned long>>>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::unordered_set<unsigned long, std::hash<unsigned long>, std::equal_to<unsigned long>, std::allocator<unsigned long>>&, int, _IO_FILE*, bool)
int assemble(const std::string &fasta_fn, _set_T &set, int32_t k, FILE* fstats, bool verbose){
	if(fstats){
		fprintf(fstats,"%s\t%lu\n",fasta_fn.c_str(),set.size());
	}

	FILE *file=nullptr;
	if(fasta_fn=="-"){
		file=stdout;
	}
	else{
		file=fopen(fasta_fn.c_str(),"w+");
		test_file(file, fasta_fn);
	}
	char kmer_str[max_allowed_kmer_length+1];
	contig_t contig(k);
	const std::vector<char> nucls = {'A','C','G','T'};

	//int32_t i=0;
	int32_t contig_id=1;
	while(set.size()>0){

		const auto central_nkmer=*(set.begin());
		set.erase(central_nkmer);

		std::string central_kmer_string;
		decode_kmer(central_nkmer,k,central_kmer_string);
		contig.new_contig(central_kmer_string.c_str());

		typename _set_T::value_type nkmer;


		for (int direction=0;direction<2;direction++){

			//std::cerr << "direction " << direction << std::endl;

			if (direction==0){
				// forward
			}
			else{
				// reverse
				reverse_complement_in_place(central_kmer_string);
			}

			strncpy(kmer_str,central_kmer_string.c_str(),k);
			kmer_str[k]='\0';

			bool extending = true;

			//std::cerr << "central k-mer: " << central_kmer_string << std::endl;


			while (extending){
				for(int32_t i=0;i<k;i++){
					kmer_str[i]=kmer_str[i+1];
				}
				kmer_str[k]='\0';

				extending=false;
				for(const char &c : nucls){
					kmer_str[k-1]=c;

					encode_canonical(kmer_str, k, nkmer);

					if(set.count( nkmer )){
						//std::cerr << "extending " << c << std::endl;
						//debug_print_kmer_set(set,k);
						//std::cerr << std::string(contig.l_ext) << c << std::endl;
						//std::cerr << std::endl;
						if(direction==0){
							contig.r_extend(c);
						}
						else{
							contig.l_extend(c);
						}
						set.erase(nkmer);

						if(!contig.is_full()){
							extending=true;
						}
						break;
					}
				}
			}
		}

		//std::cerr << "====================" << std::endl;

		std::stringstream ss;
		ss<<"c"<<contig_id;
		const std::string contig_name(ss.str());
		contig.print_to_fasta(file,contig_name.c_str());
		contig_id++;
	}

	fclose(file);

	if(verbose){
		std::cerr << "   assembly finished (" << contig_id << " contigs)" << std::endl;
	}

	return 0;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x288, %rsp            # imm = 0x288
movl %r8d, %ebp
movl %edx, %r14d
movq %rsi, %r15
movq %rdi, %rbx
testq %rcx, %rcx
je 0x4104
movq (%rbx), %rdx
movq 0x18(%r15), %rax
leaq 0x2604(%rip), %rsi      # 0x66fb
movq %rcx, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x22e0
leaq 0x23d2(%rip), %rsi      # 0x64dd
movq %rbx, %rdi
callq 0x2090
testl %eax, %eax
je 0x417f
movq (%rbx), %rdi
leaq 0x23be(%rip), %rsi      # 0x64df
callq 0x2260
movq %rax, %r13
leaq 0xc8(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0xb8(%rsp), %rbx
movq %rbx, %rdi
callq 0x470a
movq %r13, 0x10(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2734
movq 0xb8(%rsp), %rdi
cmpq %r12, %rdi
je 0x418e
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x418e
movq 0x4e22(%rip), %rax      # 0x8fa8
movq (%rax), %rax
movq %rax, 0x10(%rsp)
movl %r14d, 0x48(%rsp)
leal 0x1312d01(%r14), %eax
movslq %eax, %rbx
movq %rbx, %rdi
callq 0x2030
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2130
movq %r12, 0x50(%rsp)
movq %r12, 0x68(%rsp)
addq $0x1312d00, %r12        # imm = 0x1312D00
movq %r12, 0x70(%rsp)
leaq 0x100(%rsp), %rsi
movl $0x54474341, (%rsi)     # imm = 0x54474341
leaq 0x78(%rsp), %rdi
leaq 0x20(%rsp), %rcx
movl $0x4, %edx
callq 0x5186
movl %ebp, 0x18(%rsp)
cmpq $0x0, 0x18(%r15)
je 0x447d
movslq %r14d, %rbx
movl $0x1, %r12d
leaq 0xf8(%rsp), %rbp
movq 0x10(%r15), %rax
movq 0x8(%rax), %rax
movq %rax, 0x40(%rsp)
movq %r15, %rdi
leaq 0x40(%rsp), %rsi
callq 0x5124
movl %r12d, 0x1c(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movb $0x0, 0x30(%rsp)
movq 0x40(%rsp), %rdi
movl %r14d, %esi
leaq 0x20(%rsp), %rdx
callq 0x51ca
movq 0x20(%rsp), %rsi
leaq 0x48(%rsp), %rdi
callq 0x5222
movb $0x1, %al
movl %eax, 0xc(%rsp)
testb $0x1, %al
jne 0x4279
leaq 0x20(%rsp), %rdi
callq 0x27c9
movq 0x20(%rsp), %rsi
leaq 0x90(%rsp), %rdi
movq %rbx, %rdx
callq 0x2110
movb $0x0, 0x90(%rsp,%rbx)
xorl %eax, %eax
cmpl %r14d, %eax
jge 0x42b3
movl %eax, %ecx
leaq 0x1(%rcx), %rax
movb 0x91(%rsp,%rcx), %dl
movb %dl, 0x90(%rsp,%rcx)
jmp 0x4298
movb $0x0, 0x90(%rsp,%rbx)
movq 0x78(%rsp), %r12
movq 0x80(%rsp), %r13
cmpq %r13, %r12
je 0x4394
movb (%r12), %al
movb %al, 0x8f(%rsp,%rbx)
leaq 0x90(%rsp), %rdi
movl %r14d, %esi
movq %rbp, %rdx
callq 0x4ac7
movq %r15, %rdi
movq %rbp, %rsi
callq 0x5010
testq %rax, %rax
jne 0x4304
incq %r12
jmp 0x42c8
movsbq (%r12), %rax
leaq 0x1d30(%rip), %rcx      # 0x6040
movzbl (%rax,%rcx), %eax
testb $0x1, 0xc(%rsp)
je 0x4345
cmpl $0x4, %eax
je 0x4368
leaq 0x21a3(%rip), %rcx      # 0x64ca
movb (%rax,%rcx), %al
movq 0x60(%rsp), %rcx
movb %al, (%rcx)
movq 0x60(%rsp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x60(%rsp)
movb $0x0, 0x1(%rax)
jmp 0x4368
cmpl $0x4, %eax
je 0x4368
movq 0x58(%rsp), %rcx
leaq -0x1(%rcx), %rdx
movq %rdx, 0x58(%rsp)
leaq 0x216b(%rip), %rdx      # 0x64ca
subq %rax, %rdx
movb 0x3(%rdx), %al
movb %al, -0x1(%rcx)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x5124
movq 0x60(%rsp), %rax
cmpq 0x70(%rsp), %rax
jae 0x4394
movq 0x68(%rsp), %rcx
movl $0x0, %eax
cmpq %rcx, 0x58(%rsp)
ja 0x4298
testb $0x1, 0xc(%rsp)
movl $0x0, %eax
jne 0x4267
leaq 0x100(%rsp), %rdi
callq 0x2180
movl $0x1, %edx
leaq 0x110(%rsp), %r13
movq %r13, %rdi
leaq 0x23f1(%rip), %rsi      # 0x67b9
callq 0x2250
movl 0x1c(%rsp), %r12d
movq %r13, %rdi
movl %r12d, %esi
callq 0x2370
leaq 0xd8(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x2320
movq 0xd8(%rsp), %rdx
leaq 0x48(%rsp), %rdi
movq 0x10(%rsp), %rsi
xorl %ecx, %ecx
callq 0x52cc
movq 0xd8(%rsp), %rdi
leaq 0xe8(%rsp), %rax
cmpq %rax, %rdi
je 0x4430
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x2230
leaq 0x100(%rsp), %rdi
movq 0x4b71(%rip), %rsi      # 0x8fb0
callq 0x21a0
leaq 0x180(%rsp), %rdi
callq 0x20b0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x446d
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2230
incl %r12d
cmpq $0x0, 0x18(%r15)
jne 0x420d
jmp 0x4483
movl $0x1, %r12d
movq 0x10(%rsp), %rdi
callq 0x2210
cmpb $0x0, 0x18(%rsp)
je 0x44f9
movq 0x4b55(%rip), %rdi      # 0x8ff0
leaq 0x2319(%rip), %rsi      # 0x67bb
movl $0x16, %edx
callq 0x2250
movq 0x4b3d(%rip), %rdi      # 0x8ff0
movl %r12d, %esi
callq 0x2370
movq %rax, %rbx
leaq 0x230d(%rip), %rsi      # 0x67d2
movl $0x9, %edx
movq %rax, %rdi
callq 0x2250
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21f0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2170
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x4513
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x4522
callq 0x22b0
xorl %eax, %eax
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
cmpq %r12, %rdi
je 0x45e1
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x45e1
movq %rax, %rbx
jmp 0x45d2
jmp 0x456a
jmp 0x456f
jmp 0x4599
movq %rax, %rbx
jmp 0x45b8
movq %rax, %rbx
movq 0x4a37(%rip), %rsi      # 0x8fb0
leaq 0x100(%rsp), %rdi
callq 0x21a0
leaq 0x180(%rsp), %rdi
callq 0x20b0
jmp 0x459c
jmp 0x4599
jmp 0x4599
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x45b8
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x45d2
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x45e1
callq 0x22b0
movq %rbx, %rdi
callq 0x2380
nop