// File: /workspace/github_cpp_makefile_star2_O1/statgen[P]statgen/lib/samtools-0.1.7a-hybrid/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 (mode[0] == 'r') { // read
		fp->type |= TYPE_READ;
		if (mode[1] == '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);
					append_header_text(fp->header, textheader->text, textheader->l_text);
					bam_header_destroy(textheader);
				}
				if (fp->header->n_targets == 0)
					fprintf(stderr, "[samopen] no @SQ lines in the header.\n");
			} else fprintf(stderr, "[samopen] SAM header is present: %d sequences.\n", fp->header->n_targets);
		}
	} else if (mode[0] == 'w') { // write
		fp->header = bam_header_dup((const bam_header_t*)aux);
		if (mode[1] == 'b') { // binary
			char bmode[3];
			bmode[0] = 'w'; bmode[1] = strstr(mode, "u")? 'u' : 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 (strstr(mode, "X")) fp->type |= BAM_OFSTR<<2;
			else if (strstr(mode, "x")) fp->type |= BAM_OFHEX<<2;
			else fp->type |= BAM_OFDEC<<2;
			// write header
			if (strstr(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);
				if (alt->n_targets) { // then write the header text without dumping ->target_{name,len}
					if (alt->n_targets != fp->header->n_targets)
						fprintf(stderr, "[samopen] inconsistent number of target sequences.\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 0x3530
movq %rax, %rbx
movzbl (%r15), %eax
cmpl $0x77, %eax
je 0x148db
cmpl $0x72, %eax
jne 0x14bec
movl (%rbx), %eax
movl %eax, %ecx
orl $0x2, %ecx
movl %ecx, (%rbx)
cmpb $0x62, 0x1(%r15)
jne 0x14949
orl $0x3, %eax
movl %eax, (%rbx)
leaq 0x104ff(%rip), %rsi     # 0x24db6
movq %r14, %rdi
callq 0x34e0
testl %eax, %eax
je 0x14a6e
leaq 0x136f9(%rip), %rsi     # 0x27fc7
movq %r14, %rdi
callq 0xfa04
jmp 0x14a8b
movq %r12, %rdi
callq 0x14794
movq %rax, 0x10(%rbx)
cmpb $0x62, 0x1(%r15)
jne 0x14a46
movq %rax, %r12
movb $0x77, 0x5(%rsp)
movl $0x75, %ebp
movq %r15, %rdi
movl $0x75, %esi
callq 0x3440
xorl %ecx, %ecx
testq %rax, %rax
cmovnel %ebp, %ecx
movb %cl, 0x6(%rsp)
movb $0x0, 0x7(%rsp)
orb $0x1, (%rbx)
leaq 0x1048f(%rip), %rsi     # 0x24db6
movq %r14, %rdi
callq 0x34e0
testl %eax, %eax
je 0x14aa9
leaq 0x5(%rsp), %rsi
movq %r14, %rdi
callq 0xfa04
jmp 0x14ac4
movq %r14, %rdi
callq 0x14442
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x14ba3
movq %rax, %rdi
callq 0x13417
movq %rax, 0x10(%rbx)
movl (%rax), %edx
testl %edx, %edx
jne 0x14bfe
testq %r12, %r12
je 0x14a2f
movq %rax, %r14
movq %r12, %rdi
callq 0x13273
movq %rax, 0x10(%rbx)
movq 0x38(%r14), %r12
testq %r12, %r12
je 0x14a27
movq %rax, %r15
movslq 0x30(%r14), %r13
movl 0x30(%rax), %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 0x14a01
incl %eax
movq 0x38(%r15), %rdi
movslq %eax, %rsi
callq 0x3610
movq %rax, 0x38(%r15)
movslq 0x30(%r15), %rdi
addq 0x38(%r15), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3580
movslq 0x30(%r15), %rax
addq %r13, %rax
movl %eax, 0x30(%r15)
movq 0x38(%r15), %rcx
movb $0x0, (%rcx,%rax)
movq %r14, %rdi
callq 0x11c9f
movq 0x10(%rbx), %rax
cmpl $0x0, (%rax)
jne 0x14bec
callq 0x43b3
jmp 0x14bec
leaq 0x10369(%rip), %rsi     # 0x24db6
movq %r14, %rdi
callq 0x34e0
testl %eax, %eax
je 0x14ae1
leaq 0xf931(%rip), %rsi      # 0x24395
movq %r14, %rdi
callq 0x35f0
jmp 0x14aeb
movq 0x1a55b(%rip), %rax     # 0x2efd0
movq (%rax), %rdi
callq 0x3030
leaq 0x13543(%rip), %rsi     # 0x27fc7
movl %eax, %edi
callq 0xfb6d
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x14ba3
movq %rax, %rdi
callq 0x11d21
movq %rax, 0x10(%rbx)
jmp 0x14bec
movq 0x1a548(%rip), %rax     # 0x2eff8
movq (%rax), %rdi
callq 0x3030
leaq 0x5(%rsp), %rsi
movl %eax, %edi
callq 0xfb6d
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x14ba3
movq %rax, %rdi
movq %r12, %rsi
callq 0x11ef4
jmp 0x14bec
movq 0x1a510(%rip), %rax     # 0x2eff8
movq (%rax), %rax
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x14ba3
movq %r15, %rdi
movl $0x58, %esi
callq 0x3440
movl $0x8, %ecx
testq %rax, %rax
jne 0x14b26
movq %r15, %rdi
movl $0x78, %esi
callq 0x3440
movl $0x4, %ecx
testq %rax, %rax
je 0x14b28
orl %ecx, (%rbx)
movq %r15, %rdi
movl $0x68, %esi
callq 0x3440
testq %rax, %rax
je 0x14bec
xorl %eax, %eax
callq 0x11c86
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 0x132e8
movl $0x0, 0x30(%r14)
movq $0x0, 0x38(%r14)
movq 0x38(%r13), %rdi
movslq 0x30(%r13), %rdx
movq 0x8(%rbx), %r15
movl $0x1, %esi
movq %r15, %rcx
callq 0x3600
movl (%r14), %eax
movl (%r13), %ecx
testl %eax, %eax
je 0x14baf
cmpl %ecx, %eax
je 0x14be4
callq 0x4397
jmp 0x14be4
movq %rbx, %rdi
callq 0x32c0
xorl %ebx, %ebx
jmp 0x14bec
testl %ecx, %ecx
jle 0x14be4
leaq 0x11c74(%rip), %r12     # 0x2682e
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 0x3630
incq %rbp
movslq (%r13), %rax
cmpq %rax, %rbp
jl 0x14bbc
movq %r14, %rdi
callq 0x11c9f
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1a3e3(%rip), %rax     # 0x2efe8
movq (%rax), %rdi
leaq 0x11bbb(%rip), %rsi     # 0x267ca
xorl %eax, %eax
callq 0x3630
jmp 0x14bec