// File: /workspace/github_cpp_makefile_star2_O2/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, %r12d
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
testq %rcx, %rcx
je 0x3926
movq (%rbx), %rdx
movq 0x18(%r14), %rax
leaq 0x1de2(%rip), %rsi      # 0x56fb
movq %rcx, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x22c0
leaq 0x1bb0(%rip), %rsi      # 0x54dd
movq %rbx, %rdi
callq 0x334f
testb %al, %al
je 0x394a
movq 0x4670(%rip), %rax      # 0x7fb0
movq (%rax), %rax
movq %rax, 0x8(%rsp)
jmp 0x398c
movq (%rbx), %rdi
leaq 0x1b8b(%rip), %rsi      # 0x54df
callq 0x2210
movq %rax, %r13
leaq 0xe0(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x22a0
movq %r13, 0x8(%rsp)
movq %r13, %rdi
movq %r15, %rsi
callq 0x2626
leaq 0xe0(%rsp), %rdi
callq 0x20d0
movl %r12d, 0x14(%rsp)
leaq 0x40(%rsp), %rdi
movl %ebp, %esi
callq 0x4a34
leaq 0x100(%rsp), %rsi
movl $0x54474341, (%rsi)     # imm = 0x54474341
leaq 0xa0(%rsp), %rdi
pushq $0x4
popq %rdx
leaq 0x20(%rsp), %rcx
callq 0x4a78
movslq %ebp, %r13
pushq $0x1
popq %rax
movl %eax, (%rsp)
leaq 0xc0(%rsp), %rbx
leaq 0xb8(%rsp), %r12
cmpq $0x0, 0x18(%r14)
je 0x3bec
movq 0x10(%r14), %rax
movq 0x8(%rax), %rax
movq %rax, 0x18(%rsp)
movq %r14, %rdi
leaq 0x18(%rsp), %rsi
callq 0x49e6
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
andq $0x0, 0x28(%rsp)
movb $0x0, 0x30(%rsp)
movq 0x18(%rsp), %rdi
movl %ebp, %esi
leaq 0x20(%rsp), %rdx
callq 0x4aad
movq 0x20(%rsp), %rsi
leaq 0x40(%rsp), %rdi
callq 0x4b04
xorl %r15d, %r15d
cmpl $0x2, %r15d
je 0x3b70
testl %r15d, %r15d
je 0x3a4f
leaq 0x20(%rsp), %rdi
callq 0x26b9
movl %r15d, 0x4(%rsp)
movq 0x20(%rsp), %rsi
leaq 0x70(%rsp), %rdi
movq %r13, %rdx
callq 0x20f0
movb $0x0, 0x70(%rsp,%r13)
xorl %eax, %eax
cmpl %ebp, %eax
jge 0x3a82
movl %eax, %ecx
leaq 0x1(%rcx), %rax
movb 0x71(%rsp,%rcx), %dl
movb %dl, 0x70(%rsp,%rcx)
jmp 0x3a6e
movb $0x0, 0x70(%rsp,%r13)
movq 0xa0(%rsp), %rbx
movq 0xa8(%rsp), %r15
cmpq %r15, %rbx
je 0x3b5b
movb (%rbx), %al
movb %al, 0x6f(%rsp,%r13)
leaq 0x70(%rsp), %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x41fd
movq %r14, %rdi
movq %r12, %rsi
callq 0x4d1a
testq %rax, %rax
jne 0x3acc
incq %rbx
jmp 0x3a98
movsbq (%rbx), %rax
leaq 0x1569(%rip), %rcx      # 0x5040
movzbl (%rax,%rcx), %eax
cmpl $0x0, 0x4(%rsp)
je 0x3b07
cmpl $0x4, %eax
je 0x3b2f
movq 0x50(%rsp), %rcx
leaq -0x1(%rcx), %rdx
movq %rdx, 0x50(%rsp)
leaq 0x19ce(%rip), %rdx      # 0x54ca
subq %rax, %rdx
movb 0x3(%rdx), %al
movb %al, -0x1(%rcx)
jmp 0x3b2f
cmpl $0x4, %eax
je 0x3b2f
leaq 0x19b7(%rip), %rcx      # 0x54ca
movb (%rax,%rcx), %al
movq 0x58(%rsp), %rcx
movb %al, (%rcx)
movq 0x58(%rsp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x58(%rsp)
movb $0x0, 0x1(%rax)
movq %r14, %rdi
movq %r12, %rsi
callq 0x49e6
movq 0x58(%rsp), %rax
cmpq 0x68(%rsp), %rax
jae 0x3b5b
movq 0x60(%rsp), %rcx
movl $0x0, %eax
cmpq %rcx, 0x50(%rsp)
ja 0x3a6e
movl 0x4(%rsp), %r15d
incl %r15d
leaq 0xc0(%rsp), %rbx
jmp 0x3a36
leaq 0x100(%rsp), %rdi
callq 0x2150
leaq 0x110(%rsp), %rdi
leaq 0x1c2d(%rip), %rsi      # 0x57b9
callq 0x21c0
movq %rax, %rdi
movl (%rsp), %esi
callq 0x2350
movq %rbx, %rdi
leaq 0x118(%rsp), %rsi
callq 0x2300
movq 0xc0(%rsp), %rdx
leaq 0x40(%rsp), %rdi
movq 0x8(%rsp), %rsi
xorl %ecx, %ecx
callq 0x4bb6
incl (%rsp)
movq %rbx, %rdi
callq 0x20d0
leaq 0x100(%rsp), %rdi
callq 0x2170
leaq 0x20(%rsp), %rdi
callq 0x20d0
jmp 0x39d9
movq 0x8(%rsp), %rdi
callq 0x21d0
cmpb $0x0, 0x14(%rsp)
je 0x3c32
movq 0x43ec(%rip), %rdi      # 0x7ff0
leaq 0x1bb0(%rip), %rsi      # 0x57bb
callq 0x21c0
movq %rax, %rdi
movl (%rsp), %esi
callq 0x2350
leaq 0x1bb0(%rip), %rsi      # 0x57d2
movq %rax, %rdi
callq 0x21c0
movq %rax, %rdi
callq 0x20e0
leaq 0xa0(%rsp), %rdi
callq 0x4cb0
leaq 0x40(%rsp), %rdi
callq 0x4c4a
xorl %eax, %eax
addq $0x288, %rsp            # imm = 0x288
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0x20d0
jmp 0x3cbb
movq %rax, %rbx
jmp 0x3cb1
jmp 0x3c7a
jmp 0x3c7f
jmp 0x3c97
movq %rax, %rbx
jmp 0x3ca4
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x2170
jmp 0x3c9a
jmp 0x3c97
jmp 0x3c97
jmp 0x3c97
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x20d0
leaq 0xa0(%rsp), %rdi
callq 0x4cb0
leaq 0x40(%rsp), %rdi
callq 0x4c4a
movq %rbx, %rdi
callq 0x2360
nop