// File: /workspace/github_cpp_makefile_star2_O1/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, 0x20(%rsp)
cmpb $0x0, 0x188(%rdx)
movl 0x38(%rdx), %eax
je 0x508fd
cmpl $0x3, %eax
jl 0x50948
xorl %r13d, %r13d
leaq 0x10(%rsp), %r12
cmpb $0x0, 0x1f2(%r15)
jne 0x508d3
movq 0x20(%r14), %rax
movsbl (%rax,%r13), %edi
callq 0x9080
testl %eax, %eax
jne 0x50948
movq 0x20(%r14), %rax
movb (%rax,%r13), %al
movb %al, 0x10(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4f02e
incq %r13
movl 0x38(%r14), %eax
addl $-0x2, %eax
cltq
cmpq %rax, %r13
jl 0x508b7
jmp 0x50948
testl %eax, %eax
jle 0x50948
xorl %r13d, %r13d
leaq 0x10(%rsp), %r12
cmpb $0x0, 0x1f2(%r15)
jne 0x50925
movq 0x20(%r14), %rax
movsbl (%rax,%r13), %edi
callq 0x9080
testl %eax, %eax
jne 0x50948
movq 0x20(%r14), %rax
movb (%rax,%r13), %al
movb %al, 0x10(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x4f02e
incq %r13
movslq 0x38(%r14), %rax
cmpq %rax, %r13
jl 0x50909
leaq 0x10(%rsp), %rsi
movb $0x9, (%rsi)
movq %rbx, %rdi
callq 0x4f02e
movl $0x0, 0x1c(%rsp)
movzbl 0x188(%r14), %eax
cmpl $0x2, %eax
je 0x5097c
cmpl $0x1, %eax
jne 0x50984
movl $0x43, 0x1c(%rsp)
jmp 0x50984
movl $0x83, 0x1c(%rsp)
cmpb $0x0, 0x17c(%r14)
jne 0x50993
orb $0x10, 0x1c(%rsp)
testb %al, %al
je 0x509a6
cmpb $0x0, 0x17d(%r14)
jne 0x509a6
orb $0x20, 0x1c(%rsp)
leaq 0x1c(%rsp), %rsi
movq %rbx, %rdi
callq 0x511ff
leaq 0x7b9a(%rip), %rsi      # 0x58554
movl $0x1, %edx
movq %rax, %rdi
callq 0x512bc
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x509f6
movl (%r14), %esi
cmpq %rsi, 0x18(%rax)
jbe 0x509f6
shlq $0x5, %rsi
addq 0x8(%rax), %rsi
movzbl 0x1f1(%r15), %edx
xorl $0x1, %edx
movq %rbx, %rdi
callq 0x50fee
jmp 0x50a01
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5115d
movb $0x9, %bpl
leaq 0x10(%rsp), %r15
movb %bpl, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4f02e
movl 0x4(%r14), %eax
incl %eax
movl %eax, (%r15)
leaq 0x10(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5115d
movb %bpl, (%r15)
leaq 0x10(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4f02e
leaq 0x24(%rsp), %rsi
movq %rbx, %rdi
callq 0x511ff
movb %bpl, (%r15)
leaq 0x10(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4f02e
movq 0x68(%r14), %rax
movq %rax, (%r15)
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x510b6
leaq 0x18(%rsp), %rsi
movb $0x4d, (%rsi)
movq %rax, %rdi
callq 0x4f02e
cmpb $0x0, 0x188(%r14)
leaq 0x7a8a(%rip), %rax      # 0x58520
leaq 0x7a9b(%rip), %rsi      # 0x58538
cmoveq %rax, %rsi
movl $0x2, %edx
movq %rbx, %rdi
callq 0x512bc
cmpb $0x0, 0x188(%r14)
je 0x50ae4
leaq 0x10(%rsp), %r15
movb $0x9, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4f02e
movl 0xc(%r14), %eax
incl %eax
movl %eax, (%r15)
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x5115d
jmp 0x50af8
leaq 0x7a50(%rip), %rsi      # 0x5853b
movl $0x2, %edx
movq %rbx, %rdi
callq 0x512bc
leaq 0x10(%rsp), %rsi
movb $0x9, (%rsi)
movq %rbx, %rdi
callq 0x4f02e
cmpb $0x0, 0x188(%r14)
je 0x50b30
movq $0x0, 0x10(%rsp)
movl 0x4(%r14), %ecx
movl 0xc(%r14), %eax
cmpl %eax, %ecx
jbe 0x50b42
addq 0x68(%r14), %rcx
subq %rcx, %rax
jmp 0x50b50
leaq 0x10(%rsp), %rsi
movb $0x30, (%rsi)
movq %rbx, %rdi
callq 0x4f02e
jmp 0x50b60
subq %rcx, %rax
movzwl 0x17e(%r14), %ecx
addq %rcx, %rax
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movq %rbx, %rdi
callq 0x51246
leaq 0x10(%rsp), %rsi
movb $0x9, (%rsi)
movq %rbx, %rdi
callq 0x4f02e
cmpq $0x0, 0x68(%r14)
je 0x50bab
xorl %r12d, %r12d
leaq 0x5bd6(%rip), %r13      # 0x56757
leaq 0x10(%rsp), %r15
movq 0x50(%r14), %rax
movsbq (%rax,%r12), %rax
movb (%rax,%r13), %al
movb %al, 0x10(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4f02e
incq %r12
cmpq 0x68(%r14), %r12
jb 0x50b86
leaq 0x10(%rsp), %rsi
movb $0x9, (%rsi)
movq %rbx, %rdi
callq 0x4f02e
cmpq $0x0, 0x98(%r14)
je 0x50bf3
xorl %r12d, %r12d
leaq 0x10(%rsp), %r15
movq 0x80(%r14), %rax
movb (%rax,%r12), %al
movb %al, 0x10(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4f02e
incq %r12
cmpq 0x98(%r14), %r12
jb 0x50bcd
leaq 0x7944(%rip), %rsi      # 0x5853e
movl $0x6, %edx
movq %rbx, %rdi
callq 0x512bc
movsbl 0x180(%r14), %eax
leaq 0x10(%rsp), %r15
movl %eax, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x511ff
movq 0x68(%r14), %r13
xorl %eax, %eax
movl %eax, (%r15)
movl %eax, 0x18(%rsp)
leaq 0x790f(%rip), %rsi      # 0x58545
movl $0x6, %edx
movq %rbx, %rdi
callq 0x512bc
cmpb $0x1, 0x17c(%r14)
jne 0x50cc5
testl %r13d, %r13d
jle 0x50d42
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
xorl %r15d, %r15d
leaq 0xf(%rsp), %r12
movl %r15d, %eax
shrl $0x5, %eax
movl 0xb0(%r14,%rax,4), %eax
btl %r15d, %eax
jae 0x50cb1
incl 0x10(%rsp)
movq 0x140(%r14), %rax
movsbl (%rax,%r15), %edi
callq 0x9990
movl %eax, %ebp
movq %rbx, %rdi
leaq 0x18(%rsp), %rsi
callq 0x511ff
movb %bpl, 0xf(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x4f02e
xorl %eax, %eax
jmp 0x50cb7
movl 0x18(%rsp), %eax
incl %eax
movl %eax, 0x18(%rsp)
incq %r15
cmpq %r15, %r13
jne 0x50c65
jmp 0x50d42
testl %r13d, %r13d
jle 0x50d42
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
decq %r13
leaq 0x18(%rsp), %r15
leaq 0xf(%rsp), %r12
movl %r13d, %eax
shrl $0x5, %eax
movl 0xb0(%r14,%rax,4), %eax
btl %r13d, %eax
jae 0x50d28
incl 0x10(%rsp)
movq 0x140(%r14), %rax
movsbl (%rax,%r13), %edi
callq 0x9990
movl %eax, %ebp
movq %rbx, %rdi
movq %r15, %rsi
callq 0x511ff
movb %bpl, 0xf(%rsp)
movq %rax, %rdi
movq %r12, %rsi
callq 0x4f02e
xorl %eax, %eax
jmp 0x50d2e
movl 0x18(%rsp), %eax
incl %eax
movl %eax, 0x18(%rsp)
leaq -0x1(%r13), %rax
incq %r13
cmpq $0x1, %r13
movq %rax, %r13
jg 0x50cde
leaq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0x511ff
leaq 0x77f6(%rip), %rsi      # 0x5854c
movl $0x6, %edx
movq %rbx, %rdi
callq 0x512bc
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x511ff
cmpl $0x0, 0x20(%rsp)
jle 0x50d98
leaq 0x77b3(%rip), %rsi      # 0x58531
movl $0x6, %edx
movq %rbx, %rdi
callq 0x512bc
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x511ff
leaq 0xf(%rsp), %rsi
movb $0xa, (%rsi)
movq %rbx, %rdi
callq 0x4f02e
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop