// File: /workspace/github_cpp_makefile_star2_O1/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 $0x1e8, %rsp            # imm = 0x1E8
movq %rcx, %r14
movq %rsi, 0x10(%rsp)
movq 0x228(%rsp), %rbx
movq (%r9), %r15
movq %rdi, 0x8(%rsp)
movq $0x0, 0x74(%rdi)
cmpq $0x0, 0x18(%rdx)
je 0x23a1b
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x8(%rdx), %rsi
cmpl $0x0, 0x4(%rsi,%rax)
je 0x239fb
movq 0x8(%rsp), %rsi
incl 0x78(%rsi)
movq 0x8(%rdx), %rsi
cmpb $0x1, 0x8(%rsi,%rax)
jne 0x23a0e
movq 0x8(%rsp), %rsi
incl 0x74(%rsi)
incq %rcx
addq $0xc, %rax
cmpq 0x18(%rdx), %rcx
jb 0x239e8
movq 0x8(%rsp), %r13
movq $0x0, 0x88(%r13)
movl 0x74(%r13), %eax
cmpb $0x0, 0x1(%r13)
movl %eax, %ecx
bswapl %ecx
cmovel %eax, %ecx
leaq 0x70(%rsp), %rsi
movl %ecx, (%rsi)
movl $0x4, %edx
movq %rbx, %rdi
callq 0x5460
movl 0x74(%r13), %edi
shlq $0x2, %rdi
callq 0x5030
movq %rax, 0x80(%r13)
cmpq $0x0, 0x18(%r14)
je 0x23abb
movl $0x1, %ebp
xorl %eax, %eax
leaq 0x70(%rsp), %r12
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, 0x70(%rsp)
movl $0x4, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5460
movl %ebp, %eax
incl %ebp
cmpq %rax, 0x18(%r14)
ja 0x23a76
movl 0x78(%r13), %eax
cmpb $0x0, 0x1(%r13)
movl %eax, %ecx
bswapl %ecx
cmovel %eax, %ecx
leaq 0x70(%rsp), %rsi
movl %ecx, (%rsi)
movl $0x4, %edx
movq %rbx, %rdi
callq 0x5460
movq 0x10(%rsp), %rcx
cmpq $0x0, 0x18(%rcx)
je 0x23fa9
leaq 0x80(%rsp), %r12
leaq 0xc8(%r13), %rax
movq %rax, 0x48(%rsp)
movl %r15d, %eax
movq %rax, 0x40(%rsp)
xorl %ebp, %ebp
leaq 0x70(%rsp), %r14
movl $0x0, (%rsp)
movq 0x8(%rcx), %rax
movq (%rax,%rbp,8), %rdi
movq 0x20(%rdi), %rax
cmpq 0x28(%rdi), %rax
jne 0x23b4d
testb $0x1, 0x30(%rdi)
je 0x23b4d
callq 0x17346
leaq 0x70(%rsp), %r14
incl %ebp
movq 0x10(%rsp), %rcx
cmpq %rbp, 0x18(%rcx)
ja 0x23b19
jmp 0x23fa9
movb $0x1, %bl
movq %rbp, 0x18(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
movb $0x0, 0x30(%rsp)
movq %r12, 0x70(%rsp)
movq %r14, %rdi
leaq 0xd584(%rip), %rdx      # 0x310ff
movq %rdx, %rsi
callq 0x104c0
movq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0xf0d8
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0x23baa
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x5380
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax,%rbp,8), %r12
movq 0xd0(%r13), %r14
movq 0xe0(%r13), %rbp
testb $0x1, %bl
je 0x23be5
movq %r12, %rdi
callq 0x173b8
cmpl $0x3e, %eax
jne 0x23fbb
movl $0x3e, 0x185b3(%rip)    # 0x3c198
movl 0x185ac(%rip), %r13d    # 0x3c198
cmpl $0x3e, %r13d
je 0x23bfc
cmpl $0x23, %r13d
jne 0x23c83
shlq $0x5, %rbp
leaq (%r14,%rbp), %rbx
addq $-0x20, %rbx
movq 0x18(%rsp), %rbp
cmpl $0x23, %r13d
jne 0x23c25
movq %r12, %rdi
callq 0x173e2
movl %eax, %r13d
cmpl $-0x1, %eax
jne 0x23c0d
jmp 0x23c98
movq %r12, %rdi
callq 0x1741e
movl %eax, %r13d
cmpl $-0x1, %eax
je 0x23c98
cmpl $0xd, %r13d
je 0x23c5f
cmpl $0xa, %r13d
je 0x23c5f
testq %rbx, %rbx
je 0x23c25
movsbl %r13b, %esi
movq %rbx, %rdi
callq 0x52c0
jmp 0x23c25
movq %r12, %rdi
callq 0x1741e
movl %eax, %r13d
cmpl $0xc, %r13d
jle 0x23c6d
cmpl $0xd, %r13d
je 0x23c54
jmp 0x23c75
cmpl $0xa, %r13d
je 0x23c54
jmp 0x23c92
cmpl $0x23, %r13d
je 0x23c0d
cmpl $0x3e, %r13d
je 0x23c0d
jmp 0x23cad
movl $0x0, 0x4(%rsp)
movq 0x18(%rsp), %rbp
jmp 0x23cb3
cmpl $-0x1, %r13d
jne 0x23cad
movl $0xffffffff, 0x184f6(%rip) # imm = 0xFFFFFFFF
movb $0x1, %al
movl %eax, 0x4(%rsp)
jmp 0x23e3f
movb $0x1, %al
movl %eax, 0x4(%rsp)
movslq %r13d, %rax
leaq 0x19513(%rip), %rcx     # 0x3d1d0
movb (%rcx,%rax), %bl
movl $0x41, %eax
cmpb $0x2, %bl
je 0x23ccd
movl %r13d, %eax
movq %r15, %rcx
shrq $0x20, %rcx
testb $0x1, %cl
cmovnel %eax, %r13d
movl %r13d, %edi
callq 0x56e0
movl $0x54, %ecx
cmpl $0x43, %eax
je 0x23cf0
movl %r13d, %ecx
movq %r15, %rax
shrq $0x28, %rax
testb $0x1, %al
cmovnel %ecx, %r13d
movl $0xc, %eax
cmpb $0x1, %bl
je 0x23d3e
cmpb $0x2, %bl
je 0x23d17
movl $0x3e, %ecx
cmpl $0x3e, %r13d
je 0x23d30
movq %r12, %rdi
callq 0x1741e
movl %eax, %r13d
cmpl $-0x1, %eax
je 0x23d2b
xorl %eax, %eax
jmp 0x23d3e
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, 0x18462(%rip)     # 0x3c198
movl $0x6, %eax
movl %ecx, %r13d
testl %eax, %eax
je 0x23cb3
cmpl $0x6, %eax
je 0x23e3f
cmpl $0xc, %eax
jne 0x23f91
movl (%rsp), %eax
movl %eax, %ebp
xorl %ebx, %ebx
movslq %r13d, %rax
leaq 0x19a67(%rip), %rcx     # 0x3d7d0
cmpb $0x1, (%rcx,%rax)
jne 0x23db0
incq %rbx
leaq 0x19857(%rip), %rcx     # 0x3d5d0
movzbl (%rcx,%rax), %eax
leal 0x1(%rbp), %edx
movq 0x220(%rsp), %r9
movq 0x8(%r9), %rsi
movl %ebp, %edi
shrl $0x4, %edi
addl %ebp, %ebp
movl $0x3, %r8d
movl %ebp, %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, %ebp
movq %r12, %rdi
callq 0x1741e
movl %eax, %r13d
btq $0x20, %r15
jae 0x23ddd
movslq %r13d, %rax
leaq 0x19404(%rip), %rcx     # 0x3d1d0
cmpb $0x2, (%rcx,%rax)
movl $0x41, %eax
je 0x23dda
movl %r13d, %eax
movl %eax, %r13d
leal 0x1(%r13), %eax
cmpl $0x3f, %eax
ja 0x23e0b
movabsq $-0x7fffffefffffffff, %rcx # imm = 0x8000001000000001
btq %rax, %rcx
jae 0x23e0b
movl %r13d, 0x1839b(%rip)    # 0x3c198
xorl %r14d, %r14d
testb %r14b, %r14b
jne 0x23d5f
jmp 0x23e46
movslq %r13d, %rax
leaq 0x193bb(%rip), %rcx     # 0x3d1d0
cmpb $0x2, (%rcx,%rax)
je 0x23df6
movb $0x1, %r14b
btq $0x28, %r15
jae 0x23e00
movl %r13d, %edi
callq 0x56e0
movl $0x54, %ecx
cmpl $0x43, %eax
je 0x23e3a
movl %r13d, %ecx
movl %ecx, %r13d
jmp 0x23e00
movl (%rsp), %eax
movl %eax, %ebp
xorl %ebx, %ebx
cmpl $0x2, 0x40(%rsp)
movq 0x8(%rsp), %r13
leaq 0x80(%rsp), %r12
jne 0x23e6d
movl (%rsp), %esi
movq 0x220(%rsp), %rdi
movq %rbx, %rdx
callq 0x257e2
testl %ebx, %ebx
sete %al
movl %ebp, (%rsp)
movq 0x18(%rsp), %rbp
notb %al
andb 0x4(%rsp), %al
testb $0x1, %al
je 0x23f38
movq 0xd0(%r13), %rax
movq 0xe0(%r13), %rcx
shlq $0x5, %rcx
cmpq $0x0, -0x18(%rax,%rcx)
leaq 0x70(%rsp), %r14
jne 0x23f44
movq %r14, %rdi
callq 0x5490
movq 0xe0(%r13), %rsi
decq %rsi
movq %r14, %rdi
callq 0x51e0
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
leaq 0x78(%rsp), %rsi
callq 0x55a0
movq 0xd0(%r13), %rax
movq 0xe0(%r13), %rcx
shlq $0x5, %rcx
leaq (%rax,%rcx), %rdi
addq $-0x20, %rdi
movq %rbx, %rsi
callq 0x5430
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x23f15
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x5380
leaq 0x70(%rsp), %r14
movq %r14, %rdi
movq 0x17074(%rip), %rsi     # 0x3af98
callq 0x5190
leaq 0xe0(%rsp), %rdi
callq 0x5130
jmp 0x23f44
decq 0xe0(%r13)
leaq 0x70(%rsp), %r14
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x23f65
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x5380
leaq 0x70(%rsp), %r14
movq 0x10(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax,%rbp,8), %rdi
movq 0x20(%rdi), %rax
xorl %ebx, %ebx
cmpq 0x28(%rdi), %rax
jne 0x23b54
testb $0x1, 0x30(%rdi)
je 0x23b54
jmp 0x23b31
movb $0x1, %al
movl %ecx, 0x4(%rsp)
movq 0x8(%rsp), %r13
leaq 0x80(%rsp), %r12
jmp 0x23e7a
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1702e(%rip), %rdi     # 0x3aff0
leaq 0xc53a(%rip), %rsi      # 0x30503
movl $0x2f, %edx
callq 0x5400
movq 0x17016(%rip), %rdi     # 0x3aff0
callq 0x5170
movl $0x4, %edi
callq 0x5180
movl $0x1, (%rax)
movq 0x16f52(%rip), %rsi     # 0x3af48
movq %rax, %rdi
xorl %edx, %edx
callq 0x5650
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x24140
movq %rbx, %rdi
callq 0x5140
movq 0x16fd5(%rip), %rdi     # 0x3aff0
leaq 0xc4b1(%rip), %rsi      # 0x304d3
movl $0x2f, %edx
callq 0x5400
movq 0x16fbd(%rip), %rdi     # 0x3aff0
leaq 0xcc85(%rip), %rsi      # 0x30cbf
movl $0x4, %edx
callq 0x5400
movq 0x16fa5(%rip), %rdi     # 0x3aff0
leaq 0xa125(%rip), %rsi      # 0x2e177
movl $0x8, %edx
callq 0x5400
movq 0x16f8d(%rip), %rdi     # 0x3aff0
leaq 0xae98(%rip), %rsi      # 0x2ef02
movl $0x1, %edx
callq 0x5400
movq 0x16f75(%rip), %rdi     # 0x3aff0
movl $0xf20, %esi            # imm = 0xF20
callq 0x5680
movq %rax, %rdi
callq 0x5170
movl $0x8, %edi
callq 0x5180
movq 0x16ef2(%rip), %rcx     # 0x3af90
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x16efc(%rip), %rsi     # 0x3afa8
movq 0x16ec5(%rip), %rdx     # 0x3af78
movq %rax, %rdi
callq 0x5650
movq %rax, %rbx
callq 0x55d0
jmp 0x24140
movq %rax, %rdi
callq 0xdcfa
jmp 0x240cf
movq %rax, %rbx
movq 0x16ebf(%rip), %rsi     # 0x3af98
leaq 0x70(%rsp), %rdi
callq 0x5190
leaq 0xe0(%rsp), %rdi
callq 0x5130
jmp 0x24124
jmp 0x24121
jmp 0x24121
jmp 0x24121
jmp 0x24121
jmp 0x24121
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0x24124
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x5380
jmp 0x24124
jmp 0x24121
jmp 0x24121
jmp 0x24121
movq %rax, %rbx
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x24140
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x5380
movq %rbx, %rdi
callq 0x56a0