// File: /workspace/github_c_makefile_star2_O2/DReichLab[P]adna/sam.c
// Function: sam_format1
int sam_format1(const bam_hdr_t *h, const bam1_t *b, kstring_t *str)
{
	int i;
	uint8_t *s;
	const bam1_core_t *c = &b->core;

	str->l = 0;
	kputsn(bam_get_qname(b), c->l_qname-1, str); kputc('\t', str); // query name
	kputw(c->flag, str); kputc('\t', str); // flag
	if (c->tid >= 0) { // chr
		kputs(h->target_name[c->tid] , str);
		kputc('\t', str);
	} else kputsn("*\t", 2, str);
	kputw(c->pos + 1, str); kputc('\t', str); // pos
	kputw(c->qual, str); kputc('\t', str); // qual
	if (c->n_cigar) { // cigar
		uint32_t *cigar = bam_get_cigar(b);
		for (i = 0; i < c->n_cigar; ++i) {
			kputw(bam_cigar_oplen(cigar[i]), str);
			kputc(bam_cigar_opchr(cigar[i]), str);
		}
	} else kputc('*', str);
	kputc('\t', str);
	if (c->mtid < 0) kputsn("*\t", 2, str); // mate chr
	else if (c->mtid == c->tid) kputsn("=\t", 2, str);
	else {
		kputs(h->target_name[c->mtid], str);
		kputc('\t', str);
	}
	kputw(c->mpos + 1, str); kputc('\t', str); // mate pos
	kputw(c->isize, str); kputc('\t', str); // template len
	if (c->l_qseq) { // seq and qual
		uint8_t *s = bam_get_seq(b);
		for (i = 0; i < c->l_qseq; ++i) kputc("=ACMGRSVTWYHKDBN"[bam_seqi(s, i)], str);
		kputc('\t', str);
		s = bam_get_qual(b);
		if (s[0] == 0xff) kputc('*', str);
		else for (i = 0; i < c->l_qseq; ++i) kputc(s[i] + 33, str);
	} else kputsn("*\t*", 3, str);
	s = bam_get_aux(b); // aux
	while (s < b->data + b->l_data) {
		uint8_t type, key[2];
		key[0] = s[0]; key[1] = s[1];
		s += 2; type = *s++;
		kputc('\t', str); kputsn((char*)key, 2, str); kputc(':', str);
		if (type == 'A') { kputsn("A:", 2, str); kputc(*s, str); ++s; }
		else if (type == 'C') { kputsn("i:", 2, str); kputw(*s, str); ++s; }
		else if (type == 'c') { kputsn("i:", 2, str); kputw(*(int8_t*)s, str); ++s; }
		else if (type == 'S') { kputsn("i:", 2, str); kputw(*(uint16_t*)s, str); s += 2; }
		else if (type == 's') { kputsn("i:", 2, str); kputw(*(int16_t*)s, str); s += 2; }
		else if (type == 'I') { kputsn("i:", 2, str); kputuw(*(uint32_t*)s, str); s += 4; }
		else if (type == 'i') { kputsn("i:", 2, str); kputw(*(int32_t*)s, str); s += 4; }
		else if (type == 'f') { ksprintf(str, "f:%g", *(float*)s); s += 4; }
		else if (type == 'd') { ksprintf(str, "d:%g", *(double*)s); s += 8; }
		else if (type == 'Z' || type == 'H') { kputc(type, str); kputc(':', str); while (*s) kputc(*s++, str); ++s; }
		else if (type == 'B') {
			uint8_t sub_type = *(s++);
			int32_t n;
			memcpy(&n, s, 4);
			s += 4; // no point to the start of the array
			kputsn("B:", 2, str); kputc(sub_type, str); // write the typing
			for (i = 0; i < n; ++i) { // FIXME: for better performance, put the loop after "if"
				kputc(',', str);
				if ('c' == sub_type)      { kputw(*(int8_t*)s, str); ++s; }
				else if ('C' == sub_type) { kputw(*(uint8_t*)s, str); ++s; }
				else if ('s' == sub_type) { kputw(*(int16_t*)s, str); s += 2; }
				else if ('S' == sub_type) { kputw(*(uint16_t*)s, str); s += 2; }
				else if ('i' == sub_type) { kputw(*(int32_t*)s, str); s += 4; }
				else if ('I' == sub_type) { kputuw(*(uint32_t*)s, str); s += 4; }
				else if ('f' == sub_type) { ksprintf(str, "%g", *(float*)s); s += 4; }
			}
		}
	}
	return str->l;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %rbx
movq %rsi, %r14
andq $0x0, (%rdx)
movq %rdi, %r15
movq 0x28(%rsi), %rdi
movzbl 0xb(%rsi), %esi
decl %esi
callq 0x8900
pushq $0x9
popq %rbp
movl %ebp, %edi
movq %rbx, %rsi
callq 0x8994
movzwl 0xc(%r14), %edi
movq %rbx, %rsi
callq 0x8b1c
movl %ebp, %edi
movq %rbx, %rsi
callq 0x8994
movslq (%r14), %rax
testq %rax, %rax
js 0x9ae3
movq 0x20(%r15), %rcx
movq (%rcx,%rax,8), %rdi
movq %rbx, %rsi
callq 0x8af9
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x8994
jmp 0x9af5
leaq 0x2cba(%rip), %rdi      # 0xc7a4
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x8900
movl 0x4(%r14), %edi
incl %edi
movq %rbx, %rsi
callq 0x8b1c
pushq $0x9
popq %rbp
movl %ebp, %edi
movq %rbx, %rsi
callq 0x8994
movzbl 0xa(%r14), %edi
movq %rbx, %rsi
callq 0x8b1c
movl %ebp, %edi
movq %rbx, %rsi
callq 0x8994
movq 0x8(%r14), %rax
movq %rax, %rcx
shrq $0x30, %rcx
jne 0x9b61
pushq $0x2a
popq %rdi
movq %rbx, %rsi
callq 0x8994
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x8994
movslq 0x14(%r14), %rax
testq %rax, %rax
js 0x9baa
cmpl (%r14), %eax
jne 0x9bbe
leaq 0x2c48(%rip), %rdi      # 0xc7a7
jmp 0x9bb1
movl %eax, %r12d
shrl $0x18, %r12d
addq 0x28(%r14), %r12
xorl %r13d, %r13d
leaq 0x29e2(%rip), %rbp      # 0xc558
shrq $0x30, %rax
cmpq %rax, %r13
jae 0x9b3f
movl (%r12,%r13,4), %edi
shrl $0x4, %edi
movq %rbx, %rsi
callq 0x8b1c
movl (%r12,%r13,4), %eax
andl $0xf, %eax
movsbl (%rax,%rbp), %edi
movq %rbx, %rsi
callq 0x8994
incq %r13
movq 0x8(%r14), %rax
jmp 0x9b76
leaq 0x2bf3(%rip), %rdi      # 0xc7a4
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x8900
jmp 0x9bd9
movq 0x20(%r15), %rcx
movq (%rcx,%rax,8), %rdi
movq %rbx, %rsi
callq 0x8af9
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x8994
movl 0x18(%r14), %edi
incl %edi
movq %rbx, %rsi
callq 0x8b1c
pushq $0x9
popq %rbp
movl %ebp, %edi
movq %rbx, %rsi
callq 0x8994
movl 0x1c(%r14), %edi
movq %rbx, %rsi
callq 0x8b1c
movl %ebp, %edi
movq %rbx, %rsi
callq 0x8994
movl 0x10(%r14), %eax
testl %eax, %eax
je 0x9cce
movq 0x8(%r14), %r15
movq %r15, %rcx
shrq $0x30, %rcx
shlq $0x2, %rcx
addq 0x28(%r14), %rcx
shrl $0x18, %r15d
addq %rcx, %r15
xorl %ebp, %ebp
leaq 0x2b71(%rip), %r12      # 0xc7aa
xorl %r13d, %r13d
cmpl %eax, %r13d
jge 0x9c70
movl %r13d, %eax
shrl %eax
movzbl (%r15,%rax), %eax
movl %ebp, %ecx
notb %cl
andb $0x4, %cl
shrl %cl, %eax
andl $0xf, %eax
movsbl (%rax,%r12), %edi
movq %rbx, %rsi
callq 0x8994
incl %r13d
movl 0x10(%r14), %eax
addl $0x4, %ebp
jmp 0x9c3c
pushq $0x9
popq %rdi
movq %rbx, %rsi
callq 0x8994
movq 0x8(%r14), %r15
movq %r15, %rax
shrq $0x30, %rax
shlq $0x2, %rax
addq 0x28(%r14), %rax
shrl $0x18, %r15d
addq %rax, %r15
movl 0x10(%r14), %eax
leal 0x1(%rax), %ecx
sarl %ecx
movslq %ecx, %rcx
cmpb $-0x1, (%rcx,%r15)
je 0x9ce2
addq %rcx, %r15
xorl %r12d, %r12d
cltq
cmpq %rax, %r12
jge 0x9ced
movzbl (%r15,%r12), %edi
addl $0x21, %edi
movq %rbx, %rsi
callq 0x8994
incq %r12
movl 0x10(%r14), %eax
jmp 0x9cae
leaq 0x2ae6(%rip), %rdi      # 0xc7bb
pushq $0x3
popq %rsi
movq %rbx, %rdx
callq 0x8900
jmp 0x9ced
pushq $0x2a
popq %rdi
movq %rbx, %rsi
callq 0x8994
movq 0x8(%r14), %rcx
movq 0x28(%r14), %rax
movq %rcx, %rdx
shrq $0x30, %rdx
leaq (%rax,%rdx,4), %rdx
shrl $0x18, %ecx
addq %rdx, %rcx
movslq 0x10(%r14), %rdx
leal 0x1(%rdx), %esi
sarl %esi
movslq %esi, %r12
addq %rcx, %r12
addq %rdx, %r12
pushq $0x9
popq %rbp
movslq 0x20(%r14), %rcx
addq %rax, %rcx
cmpq %rcx, %r12
jae 0xa00d
movb (%r12), %al
movb %al, 0x6(%rsp)
movb 0x1(%r12), %al
movb %al, 0x7(%rsp)
leaq 0x3(%r12), %r13
movzbl 0x2(%r12), %r15d
movl %ebp, %edi
movq %rbx, %rsi
callq 0x8994
leaq 0x6(%rsp), %rdi
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x8900
pushq $0x3a
popq %rdi
movq %rbx, %rsi
callq 0x8994
leal -0x41(%r15), %eax
cmpl $0x8, %eax
jbe 0x9dab
leal -0x63(%r15), %eax
cmpl $0x6, %eax
ja 0x9ddd
leaq 0x26ab(%rip), %rcx      # 0xc430
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x2a2d(%rip), %rdi      # 0xc7c2
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x8900
movsbl 0x3(%r12), %edi
jmp 0x9e53
leaq 0x265a(%rip), %rcx      # 0xc40c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x29fd(%rip), %rdi      # 0xc7bf
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x8900
movzbl 0x3(%r12), %edi
movq %rbx, %rsi
callq 0x8994
jmp 0x9e5b
cmpl $0x53, %r15d
je 0x9fda
cmpl $0x5a, %r15d
je 0x9e14
cmpl $0x73, %r15d
jne 0xa001
leaq 0x29c4(%rip), %rdi      # 0xc7c2
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x8900
movswl 0x3(%r12), %edi
jmp 0x9ff2
movl %r15d, %edi
movq %rbx, %rsi
callq 0x8994
pushq $0x3a
popq %rdi
movq %rbx, %rsi
callq 0x8994
movzbl (%r13), %edi
incq %r13
testl %edi, %edi
jne 0x9e22
jmp 0xa001
leaq 0x2980(%rip), %rdi      # 0xc7c2
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x8900
movzbl 0x3(%r12), %edi
movq %rbx, %rsi
callq 0x8b1c
addq $0x4, %r12
jmp 0x9ffe
movzbl 0x3(%r12), %r15d
movl 0x4(%r12), %ebp
addq $0x8, %r12
leaq 0x2955(%rip), %rdi      # 0xc7cf
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x8900
movl %r15d, %edi
movq %rbx, %rsi
callq 0x8994
testl %ebp, %ebp
movl $0x0, %eax
cmovlel %eax, %ebp
movq %r12, %r13
pushq $0x2c
popq %r12
subl $0x1, %ebp
jb 0x9f54
movl %r12d, %edi
movq %rbx, %rsi
callq 0x8994
cmpl $0x43, %r15d
je 0x9f14
cmpl $0x49, %r15d
je 0x9f3f
cmpl $0x53, %r15d
je 0x9f29
cmpl $0x73, %r15d
je 0x9ee6
cmpl $0x66, %r15d
je 0x9efb
cmpl $0x69, %r15d
je 0x9eed
cmpl $0x63, %r15d
jne 0x9ea1
movsbl (%r13), %edi
jmp 0x9f19
movswl (%r13), %edi
jmp 0x9f2e
movl (%r13), %edi
movq %rbx, %rsi
callq 0x8b1c
jmp 0x9f4b
cvtss2sd (%r13), %xmm0
movq %rbx, %rdi
leaq 0x28c1(%rip), %rsi      # 0xc7cc
movb $0x1, %al
callq 0xa0e3
jmp 0x9f4b
movzbl (%r13), %edi
movq %rbx, %rsi
callq 0x8b1c
incq %r13
jmp 0x9ea1
movzwl (%r13), %edi
movq %rbx, %rsi
callq 0x8b1c
addq $0x2, %r13
jmp 0x9ea1
movl (%r13), %edi
movq %rbx, %rsi
callq 0xa01e
addq $0x4, %r13
jmp 0x9ea1
pushq $0x9
popq %rbp
jmp 0xa001
leaq 0x285f(%rip), %rdi      # 0xc7c2
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x8900
movl 0x3(%r12), %edi
movq %rbx, %rsi
callq 0xa01e
jmp 0x9fd4
movsd 0x3(%r12), %xmm0
movq %rbx, %rdi
leaq 0x283c(%rip), %rsi      # 0xc7ca
movb $0x1, %al
callq 0xa0e3
addq $0xb, %r12
jmp 0x9ffe
leaq 0x2820(%rip), %rdi      # 0xc7c2
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x8900
movl 0x3(%r12), %edi
movq %rbx, %rsi
callq 0x8b1c
jmp 0x9fd4
cvtss2sd 0x3(%r12), %xmm0
movq %rbx, %rdi
leaq 0x27f8(%rip), %rsi      # 0xc7c5
movb $0x1, %al
callq 0xa0e3
addq $0x7, %r12
jmp 0x9ffe
leaq 0x27e1(%rip), %rdi      # 0xc7c2
pushq $0x2
popq %rsi
movq %rbx, %rdx
callq 0x8900
movzwl 0x3(%r12), %edi
movq %rbx, %rsi
callq 0x8b1c
addq $0x5, %r12
movq %r12, %r13
movq 0x28(%r14), %rax
movq %r13, %r12
jmp 0x9d1b
movl (%rbx), %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq