// File: /workspace/github_cpp_makefile_star2_O2/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)
je 0x44c04
xorl %r13d, %r13d
movl 0x38(%r14), %eax
addl $-0x2, %eax
cltq
cmpq %rax, %r13
jge 0x44c3f
movq 0x20(%r14), %rax
movsbl (%rax,%r13), %r12d
cmpb $0x0, 0x1f2(%r15)
jne 0x44bf4
movl %r12d, %edi
callq 0x9070
testl %eax, %eax
jne 0x44c3f
movq %rbx, %rdi
movl %r12d, %esi
callq 0x45297
incq %r13
jmp 0x44bc7
xorl %r13d, %r13d
movslq 0x38(%r14), %rax
cmpq %rax, %r13
jge 0x44c3f
movq 0x20(%r14), %rax
movsbl (%rax,%r13), %r12d
cmpb $0x0, 0x1f2(%r15)
jne 0x44c2f
movl %r12d, %edi
callq 0x9070
testl %eax, %eax
jne 0x44c3f
movq %rbx, %rdi
movl %r12d, %esi
callq 0x45297
incq %r13
jmp 0x44c07
pushq $0x9
popq %rsi
movq %rbx, %rdi
callq 0x45297
andl $0x0, 0xc(%rsp)
movzbl 0x188(%r14), %ecx
cmpl $0x2, %ecx
je 0x44c70
xorl %eax, %eax
cmpl $0x1, %ecx
jne 0x44c7d
movl $0x43, 0xc(%rsp)
pushq $0x43
popq %rax
jmp 0x44c7d
movl $0x83, 0xc(%rsp)
movl $0x83, %eax
cmpb $0x0, 0x17c(%r14)
jne 0x44c8e
orl $0x10, %eax
movl %eax, 0xc(%rsp)
testb %cl, %cl
je 0x44ca3
cmpb $0x0, 0x17d(%r14)
jne 0x44ca3
orl $0x20, %eax
movl %eax, 0xc(%rsp)
leaq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0x452f4
leaq 0x78bd(%rip), %rsi      # 0x4c574
movq %rax, %rdi
callq 0x453a8
movq 0x10(%r15), %rax
testq %rax, %rax
je 0x44cee
movl (%r14), %esi
cmpq %rsi, 0x18(%rax)
jbe 0x44cee
shlq $0x5, %rsi
addq 0x8(%rax), %rsi
movzbl 0x1f1(%r15), %edx
xorl $0x1, %edx
movq %rbx, %rdi
callq 0x451d8
jmp 0x44cf9
movq %rbx, %rdi
movq %r14, %rsi
callq 0x452b8
pushq $0x9
popq %r15
movq %rbx, %rdi
movl %r15d, %esi
callq 0x45297
movl 0x4(%r14), %ecx
incl %ecx
leaq 0x10(%rsp), %r12
movl %ecx, (%r12)
movq %rax, %rdi
movq %r12, %rsi
callq 0x452b8
movq %rbx, %rdi
movl %r15d, %esi
callq 0x45297
leaq 0x24(%rsp), %rsi
movq %rax, %rdi
callq 0x452f4
movq %rbx, %rdi
movl %r15d, %esi
callq 0x45297
movq 0x68(%r14), %rcx
movq %rcx, (%r12)
leaq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0x45259
pushq $0x4d
popq %rsi
movq %rax, %rdi
callq 0x45297
cmpb $0x0, 0x188(%r14)
leaq 0x77cc(%rip), %rax      # 0x4c540
leaq 0x77dd(%rip), %rsi      # 0x4c558
cmoveq %rax, %rsi
movq %rbx, %rdi
callq 0x453a8
cmpb $0x0, 0x188(%r14)
je 0x44db3
pushq $0x9
popq %rsi
movq %rbx, %rdi
callq 0x45297
movl 0xc(%r14), %ecx
incl %ecx
leaq 0x10(%rsp), %rsi
movl %ecx, (%rsi)
movq %rax, %rdi
callq 0x452b8
jmp 0x44dc2
leaq 0x77a1(%rip), %rsi      # 0x4c55b
movq %rbx, %rdi
callq 0x453a8
pushq $0x9
popq %rsi
movq %rbx, %rdi
callq 0x45297
cmpb $0x0, 0x188(%r14)
je 0x44dec
movl 0x4(%r14), %ecx
movl 0xc(%r14), %eax
cmpl %eax, %ecx
jbe 0x44df9
addq 0x68(%r14), %rcx
subq %rcx, %rax
jmp 0x44e07
pushq $0x30
popq %rsi
movq %rbx, %rdi
callq 0x45297
jmp 0x44e17
subq %rcx, %rax
movzwl 0x17e(%r14), %ecx
addq %rcx, %rax
leaq 0x10(%rsp), %rsi
movq %rax, (%rsi)
movq %rbx, %rdi
callq 0x45330
pushq $0x9
popq %rsi
movq %rbx, %rdi
callq 0x45297
xorl %r15d, %r15d
leaq 0x5976(%rip), %r12      # 0x4a7a2
cmpq 0x68(%r14), %r15
jae 0x44e4d
movq 0x50(%r14), %rax
movsbq (%rax,%r15), %rax
movsbl (%rax,%r12), %esi
movq %rbx, %rdi
callq 0x45297
incq %r15
jmp 0x44e2c
pushq $0x9
popq %rsi
movq %rbx, %rdi
callq 0x45297
xorl %r15d, %r15d
cmpq 0x98(%r14), %r15
jae 0x44e7d
movq 0x80(%r14), %rax
movsbl (%rax,%r15), %esi
movq %rbx, %rdi
callq 0x45297
incq %r15
jmp 0x44e5b
leaq 0x76da(%rip), %rsi      # 0x4c55e
movq %rbx, %rdi
callq 0x453a8
movsbl 0x180(%r14), %eax
leaq 0x10(%rsp), %r15
movl %eax, (%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x452f4
movl 0x68(%r14), %r13d
andl $0x0, (%r15)
andl $0x0, 0x8(%rsp)
leaq 0x76aa(%rip), %rsi      # 0x4c565
movq %rbx, %rdi
callq 0x453a8
cmpb $0x1, 0x17c(%r14)
jne 0x44f3a
xorl %eax, %eax
testl %r13d, %r13d
cmovlel %eax, %r13d
xorl %ebp, %ebp
xorl %r15d, %r15d
cmpq %r15, %r13
je 0x44fa5
movl %r15d, %ecx
shrl $0x5, %ecx
movl 0xb0(%r14,%rcx,4), %ecx
btl %r15d, %ecx
jae 0x44f2f
incl %ebp
movl %ebp, 0x10(%rsp)
movq 0x140(%r14), %rax
movsbl (%rax,%r15), %edi
callq 0x99b0
movl %eax, %r12d
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
callq 0x452f4
movsbl %r12b, %esi
movq %rax, %rdi
callq 0x45297
xorl %eax, %eax
jmp 0x44f31
incl %eax
movl %eax, 0x8(%rsp)
incq %r15
jmp 0x44edb
xorl %eax, %eax
xorl %ebp, %ebp
testl %r13d, %r13d
jle 0x44fa5
leaq -0x1(%r13), %r15
movl %r15d, %ecx
shrl $0x5, %ecx
movl 0xb0(%r14,%rcx,4), %ecx
decb %r13b
movzbl %r13b, %edx
btl %edx, %ecx
jae 0x44f9a
incl %ebp
movl %ebp, 0x10(%rsp)
movl %r15d, %eax
movq 0x140(%r14), %rcx
movsbl (%rcx,%rax), %edi
callq 0x99b0
movl %eax, %r12d
movq %rbx, %rdi
leaq 0x8(%rsp), %rsi
callq 0x452f4
movsbl %r12b, %esi
movq %rax, %rdi
callq 0x45297
xorl %eax, %eax
jmp 0x44f9c
incl %eax
movl %eax, 0x8(%rsp)
movq %r15, %r13
jmp 0x44f3e
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x452f4
leaq 0x75b3(%rip), %rsi      # 0x4c56c
movq %rbx, %rdi
callq 0x453a8
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x452f4
cmpl $0x0, 0x1c(%rsp)
jle 0x44ff1
leaq 0x7575(%rip), %rsi      # 0x4c551
movq %rbx, %rdi
callq 0x453a8
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x452f4
pushq $0xa
popq %rsi
movq %rbx, %rdi
callq 0x45297
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop