// File: BenLangmead[P]bowtie/pat.cpp
// Function: FastaContinuousPatternSource::nextBatchFromFile(PerThreadReadBuf&, bool, unsigned long)
pair<bool, int> FastaContinuousPatternSource::nextBatchFromFile(
	PerThreadReadBuf& pt,
	bool batch_a,
	size_t readi)
{
	int c = -1;
	EList<Read>& readbuf = batch_a ? pt.bufa_ : pt.bufb_;
	while(readi < pt.max_buf_) {
		c = getc_wrapper();
		if(c < 0) {
			break;
		}
		if(c == '>') {
			resetForNextFile();
			c = getc_wrapper();
			bool sawSpace = false;
			while(c != '\n' && c != '\r') {
				if(!sawSpace) {
					sawSpace = isspace(c);
				}
				if(!sawSpace) {
					// Put it in the name prefix buffer so we
					// can re-use this prefix for all the reads
					// that are substrings of this FASTA sequence
					name_prefix_buf_.push_back(c);
				}
				c = getc_wrapper();
			}
			while(c == '\n' || c == '\r') {
				c = getc_wrapper();
			}
			if(c < 0) {
				break;
			}
			name_prefix_buf_.push_back('_');
		}
		int cat = asc2dnacat[c];
		if(cat >= 2) c = 'N';
		if(cat == 0) {
			// Non-DNA, non-IUPAC char; skip
			continue;
		} else {
			// DNA char
			buf_[bufCur_++] = c;
			if(bufCur_ == 1024) {
				bufCur_ = 0; // wrap around circular buf
			}
			if(eat_ > 0) {
				eat_--;
				// Try to keep readCnt_ aligned with the offset
				// into the reference; that lets us see where
				// the sampling gaps are by looking at the read
				// name
				if(!beginning_) {
					cur_++;
				}
				continue;
			}
			// install name
			readbuf[readi].readOrigBuf.append(name_prefix_buf_.c_str());
			itoa10<TReadId>(cur_ - last_, name_int_buf_);
			readbuf[readi].readOrigBuf.append(name_int_buf_);
			readbuf[readi].readOrigBuf.append('\t');
			// install sequence
			for(size_t i = 0; i < length_; i++) {
				if(length_ - i <= bufCur_) {
					c = buf_[bufCur_ - (length_ - i)];
				} else {
					// Rotate
					c = buf_[bufCur_ - (length_ - i) + 1024];
				}
				readbuf[readi].readOrigBuf.append(c);
			}
			eat_ = freq_-1;
			cur_++;
			beginning_ = false;
			readi++;
		}
	}
	return make_pair(c < 0, readi);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %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, -0x80(%rbp)
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
testb $0x1, -0x19(%rbp)
je 0x80bb4
movq -0x18(%rbp), %rax
addq $0x8, %rax
movq %rax, -0x88(%rbp)
jmp 0x80bc3
movq -0x18(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq (%rcx), %rax
jae 0x80f2e
movq -0x80(%rbp), %rdi
callq 0x83130
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x2c(%rbp)
jge 0x80bf6
jmp 0x80f2e
cmpl $0x3e, -0x2c(%rbp)
jne 0x80cdb
movq -0x80(%rbp), %rdi
movq (%rdi), %rax
callq *0x38(%rax)
movq -0x80(%rbp), %rdi
callq 0x83130
movl %eax, -0x2c(%rbp)
movb $0x0, -0x39(%rbp)
xorl %eax, %eax
cmpl $0xa, -0x2c(%rbp)
movb %al, -0x89(%rbp)
je 0x80c35
cmpl $0xd, -0x2c(%rbp)
setne %al
movb %al, -0x89(%rbp)
movb -0x89(%rbp), %al
testb $0x1, %al
jne 0x80c41
jmp 0x80c84
testb $0x1, -0x39(%rbp)
jne 0x80c5a
movl -0x2c(%rbp), %edi
callq 0xa0a0
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, -0x39(%rbp)
testb $0x1, -0x39(%rbp)
jne 0x80c76
movq -0x80(%rbp), %rdi
addq $0x204c8, %rdi          # imm = 0x204C8
movl -0x2c(%rbp), %eax
movsbl %al, %esi
callq 0xa510
movq -0x80(%rbp), %rdi
callq 0x83130
movl %eax, -0x2c(%rbp)
jmp 0x80c1a
jmp 0x80c86
movb $0x1, %al
cmpl $0xa, -0x2c(%rbp)
movb %al, -0x8a(%rbp)
je 0x80ca1
cmpl $0xd, -0x2c(%rbp)
sete %al
movb %al, -0x8a(%rbp)
movb -0x8a(%rbp), %al
testb $0x1, %al
jne 0x80cad
jmp 0x80cbb
movq -0x80(%rbp), %rdi
callq 0x83130
movl %eax, -0x2c(%rbp)
jmp 0x80c86
cmpl $0x0, -0x2c(%rbp)
jge 0x80cc6
jmp 0x80f2e
movq -0x80(%rbp), %rdi
addq $0x204c8, %rdi          # imm = 0x204C8
movl $0x5f, %esi
callq 0xa510
movslq -0x2c(%rbp), %rcx
leaq 0x29bca(%rip), %rax     # 0xaa8b0
movzbl (%rax,%rcx), %eax
movl %eax, -0x40(%rbp)
cmpl $0x2, -0x40(%rbp)
jl 0x80cfa
movl $0x4e, -0x2c(%rbp)
cmpl $0x0, -0x40(%rbp)
jne 0x80d05
jmp 0x80bce
movq -0x80(%rbp), %rax
movl -0x2c(%rbp), %ecx
movb %cl, %dl
movq 0x20500(%rax), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, 0x20500(%rax)
movb %dl, 0x200c1(%rax,%rcx)
cmpq $0x400, 0x20500(%rax) # imm = 0x400
jne 0x80d46
movq -0x80(%rbp), %rax
movq $0x0, 0x20500(%rax)
movq -0x80(%rbp), %rax
cmpq $0x0, 0x200b8(%rax)
jbe 0x80d8e
movq -0x80(%rbp), %rax
movq 0x200b8(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x200b8(%rax)
testb $0x1, 0x200c0(%rax)
jne 0x80d89
movq -0x80(%rbp), %rax
movq 0x20508(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x20508(%rax)
jmp 0x80bce
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x36630
movq -0x80(%rbp), %rdi
addq $0x120, %rax            # imm = 0x120
movq %rax, -0x98(%rbp)
addq $0x204c8, %rdi          # imm = 0x204C8
callq 0xa210
movq -0x98(%rbp), %rdi
movq %rax, %rsi
callq 0x82f40
movq -0x80(%rbp), %rsi
movq 0x20508(%rsi), %rax
subq 0x20510(%rsi), %rax
movq %rax, -0x48(%rbp)
addq $0x204e8, %rsi          # imm = 0x204E8
leaq -0x48(%rbp), %rdi
callq 0x82eb0
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x36630
movq -0x80(%rbp), %rsi
movq %rax, %rdi
addq $0x120, %rdi            # imm = 0x120
addq $0x204e8, %rsi          # imm = 0x204E8
callq 0x82f40
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x36630
movq %rax, %rdi
addq $0x120, %rdi            # imm = 0x120
movb $0x9, -0x49(%rbp)
leaq -0x49(%rbp), %rsi
callq 0x831e0
movq $0x0, -0x58(%rbp)
movq -0x80(%rbp), %rcx
movq -0x58(%rbp), %rax
cmpq 0x200a8(%rcx), %rax
jae 0x80eee
movq -0x80(%rbp), %rcx
movq 0x200a8(%rcx), %rax
subq -0x58(%rbp), %rax
cmpq 0x20500(%rcx), %rax
ja 0x80e93
movq -0x80(%rbp), %rax
movq 0x20500(%rax), %rcx
movq 0x200a8(%rax), %rdx
subq -0x58(%rbp), %rdx
subq %rdx, %rcx
movsbl 0x200c1(%rax,%rcx), %eax
movl %eax, -0x2c(%rbp)
jmp 0x80eb7
movq -0x80(%rbp), %rax
movq 0x20500(%rax), %rcx
movq 0x200a8(%rax), %rdx
subq -0x58(%rbp), %rdx
subq %rdx, %rcx
movsbl 0x204c1(%rax,%rcx), %eax
movl %eax, -0x2c(%rbp)
movq -0x38(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x36630
movq %rax, %rdi
addq $0x120, %rdi            # imm = 0x120
movl -0x2c(%rbp), %eax
movb %al, -0x59(%rbp)
leaq -0x59(%rbp), %rsi
callq 0x831e0
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x80e40
movq -0x80(%rbp), %rax
movq 0x200b0(%rax), %rcx
subq $0x1, %rcx
movq %rcx, 0x200b8(%rax)
movq 0x20508(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x20508(%rax)
movb $0x0, 0x200c0(%rax)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x80bce
cmpl $0x0, -0x2c(%rbp)
setl %al
andb $0x1, %al
movb %al, -0x71(%rbp)
leaq -0x71(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x82f80
movb %al, -0x70(%rbp)
movq %rdx, -0x68(%rbp)
leaq -0x8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x82bd0
movq -0x8(%rbp), %rax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)