// File: /workspace/github_cpp_makefile_star2_O3/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, %r13
movq %rsi, %r12
movq %rdi, %r15
movl $0x1, %edi
movl $0x18, %esi
callq 0x3530
movq %rax, %rbx
movzbl (%r12), %eax
cmpl $0x77, %eax
je 0x14d95
cmpl $0x72, %eax
jne 0x150b0
movl $0x2, (%rbx)
cmpb $0x62, 0x1(%r12)
jne 0x14e19
movl $0x3, (%rbx)
cmpb $0x2d, (%r15)
jne 0x14d68
cmpb $0x0, 0x1(%r15)
je 0x14fe6
leaq 0x12358(%rip), %rsi     # 0x270c7
movq %r15, %rdi
callq 0x10048
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x14fd7
movq %rax, %rdi
callq 0x12383
movq %rax, 0x10(%rbx)
jmp 0x150b0
movq %r13, %rdi
callq 0x14c3c
movq %rax, %r14
movq %rax, 0x10(%rbx)
cmpb $0x62, 0x1(%r12)
jne 0x14ef5
movb $0x77, 0x5(%rsp)
movl $0x75, %ebp
movq %r12, %rdi
movl $0x75, %esi
callq 0x3440
xorl %ecx, %ecx
testq %rax, %rax
cmovnel %ebp, %ecx
movb %cl, 0x6(%rsp)
movb $0x0, 0x7(%rsp)
movl $0x1, (%rbx)
cmpb $0x2d, (%r15)
jne 0x14def
cmpb $0x0, 0x1(%r15)
je 0x15008
leaq 0x5(%rsp), %rsi
movq %r15, %rdi
callq 0x10048
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x14fd7
movq %rax, %rdi
movq %r14, %rsi
callq 0x1255d
jmp 0x150b0
movq %r15, %rdi
callq 0x14b45
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x14fd7
movq %rax, %rdi
callq 0x13ad0
movq %rax, 0x10(%rbx)
movl (%rax), %edx
testl %edx, %edx
jne 0x150c2
testq %r13, %r13
je 0x1506d
movq %rax, %r14
movq %r13, %rdi
callq 0x13945
movq %rax, %r15
movq %rax, 0x10(%rbx)
movq 0x38(%r14), %r13
testq %r13, %r13
je 0x1505f
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 0x15037
incl %eax
movq 0x38(%r15), %rdi
movslq %eax, %rsi
callq 0x3610
movq %rax, 0x38(%r15)
movl 0x30(%r15), %ecx
jmp 0x1503b
cmpb $0x2d, (%r15)
jne 0x14f06
cmpb $0x0, 0x1(%r15)
je 0x15028
leaq 0xe4a0(%rip), %rsi      # 0x233ad
movq %r15, %rdi
callq 0x35f0
movq %rax, %r15
movq %r15, 0x8(%rbx)
testq %r15, %r15
je 0x14fd7
movq %r12, %rdi
movl $0x58, %esi
callq 0x3440
movl $0x8, %ecx
testq %rax, %rax
jne 0x14f53
movq %r12, %rdi
movl $0x78, %esi
callq 0x3440
movl $0x4, %ecx
testq %rax, %rax
je 0x14f55
movl %ecx, (%rbx)
movq %r12, %rdi
movl $0x68, %esi
callq 0x3440
testq %rax, %rax
je 0x150b0
xorl %eax, %eax
callq 0x122e6
movq %rax, %r12
movl 0x30(%r14), %eax
movl %eax, 0x30(%r12)
movq 0x38(%r14), %rax
movq %rax, 0x38(%r12)
movq %r12, %rdi
callq 0x139af
movl $0x0, 0x30(%r12)
movq $0x0, 0x38(%r12)
movq 0x38(%r14), %rdi
movslq 0x30(%r14), %rdx
movl $0x1, %esi
movq %r15, %rcx
callq 0x3600
movl (%r12), %eax
movl (%r14), %ecx
testl %eax, %eax
je 0x15074
cmpl %ecx, %eax
je 0x150a8
callq 0x4638
jmp 0x150a8
movq %rbx, %rdi
callq 0x32c0
xorl %ebx, %ebx
jmp 0x150b0
movq 0x18fe3(%rip), %rax     # 0x2dfd0
movq (%rax), %rdi
callq 0x3030
leaq 0x120cb(%rip), %rsi     # 0x270c7
movl %eax, %edi
callq 0x101a3
jmp 0x14d77
movq 0x18fe9(%rip), %rax     # 0x2dff8
movq (%rax), %rdi
callq 0x3030
leaq 0x5(%rsp), %rsi
movl %eax, %edi
callq 0x101a3
jmp 0x14dfc
movq 0x18fc9(%rip), %rax     # 0x2dff8
movq (%rax), %r15
jmp 0x14f18
movq 0x38(%r15), %rax
movslq %ecx, %rdi
addq %rax, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x3580
movslq 0x30(%r15), %rax
addq %r12, %rax
movl %eax, 0x30(%r15)
movq 0x38(%r15), %rcx
movb $0x0, (%rcx,%rax)
movq %r14, %rdi
callq 0x122ff
cmpl $0x0, (%r15)
jne 0x150b0
callq 0x4654
jmp 0x150b0
testl %ecx, %ecx
jle 0x150a8
leaq 0x1084d(%rip), %r13     # 0x258cc
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 0x3630
incq %rbp
movslq (%r14), %rax
cmpq %rax, %rbp
jl 0x15081
movq %r12, %rdi
callq 0x122ff
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18f1f(%rip), %rax     # 0x2dfe8
movq (%rax), %rdi
leaq 0x10795(%rip), %rsi     # 0x25868
xorl %eax, %eax
callq 0x3630
jmp 0x150b0