// File: BenLangmead[P]bowtie/pat.cpp
// Function: FastqPatternSource::nextBatchFromFile(PerThreadReadBuf&, bool, unsigned long)
pair<bool, int> FastqPatternSource::nextBatchFromFile(
	PerThreadReadBuf& pt,
	bool batch_a,
	size_t readi)
{
	int c = 0;
	EList<Read>* readBuf = batch_a ? &pt.bufa_ : &pt.bufb_;
	if(first_) {
		c = getc_wrapper();
		while(c == '\r' || c == '\n') {
			c = getc_wrapper();
		}
		if(c != '@') {
			cerr << "Error: reads file does not look like a FASTQ file" << endl;
			throw 1;
		}
		first_ = false;
		(*readBuf)[readi].readOrigBuf.append('@');
	}
	bool done = false, aborted = false;
	// Read until we run out of input or until we've filled the buffer
	while (readi < pt.max_buf_ && !done) {
		Read::TBuf& buf = (*readBuf)[readi].readOrigBuf;
		assert(readi == 0 || (*readBuf)[readi].readOrigBuf.length() == 0);
		int newlines = 4;
		while(newlines) {
			c = getc_wrapper();
			done = c < 0;
			if(c == '\n' || (done && newlines == 1)) {
				// Saw newline, or EOF that we're
				// interpreting as final newline
				newlines--;
				c = '\n';
			} else if(done) {
				if (newlines == 4) {
					newlines = 0;
				} else {
					aborted = true; // Unexpected EOF
				}
				break;
			}
			buf.append(c);
		}
		if (c > 0) {
			if (interleaved_) {
				// alternate between read buffers
				batch_a = !batch_a;
				readBuf = batch_a ? &pt.bufa_ : &pt.bufb_;
				// increment read counter after each pair gets read
				readi = batch_a ? readi + 1 : readi;
			}
			else {
				readi++;
			}
		}
	}
	if(aborted) {
		readi--;
	}
	return make_pair(done, readi);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movb %dl, %al
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movq %rcx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x2c(%rbp)
testb $0x1, -0x19(%rbp)
je 0x811c1
movq -0x18(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x70(%rbp)
jmp 0x811cd
movq -0x18(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rax
movq -0x70(%rbp), %rcx
movq %rcx, -0x38(%rbp)
testb $0x1, 0x200a3(%rax)
je 0x81298
movq -0x68(%rbp), %rdi
callq 0x83130
movl %eax, -0x2c(%rbp)
movb $0x1, %al
cmpl $0xd, -0x2c(%rbp)
movb %al, -0x71(%rbp)
je 0x81207
cmpl $0xa, -0x2c(%rbp)
sete %al
movb %al, -0x71(%rbp)
movb -0x71(%rbp), %al
testb $0x1, %al
jne 0x81210
jmp 0x8121e
movq -0x68(%rbp), %rdi
callq 0x83130
movl %eax, -0x2c(%rbp)
jmp 0x811f2
cmpl $0x40, -0x2c(%rbp)
je 0x81269
movq 0x26dc5(%rip), %rdi     # 0xa7ff0
leaq 0xd138(%rip), %rsi      # 0x8e36a
callq 0xa5d0
movq %rax, %rdi
movq 0x26d27(%rip), %rsi     # 0xa7f68
callq 0xa640
movl $0x4, %edi
callq 0xa290
movq %rax, %rdi
movl $0x1, (%rdi)
movq 0x26cf0(%rip), %rsi     # 0xa7f50
xorl %eax, %eax
movl %eax, %edx
callq 0xab10
movq -0x68(%rbp), %rax
movb $0x0, 0x200a3(%rax)
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x36630
movq %rax, %rdi
addq $0x120, %rdi            # imm = 0x120
movb $0x40, -0x39(%rbp)
leaq -0x39(%rbp), %rsi
callq 0x831e0
movb $0x0, -0x3a(%rbp)
movb $0x0, -0x3b(%rbp)
movq -0x28(%rbp), %rcx
movq -0x18(%rbp), %rdx
xorl %eax, %eax
cmpq (%rdx), %rcx
movb %al, -0x72(%rbp)
jae 0x812ba
movb -0x3a(%rbp), %al
xorb $-0x1, %al
movb %al, -0x72(%rbp)
movb -0x72(%rbp), %al
testb $0x1, %al
jne 0x812c6
jmp 0x813df
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x36630
addq $0x120, %rax            # imm = 0x120
movq %rax, -0x48(%rbp)
movl $0x4, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
je 0x81358
movq -0x68(%rbp), %rdi
callq 0x83130
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
setl %al
andb $0x1, %al
movb %al, -0x3a(%rbp)
cmpl $0xa, -0x2c(%rbp)
je 0x81314
testb $0x1, -0x3a(%rbp)
je 0x81326
cmpl $0x1, -0x4c(%rbp)
jne 0x81326
movl -0x4c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x4c(%rbp)
movl $0xa, -0x2c(%rbp)
jmp 0x81343
testb $0x1, -0x3a(%rbp)
je 0x81341
cmpl $0x4, -0x4c(%rbp)
jne 0x8133b
movl $0x0, -0x4c(%rbp)
jmp 0x8133f
movb $0x1, -0x3b(%rbp)
jmp 0x81358
jmp 0x81343
movq -0x48(%rbp), %rdi
movl -0x2c(%rbp), %eax
movb %al, -0x4d(%rbp)
leaq -0x4d(%rbp), %rsi
callq 0x831e0
jmp 0x812e4
cmpl $0x0, -0x2c(%rbp)
jle 0x813da
movq -0x68(%rbp), %rax
testb $0x1, 0x200a7(%rax)
je 0x813cc
movb -0x19(%rbp), %al
xorb $-0x1, %al
andb $0x1, %al
movb %al, -0x19(%rbp)
testb $0x1, -0x19(%rbp)
je 0x81389
movq -0x18(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x80(%rbp)
jmp 0x81395
movq -0x18(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x38(%rbp)
testb $0x1, -0x19(%rbp)
je 0x813b4
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
jmp 0x813bf
movq -0x28(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x28(%rbp)
jmp 0x813d8
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x813da
jmp 0x812a0
testb $0x1, -0x3b(%rbp)
je 0x813f1
movq -0x28(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x28(%rbp)
leaq -0x3a(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x82ba0
movb %al, -0x60(%rbp)
movq %rdx, -0x58(%rbp)
leaq -0x8(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x82bd0
movq -0x8(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
nop