// File: /workspace/github_cpp_makefile_star2_O3/BenLangmead[P]bowtie/sam.cpp
// Function: SAMHitSink::append(SStringExpandable<char, 25, 2, 0>&, Hit const&, int, int)
void SAMHitSink::append(BTString& o, const Hit& h, int mapq, int xms) {
	// QNAME
	if(h.mate > 0) {
		// truncate final 2 chars
		for(int i = 0; i < (int)h.patName.length()-2; i++) {
			if(!noQnameTrunc_ && isspace((int)h.patName[i])) break;
			o << h.patName[i];
		}
	} else {
		for(int i = 0; i < (int)h.patName.length(); i++) {
			if(!noQnameTrunc_ && isspace((int)h.patName[i])) break;
			o << h.patName[i];
		}
	}
	o << '\t';
	// FLAG
	int flags = 0;
	if(h.mate == 1) {
		flags |= SAM_FLAG_PAIRED | SAM_FLAG_FIRST_IN_PAIR | SAM_FLAG_MAPPED_PAIRED;
	} else if(h.mate == 2) {
		flags |= SAM_FLAG_PAIRED | SAM_FLAG_SECOND_IN_PAIR | SAM_FLAG_MAPPED_PAIRED;
	}
	if(!h.fw) flags |= SAM_FLAG_QUERY_STRAND;
	if(h.mate > 0 && !h.mfw) flags |= SAM_FLAG_MATE_STRAND;
	o << flags << "\t";
	// RNAME
	if(_refnames != NULL && h.h.first < _refnames->size()) {
		printUptoWs(o, (*_refnames)[h.h.first], !fullRef_);
	} else {
		o << h.h.first;
	}
	// POS
	o << '\t' << (h.h.second + 1);
	// MAPQ
	o << '\t' << mapq;
	// CIGAR
	o << '\t' << h.length() << 'M';
	// MRNM
	if(h.mate > 0) {
		o << "\t=";
	} else {
		o << "\t*";
	}
	// MPOS
	if(h.mate > 0) {
		o << '\t' << (h.mh.second + 1);
	} else {
		o << "\t0";
	}
	// ISIZE
	o << '\t';
	if(h.mate > 0) {
		assert_eq(h.h.first, h.mh.first);
		int64_t inslen = 0;
		if(h.h.second > h.mh.second) {
			inslen = (int64_t)h.h.second - (int64_t)h.mh.second + (int64_t)h.length();
			inslen = -inslen;
		} else {
			inslen = (int64_t)h.mh.second - (int64_t)h.h.second + (int64_t)h.mlen;
		}
		o << inslen;
	} else {
		o << '0';
	}
	// SEQ
	o << '\t';
	for(size_t i = 0; i < h.patSeq.length(); i++) {
		o << (char)h.patSeq.toChar(i);
	}
	// QUAL
	o << '\t';
	for(size_t i = 0; i < h.quals.length(); i++) {
		o << (char)h.quals[i];
	}
	//
	// Optional fields
	//
	// Always output stratum
	o << "\tXA:i:" << (int)h.stratum;
	// Always output cost
	//ss << "\tXC:i:" << (int)h.cost;
	// Look for SNP annotations falling within the alignment
	// Output MD field
	size_t len = h.patSeq.length();
	int nm = 0;
	int run = 0;
	o << "\tMD:Z:";
	const FixedBitset<1024> *mms = &h.mms;
	ASSERT_ONLY(const BTDnaString* pat = &h.patSeq);
	const EList<char>* refcs = &h.refcs;
	if(h.fw) {
		for (int i = 0; i < (int)len; ++ i) {
			if(mms->test(i)) {
				nm++;
				// There's a mismatch at this position
				assert_gt((int)refcs->size(), i);
				char refChar = toupper((*refcs)[i]);
				ASSERT_ONLY(char qryChar = (h.fw ? (*pat)[i] : (*pat)[len-i-1]));
				assert_neq(refChar, qryChar);
				o << run << refChar;
				run = 0;
			} else {
				run++;
			}
		}
	} else {
		for (int i = (int)len-1; i >= 0; -- i) {
			if(mms->test(i)) {
				nm++;
				// There's a mismatch at this position
				assert_gt((int)refcs->size(), i);
				char refChar = toupper((*refcs)[i]);
				ASSERT_ONLY(char qryChar = (h.fw ? (*pat)[i] : (*pat)[len-i-1]));
				assert_neq(refChar, qryChar);
				o << run << refChar;
				run = 0;
			} else {
				run++;
			}
		}
	}
	o << run;
	// Add optional edit distance field
	o << "\tNM:i:" << nm;
	if(xms > 0) {
		o << "\tXM:i:" << xms;
	}
	o << '\n';
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
movl %ecx, 0x24(%rsp)
movl %r8d, 0x1c(%rsp)
movl %r8d, 0x20(%rsp)
cmpb $0x0, 0x188(%rdx)
movl 0x38(%rdx), %eax
je 0x4f588
cmpl $0x3, %eax
jl 0x4f5cf
xorl %r13d, %r13d
leaq 0x8(%rsp), %r12
movq 0x20(%r14), %rax
movb (%rax,%r13), %bpl
cmpb $0x0, 0x1f2(%r15)
jne 0x4f565
movsbl %bpl, %edi
callq 0x9080
testl %eax, %eax
jne 0x4f5cf
movb %bpl, 0x8(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4dc0a
incq %r13
movl 0x38(%r14), %eax
addl $-0x2, %eax
cltq
cmpq %rax, %r13
jl 0x4f546
jmp 0x4f5cf
testl %eax, %eax
jle 0x4f5cf
xorl %r13d, %r13d
leaq 0x8(%rsp), %r12
movq 0x20(%r14), %rax
movb (%rax,%r13), %bpl
cmpb $0x0, 0x1f2(%r15)
jne 0x4f5b3
movsbl %bpl, %edi
callq 0x9080
testl %eax, %eax
jne 0x4f5cf
movb %bpl, 0x8(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4dc0a
incq %r13
movslq 0x38(%r14), %rax
cmpq %rax, %r13
jl 0x4f594
leaq 0x8(%rsp), %rsi
movb $0x9, (%rsi)
movq %rbx, %rdi
callq 0x4dc0a
movl $0x0, 0x18(%rsp)
movzbl 0x188(%r14), %ecx
cmpl $0x2, %ecx
je 0x4f60a
xorl %eax, %eax
cmpl $0x1, %ecx
jne 0x4f617
movl $0x43, 0x18(%rsp)
movl $0x43, %eax
jmp 0x4f617
movl $0x83, 0x18(%rsp)
movl $0x83, %eax
cmpb $0x0, 0x17c(%r14)
jne 0x4f628
orl $0x10, %eax
movl %eax, 0x18(%rsp)
testb %cl, %cl
je 0x4f63d
cmpb $0x0, 0x17d(%r14)
jne 0x4f63d
orl $0x20, %eax
movl %eax, 0x18(%rsp)
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x4fe23
leaq 0x7fe5(%rip), %rsi      # 0x57636
movl $0x1, %edx
movq %rax, %rdi
callq 0x4fee0
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x4f68d
movl (%r14), %esi
cmpq %rsi, 0x18(%rax)
jbe 0x4f68d
shlq $0x5, %rsi
addq 0x8(%rax), %rsi
movzbl 0x1f1(%r15), %edx
xorl $0x1, %edx
movq %rbx, %rdi
callq 0x4fc12
jmp 0x4f698
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4fd81
movb $0x9, %bpl
leaq 0x8(%rsp), %r15
movb %bpl, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4dc0a
movl 0x4(%r14), %eax
incl %eax
movl %eax, (%r15)
leaq 0x8(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4fd81
movb %bpl, (%r15)
leaq 0x8(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4dc0a
leaq 0x24(%rsp), %rsi
movq %rbx, %rdi
callq 0x4fe23
movb %bpl, (%r15)
leaq 0x8(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4dc0a
movq 0x68(%r14), %rax
movq %rax, (%r15)
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x4fcda
leaq 0x14(%rsp), %rsi
movb $0x4d, (%rsi)
movq %rax, %rdi
callq 0x4dc0a
cmpb $0x0, 0x188(%r14)
leaq 0x7ed5(%rip), %rax      # 0x57602
leaq 0x7ee6(%rip), %rsi      # 0x5761a
cmoveq %rax, %rsi
movl $0x2, %edx
movq %rbx, %rdi
callq 0x4fee0
cmpb $0x0, 0x188(%r14)
je 0x4f77b
leaq 0x8(%rsp), %r15
movb $0x9, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4dc0a
movl 0xc(%r14), %eax
incl %eax
movl %eax, (%r15)
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x4fd81
jmp 0x4f78f
leaq 0x7e9b(%rip), %rsi      # 0x5761d
movl $0x2, %edx
movq %rbx, %rdi
callq 0x4fee0
leaq 0x8(%rsp), %rsi
movb $0x9, (%rsi)
movq %rbx, %rdi
callq 0x4dc0a
cmpb $0x0, 0x188(%r14)
je 0x4f7be
movl 0x4(%r14), %ecx
movl 0xc(%r14), %eax
cmpl %eax, %ecx
jbe 0x4f7d0
addq 0x68(%r14), %rcx
subq %rcx, %rax
jmp 0x4f7de
leaq 0x8(%rsp), %rsi
movb $0x30, (%rsi)
movq %rbx, %rdi
callq 0x4dc0a
jmp 0x4f7ee
subq %rcx, %rax
movzwl 0x17e(%r14), %ecx
addq %rcx, %rax
leaq 0x8(%rsp), %rsi
movq %rax, (%rsi)
movq %rbx, %rdi
callq 0x4fe6a
leaq 0x8(%rsp), %rsi
movb $0x9, (%rsi)
movq %rbx, %rdi
callq 0x4dc0a
cmpq $0x0, 0x68(%r14)
je 0x4f839
xorl %r12d, %r12d
leaq 0x6028(%rip), %r13      # 0x55837
leaq 0x8(%rsp), %r15
movq 0x50(%r14), %rax
movsbq (%rax,%r12), %rax
movb (%rax,%r13), %al
movb %al, 0x8(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4dc0a
incq %r12
cmpq 0x68(%r14), %r12
jb 0x4f814
leaq 0x8(%rsp), %rsi
movb $0x9, (%rsi)
movq %rbx, %rdi
callq 0x4dc0a
cmpq $0x0, 0x98(%r14)
je 0x4f881
xorl %r12d, %r12d
leaq 0x8(%rsp), %r15
movq 0x80(%r14), %rax
movb (%rax,%r12), %al
movb %al, 0x8(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4dc0a
incq %r12
cmpq 0x98(%r14), %r12
jb 0x4f85b
leaq 0x7d98(%rip), %rsi      # 0x57620
movl $0x6, %edx
movq %rbx, %rdi
callq 0x4fee0
movsbl 0x180(%r14), %eax
leaq 0x8(%rsp), %r15
movl %eax, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4fe23
movq 0x68(%r14), %rbp
xorl %eax, %eax
movl %eax, (%r15)
movl %eax, 0x14(%rsp)
leaq 0x7d63(%rip), %rsi      # 0x57627
movl $0x6, %edx
movq %rbx, %rdi
callq 0x4fee0
cmpb $0x1, 0x17c(%r14)
jne 0x4f954
testl %ebp, %ebp
jle 0x4f9cf
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
xorl %eax, %eax
xorl %r15d, %r15d
xorl %r12d, %r12d
movl %r12d, %ecx
shrl $0x5, %ecx
movl 0xb0(%r14,%rcx,4), %ecx
btl %r12d, %ecx
jae 0x4f944
incl %r15d
movl %r15d, 0x8(%rsp)
movq 0x140(%r14), %rax
movsbl (%rax,%r12), %edi
callq 0x9990
movl %eax, %r13d
movq %rbx, %rdi
leaq 0x14(%rsp), %rsi
callq 0x4fe23
movb %r13b, 0x7(%rsp)
movq %rax, %rdi
leaq 0x7(%rsp), %rsi
callq 0x4dc0a
xorl %eax, %eax
jmp 0x4f946
incl %eax
movl %eax, 0x14(%rsp)
incq %r12
cmpq %r12, %rbp
jne 0x4f8f1
jmp 0x4f9cf
testl %ebp, %ebp
jle 0x4f9cf
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
decq %rbp
xorl %eax, %eax
leaq 0x7(%rsp), %r12
xorl %r15d, %r15d
movl %ebp, %ecx
shrl $0x5, %ecx
movl 0xb0(%r14,%rcx,4), %ecx
btl %ebp, %ecx
jae 0x4f9b9
incl %r15d
movl %r15d, 0x8(%rsp)
movq 0x140(%r14), %rax
movsbl (%rax,%rbp), %edi
callq 0x9990
movl %eax, %r13d
movq %rbx, %rdi
leaq 0x14(%rsp), %rsi
callq 0x4fe23
movb %r13b, 0x7(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x4dc0a
xorl %eax, %eax
jmp 0x4f9bb
incl %eax
movl %eax, 0x14(%rsp)
leaq -0x1(%rbp), %rcx
incq %rbp
cmpq $0x1, %rbp
movq %rcx, %rbp
ja 0x4f96b
leaq 0x14(%rsp), %rsi
movq %rbx, %rdi
callq 0x4fe23
leaq 0x7c4b(%rip), %rsi      # 0x5762e
movl $0x6, %edx
movq %rbx, %rdi
callq 0x4fee0
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x4fe23
cmpl $0x0, 0x1c(%rsp)
jle 0x4fa25
leaq 0x7c08(%rip), %rsi      # 0x57613
movl $0x6, %edx
movq %rbx, %rdi
callq 0x4fee0
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x4fe23
leaq 0x7(%rsp), %rsi
movb $0xa, (%rsi)
movq %rbx, %rdi
callq 0x4dc0a
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq