// File: BenLangmead[P]bowtie/./ref_read.h
// Function: RefRecord fastaRefReadAppend<SString<char>>(FileBuf&, bool, SString<char>&, unsigned int&, RefReadInParams&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*)
static RefRecord fastaRefReadAppend(FileBuf& in,
                                    bool first,
                                    TStr& dst,
				    TIndexOffU& dstoff,
                                    RefReadInParams& rparms,
                                    string* name = NULL)
{
	int c;
	static int lastc = '>';
	if(first) {
		c = in.getPastWhitespace();
		if(c != '>') {
			cerr << "Reference file does not seem to be a FASTA file" << endl;
			throw 1;
		}
		lastc = c;
	}
	assert_neq(-1, lastc);

	// RefRecord params
	size_t len = 0;
	size_t off = 0;
	first = true;

	size_t ilen = dstoff;

	// Chew up the id line; if the next line is either
	// another id line or a comment line, keep chewing
	c = lastc;
	if(c == '>' || c == '#') {
		do {
			while (c == '#') {
				if((c = in.getPastNewline()) == -1) {
					lastc = -1;
					goto bail;
				}
			}
			assert_eq('>', c);
			while(true) {
				c = in.get();
				if(c == -1) {
					lastc = -1;
					goto bail;
				}
				if(c == '\n' || c == '\r') {
					while(c == '\r' || c == '\n') c = in.get();
					if(c == -1) {
						lastc = -1;
						goto bail;
					}
					break;
				}
				if (name) name->push_back(c);
			}
			// c holds the first character on the line after the name
			// line
		} while (c == '>' || c == '#');
	} else {
		ASSERT_ONLY(int cc = toupper(c));
		assert(cc != 'A' && cc != 'C' && cc != 'G' && cc != 'T');
		first = false;
	}

	// Skip over an initial stretch of gaps or ambiguous characters.
	while(true) {
		int cat = dna4Cat[c];
		if(rparms.nsToAs && cat == 2) {
			c = 'A';
		}
		int cc = toupper(c);
		if(rparms.bisulfite && cc == 'C') c = cc = 'T';
		if(cat == 1) {
			break; // to read-in loop
		} else if(cat == 2) {
			off++; // skip it
		} else if(c == '>') {
			lastc = '>';
			goto bail;
		}
		c = in.get();
		if(c == -1) {
			lastc = -1;
			goto bail;
		}
	}
	assert_eq(1, dna4Cat[c]);

	// in now points just past the first character of a sequence
	// line, and c holds the first character
	while(true) {
		// Note: can't have a comment in the middle of a sequence,
		// though a comment can end a sequence
		int cat = asc2dnacat[c];
		assert_neq(2, cat);
		if(cat == 1) {
			// Consume it
			len++;
			// Add it to referenece buffer
			dst.set(asc2dna[c], dstoff++);
			assert_lt((int)dst[dstoff-1], 4);
		}
		c = in.get();
		if(rparms.nsToAs && dna4Cat[c] == 2) c = 'A';
		if (c == -1 || c == '>' || c == '#' || dna4Cat[c] == 2) {
			lastc = c;
			break;
		}
		if(rparms.bisulfite && toupper(c) == 'C') c = 'T';
	}

  bail:
	// Optionally reverse the portion that we just appended.
	// ilen = length of buffer before this last sequence was appended.
	if(rparms.reverse == REF_READ_REVERSE_EACH) {
		// Find limits of the portion we just appended
		dst.reverseWindow(ilen, len);
	}
	return RefRecord((TIndexOffU)off, (TIndexOffU)len, first);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movb %sil, %al
movq %rdi, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
testb $0x1, -0x19(%rbp)
je 0x11d4d
movq -0x18(%rbp), %rdi
callq 0x21de0
movl %eax, -0x44(%rbp)
cmpl $0x3e, -0x44(%rbp)
je 0x11d44
movq 0x402ea(%rip), %rdi     # 0x51ff0
leaq 0x34779(%rip), %rsi     # 0x46486
callq 0x6410
movq %rax, %rdi
movq 0x4024c(%rip), %rsi     # 0x51f68
callq 0x6470
movl $0x4, %edi
callq 0x61f0
movq %rax, %rdi
movl $0x1, (%rdi)
movq 0x40215(%rip), %rsi     # 0x51f50
xorl %eax, %eax
movl %eax, %edx
callq 0x67f0
movl -0x44(%rbp), %eax
movl %eax, 0x40b27(%rip)     # 0x52874
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movb $0x1, -0x19(%rbp)
movq -0x30(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x60(%rbp)
movl 0x40b03(%rip), %eax     # 0x52874
movl %eax, -0x44(%rbp)
cmpl $0x3e, -0x44(%rbp)
je 0x11d84
cmpl $0x23, -0x44(%rbp)
jne 0x11e75
jmp 0x11d86
jmp 0x11d88
cmpl $0x23, -0x44(%rbp)
jne 0x11db0
movq -0x18(%rbp), %rdi
callq 0x21e40
movl %eax, -0x44(%rbp)
cmpl $-0x1, %eax
jne 0x11dae
movl $0xffffffff, 0x40acb(%rip) # imm = 0xFFFFFFFF
jmp 0x11ffe
jmp 0x11d88
jmp 0x11db2
movq -0x18(%rbp), %rdi
callq 0x21ec0
movl %eax, -0x44(%rbp)
cmpl $-0x1, -0x44(%rbp)
jne 0x11dd3
movl $0xffffffff, 0x40aa6(%rip) # imm = 0xFFFFFFFF
jmp 0x11ffe
cmpl $0xa, -0x44(%rbp)
je 0x11ddf
cmpl $0xd, -0x44(%rbp)
jne 0x11e2d
jmp 0x11de1
movb $0x1, %al
cmpl $0xd, -0x44(%rbp)
movb %al, -0x81(%rbp)
je 0x11dfc
cmpl $0xa, -0x44(%rbp)
sete %al
movb %al, -0x81(%rbp)
movb -0x81(%rbp), %al
testb $0x1, %al
jne 0x11e08
jmp 0x11e16
movq -0x18(%rbp), %rdi
callq 0x21ec0
movl %eax, -0x44(%rbp)
jmp 0x11de1
cmpl $-0x1, -0x44(%rbp)
jne 0x11e2b
movl $0xffffffff, 0x40a4e(%rip) # imm = 0xFFFFFFFF
jmp 0x11ffe
jmp 0x11e48
cmpq $0x0, -0x40(%rbp)
je 0x11e43
movq -0x40(%rbp), %rdi
movl -0x44(%rbp), %eax
movsbl %al, %esi
callq 0x6360
jmp 0x11db2
jmp 0x11e4a
movb $0x1, %al
cmpl $0x3e, -0x44(%rbp)
movb %al, -0x82(%rbp)
je 0x11e65
cmpl $0x23, -0x44(%rbp)
sete %al
movb %al, -0x82(%rbp)
movb -0x82(%rbp), %al
testb $0x1, %al
jne 0x11d86
jmp 0x11e79
movb $0x0, -0x19(%rbp)
jmp 0x11e7b
movslq -0x44(%rbp), %rcx
leaq 0x4245a(%rip), %rax     # 0x542e0
movzbl (%rax,%rcx), %eax
movl %eax, -0x64(%rbp)
movq -0x38(%rbp), %rax
testb $0x1, 0x4(%rax)
je 0x11ea4
cmpl $0x2, -0x64(%rbp)
jne 0x11ea4
movl $0x41, -0x44(%rbp)
movl -0x44(%rbp), %edi
callq 0x68d0
movl %eax, -0x68(%rbp)
movq -0x38(%rbp), %rax
testb $0x1, 0x5(%rax)
je 0x11ecd
cmpl $0x43, -0x68(%rbp)
jne 0x11ecd
movl $0x54, -0x68(%rbp)
movl $0x54, -0x44(%rbp)
cmpl $0x1, -0x64(%rbp)
jne 0x11ed5
jmp 0x11f28
cmpl $0x2, -0x64(%rbp)
jne 0x11ee9
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x11f00
cmpl $0x3e, -0x44(%rbp)
jne 0x11efe
movl $0x3e, 0x4097b(%rip)    # 0x52874
jmp 0x11ffe
jmp 0x11f00
jmp 0x11f02
movq -0x18(%rbp), %rdi
callq 0x21ec0
movl %eax, -0x44(%rbp)
cmpl $-0x1, -0x44(%rbp)
jne 0x11f23
movl $0xffffffff, 0x40956(%rip) # imm = 0xFFFFFFFF
jmp 0x11ffe
jmp 0x11e7b
jmp 0x11f2a
movslq -0x44(%rbp), %rcx
leaq 0x429ab(%rip), %rax     # 0x548e0
movzbl (%rax,%rcx), %eax
movl %eax, -0x6c(%rbp)
cmpl $0x1, -0x6c(%rbp)
jne 0x11f77
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movq -0x28(%rbp), %rdi
movslq -0x44(%rbp), %rcx
leaq 0x42783(%rip), %rax     # 0x546e0
movzbl (%rax,%rcx), %esi
movq -0x30(%rbp), %rcx
movl (%rcx), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, (%rcx)
movl %eax, %eax
movl %eax, %edx
callq 0x21f30
movq -0x18(%rbp), %rdi
callq 0x21ec0
movl %eax, -0x44(%rbp)
movq -0x38(%rbp), %rax
testb $0x1, 0x4(%rax)
je 0x11fa8
movslq -0x44(%rbp), %rcx
leaq 0x42348(%rip), %rax     # 0x542e0
movzbl (%rax,%rcx), %eax
cmpl $0x2, %eax
jne 0x11fa8
movl $0x41, -0x44(%rbp)
cmpl $-0x1, -0x44(%rbp)
je 0x11fce
cmpl $0x3e, -0x44(%rbp)
je 0x11fce
cmpl $0x23, -0x44(%rbp)
je 0x11fce
movslq -0x44(%rbp), %rcx
leaq 0x4231b(%rip), %rax     # 0x542e0
movzbl (%rax,%rcx), %eax
cmpl $0x2, %eax
jne 0x11fd9
movl -0x44(%rbp), %eax
movl %eax, 0x4089d(%rip)     # 0x52874
jmp 0x11ffc
movq -0x38(%rbp), %rax
testb $0x1, 0x5(%rax)
je 0x11ff7
movl -0x44(%rbp), %edi
callq 0x68d0
cmpl $0x43, %eax
jne 0x11ff7
movl $0x54, -0x44(%rbp)
jmp 0x11f2a
jmp 0x11ffe
movq -0x38(%rbp), %rax
cmpl $0x2, (%rax)
jne 0x12018
movq -0x28(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x50(%rbp), %rdx
callq 0x21f60
movl -0x58(%rbp), %esi
movl -0x50(%rbp), %edx
movzbl -0x19(%rbp), %ecx
andl $0x1, %ecx
leaq -0xc(%rbp), %rdi
callq 0x22010
movl -0x4(%rbp), %eax
movl %eax, -0x78(%rbp)
movq -0xc(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movb -0x78(%rbp), %dl
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)