// File: /workspace/github_c_makefile_star2_O2/genometools[P]genometools/src/external/samtools-0.1.18/sam.c
// Function: samopen
samfile_t *samopen(const char *fn, const char *mode, const void *aux)
{
	samfile_t *fp;
	fp = (samfile_t*)calloc(1, sizeof(samfile_t));
	if (strchr(mode, 'r')) { // read
		fp->type |= TYPE_READ;
		if (strchr(mode, 'b')) { // binary
			fp->type |= TYPE_BAM;
			fp->x.bam = strcmp(fn, "-")? bam_open(fn, "r") : bam_dopen(fileno(stdin), "r");
			if (fp->x.bam == 0) goto open_err_ret;
			fp->header = bam_header_read(fp->x.bam);
		} else { // text
			fp->x.tamr = sam_open(fn);
			if (fp->x.tamr == 0) goto open_err_ret;
			fp->header = sam_header_read(fp->x.tamr);
			if (fp->header->n_targets == 0) { // no @SQ fields
				if (aux) { // check if aux is present
					bam_header_t *textheader = fp->header;
					fp->header = sam_header_read2((const char*)aux);
					if (fp->header == 0) goto open_err_ret;
					append_header_text(fp->header, textheader->text, textheader->l_text);
					bam_header_destroy(textheader);
				}
				if (fp->header->n_targets == 0 && bam_verbose >= 1)
					fprintf(stderr, "[samopen] no @SQ lines in the header.\n");
			} else if (bam_verbose >= 2) fprintf(stderr, "[samopen] SAM header is present: %d sequences.\n", fp->header->n_targets);
		}
	} else if (strchr(mode, 'w')) { // write
		fp->header = bam_header_dup((const bam_header_t*)aux);
		if (strchr(mode, 'b')) { // binary
			char bmode[3];
			int i, compress_level = -1;
			for (i = 0; mode[i]; ++i) if (mode[i] >= '0' && mode[i] <= '9') break;
			if (mode[i]) compress_level = mode[i] - '0';
			if (strchr(mode, 'u')) compress_level = 0;
			bmode[0] = 'w'; bmode[1] = compress_level < 0? 0 : compress_level + '0'; bmode[2] = 0;
			fp->type |= TYPE_BAM;
			fp->x.bam = strcmp(fn, "-")? bam_open(fn, bmode) : bam_dopen(fileno(stdout), bmode);
			if (fp->x.bam == 0) goto open_err_ret;
			bam_header_write(fp->x.bam, fp->header);
		} else { // text
			// open file
			fp->x.tamw = strcmp(fn, "-")? fopen(fn, "w") : stdout;
			if (fp->x.tamr == 0) goto open_err_ret;
			if (strchr(mode, 'X')) fp->type |= BAM_OFSTR<<2;
			else if (strchr(mode, 'x')) fp->type |= BAM_OFHEX<<2;
			else fp->type |= BAM_OFDEC<<2;
			// write header
			if (strchr(mode, 'h')) {
				int i;
				bam_header_t *alt;
				// parse the header text 
				alt = bam_header_init();
				alt->l_text = fp->header->l_text; alt->text = fp->header->text;
				sam_header_parse(alt);
				alt->l_text = 0; alt->text = 0;
				// check if there are @SQ lines in the header
				fwrite(fp->header->text, 1, fp->header->l_text, fp->x.tamw); // FIXME: better to skip the trailing NULL
				if (alt->n_targets) { // then write the header text without dumping ->target_{name,len}
					if (alt->n_targets != fp->header->n_targets && bam_verbose >= 1)
						fprintf(stderr, "[samopen] inconsistent number of target sequences. Output the text header.\n");
				} else { // then dump ->target_{name,len}
					for (i = 0; i < fp->header->n_targets; ++i)
						fprintf(fp->x.tamw, "@SQ\tSN:%s\tLN:%d\n", fp->header->target_name[i], fp->header->target_len[i]);
				}
				bam_header_destroy(alt);
			}
		}
	}
	return fp;

open_err_ret:
	free(fp);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r13
movq %rsi, %r12
movq %rdi, %r15
pushq $0x1
popq %rdi
pushq $0x18
popq %rsi
callq 0x174c0
movq %rax, %rbx
pushq $0x72
popq %rsi
movq %r12, %rdi
callq 0x17260
testq %rax, %rax
je 0x1f7c6c
movl $0x2, (%rbx)
pushq $0x62
popq %rsi
movq %r12, %rdi
callq 0x17260
testq %rax, %rax
je 0x1f7d3a
movl $0x3, (%rbx)
cmpb $0x2d, (%r15)
jne 0x1f7c3f
cmpb $0x0, 0x1(%r15)
je 0x1f7ddd
leaq 0x1031e7(%rip), %rsi    # 0x2fae2d
movq %r15, %rdi
callq 0x2d5e63
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x1f7eba
movq %rax, %rdi
callq 0x1f4ac8
movq %rax, 0x10(%rbx)
jmp 0x1f7ec4
pushq $0x77
popq %rsi
movq %r12, %rdi
callq 0x17260
testq %rax, %rax
je 0x1f7ec4
movq %r13, %rdi
callq 0x1f7b24
movq %rax, %r14
movq %rax, 0x10(%rbx)
pushq $0x62
popq %rsi
movq %r12, %rdi
callq 0x17260
testq %rax, %rax
je 0x1f7d92
movq %r12, %rcx
movzbl (%rcx), %eax
testl %eax, %eax
je 0x1f7cb8
leal -0x30(%rax), %edx
incq %rcx
cmpb $0x9, %dl
ja 0x1f7ca6
leal -0x30(%rax), %ecx
xorl %ebp, %ebp
cmpl $0x1, %eax
movl $0x0, %r13d
sbbl %r13d, %r13d
orl %ecx, %r13d
pushq $0x75
popq %rsi
movq %r12, %rdi
callq 0x17260
testq %rax, %rax
cmovnel %ebp, %r13d
movb $0x77, 0x5(%rsp)
leal 0x30(%r13), %eax
testl %r13d, %r13d
movzbl %al, %eax
cmovsl %ebp, %eax
movb %al, 0x6(%rsp)
movb $0x0, 0x7(%rsp)
movl $0x1, (%rbx)
cmpb $0x2d, (%r15)
jne 0x1f7d10
cmpb $0x0, 0x1(%r15)
je 0x1f7ed6
leaq 0x5(%rsp), %rsi
movq %r15, %rdi
callq 0x2d5e63
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x1f7eba
movq %rax, %rdi
movq %r14, %rsi
callq 0x1f4cae
jmp 0x1f7ec4
movq %r15, %rdi
callq 0x1f7a4e
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x1f7eba
movq %rax, %rdi
callq 0x1f68fa
movq %rax, 0x10(%rbx)
movl (%rax), %edx
testl %edx, %edx
je 0x1f7dff
leaq 0x2422b4(%rip), %rax    # 0x43a020
cmpl $0x2, (%rax)
jl 0x1f7ec4
movq 0x2401ec(%rip), %rax    # 0x437f68
movq (%rax), %rdi
leaq 0x172c81(%rip), %rsi    # 0x36aa07
xorl %eax, %eax
callq 0x17a50
jmp 0x1f7ec4
cmpb $0x2d, (%r15)
jne 0x1f7da3
cmpb $0x0, 0x1(%r15)
je 0x1f7f8c
leaq 0x13bb64(%rip), %rsi    # 0x33390e
movq %r15, %rdi
callq 0x17860
movq %rax, %r15
movq %r15, 0x8(%rbx)
testq %r15, %r15
je 0x1f7eba
pushq $0x58
popq %rsi
movq %r12, %rdi
callq 0x17260
testq %rax, %rax
je 0x1f7ef6
pushq $0x8
jmp 0x1f7f08
movq 0x240114(%rip), %rax    # 0x437ef8
movq (%rax), %rdi
callq 0x17800
leaq 0x10303a(%rip), %rsi    # 0x2fae2d
movl %eax, %edi
callq 0x2d6190
jmp 0x1f7c4e
testq %r13, %r13
je 0x1f7f88
movq %rax, %r14
movq %r13, %rdi
callq 0x1f60ad
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x1f7eba
movq %rax, %r15
movq 0x40(%r14), %r13
testq %r13, %r13
je 0x1f7ff0
movq 0x30(%r14), %rax
movslq %eax, %r12
movq 0x30(%r15), %rdi
addl %edi, %eax
movd %eax, %xmm0
movd %edi, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movdqa %xmm1, %xmm0
psrad $0x1, %xmm0
por %xmm1, %xmm0
movdqa %xmm0, %xmm1
psrad $0x2, %xmm1
por %xmm0, %xmm1
movdqa %xmm1, %xmm0
psrad $0x4, %xmm0
por %xmm1, %xmm0
movdqa %xmm0, %xmm1
psrad $0x8, %xmm1
por %xmm0, %xmm1
movdqa %xmm1, %xmm0
psrad $0x10, %xmm0
por %xmm1, %xmm0
movd %xmm0, %ecx
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %eax
cmpl %eax, %ecx
jge 0x1f7fcd
incl %eax
movq 0x40(%r15), %rdi
movslq %eax, %rsi
callq 0x17f58
movq %rax, 0x40(%r15)
movq 0x30(%r15), %rdi
jmp 0x1f7fd1
movq %rbx, %rdi
callq 0x17f48
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x23ffd3(%rip), %rax    # 0x437eb0
movq (%rax), %rdi
callq 0x17800
leaq 0x5(%rsp), %rsi
movl %eax, %edi
callq 0x2d6190
jmp 0x1f7d1d
pushq $0x78
popq %rsi
movq %r12, %rdi
callq 0x17260
testq %rax, %rax
je 0x1f7f0b
pushq $0x4
popq %rax
movl %eax, (%rbx)
pushq $0x68
popq %rsi
movq %r12, %rdi
callq 0x17260
testq %rax, %rax
je 0x1f7ec4
xorl %eax, %eax
callq 0x1f4a38
movq %rax, %r12
movq 0x30(%r14), %rax
movq %rax, 0x30(%r12)
movq 0x40(%r14), %rax
movq %rax, 0x40(%r12)
movq %r12, %rdi
callq 0x1f67cd
andq $0x0, 0x30(%r12)
andq $0x0, 0x40(%r12)
movq 0x30(%r14), %rdx
movq 0x40(%r14), %rdi
pushq $0x1
popq %rsi
movq %r15, %rcx
callq 0x17f00
movl (%r12), %eax
testl %eax, %eax
je 0x1f7f9b
cmpl (%r14), %eax
je 0x1f7f7b
leaq 0x2420ae(%rip), %rax    # 0x43a020
cmpl $0x0, (%rax)
jg 0x1f8038
movq %r12, %rdi
callq 0x1f4a4d
jmp 0x1f7ec4
movb $0x1, %al
jmp 0x1f7fff
movq 0x23ff1d(%rip), %rax    # 0x437eb0
movq (%rax), %r15
jmp 0x1f7db5
leaq 0x172ae1(%rip), %r13    # 0x36aa83
xorl %ebp, %ebp
movslq (%r14), %rax
cmpq %rax, %rbp
jge 0x1f7f7b
movq 0x8(%r14), %rax
movq 0x10(%r14), %rcx
movq (%rax,%rbp,8), %rdx
movl (%rcx,%rbp,4), %ecx
movq %r15, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x17a50
incq %rbp
jmp 0x1f7fa4
movq 0x40(%r15), %rax
addq %rax, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x17350
addq 0x30(%r15), %r12
movq %r12, 0x30(%r15)
movq 0x40(%r15), %rax
movb $0x0, (%rax,%r12)
movq %r14, %rdi
callq 0x1f4a4d
cmpl $0x0, (%r15)
sete %al
testb %al, %al
je 0x1f7ec4
leaq 0x242012(%rip), %rax    # 0x43a020
cmpl $0x0, (%rax)
jle 0x1f7ec4
movq 0x23ff4a(%rip), %rax    # 0x437f68
movq (%rax), %rcx
leaq 0x1729b8(%rip), %rdi    # 0x36a9e0
pushq $0x26
popq %rsi
pushq $0x1
popq %rdx
callq 0x17f00
jmp 0x1f7ec4
movq 0x23ff29(%rip), %rax    # 0x437f68
movq (%rax), %rcx
leaq 0x1729ee(%rip), %rdi    # 0x36aa37
pushq $0x4b
popq %rsi
pushq $0x1
popq %rdx
callq 0x17f00
jmp 0x1f7f7b