// File: /workspace/github_c_makefile_star2_O1/xiongxu[P]bam2bedGraph2/samtools-0.1.19/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.tamw == 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, %r12
movq %rsi, %r15
movq %rdi, %r14
movl $0x1, %edi
movl $0x18, %esi
callq 0x32e0
movq %rax, %rbx
movq %r15, %rdi
movl $0x72, %esi
callq 0x3200
testq %rax, %rax
je 0xe260
movl (%rbx), %ebp
movl %ebp, %eax
orl $0x2, %eax
movl %eax, (%rbx)
movq %r15, %rdi
movl $0x62, %esi
callq 0x3200
testq %rax, %rax
je 0xe31f
orl $0x3, %ebp
movl %ebp, (%rbx)
leaq 0xbc31(%rip), %rsi      # 0x19e6d
movq %r14, %rdi
callq 0x3300
testl %eax, %eax
je 0xe377
leaq 0xd5b3(%rip), %rsi      # 0x1b806
movq %r14, %rdi
callq 0x17198
jmp 0xe394
movq %r15, %rdi
movl $0x77, %esi
callq 0x3200
testq %rax, %rax
je 0xe5dc
movq %r12, %rdi
callq 0xe0f0
movq %rax, %r12
movq %rax, 0x10(%rbx)
movq %r15, %rdi
movl $0x62, %esi
callq 0x3200
testq %rax, %rax
je 0xe3b2
movq %r15, %rcx
movzbl (%rcx), %eax
testl %eax, %eax
je 0xe2b0
leal -0x30(%rax), %edx
incq %rcx
cmpb $0x9, %dl
ja 0xe29e
leal -0x30(%rax), %ecx
xorl %ebp, %ebp
cmpl $0x1, %eax
movl $0x0, %r13d
sbbl %r13d, %r13d
orl %ecx, %r13d
movq %r15, %rdi
movl $0x75, %esi
callq 0x3200
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)
orb $0x1, (%rbx)
leaq 0xbb70(%rip), %rsi      # 0x19e6d
movq %r14, %rdi
callq 0x3300
testl %eax, %eax
je 0xe3dd
leaq 0x5(%rsp), %rsi
movq %r14, %rdi
callq 0x17198
jmp 0xe3f8
movq %r14, %rdi
callq 0xdd9e
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0xe5d2
movq %rax, %rdi
callq 0xc99d
movq %rax, 0x10(%rbx)
movl (%rax), %edx
testl %edx, %edx
je 0xe415
leaq 0x141bf(%rip), %rax     # 0x22510
cmpl $0x2, (%rax)
jl 0xe5dc
movq 0x13c97(%rip), %rax     # 0x21ff8
movq (%rax), %rdi
leaq 0xcc91(%rip), %rsi      # 0x1affc
xorl %eax, %eax
callq 0x3320
jmp 0xe5dc
movq 0x13c4a(%rip), %rax     # 0x21fc8
movq (%rax), %rdi
callq 0x3390
leaq 0xd479(%rip), %rsi      # 0x1b806
movl %eax, %edi
callq 0x1734b
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0xe5d2
movq %rax, %rdi
callq 0x9806
movq %rax, 0x10(%rbx)
jmp 0xe5dc
leaq 0xbab4(%rip), %rsi      # 0x19e6d
movq %r14, %rdi
callq 0x3300
testl %eax, %eax
je 0xe50a
leaq 0xcc5c(%rip), %rsi      # 0x1b02c
movq %r14, %rdi
callq 0x3340
jmp 0xe514
movq 0x13bdc(%rip), %rax     # 0x21fc0
movq (%rax), %rdi
callq 0x3390
leaq 0x5(%rsp), %rsi
movl %eax, %edi
callq 0x1734b
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0xe5d2
movq %rax, %rdi
movq %r12, %rsi
callq 0x99d5
jmp 0xe5dc
testq %r12, %r12
je 0xe4e3
movq %rax, %r14
movq %r12, %rdi
callq 0xc72b
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0xe5d2
movq %rax, %r15
movq 0x38(%r14), %r12
testq %r12, %r12
je 0xe4d2
movslq 0x30(%r14), %r13
movl 0x30(%r15), %eax
leal (%rax,%r13), %ecx
movl %eax, %edx
sarl %edx
orl %eax, %edx
movl %edx, %eax
sarl $0x2, %eax
orl %edx, %eax
movl %eax, %edx
sarl $0x4, %edx
orl %eax, %edx
movl %edx, %eax
sarl $0x8, %eax
orl %edx, %eax
movl %eax, %edx
sarl $0x10, %edx
orl %eax, %edx
movl %ecx, %eax
sarl %eax
orl %ecx, %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, %eax
sarl $0x10, %eax
orl %ecx, %eax
cmpl %eax, %edx
jge 0xe4ac
incl %eax
movq 0x38(%r15), %rdi
movslq %eax, %rsi
callq 0x3430
movq %rax, 0x38(%r15)
movl 0x30(%r15), %edi
addq 0x38(%r15), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x30b0
movl 0x30(%r15), %eax
addl %r13d, %eax
movl %eax, 0x30(%r15)
movq 0x38(%r15), %rcx
movb $0x0, (%rcx,%rax)
movq %r14, %rdi
callq 0x9784
testq %r15, %r15
je 0xe5d2
movq 0x10(%rbx), %rax
cmpl $0x0, (%rax)
jne 0xe5dc
leaq 0x14019(%rip), %rax     # 0x22510
cmpl $0x0, (%rax)
jle 0xe5dc
callq 0x3ba6
jmp 0xe5dc
movq 0x13aaf(%rip), %rax     # 0x21fc0
movq (%rax), %rax
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0xe5d2
movq %r15, %rdi
movl $0x58, %esi
callq 0x3200
movl $0x8, %ecx
testq %rax, %rax
jne 0xe54f
movq %r15, %rdi
movl $0x78, %esi
callq 0x3200
movl $0x4, %ecx
testq %rax, %rax
je 0xe551
orl %ecx, (%rbx)
movq %r15, %rdi
movl $0x68, %esi
callq 0x3200
testq %rax, %rax
je 0xe5dc
callq 0x976b
movq %rax, %r14
movq 0x10(%rbx), %r13
movl 0x30(%r13), %eax
movl %eax, 0x30(%r14)
movq 0x38(%r13), %rax
movq %rax, 0x38(%r14)
movq %r14, %rdi
callq 0xc86e
movl $0x0, 0x30(%r14)
movq $0x0, 0x38(%r14)
movq 0x38(%r13), %rdi
movl 0x30(%r13), %edx
movq 0x8(%rbx), %r15
movl $0x1, %esi
movq %r15, %rcx
callq 0x3540
movl (%r14), %eax
movl (%r13), %ecx
testl %eax, %eax
je 0xe5ee
cmpl %ecx, %eax
je 0xe623
leaq 0x13f4a(%rip), %rax     # 0x22510
cmpl $0x0, (%rax)
jle 0xe623
callq 0x3bc2
jmp 0xe623
movq %rbx, %rdi
callq 0x3060
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %ecx, %ecx
jle 0xe623
leaq 0xca81(%rip), %r12      # 0x1b07a
xorl %ebp, %ebp
movq 0x8(%r13), %rax
movq 0x10(%r13), %rcx
movq (%rax,%rbp,8), %rdx
movl (%rcx,%rbp,4), %ecx
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0x3320
incq %rbp
movslq (%r13), %rax
cmpq %rax, %rbp
jl 0xe5fb
movq %r14, %rdi
callq 0x9784
jmp 0xe5dc