// File: /workspace/github_c_makefile_star2_O3/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, %r13
movq %rsi, %r12
movq %rdi, %r15
movl $0x1, %edi
movl $0x18, %esi
callq 0x32e0
movq %rax, %rbx
movq %r12, %rdi
movl $0x72, %esi
callq 0x3200
testq %rax, %rax
je 0xe588
movl $0x2, (%rbx)
movq %r12, %rdi
movl $0x62, %esi
callq 0x3200
testq %rax, %rax
je 0xe65c
movl $0x3, (%rbx)
cmpb $0x2d, (%r15)
jne 0xe55b
cmpb $0x0, 0x1(%r15)
je 0xe7a4
leaq 0xe3c4(%rip), %rsi      # 0x1c926
movq %r15, %rdi
callq 0x17710
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0xe880
movq %rax, %rdi
callq 0x9bfb
movq %rax, 0x10(%rbx)
jmp 0xe88a
movq %r12, %rdi
movl $0x77, %esi
callq 0x3200
testq %rax, %rax
je 0xe88a
movq %r13, %rdi
callq 0xe408
movq %rax, %r14
movq %rax, 0x10(%rbx)
movq %r12, %rdi
movl $0x62, %esi
callq 0x3200
testq %rax, %rax
je 0xe6b4
movq %r12, %rcx
movzbl (%rcx), %eax
testl %eax, %eax
je 0xe5d8
leal -0x30(%rax), %edx
incq %rcx
cmpb $0x9, %dl
ja 0xe5c6
leal -0x30(%rax), %ecx
xorl %ebp, %ebp
cmpl $0x1, %eax
movl $0x0, %r13d
sbbl %r13d, %r13d
orl %ecx, %r13d
movq %r12, %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)
movl $0x1, (%rbx)
cmpb $0x2d, (%r15)
jne 0xe632
cmpb $0x0, 0x1(%r15)
je 0xe89c
leaq 0x5(%rsp), %rsi
movq %r15, %rdi
callq 0x17710
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0xe880
movq %rax, %rdi
movq %r14, %rsi
callq 0x9dd0
jmp 0xe88a
movq %r15, %rdi
callq 0xe311
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0xe880
movq %rax, %rdi
callq 0xceb9
movq %rax, 0x10(%rbx)
movl (%rax), %edx
testl %edx, %edx
je 0xe7c6
leaq 0x14e82(%rip), %rax     # 0x23510
cmpl $0x2, (%rax)
jl 0xe88a
movq 0x1495a(%rip), %rax     # 0x22ff8
movq (%rax), %rdi
leaq 0xda12(%rip), %rsi      # 0x1c0ba
xorl %eax, %eax
callq 0x3320
jmp 0xe88a
cmpb $0x2d, (%r15)
jne 0xe6c5
cmpb $0x0, 0x1(%r15)
je 0xe8c3
leaq 0xda1e(%rip), %rsi      # 0x1c0ea
movq %r15, %rdi
callq 0x3340
movq %rax, %r15
movq %r15, 0x8(%rbx)
testq %r15, %r15
je 0xe880
movq %r12, %rdi
movl $0x58, %esi
callq 0x3200
movl $0x8, %ecx
testq %rax, %rax
jne 0xe712
movq %r12, %rdi
movl $0x78, %esi
callq 0x3200
movl $0x4, %ecx
testq %rax, %rax
je 0xe714
movl %ecx, (%rbx)
movq %r12, %rdi
movl $0x68, %esi
callq 0x3200
testq %rax, %rax
je 0xe88a
callq 0x9b5e
movq %rax, %r12
movl 0x30(%r14), %eax
movl %eax, 0x30(%r12)
movq 0x38(%r14), %rax
movq %rax, 0x38(%r12)
movq %r12, %rdi
callq 0xcd98
movl $0x0, 0x30(%r12)
movq $0x0, 0x38(%r12)
movq 0x38(%r14), %rdi
movl 0x30(%r14), %edx
movl $0x1, %esi
movq %r15, %rcx
callq 0x3540
movl (%r12), %eax
movl (%r14), %ecx
testl %eax, %eax
je 0xe8d2
cmpl %ecx, %eax
je 0xe906
leaq 0x14d7f(%rip), %rax     # 0x23510
cmpl $0x0, (%rax)
jle 0xe906
callq 0x3e92
jmp 0xe906
movq 0x1481d(%rip), %rax     # 0x22fc8
movq (%rax), %rdi
callq 0x3390
leaq 0xe16c(%rip), %rsi      # 0x1c926
movl %eax, %edi
callq 0x1788f
jmp 0xe56a
testq %r13, %r13
je 0xe8bc
movq %rax, %r14
movq %r13, %rdi
callq 0xcc5d
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0xe880
movq %rax, %r15
movq 0x38(%r14), %r13
testq %r13, %r13
je 0xe93a
movslq 0x30(%r14), %r12
movl 0x30(%r15), %ecx
leal (%rcx,%r12), %eax
movd %eax, %xmm0
movd %ecx, %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, %edx
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %eax
cmpl %eax, %edx
jge 0xe913
incl %eax
movq 0x38(%r15), %rdi
movslq %eax, %rsi
callq 0x3430
movq %rax, 0x38(%r15)
movl 0x30(%r15), %ecx
jmp 0xe917
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
movq 0x1471d(%rip), %rax     # 0x22fc0
movq (%rax), %rdi
callq 0x3390
leaq 0x5(%rsp), %rsi
movl %eax, %edi
callq 0x1788f
jmp 0xe63f
movb $0x1, %al
jmp 0xe949
movq 0x146f6(%rip), %rax     # 0x22fc0
movq (%rax), %r15
jmp 0xe6d7
testl %ecx, %ecx
jle 0xe906
leaq 0xd85b(%rip), %r13      # 0x1c138
xorl %ebp, %ebp
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 0x3320
incq %rbp
movslq (%r14), %rax
cmpq %rax, %rbp
jl 0xe8df
movq %r12, %rdi
callq 0x9b77
jmp 0xe88a
movq 0x38(%r15), %rax
movl %ecx, %edi
addq %rax, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x30b0
movl 0x30(%r15), %eax
addl %r12d, %eax
movl %eax, 0x30(%r15)
movq 0x38(%r15), %rcx
movb $0x0, (%rcx,%rax)
movq %r14, %rdi
callq 0x9b77
cmpl $0x0, (%r15)
sete %al
testb %al, %al
je 0xe88a
leaq 0x14bb8(%rip), %rax     # 0x23510
cmpl $0x0, (%rax)
jle 0xe88a
callq 0x3e76
jmp 0xe88a