// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/sam_view.c
// Function: main_bam2fq
int main_bam2fq(int argc, char *argv[])
{
	bamFile fp;
	bam_header_t *h;
	bam1_t *b;
	int8_t *buf;
	int max_buf, c, no12 = 0;
	while ((c = getopt(argc, argv, "n")) > 0)
		if (c == 'n') no12 = 1;
	if (argc == 1) {
		fprintf(stderr, "Usage: samtools bam2fq <in.bam>\n");
		return 1;
	}
	fp = strcmp(argv[optind], "-")? bam_open(argv[optind], "r") : bam_dopen(fileno(stdin), "r");
	if (fp == 0) return 1;
	h = bam_header_read(fp);
	b = bam_init1();
	buf = 0;
	max_buf = 0;
	while (bam_read1(fp, b) >= 0) {
		int i, qlen = b->core.l_qseq;
		uint8_t *seq;
		putchar('@'); fputs(bam1_qname(b), stdout);
		if (no12) putchar('\n');
		else {
			if ((b->core.flag & 0x40) && !(b->core.flag & 0x80)) puts("/1");
			else if ((b->core.flag & 0x80) && !(b->core.flag & 0x40)) puts("/2");
			else putchar('\n');
		}
		if (max_buf < qlen + 1) {
			max_buf = qlen + 1;
			kroundup32(max_buf);
			buf = realloc(buf, max_buf);
		}
		buf[qlen] = 0;
		seq = bam1_seq(b);
		for (i = 0; i < qlen; ++i)
			buf[i] = bam1_seqi(seq, i);
		if (b->core.flag & 16) { // reverse complement
			for (i = 0; i < qlen>>1; ++i) {
				int8_t t = seq_comp_table[buf[qlen - 1 - i]];
				buf[qlen - 1 - i] = seq_comp_table[buf[i]];
				buf[i] = t;
			}
			if (qlen&1) buf[i] = seq_comp_table[buf[i]];
		}
		for (i = 0; i < qlen; ++i)
			buf[i] = bam_nt16_rev_table[buf[i]];
		puts((char*)buf);
		puts("+");
		seq = bam1_qual(b);
		for (i = 0; i < qlen; ++i)
			buf[i] = 33 + seq[i];
		if (b->core.flag & 16) { // reverse
			for (i = 0; i < qlen>>1; ++i) {
				int8_t t = buf[qlen - 1 - i];
				buf[qlen - 1 - i] = buf[i];
				buf[i] = t;
			}
		}
		puts((char*)buf);
	}
	free(buf);
	bam_destroy1(b);
	bam_header_destroy(h);
	bam_close(fp);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x378fa(%rip), %rdx     # 0x42371
callq 0x3210
testl %eax, %eax
jle 0xaab2
xorl %r15d, %r15d
movl $0x1, %r12d
leaq 0x378e1(%rip), %r14     # 0x42371
cmpl $0x6e, %eax
cmovel %r12d, %r15d
movl %ebp, %edi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3210
testl %eax, %eax
jg 0xaa90
testl %r15d, %r15d
sete 0x7(%rsp)
jmp 0xaab7
movb $0x1, 0x7(%rsp)
cmpl $0x1, %ebp
je 0xadba
movq 0x444f1(%rip), %rax     # 0x4efb8
movslq (%rax), %rax
movq (%rbx,%rax,8), %rbx
leaq 0x34383(%rip), %rsi     # 0x3ee58
movq %rbx, %rdi
callq 0x35a0
testl %eax, %eax
je 0xaaf2
leaq 0x3a187(%rip), %rsi     # 0x44c6f
movq %rbx, %rdi
callq 0x1fdb8
jmp 0xab0f
movq 0x444d7(%rip), %rax     # 0x4efd0
movq (%rax), %rdi
callq 0x3030
leaq 0x3a167(%rip), %rsi     # 0x44c6f
movl %eax, %edi
callq 0x1ff6b
movq %rax, %rbx
testq %rax, %rax
je 0xadbf
movq %rbx, %rdi
callq 0x22efa
movq %rax, 0x8(%rsp)
movl $0x1, %edi
movl $0x38, %esi
callq 0x3610
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
callq 0x23238
xorl %r12d, %r12d
testl %eax, %eax
js 0xad83
leaq 0x45589(%rip), %rbp     # 0x500e0
leaq 0x44942(%rip), %r14     # 0x4f4a0
xorl %r13d, %r13d
movq %rbx, 0x10(%rsp)
movslq 0x10(%r15), %rbx
movl $0x40, %edi
callq 0x31e0
movq 0x30(%r15), %rdi
movq 0x44479(%rip), %rax     # 0x4eff8
movq (%rax), %rsi
callq 0x34a0
cmpb $0x0, 0x7(%rsp)
je 0xabba
movl 0xc(%r15), %eax
andl $0xc0, %eax
cmpl $0x40, %eax
jne 0xaba5
leaq 0x34902(%rip), %rdi     # 0x3f4a5
jmp 0xabb3
cmpl $0x80, %eax
jne 0xabba
leaq 0x348f5(%rip), %rdi     # 0x3f4a8
callq 0x3150
jmp 0xabc4
movl $0xa, %edi
callq 0x31e0
cmpl %ebx, %r12d
jg 0xabff
movl %ebx, %eax
sarl %eax
orl %ebx, %eax
movl %eax, %ecx
sarl $0x2, %ecx
orl %eax, %ecx
movl %ecx, %eax
sarl $0x4, %eax
orl %ecx, %eax
movl %eax, %ecx
sarl $0x8, %ecx
orl %eax, %ecx
movl %ecx, %r12d
sarl $0x10, %r12d
orl %ecx, %r12d
incl %r12d
movslq %r12d, %rsi
movq %r13, %rdi
callq 0x3720
movq %rax, %r13
movb $0x0, (%r13,%rbx)
testl %ebx, %ebx
jle 0xac4c
movq 0x8(%r15), %rax
movq %rax, %rcx
shrq $0x30, %rcx
shlq $0x2, %rcx
addq 0x30(%r15), %rcx
shrl $0x18, %eax
addq %rcx, %rax
xorl %edx, %edx
xorl %esi, %esi
movl %esi, %ecx
shrl %ecx
movb (%rax,%rcx), %dil
movl %edx, %ecx
notb %cl
andb $0x4, %cl
shrb %cl, %dil
andb $0xf, %dil
movb %dil, (%r13,%rsi)
incq %rsi
addb $0x4, %dl
cmpq %rsi, %rbx
jne 0xac26
movabsq $0x1000000000, %rax     # imm = 0x1000000000
testq %rax, 0x8(%r15)
je 0xacae
movl %ebx, %eax
sarl %eax
testl %eax, %eax
jle 0xac98
movl %eax, %ecx
leal -0x1(%rbx), %edx
xorl %eax, %eax
movslq %edx, %rdx
movsbq (%r13,%rdx), %rsi
movb (%rsi,%r14), %sil
movsbq (%r13,%rax), %rdi
movb (%rdi,%r14), %dil
movb %dil, (%r13,%rdx)
movb %sil, (%r13,%rax)
incq %rax
decl %edx
cmpq %rax, %rcx
jne 0xac6b
jmp 0xac9a
xorl %eax, %eax
testb $0x1, %bl
je 0xacae
movsbq (%r13,%rax), %rcx
movb (%rcx,%r14), %cl
movb %cl, (%r13,%rax)
testl %ebx, %ebx
jle 0xacce
xorl %eax, %eax
movq (%rbp), %rcx
movsbq (%r13,%rax), %rdx
movb (%rcx,%rdx), %cl
movb %cl, (%r13,%rax)
incq %rax
cmpq %rax, %rbx
jne 0xacb4
movq %r13, %rdi
callq 0x3150
leaq 0x347ce(%rip), %rdi     # 0x3f4ab
callq 0x3150
testl %ebx, %ebx
jle 0xad21
movq 0x8(%r15), %rcx
movq %rcx, %rdx
shrq $0x30, %rdx
shlq $0x2, %rdx
addq 0x30(%r15), %rdx
shrl $0x18, %ecx
movl 0x10(%r15), %eax
incl %eax
sarl %eax
cltq
addq %rcx, %rax
addq %rdx, %rax
xorl %ecx, %ecx
movb (%rax,%rcx), %dl
addb $0x21, %dl
movb %dl, (%r13,%rcx)
incq %rcx
cmpq %rcx, %rbx
jne 0xad0e
movabsq $0x1000000000, %rax     # imm = 0x1000000000
testq %rax, 0x8(%r15)
je 0xad61
movl %ebx, %eax
sarl %eax
testl %eax, %eax
jle 0xad61
movl %eax, %eax
leal -0x1(%rbx), %ecx
xorl %edx, %edx
movslq %ecx, %rcx
movb (%r13,%rcx), %sil
movb (%r13,%rdx), %dil
movb %dil, (%r13,%rcx)
movb %sil, (%r13,%rdx)
incq %rdx
decl %ecx
cmpq %rdx, %rax
jne 0xad40
movq %r13, %rdi
callq 0x3150
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x23238
testl %eax, %eax
jns 0xab66
jmp 0xad86
xorl %r13d, %r13d
movq %r13, %rdi
callq 0x3350
testq %r15, %r15
je 0xada4
movq 0x30(%r15), %rdi
callq 0x3350
movq %r15, %rdi
callq 0x3350
movq 0x8(%rsp), %rdi
callq 0x22e78
movq %rbx, %rdi
callq 0x20aae
xorl %eax, %eax
jmp 0xadc4
callq 0x3dab
movl $0x1, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq