// File: /workspace/github_cpp_makefile_star2_O1/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 $0x298, %rsp            # imm = 0x298
movl %r8d, %ebp
movq %rdx, (%rsp)
movq %rsi, %r15
movq %rdi, %rbx
testq %rcx, %rcx
je 0x4117
movq (%rbx), %rdx
movq 0x18(%r15), %rax
leaq 0x25f1(%rip), %rsi      # 0x66fb
movq %rcx, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq 0x22e0
leaq 0x23bf(%rip), %rsi      # 0x64dd
movq %rbx, %rdi
callq 0x2090
testl %eax, %eax
movl %ebp, 0x18(%rsp)
je 0x4195
movq (%rbx), %rdi
leaq 0x23a7(%rip), %rsi      # 0x64df
callq 0x2260
movq %rax, %r12
leaq 0xd8(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x4770
movq %r12, 0x8(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x2734
movq 0xc8(%rsp), %rdi
cmpq %r14, %rdi
je 0x41a4
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x41a4
movq 0x4e0c(%rip), %rax      # 0x8fa8
movq (%rax), %rax
movq %rax, 0x8(%rsp)
movq (%rsp), %rax
movl %eax, 0x50(%rsp)
addl $0x1312d01, %eax        # imm = 0x1312D01
movslq %eax, %rbx
movq %rbx, %rdi
callq 0x2030
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x2130
movq %r12, 0x58(%rsp)
movq %r12, 0x70(%rsp)
addq $0x1312d00, %r12        # imm = 0x1312D00
movq %r12, 0x78(%rsp)
leaq 0x110(%rsp), %rsi
movl $0x54474341, (%rsi)     # imm = 0x54474341
leaq 0x80(%rsp), %rdi
leaq 0x20(%rsp), %rcx
movl $0x4, %edx
callq 0x51d0
cmpq $0x0, 0x18(%r15)
je 0x44ea
movq (%rsp), %rax
movslq %eax, %rcx
movq %rcx, 0x10(%rsp)
movl %eax, %eax
movq %rax, 0x98(%rsp)
movl $0x1, %ebx
leaq 0x50(%rsp), %r13
leaq 0x108(%rsp), %r12
movq 0x10(%r15), %rax
movq 0x8(%rax), %rax
movq %rax, 0x48(%rsp)
movq %r15, %rdi
leaq 0x48(%rsp), %rsi
callq 0x5180
movl %ebx, 0x1c(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movb $0x0, 0x30(%rsp)
movq 0x48(%rsp), %rdi
movq (%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x5214
movq 0x20(%rsp), %rsi
movq %r13, %rdi
callq 0x5268
xorl %eax, %eax
movq %rax, 0x40(%rsp)
testl %eax, %eax
je 0x42a5
leaq 0x20(%rsp), %rdi
callq 0x27c9
movq 0x20(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rdx
callq 0x2110
movb $0x0, 0xa0(%rsp,%rbx)
cmpl $0x0, (%rsp)
jle 0x42ea
leaq 0xa0(%rsp), %rdi
leaq 0xa1(%rsp), %rsi
movq 0x98(%rsp), %rdx
callq 0x2330
movq 0x10(%rsp), %rax
movb $0x0, 0xa0(%rsp,%rax)
movq 0x80(%rsp), %r13
movq 0x88(%rsp), %r14
cmpq %r14, %r13
je 0x4401
incq %r13
xorl %ebp, %ebp
movb -0x1(%r13), %al
movq 0x10(%rsp), %rcx
movb %al, 0x9f(%rsp,%rcx)
leaq 0xa0(%rsp), %rdi
movq (%rsp), %rsi
movq %r12, %rdx
callq 0x4b19
movq %r15, %rdi
movq %r12, %rsi
callq 0x540e
movq %rax, %rbx
testq %rax, %rax
je 0x43e2
movsbq -0x1(%r13), %rax
leaq 0x1ce4(%rip), %rcx      # 0x6040
movzbl (%rax,%rcx), %eax
cmpl $0x0, 0x40(%rsp)
je 0x438c
cmpl $0x4, %eax
je 0x43b4
movq 0x60(%rsp), %rcx
leaq -0x1(%rcx), %rdx
movq %rdx, 0x60(%rsp)
leaq 0x2149(%rip), %rdx      # 0x64ca
subq %rax, %rdx
movb 0x3(%rdx), %al
movb %al, -0x1(%rcx)
jmp 0x43b4
cmpl $0x4, %eax
je 0x43b4
leaq 0x2132(%rip), %rcx      # 0x64ca
movb (%rax,%rcx), %al
movq 0x68(%rsp), %rcx
movb %al, (%rcx)
movq 0x68(%rsp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x68(%rsp)
movb $0x0, 0x1(%rax)
movq %r15, %rdi
movq %r12, %rsi
callq 0x5180
movq 0x68(%rsp), %rax
movq 0x70(%rsp), %rcx
cmpq %rcx, 0x60(%rsp)
movzbl %bpl, %ecx
movl $0x1, %ebp
cmovbel %ecx, %ebp
cmpq 0x78(%rsp), %rax
cmovael %ecx, %ebp
testq %rbx, %rbx
jne 0x43f7
leaq 0x1(%r13), %rax
cmpq %r14, %r13
movq %rax, %r13
jne 0x4315
testb $0x1, %bpl
jne 0x42c7
movq 0x40(%rsp), %rcx
leal 0x1(%rcx), %eax
testl %ecx, %ecx
je 0x4292
leaq 0x110(%rsp), %rdi
callq 0x2180
movl $0x1, %edx
leaq 0x120(%rsp), %r14
movq %r14, %rdi
leaq 0x2384(%rip), %rsi      # 0x67b9
callq 0x2250
movl 0x1c(%rsp), %ebx
leaq 0x50(%rsp), %r13
movq %r14, %rdi
movl %ebx, %esi
callq 0x2370
leaq 0xe8(%rsp), %rdi
leaq 0x128(%rsp), %rsi
callq 0x2320
movq 0xe8(%rsp), %rdx
movq %r13, %rdi
movq 0x8(%rsp), %rsi
xorl %ecx, %ecx
callq 0x530e
movq 0xe8(%rsp), %rdi
leaq 0xf8(%rsp), %rax
cmpq %rax, %rdi
je 0x449e
movq 0xf8(%rsp), %rsi
incq %rsi
callq 0x2230
leaq 0x110(%rsp), %rdi
movq 0x4b03(%rip), %rsi      # 0x8fb0
callq 0x21a0
leaq 0x190(%rsp), %rdi
callq 0x20b0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x44db
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2230
incl %ebx
cmpq $0x0, 0x18(%r15)
jne 0x423a
jmp 0x44ef
movl $0x1, %ebx
movq 0x8(%rsp), %rdi
callq 0x2210
cmpb $0x0, 0x18(%rsp)
je 0x4564
movq 0x4ae9(%rip), %rdi      # 0x8ff0
leaq 0x22ad(%rip), %rsi      # 0x67bb
movl $0x16, %edx
callq 0x2250
movq 0x4ad1(%rip), %rdi      # 0x8ff0
movl %ebx, %esi
callq 0x2370
movq %rax, %rbx
leaq 0x22a2(%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 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x4581
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4590
callq 0x22b0
xorl %eax, %eax
addq $0x298, %rsp            # imm = 0x298
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
cmpq %r14, %rdi
je 0x4650
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x2230
jmp 0x4650
movq %rax, %rbx
jmp 0x4641
jmp 0x45d8
jmp 0x45dd
jmp 0x4605
movq %rax, %rbx
jmp 0x4624
movq %rax, %rbx
movq 0x49c9(%rip), %rsi      # 0x8fb0
leaq 0x110(%rsp), %rdi
callq 0x21a0
leaq 0x190(%rsp), %rdi
callq 0x20b0
jmp 0x4608
jmp 0x4605
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x4624
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x2230
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x4641
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
movq 0x58(%rsp), %rdi
testq %rdi, %rdi
je 0x4650
callq 0x22b0
movq %rbx, %rdi
callq 0x2380