// File: /workspace/github_cpp_makefile_star2_O3/BenLangmead[P]bowtie/./ebwt.h
// Function: void Ebwt::joinToDisk<S2bDnaString>(EList<FileBuf*, 128>&, EList<RefRecord, 128>&, EList<unsigned int, 128>&, unsigned int, RefReadInParams const&, S2bDnaString&, std::ostream&, std::ostream&, unsigned int)
void Ebwt::joinToDisk(
	EList<FileBuf*>& l,
	EList<RefRecord>& szs,
	EList<uint32_t>& plens,
	TIndexOffU sztot,
	const RefReadInParams& refparams,
	TStr& ret,
	ostream& out1,
	ostream& out2,
	uint32_t seed)
{
	RandomSource rand; // reproducible given same seed
	rand.init(seed);
	RefReadInParams rpcp = refparams;
	assert_gt(szs.size(), 0);
	assert_gt(l.size(), 0);
	assert_gt(sztot, 0);
	// Not every fragment represents a distinct sequence - many
	// fragments may correspond to a single sequence.  Count the
	// number of sequences here by counting the number of "first"
	// fragments.
	this->_nPat = 0;
	this->_nFrag = 0;
#ifdef ACCOUNT_FOR_ALL_GAP_REFS
	int nGapFrag = 0;
#endif
	for(size_t i = 0; i < szs.size(); i++) {
		if(szs[i].len > 0) this->_nFrag++;
#ifdef ACCOUNT_FOR_ALL_GAP_REFS
		if(szs[i].len == 0 && szs[i].off > 0) nGapFrag++;
		if(szs[i].first && szs[i].len > 0) this->_nPat++;
#else
		// For all records where len=0 and first=1, set first=0
		assert(szs[i].len > 0 || !szs[i].first);
		if(szs[i].first) this->_nPat++;
#endif
	}
	assert_gt(this->_nPat, 0);
	assert_geq(this->_nFrag, this->_nPat);
	this->_rstarts = NULL;
	writeU<TIndexOffU>(out1, this->_nPat, this->toBe());
	assert_eq(plens.size(), this->_nPat);
	// Allocate plen[]
	try {
		this->_plen = new TIndexOffU[this->_nPat];
	} catch(bad_alloc& e) {
		cerr << "Out of memory allocating plen[] in Ebwt::join()"
		     << " at " << __FILE__ << ":" << __LINE__ << endl;
		throw e;
	}
	// For each pattern, set plen
	for(TIndexOffU i = 0; i < plens.size(); i++) {
		this->_plen[i] = plens[i];
		writeU<TIndexOffU>(out1, this->_plen[i], this->toBe());
	}
	// Write the number of fragments
	writeU<TIndexOffU>(out1, this->_nFrag, this->toBe());
	TIndexOffU seqsRead = 0;
	ASSERT_ONLY(TIndexOffU szsi = 0);
	ASSERT_ONLY(TIndexOffU entsWritten = 0);
	TIndexOffU dstoff = 0;
	// For each filebuf
	for(unsigned int i = 0; i < l.size(); i++) {
		assert(!l[i]->eof());
		bool first = true;
		TIndexOffU patoff = 0;
		// For each *fragment* (not necessary an entire sequence) we
		// can pull out of istream l[i]...
		while(!l[i]->eof()) {
			string name;
			// Push a new name onto our vector
			_refnames.push_back("");
			//uint32_t oldRetLen = length(ret);
			RefRecord rec = fastaRefReadAppend(*l[i], first, ret, dstoff, rpcp, &_refnames.back());
#ifndef ACCOUNT_FOR_ALL_GAP_REFS
			if(rec.first && rec.len == 0) rec.first = false;
#endif
			first = false;
			if(rec.first) {
				if(_refnames.back().length() == 0) {
					// If name was empty, replace with an index
					ostringstream stm;
					stm << (_refnames.size()-1);
					_refnames.back() = stm.str();
				}
			} else {
				// This record didn't actually start a new sequence so
				// no need to add a name
				//assert_eq(0, _refnames.back().length());
				_refnames.pop_back();
			}
			assert_lt(szsi, szs.size());
			assert(szs[szsi].first == 0 || szs[szsi].first == 1);
			assert_eq(rec.off, szs[szsi].off);
			assert_eq(rec.len, szs[szsi].len);
			// szs[szsi].first == 2 sometimes?!?!  g++ is unable to do
			// the following correctly, regardless of how I write it
			//assert((rec.first == 0) == (szs[szsi].first == 0));
			assert(rec.first || rec.off > 0);
			ASSERT_ONLY(szsi++);
#ifdef ACCOUNT_FOR_ALL_GAP_REFS
			if(rec.len == 0) continue;
			if(rec.first && rec.len > 0) seqsRead++;
			assert_leq(rec.len, this->_plen[seqsRead-1]);
#else
			if(rec.first) seqsRead++;
			if(rec.len == 0) continue;
			assert_leq(rec.len, this->_plen[seqsRead-1]);
#endif
			// Reset the patoff if this is the first fragment
			if(rec.first) patoff = 0;
			patoff += rec.off; // add fragment's offset from end of last frag.
			// Adjust rpcps
			//uint32_t seq = seqsRead-1;
			ASSERT_ONLY(entsWritten++);
			// This is where rstarts elements are written to the output stream
			//writeU32(out1, oldRetLen, this->toBe()); // offset from beginning of joined string
			//writeU32(out1, seq,       this->toBe()); // sequence id
			//writeU32(out1, patoff,    this->toBe()); // offset into sequence
			patoff += rec.len;
		}
		assert_gt(szsi, 0);
		l[i]->reset();
		assert(!l[i]->eof());
		#ifndef NDEBUG
		int c = l[i]->get();
		assert_eq('>', c);
		assert(!l[i]->eof());
		l[i]->reset();
		assert(!l[i]->eof());
		#endif
	}
	assert_eq(entsWritten, this->_nFrag);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rcx, %r14
movq %rdi, %rbx
movq 0x238(%rsp), %r15
movq (%r9), %rax
movq %rax, 0x10(%rsp)
movq $0x0, 0x74(%rdi)
cmpq $0x0, 0x18(%rdx)
je 0x23702
xorl %ecx, %ecx
xorl %eax, %eax
xorl %r9d, %r9d
xorl %edi, %edi
movq 0x8(%rdx), %r8
cmpl $0x0, 0x4(%r8,%rcx)
je 0x236e6
incl %r9d
movl %r9d, 0x78(%rbx)
movq 0x8(%rdx), %r8
cmpb $0x1, 0x8(%r8,%rcx)
jne 0x236f3
incl %eax
movl %eax, 0x74(%rbx)
incq %rdi
addq $0xc, %rcx
cmpq 0x18(%rdx), %rdi
jb 0x236cf
jmp 0x23704
xorl %eax, %eax
movq %rsi, 0x18(%rsp)
movq $0x0, 0x88(%rbx)
cmpb $0x0, 0x1(%rbx)
movl %eax, %ecx
bswapl %ecx
cmovel %eax, %ecx
leaq 0x80(%rsp), %rsi
movl %ecx, (%rsi)
movl $0x4, %edx
movq %r15, %rdi
callq 0x5450
movl 0x74(%rbx), %edi
shlq $0x2, %rdi
callq 0x5030
movq %rax, 0x80(%rbx)
cmpq $0x0, 0x18(%r14)
movq %rbx, %r13
je 0x237aa
movl $0x1, %ebp
xorl %eax, %eax
leaq 0x80(%rsp), %rbx
movq 0x8(%r14), %rcx
movl (%rcx,%rax,4), %ecx
movq 0x80(%r13), %rdx
movl %ecx, (%rdx,%rax,4)
movq 0x80(%r13), %rcx
movl (%rcx,%rax,4), %eax
cmpb $0x0, 0x1(%r13)
movl %eax, %ecx
bswapl %ecx
cmovel %eax, %ecx
movl %ecx, 0x80(%rsp)
movl $0x4, %edx
movq %r15, %rdi
movq %rbx, %rsi
callq 0x5450
movl %ebp, %eax
incl %ebp
cmpq %rax, 0x18(%r14)
ja 0x23762
movl 0x78(%r13), %eax
cmpb $0x0, 0x1(%r13)
movl %eax, %ecx
bswapl %ecx
cmovel %eax, %ecx
leaq 0x80(%rsp), %rsi
movl %ecx, (%rsi)
movl $0x4, %edx
movq %r15, %rdi
callq 0x5450
movq 0x18(%rsp), %r14
cmpq $0x0, 0x18(%r14)
je 0x23c81
leaq 0x90(%rsp), %r15
leaq 0xc8(%r13), %rax
movq %rax, 0x58(%rsp)
movl 0x10(%rsp), %eax
movq %rax, 0x50(%rsp)
xorl %ebp, %ebp
leaq 0x80(%rsp), %r12
movq $0x0, 0x8(%rsp)
movq %r13, 0x20(%rsp)
movq 0x8(%r14), %rax
movq (%rax,%rbp,8), %rdi
movq 0x20(%rdi), %rax
cmpq 0x28(%rdi), %rax
jne 0x23848
testb $0x1, 0x30(%rdi)
je 0x23848
callq 0x170d8
leaq 0x80(%rsp), %r12
incl %ebp
cmpq %rbp, 0x18(%r14)
ja 0x23816
jmp 0x23c81
movb $0x1, %bl
movq %rbp, 0x28(%rsp)
leaq 0x40(%rsp), %rax
movq %rax, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
movb $0x0, 0x40(%rsp)
movq %r15, 0x80(%rsp)
movq %r12, %rdi
leaq 0xd8d3(%rip), %rdx      # 0x3114c
movq %rdx, %rsi
callq 0x10400
movq 0x58(%rsp), %rdi
movq %r12, %rsi
callq 0xefb8
movq 0x80(%rsp), %rdi
cmpq %r15, %rdi
je 0x238ab
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x5370
movq 0x8(%r14), %rax
movq (%rax,%rbp,8), %r15
movq 0xd0(%r13), %r14
movq 0xe0(%r13), %rbp
testb $0x1, %bl
je 0x238ed
movq %r15, %rdi
callq 0x1714a
cmpl $0x3e, %eax
jne 0x23c93
movl $0x3e, 0x188a7(%rip)    # 0x3c188
movl 0x8(%rsp), %r12d
movl $0x3e, %eax
jmp 0x23906
movl 0x18895(%rip), %eax     # 0x3c188
movl 0x8(%rsp), %r12d
cmpl $0x3e, %eax
je 0x23906
cmpl $0x23, %eax
jne 0x2398e
shlq $0x5, %rbp
addq %r14, %rbp
addq $-0x20, %rbp
cmpl $0x23, %eax
jne 0x23928
movq %r15, %rdi
callq 0x17174
cmpl $-0x1, %eax
jne 0x23911
jmp 0x23c75
movq %r15, %rdi
callq 0x171b0
cmpl $-0x1, %eax
je 0x2397d
cmpl $0xd, %eax
je 0x23959
cmpl $0xa, %eax
je 0x23959
testq %rbp, %rbp
je 0x23928
movsbl %al, %esi
movq %rbp, %rdi
callq 0x52b0
jmp 0x23928
movq %r15, %rdi
callq 0x171b0
cmpl $0xc, %eax
jle 0x23965
cmpl $0xd, %eax
je 0x23951
jmp 0x2396c
cmpl $0xa, %eax
je 0x23951
jmp 0x23978
cmpl $0x23, %eax
je 0x23911
cmpl $0x3e, %eax
je 0x23911
jmp 0x23986
cmpl $-0x1, %eax
jne 0x23986
movb $0x1, %bl
xorl %ebp, %ebp
jmp 0x23afa
movb $0x1, %cl
movl %ecx, 0x4(%rsp)
jmp 0x23996
movl $0x0, 0x4(%rsp)
movq 0x10(%rsp), %rcx
movq %rcx, %r13
shrq $0x28, %r13
shrq $0x20, %rcx
cltq
leaq 0x19811(%rip), %rdx     # 0x3d1c0
movb (%rdx,%rax), %r14b
cmpb $0x2, %r14b
movl %eax, %ebp
movl $0x41, %edx
cmovel %edx, %ebp
testb $0x1, %cl
cmovel %eax, %ebp
movl %ebp, %edi
callq 0x56c0
cmpl $0x43, %eax
movl %ebp, %ebx
movl $0x54, %eax
cmovel %eax, %ebx
testb $0x1, %r13b
cmovel %ebp, %ebx
cmpb $0x1, %r14b
je 0x23a0e
cmpb $0x2, %r14b
je 0x239f7
cmpl $0x3e, %ebx
je 0x23aef
movq %r15, %rdi
callq 0x171b0
cmpl $-0x1, %eax
jne 0x23996
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x23af4
xorl %ebp, %ebp
movslq %ebx, %rax
leaq 0x19da6(%rip), %rcx     # 0x3d7c0
cmpb $0x1, (%rcx,%rax)
jne 0x23a6f
incq %rbp
leaq 0x19b96(%rip), %rcx     # 0x3d5c0
movzbl (%rcx,%rax), %eax
movq 0x8(%rsp), %r10
leal 0x1(%r10), %edx
movq 0x230(%rsp), %r9
movq 0x8(%r9), %rsi
movl %r10d, %edi
shrl $0x4, %edi
addl %r10d, %r10d
movl $0x3, %r8d
movl %r10d, %ecx
shll %cl, %r8d
notl %r8d
andl %r8d, (%rsi,%rdi,4)
movq 0x8(%r9), %rsi
shll %cl, %eax
orl %eax, (%rsi,%rdi,4)
movl %edx, %eax
movq %rax, 0x8(%rsp)
movq %r15, %rdi
callq 0x171b0
btq $0x20, 0x10(%rsp)
jae 0x23a95
movslq %eax, %rcx
movl $0x41, %ebx
leaq 0x19731(%rip), %rdx     # 0x3d1c0
cmpb $0x2, (%rdx,%rcx)
je 0x23ab4
movl %eax, %ecx
incl %ecx
cmpl $0x3f, %ecx
ja 0x23ab2
movabsq $-0x7fffffefffffffff, %rdx # imm = 0x8000001000000001
btq %rcx, %rdx
movl %eax, %ebx
jae 0x23ab4
jmp 0x23af6
movl %eax, %ebx
movslq %ebx, %rax
leaq 0x19702(%rip), %rcx     # 0x3d1c0
cmpb $0x2, (%rcx,%rax)
je 0x23c6e
btq $0x28, 0x10(%rsp)
jae 0x23a10
movl %ebx, %edi
callq 0x56c0
movl $0x54, %ecx
cmpl $0x43, %eax
je 0x23ae8
movl %ebx, %ecx
movl %ecx, %ebx
jmp 0x23a10
movl $0x3e, %eax
xorl %ebp, %ebp
movl 0x4(%rsp), %ebx
movl %eax, 0x18688(%rip)     # 0x3c188
cmpl $0x2, 0x50(%rsp)
jne 0x23b1a
movq 0x230(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x25434
testb %bl, %bl
je 0x23bfd
movl %ebp, %eax
testq %rax, %rax
je 0x23bfd
movq 0x20(%rsp), %r13
movq 0xd0(%r13), %rax
movq 0xe0(%r13), %rcx
shlq $0x5, %rcx
cmpq $0x0, -0x18(%rax,%rcx)
movq 0x18(%rsp), %r14
leaq 0x90(%rsp), %r15
movq 0x28(%rsp), %rbp
leaq 0x80(%rsp), %r12
jne 0x23c23
movq %r12, %rdi
callq 0x5480
movq 0xe0(%r13), %rsi
decq %rsi
movq %r12, %rdi
callq 0x51e0
leaq 0x60(%rsp), %rbx
movq %rbx, %rdi
leaq 0x88(%rsp), %rsi
callq 0x5580
movq 0xd0(%r13), %rax
movq 0xe0(%r13), %rcx
shlq $0x5, %rcx
leaq (%rax,%rcx), %rdi
addq $-0x20, %rdi
movq %rbx, %rsi
callq 0x5420
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x23bd7
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x5370
leaq 0x80(%rsp), %r12
movq %r12, %rdi
movq 0x173af(%rip), %rsi     # 0x3af98
callq 0x5190
leaq 0xf0(%rsp), %rdi
callq 0x5130
jmp 0x23c23
movq 0x20(%rsp), %r13
decq 0xe0(%r13)
movq 0x18(%rsp), %r14
leaq 0x90(%rsp), %r15
movq 0x28(%rsp), %rbp
leaq 0x80(%rsp), %r12
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x23c47
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5370
leaq 0x80(%rsp), %r12
movq 0x8(%r14), %rax
movq (%rax,%rbp,8), %rdi
movq 0x20(%rdi), %rax
xorl %ebx, %ebx
cmpq 0x28(%rdi), %rax
jne 0x2384f
testb $0x1, 0x30(%rdi)
je 0x2384f
jmp 0x2382e
movl %ebx, %eax
jmp 0x23af6
movb $0x1, %bl
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x2397f
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x17356(%rip), %rdi     # 0x3aff0
leaq 0xc8b2(%rip), %rsi      # 0x30553
movl $0x2f, %edx
callq 0x53f0
movq 0x1733e(%rip), %rdi     # 0x3aff0
callq 0x5170
movl $0x4, %edi
callq 0x5180
movl $0x1, (%rax)
movq 0x1727a(%rip), %rsi     # 0x3af48
movq %rax, %rdi
xorl %edx, %edx
callq 0x5630
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x23e21
movq %rbx, %rdi
callq 0x5140
movq 0x172fd(%rip), %rdi     # 0x3aff0
leaq 0xc829(%rip), %rsi      # 0x30523
movl $0x2f, %edx
callq 0x53f0
movq 0x172e5(%rip), %rdi     # 0x3aff0
leaq 0xcffd(%rip), %rsi      # 0x30d0f
movl $0x4, %edx
callq 0x53f0
movq 0x172cd(%rip), %rdi     # 0x3aff0
leaq 0xa49d(%rip), %rsi      # 0x2e1c7
movl $0x8, %edx
callq 0x53f0
movq 0x172b5(%rip), %rdi     # 0x3aff0
leaq 0xb210(%rip), %rsi      # 0x2ef52
movl $0x1, %edx
callq 0x53f0
movq 0x1729d(%rip), %rdi     # 0x3aff0
movl $0xf20, %esi            # imm = 0xF20
callq 0x5660
movq %rax, %rdi
callq 0x5170
movl $0x8, %edi
callq 0x5180
movq 0x1721a(%rip), %rcx     # 0x3af90
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x17224(%rip), %rsi     # 0x3afa8
movq 0x171ed(%rip), %rdx     # 0x3af78
movq %rax, %rdi
callq 0x5630
movq %rax, %rbx
callq 0x55b0
jmp 0x23e21
movq %rax, %rdi
callq 0xdbdd
jmp 0x23daa
movq %rax, %rbx
movq 0x171e4(%rip), %rsi     # 0x3af98
leaq 0x80(%rsp), %rdi
callq 0x5190
leaq 0xf0(%rsp), %rdi
callq 0x5130
jmp 0x23e05
jmp 0x23e02
jmp 0x23e02
jmp 0x23e02
jmp 0x23e02
movq %rax, %rbx
movq 0x80(%rsp), %rdi
cmpq %r15, %rdi
je 0x23e05
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x5370
jmp 0x23e05
jmp 0x23e02
jmp 0x23e02
jmp 0x23e02
jmp 0x23e02
movq %rax, %rbx
movq 0x30(%rsp), %rdi
leaq 0x40(%rsp), %rax
cmpq %rax, %rdi
je 0x23e21
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x5370
movq %rbx, %rdi
callq 0x5680
nop