// File: /workspace/github_cpp_makefile_star2_O2/BenLangmead[P]bowtie/ref_read.cpp
// Function: fastaRefReadSize(FileBuf&, RefReadInParams const&, bool, BitpairOutFileBuf*)
RefRecord fastaRefReadSize(FileBuf& in,
                           const RefReadInParams& rparms,
                           bool first,
                           BitpairOutFileBuf* bpout)
{
	int c;
	static int lastc = '>'; // last character seen

	// RefRecord params
	TIndexOffU len = 0; // 'len' counts toward total length
	// 'off' counts number of ambiguous characters before first
	// unambiguous character
	size_t off = 0;

	// Pick off the first carat and any preceding whitespace
	if(first) {
		assert(!in.eof());
		lastc = '>';
		c = in.getPastWhitespace();
		if(in.eof()) {
			// Got eof right away; emit warning
			cerr << "Warning: Empty input file" << endl;
			lastc = -1;
			return RefRecord(0, 0, true);
		}
		assert(c == '>');
	}

	first = true;
	// Skip to the end of the id line; if the next line is either
	// another id line or a comment line, keep skipping
	if(lastc == '>') {
		// Skip to the end of the name line
		do {
			if((c = in.getPastNewline()) == -1) {
				// No more input
				cerr << "Warning: Encountered empty reference sequence" << endl;
				lastc = -1;
				return RefRecord(0, 0, true);
			}
			if(c == '>') {
				cerr << "Warning: Encountered empty reference sequence" << endl;
			}
			// continue until a non-name, non-comment line
		} while (c == '>');
	} else {
		first = false; // not the first in a sequence
		off = 1; // The gap has already been consumed, so count it
		if((c = in.get()) == -1) {
			// Don't emit a warning, since this might legitimately be
			// a gap on the end of the final sequence in the file
			lastc = -1;
			return RefRecord((TIndexOffU)off, (TIndexOffU)len, first);
		}
	}

	// Now skip to the first DNA character, counting gap characters
	// as we go
	while(true) {
		int cat = dna4Cat[c];
		if(rparms.nsToAs && cat == 2) c = 'A';
		if(cat == 1) {
			// This is a DNA character
			break; // to read-in loop
                } else if (cat == 2) {
			off++; // skip over gap character and increment
                } else if (c == '>') {
                  if (off > 0 && lastc == '>') {
                    cerr << "Warning: Encountered reference sequence with only "
                            "gaps"
                         << endl;
                  } else if (lastc == '>') {
                    cerr << "Warning: Encountered empty reference sequence"
                         << endl;
                  }
                  lastc = '>';
                  return RefRecord((TIndexOffU)off, 0, first);
                }
                c = in.get();
		if(c == -1) {
			// End-of-file
			if(off > 0 && lastc == '>') {
				cerr << "Warning: Encountered reference sequence with only gaps" << endl;
			} else if(lastc == '>') {
				cerr << "Warning: Encountered empty reference sequence" << endl;
			}
			lastc = -1;
			return RefRecord((TIndexOffU)off, 0, first);
		}
	}
	assert_eq(1, dna4Cat[c]); // C must be unambiguous base

	// in now points just past the first character of a sequence
	// line, and c holds the first character
	while(c != -1 && c != '>') {
		if(rparms.nsToAs && dna4Cat[c] == 2) c = 'A';
		uint8_t cat = dna4Cat[c];
		int cc = toupper(c);
		if(rparms.bisulfite && cc == 'C') c = cc = 'T';
		if(cat == 1) {
			// It's a DNA character
			assert(cc == 'A' || cc == 'C' || cc == 'G' || cc == 'T');
			// Consume it
			len++;
			// Output it
			if(bpout != NULL) {
				// output nucleotide
				bpout->write(asc2dna[c]);
			}
		} else if(cat == 2) {
			// It's an N or a gap
			lastc = c;
			assert(cc != 'A' && cc != 'C' && cc != 'G' && cc != 'T');
			return RefRecord((TIndexOffU)off, (TIndexOffU)len, first);
		} else {
			// Not DNA and not a gap, ignore it
#ifndef NDEBUG
			if(!isspace(c)) {
				cerr << "Unexpected character in sequence: ";
				if(isprint(c)) {
					cerr << ((char)c) << endl;
				} else {
					cerr << "(" << c << ")" << endl;
				}
			}
#endif
		}
		c = in.get();
	}
	lastc = c;
	return RefRecord((TIndexOffU)off, (TIndexOffU)len, first);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %r8, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, %r12
movq %rdi, %rbx
testl %ecx, %ecx
je 0x256ac
movq 0x20(%r12), %rax
cmpq 0x28(%r12), %rax
jne 0x2566a
testb $0x1, 0x30(%r12)
jne 0x259bd
movl $0x3e, 0xfeac(%rip)     # 0x35520
movq %r12, %rdi
callq 0x26968
movq 0x20(%r12), %rcx
cmpq 0x28(%r12), %rcx
jne 0x256a3
testb $0x1, 0x30(%r12)
je 0x256a3
movq 0xe959(%rip), %rdi      # 0x33ff0
leaq 0x6b17(%rip), %rsi      # 0x2c1b5
jmp 0x25726
cmpl $0x3e, %eax
jne 0x259d9
movl 0xfe6d(%rip), %r14d     # 0x35520
cmpl $0x3e, %r14d
jne 0x256ee
movl %r14d, 0x4(%rsp)
movq 0xe92b(%rip), %r13      # 0x33ff0
leaq 0x6b0c(%rip), %r14      # 0x2c1d8
movq %r12, %rdi
callq 0x26992
cmpl $0x3e, %eax
jne 0x25711
movq %r13, %rdi
movq %r14, %rsi
callq 0x42d0
movq %rax, %rdi
callq 0x4130
jmp 0x256cc
movq %r12, %rdi
callq 0x1d2b6
cmpl $-0x1, %eax
je 0x257c4
movl %eax, %ebp
movl %r14d, 0x4(%rsp)
movq %rbx, 0x8(%rsp)
pushq $0x1
popq %r14
jmp 0x25751
movl %eax, %ebp
cmpl $-0x1, %eax
jne 0x25749
movq 0xe8d1(%rip), %rdi      # 0x33ff0
leaq 0x6ab2(%rip), %rsi      # 0x2c1d8
callq 0x42d0
movq %rax, %rdi
callq 0x4130
orl $-0x1, 0xfde6(%rip)     # 0x35520
xorps %xmm0, %xmm0
movups %xmm0, (%rbx)
movb $0x1, 0x10(%rbx)
jmp 0x25987
movq %rbx, 0x8(%rsp)
xorl %r14d, %r14d
leaq 0xfdd8(%rip), %rbx      # 0x35530
movslq %ebp, %r13
movzbl (%rbx,%r13), %eax
cmpl $0x2, %eax
je 0x25774
cmpl $0x1, %eax
je 0x257e0
cmpl $0x3e, %ebp
jne 0x25777
jmp 0x25917
incq %r14
movq %r12, %rdi
callq 0x1d2b6
movl %eax, %ebp
cmpl $-0x1, %eax
jne 0x25758
cmpl $0x3e, 0xfd93(%rip)     # 0x35520
jne 0x257b8
testq %r14, %r14
leaq 0x6a3f(%rip), %rax      # 0x2c1d8
leaq 0x6a66(%rip), %rsi      # 0x2c206
cmoveq %rax, %rsi
movq 0xe845(%rip), %rdi      # 0x33ff0
callq 0x42d0
movq %rax, %rdi
callq 0x4130
orl $-0x1, 0xfd61(%rip)     # 0x35520
jmp 0x25953
orl $-0x1, 0xfd55(%rip)     # 0x35520
movq $0x1, (%rbx)
andq $0x0, 0x8(%rbx)
movb $0x0, 0x10(%rbx)
jmp 0x25987
cmpb $0x1, (%rbx,%r13)
jne 0x25a11
movq $0x0, 0x10(%rsp)
cmpl $-0x1, %ebp
je 0x25967
cmpl $0x3e, %ebp
je 0x25967
movq 0x18(%rsp), %rax
cmpb $0x1, 0x4(%rax)
jne 0x25821
movslq %ebp, %rax
cmpb $0x2, (%rbx,%rax)
pushq $0x41
popq %rax
je 0x2581f
movl %ebp, %eax
movl %eax, %ebp
movslq %ebp, %rdi
movzbl (%rbx,%rdi), %r15d
callq 0x4620
cmpl $0x43, %eax
sete %dl
pushq $0x54
popq %rcx
movl %ecx, %r13d
movq 0x18(%rsp), %rsi
testb %dl, 0x5(%rsi)
jne 0x25849
movl %ebp, %r13d
movl %eax, %ecx
cmpl $0x1, %r15d
je 0x25899
cmpl $0x2, %r15d
je 0x25999
movl %r13d, %edi
callq 0x4050
testl %eax, %eax
jne 0x25908
movq 0xe780(%rip), %r15      # 0x33ff0
movq %r15, %rdi
leaq 0x6a25(%rip), %rsi      # 0x2c29f
callq 0x42d0
movl %r13d, %edi
callq 0x45f0
testl %eax, %eax
je 0x258d7
movsbl %r13b, %esi
movq %r15, %rdi
callq 0x4380
jmp 0x25900
addl $-0x41, %ecx
cmpl $0x13, %ecx
ja 0x259f5
movl $0x80045, %eax          # imm = 0x80045
btl %ecx, %eax
jae 0x259f5
incq 0x10(%rsp)
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x25908
movslq %r13d, %rax
leaq 0x10064(%rip), %rcx     # 0x35930
movzbl (%rcx,%rax), %esi
callq 0x269ce
jmp 0x25908
movq %r15, %rdi
leaq 0x34ce(%rip), %rsi      # 0x28daf
callq 0x42d0
movq %rax, %rdi
movl %r13d, %esi
callq 0x45d0
movq %rax, %rdi
leaq 0x4b1d(%rip), %rsi      # 0x2a418
callq 0x42d0
movq %rax, %rdi
callq 0x4130
movq %r12, %rdi
callq 0x1d2b6
movl %eax, %ebp
jmp 0x257f4
cmpl $0x3e, 0xfc02(%rip)     # 0x35520
jne 0x25949
testq %r14, %r14
leaq 0x68ae(%rip), %rax      # 0x2c1d8
leaq 0x68d5(%rip), %rsi      # 0x2c206
cmoveq %rax, %rsi
movq 0xe6b4(%rip), %rdi      # 0x33ff0
callq 0x42d0
movq %rax, %rdi
callq 0x4130
movl $0x3e, 0xfbcd(%rip)     # 0x35520
movq 0x8(%rsp), %rbx
andq $0x0, 0x8(%rbx)
cmpl $0x3e, 0x4(%rsp)
movq %r14, (%rbx)
jmp 0x25983
cmpl $0x3e, 0x4(%rsp)
movl %ebp, 0xfbae(%rip)      # 0x35520
movq 0x8(%rsp), %rbx
movq %r14, (%rbx)
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rbx)
sete 0x10(%rbx)
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r13d, 0xfb80(%rip)     # 0x35520
addl $-0x41, %ecx
cmpl $0x13, %ecx
ja 0x259b6
movl $0x80045, %eax          # imm = 0x80045
btl %ecx, %eax
jb 0x25b3d
cmpl $0x3e, 0x4(%rsp)
jmp 0x25972
leaq 0x6780(%rip), %rdi      # 0x2c144
leaq 0x6783(%rip), %rsi      # 0x2c14e
leaq 0x6789(%rip), %rcx      # 0x2c15b
pushq $0x1a
jmp 0x25b54
leaq 0x67ef(%rip), %rdi      # 0x2c1cf
leaq 0x6767(%rip), %rsi      # 0x2c14e
leaq 0x676d(%rip), %rcx      # 0x2c15b
pushq $0x23
jmp 0x25b54
leaq 0x6841(%rip), %rdi      # 0x2c23d
leaq 0x674b(%rip), %rsi      # 0x2c14e
leaq 0x6751(%rip), %rcx      # 0x2c15b
pushq $0x6f
jmp 0x25b54
movq 0xe5a0(%rip), %r15      # 0x33fb8
leaq 0x273a(%rip), %rsi      # 0x28159
movq %r15, %rdi
callq 0x42d0
pushq $0x1
popq %rbp
movq %rax, %rdi
movl %ebp, %esi
callq 0x45d0
leaq 0x2734(%rip), %r14      # 0x2816f
movq %rax, %rdi
movq %r14, %rsi
callq 0x42d0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rax,%rcx)
movq %rax, %rdi
movl %ebp, %esi
callq 0x45d0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
leaq 0x26f3(%rip), %rsi      # 0x28174
movq %rax, %rdi
callq 0x42d0
movzbl (%rbx,%r13), %esi
movq %rax, %rdi
callq 0x44c0
movq %rax, %rdi
movq %r14, %rsi
callq 0x42d0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x8, %edx
movl %edx, 0x18(%rax,%rcx)
movzbl (%rbx,%r13), %esi
movq %rax, %rdi
callq 0x44c0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl $-0x4b, %edx
orl $0x2, %edx
movl %edx, 0x18(%rax,%rcx)
leaq 0x4939(%rip), %rsi      # 0x2a418
movq %rax, %rdi
callq 0x42d0
movq %rax, %rdi
callq 0x4130
leaq 0x6658(%rip), %r14      # 0x2c14e
movq %r15, %rdi
movq %r14, %rsi
callq 0x42d0
leaq 0x3c33(%rip), %rsi      # 0x2973b
movq %rax, %rdi
callq 0x42d0
pushq $0x64
popq %rbx
movq %rax, %rdi
movl %ebx, %esi
callq 0x45d0
movq %rax, %rdi
callq 0x4130
leaq 0x3aef(%rip), %rdi      # 0x2961b
leaq 0x6628(%rip), %rcx      # 0x2c15b
movq %r14, %rsi
movl %ebx, %edx
callq 0x4190
leaq 0x672a(%rip), %rdi      # 0x2c26e
leaq 0x6603(%rip), %rsi      # 0x2c14e
leaq 0x6609(%rip), %rcx      # 0x2c15b
pushq $0x7a
popq %rdx
callq 0x4190