// File: /workspace/github_cpp_makefile_star2_O2/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, %r12
movq %rsi, %r14
movq %rdi, %r13
movq 0x238(%rsp), %r15
movq (%r9), %rax
movq %rax, 0x8(%rsp)
andq $0x0, 0x74(%rdi)
xorl %eax, %eax
xorl %ecx, %ecx
xorl %esi, %esi
xorl %edi, %edi
cmpq 0x18(%rdx), %rdi
jae 0x1c41a
movq 0x8(%rdx), %r8
cmpl $0x0, 0x4(%r8,%rax)
je 0x1c403
incl %ecx
movl %ecx, 0x78(%r13)
movq 0x8(%rdx), %r8
cmpb $0x1, 0x8(%r8,%rax)
jne 0x1c411
incl %esi
movl %esi, 0x74(%r13)
incq %rdi
addq $0xc, %rax
jmp 0x1c3e7
andq $0x0, 0x88(%r13)
movzbl 0x1(%r13), %edx
movq %r15, %rdi
callq 0x80de
movl 0x74(%r13), %edi
shlq $0x2, %rdi
callq 0x5030
movq %rax, 0x80(%r13)
xorl %eax, %eax
movl %eax, %ebx
cmpq %rbx, 0x18(%r12)
jbe 0x1c47c
movq 0x8(%r12), %rax
movl (%rax,%rbx,4), %eax
movq 0x80(%r13), %rcx
movl %eax, (%rcx,%rbx,4)
movq 0x80(%r13), %rax
movl (%rax,%rbx,4), %esi
movzbl 0x1(%r13), %edx
movq %r15, %rdi
callq 0x80de
leal 0x1(%rbx), %eax
jmp 0x1c445
movl 0x78(%r13), %esi
movzbl 0x1(%r13), %edx
movq %r15, %rdi
callq 0x80de
leaq 0xc8(%r13), %rax
movq %rax, 0x38(%rsp)
movl 0x8(%rsp), %eax
movq %rax, 0x30(%rsp)
movq $0x0, 0x10(%rsp)
leaq 0x80(%rsp), %r15
leaq 0x60(%rsp), %r12
xorl %ebp, %ebp
movq %r14, 0x20(%rsp)
movq %r13, 0x18(%rsp)
movl %ebp, %ebp
cmpq %rbp, 0x18(%r14)
jbe 0x1c865
movb $0x1, %bl
movq %rbp, 0x28(%rsp)
movq 0x8(%r14), %rax
movq (%rax,%rbp,8), %rdi
movq 0x20(%rdi), %rax
cmpq 0x28(%rdi), %rax
jne 0x1c4f3
testb $0x1, 0x30(%rdi)
jne 0x1c859
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
andq $0x0, 0x48(%rsp)
movb $0x0, 0x50(%rsp)
movq %r15, %rdi
leaq 0xabc8(%rip), %rsi      # 0x270da
movq %r12, %rdx
callq 0xb4e4
movq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0xc1f0
movq %r15, %rdi
callq 0x5778
movq 0x8(%r14), %rax
movq (%rax,%rbp,8), %r15
movq 0xd0(%r13), %r12
movq 0xe0(%r13), %rbp
movl 0x10(%rsp), %r14d
testb $0x1, %bl
je 0x1c56f
movq %r15, %rdi
callq 0x1267a
cmpl $0x3e, %eax
jne 0x1c877
movl $0x3e, 0x16c1e(%rip)    # 0x33188
pushq $0x3e
popq %rax
jmp 0x1c583
movl 0x16c13(%rip), %eax     # 0x33188
cmpl $0x3e, %eax
je 0x1c583
cmpl $0x23, %eax
jne 0x1c603
shlq $0x5, %rbp
addq %rbp, %r12
addq $-0x20, %r12
cmpl $0x23, %eax
jne 0x1c5a5
movq %r15, %rdi
callq 0x126a4
cmpl $-0x1, %eax
jne 0x1c58e
jmp 0x1c84e
movq %r15, %rdi
callq 0x126e0
cmpl $-0x1, %eax
je 0x1c5f8
cmpl $0xd, %eax
je 0x1c5d6
cmpl $0xa, %eax
je 0x1c5d6
testq %r12, %r12
je 0x1c5a5
movsbl %al, %esi
movq %r12, %rdi
callq 0x52b0
jmp 0x1c5a5
movq %r15, %rdi
callq 0x126e0
cmpl $0xa, %eax
je 0x1c5ce
cmpl $0xd, %eax
je 0x1c5ce
cmpl $-0x1, %eax
je 0x1c5f8
cmpl $0x23, %eax
je 0x1c58e
cmpl $0x3e, %eax
je 0x1c58e
movq %r14, (%rsp)
movb $0x1, %bpl
jmp 0x1c609
movb $0x1, %bpl
xorl %r12d, %r12d
jmp 0x1c746
movq %r14, (%rsp)
xorl %ebp, %ebp
movq 0x8(%rsp), %rcx
movq %rcx, %r13
shrq $0x28, %r13
shrq $0x20, %rcx
cltq
leaq 0x17b9e(%rip), %rdx     # 0x341c0
movb (%rdx,%rax), %r14b
cmpb $0x2, %r14b
movl %eax, %r12d
pushq $0x41
popq %rdx
cmovel %edx, %r12d
testb $0x1, %cl
cmovel %eax, %r12d
movl %r12d, %edi
callq 0x56c0
cmpl $0x43, %eax
movl %r12d, %ebx
pushq $0x54
popq %rax
cmovel %eax, %ebx
testb $0x1, %r13b
cmovel %r12d, %ebx
cmpb $0x1, %r14b
je 0x1c680
cmpb $0x2, %r14b
je 0x1c66c
cmpl $0x3e, %ebx
je 0x1c73c
movq %r15, %rdi
callq 0x126e0
cmpl $-0x1, %eax
jne 0x1c609
pushq $-0x1
jmp 0x1c73e
xorl %r12d, %r12d
movq (%rsp), %r14
movslq %ebx, %rax
leaq 0x1812f(%rip), %rcx     # 0x347c0
cmpb $0x1, (%rcx,%rax)
jne 0x1c6c0
incq %r12
leaq 0x17f1f(%rip), %rcx     # 0x345c0
movzbl (%rcx,%rax), %esi
movq 0x10(%rsp), %rax
movl %eax, %edx
incl %eax
movq %rax, 0x10(%rsp)
movq 0x230(%rsp), %rdi
callq 0x1dabe
movq %r15, %rdi
callq 0x126e0
btq $0x20, 0x8(%rsp)
jae 0x1c6e4
movslq %eax, %rcx
leaq 0x17ae5(%rip), %rdx     # 0x341c0
cmpb $0x2, (%rdx,%rcx)
pushq $0x41
popq %rbx
je 0x1c703
movl %eax, %ecx
incl %ecx
cmpl $0x3f, %ecx
ja 0x1c701
movabsq $-0x7fffffefffffffff, %rdx # imm = 0x8000001000000001
btq %rcx, %rdx
movl %eax, %ebx
jae 0x1c703
jmp 0x1c746
movl %eax, %ebx
movslq %ebx, %rax
leaq 0x17ab3(%rip), %rcx     # 0x341c0
cmpb $0x2, (%rcx,%rax)
je 0x1c847
btq $0x28, 0x8(%rsp)
jae 0x1c687
movl %ebx, %edi
callq 0x56c0
pushq $0x54
popq %rcx
cmpl $0x43, %eax
je 0x1c735
movl %ebx, %ecx
movl %ecx, %ebx
jmp 0x1c687
pushq $0x3e
popq %rax
xorl %r12d, %r12d
movq (%rsp), %r14
movl %eax, 0x16a3c(%rip)     # 0x33188
cmpl $0x2, 0x30(%rsp)
jne 0x1c766
movq 0x230(%rsp), %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x1dae2
testb %bpl, %bpl
je 0x1c813
movl %r12d, %eax
testq %rax, %rax
je 0x1c813
movq 0x18(%rsp), %r13
movq 0xd0(%r13), %rax
movq 0xe0(%r13), %rcx
shlq $0x5, %rcx
cmpq $0x0, -0x18(%rax,%rcx)
movq 0x20(%rsp), %r14
leaq 0x80(%rsp), %r15
leaq 0x60(%rsp), %r12
movq 0x28(%rsp), %rbp
jne 0x1c836
movq %r15, %rdi
callq 0x5490
movq 0xe0(%r13), %rsi
decq %rsi
movq %r15, %rdi
callq 0x51e0
movq %r12, %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 %r12, %rsi
callq 0x5420
movq %r12, %rdi
callq 0x5778
movq %r15, %rdi
callq 0x50e0
jmp 0x1c836
movq 0x18(%rsp), %r13
decq 0xe0(%r13)
movq 0x20(%rsp), %r14
leaq 0x80(%rsp), %r15
leaq 0x60(%rsp), %r12
movq 0x28(%rsp), %rbp
leaq 0x40(%rsp), %rdi
callq 0x5778
xorl %ebx, %ebx
jmp 0x1c4d7
movl %ebx, %eax
jmp 0x1c746
movb $0x1, %bpl
pushq $-0x1
popq %rax
jmp 0x1c5fb
callq 0x12608
incl %ebp
jmp 0x1c4c4
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x15772(%rip), %rdi     # 0x31ff0
leaq 0x9c91(%rip), %rsi      # 0x26516
callq 0x5350
movq %rax, %rdi
callq 0x5180
pushq $0x4
popq %rdi
callq 0x5190
movl $0x1, (%rax)
movq 0x156b9(%rip), %rsi     # 0x31f60
movq %rax, %rdi
xorl %edx, %edx
callq 0x5630
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x1c99b
movq %rbx, %rdi
callq 0x5150
movq 0x15724(%rip), %rdi     # 0x31ff0
leaq 0x9c13(%rip), %rsi      # 0x264e6
callq 0x5350
leaq 0xa3f3(%rip), %rsi      # 0x26cd2
movq %rax, %rdi
callq 0x5350
leaq 0x789c(%rip), %rsi      # 0x2418a
movq %rax, %rdi
callq 0x5350
leaq 0x8618(%rip), %rsi      # 0x24f15
movq %rax, %rdi
callq 0x5350
movq %rax, %rdi
movl $0xf20, %esi            # imm = 0xF20
callq 0x5660
movq %rax, %rdi
callq 0x5180
pushq $0x8
popq %rdi
callq 0x5190
movq 0x1566f(%rip), %rcx     # 0x31f98
addq $0x10, %rcx
movq %rcx, (%rax)
movq 0x15671(%rip), %rsi     # 0x31fa8
movq 0x1564a(%rip), %rdx     # 0x31f88
movq %rax, %rdi
callq 0x5630
movq %rax, %rbx
callq 0x55c0
jmp 0x1c99b
movq %rax, %rdi
callq 0xb51f
jmp 0x1c95a
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x50e0
jmp 0x1c991
jmp 0x1c98e
jmp 0x1c98e
jmp 0x1c98e
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x5778
jmp 0x1c991
jmp 0x1c98e
jmp 0x1c98e
jmp 0x1c98e
jmp 0x1c98e
jmp 0x1c98e
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x5778
movq %rbx, %rdi
callq 0x5680
nop