// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
movl $0x1, %edi
movl $0x18, %esi
callq 0x194a0
movq %rax, 0x40(%rsp)
movq 0x50(%rsp), %rdi
movl $0x72, %esi
callq 0x19240
cmpq $0x0, %rax
je 0x36bcb7
movq 0x40(%rsp), %rax
movl (%rax), %ecx
orl $0x2, %ecx
movl %ecx, (%rax)
movq 0x50(%rsp), %rdi
movl $0x62, %esi
callq 0x19240
cmpq $0x0, %rax
je 0x36bb8f
movq 0x40(%rsp), %rax
movl (%rax), %ecx
orl $0x1, %ecx
movl %ecx, (%rax)
movq 0x58(%rsp), %rdi
leaq 0x22f81a(%rip), %rsi    # 0x59b327
callq 0x19980
cmpl $0x0, %eax
je 0x36bb2f
movq 0x58(%rsp), %rdi
leaq 0x1c04b7(%rip), %rsi    # 0x52bfda
callq 0x4f6a10
movq %rax, 0x18(%rsp)
jmp 0x36bb51
movq 0x2d042a(%rip), %rax    # 0x63bf60
movq (%rax), %rdi
callq 0x197a0
movl %eax, %edi
leaq 0x1c0493(%rip), %rsi    # 0x52bfda
callq 0x4f6e90
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x40(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x36bb70
jmp 0x36c070
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x365800
movq %rax, %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x10(%rax)
jmp 0x36bcb2
movq 0x58(%rsp), %rdi
callq 0x36aec0
movq %rax, %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x40(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x36bbb6
jmp 0x36c070
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x368990
movq %rax, %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
cmpl $0x0, (%rax)
jne 0x36bc7f
cmpq $0x0, 0x48(%rsp)
je 0x36bc4b
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movq %rax, 0x38(%rsp)
movq 0x48(%rsp), %rdi
callq 0x367f50
movq %rax, %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x40(%rsp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x36bc1f
jmp 0x36c070
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x38(%rsp), %rax
movq 0x40(%rax), %rsi
movq 0x38(%rsp), %rax
movq 0x30(%rax), %rax
movl %eax, %edx
callq 0x36c090
movq 0x38(%rsp), %rdi
callq 0x365720
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
cmpl $0x0, (%rax)
jne 0x36bc7d
leaq 0x2d3500(%rip), %rax    # 0x63f160
cmpl $0x1, (%rax)
jl 0x36bc7d
movq 0x2d0334(%rip), %rax    # 0x63bfa0
movq (%rax), %rdi
leaq 0x231dca(%rip), %rsi    # 0x59da40
movb $0x0, %al
callq 0x199f0
jmp 0x36bcb0
leaq 0x2d34da(%rip), %rax    # 0x63f160
cmpl $0x2, (%rax)
jl 0x36bcae
movq 0x2d030e(%rip), %rax    # 0x63bfa0
movq (%rax), %rdi
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movl (%rax), %edx
leaq 0x231dc0(%rip), %rsi    # 0x59da67
movb $0x0, %al
callq 0x199f0
jmp 0x36bcb0
jmp 0x36bcb2
jmp 0x36c064
movq 0x50(%rsp), %rdi
movl $0x77, %esi
callq 0x19240
cmpq $0x0, %rax
je 0x36c062
movq 0x48(%rsp), %rdi
callq 0x36b940
movq %rax, %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x10(%rax)
movq 0x50(%rsp), %rdi
movl $0x62, %esi
callq 0x19240
cmpq $0x0, %rax
je 0x36be57
movl $0xffffffff, 0x2c(%rsp) # imm = 0xFFFFFFFF
movl $0x0, 0x30(%rsp)
movq 0x50(%rsp), %rax
movslq 0x30(%rsp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x36bd56
movq 0x50(%rsp), %rax
movslq 0x30(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x30, %eax
jl 0x36bd47
movq 0x50(%rsp), %rax
movslq 0x30(%rsp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x39, %eax
jg 0x36bd47
jmp 0x36bd56
jmp 0x36bd49
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x30(%rsp)
jmp 0x36bd0f
movq 0x50(%rsp), %rax
movslq 0x30(%rsp), %rcx
cmpb $0x0, (%rax,%rcx)
je 0x36bd7b
movq 0x50(%rsp), %rax
movslq 0x30(%rsp), %rcx
movsbl (%rax,%rcx), %eax
subl $0x30, %eax
movl %eax, 0x2c(%rsp)
movq 0x50(%rsp), %rdi
movl $0x75, %esi
callq 0x19240
cmpq $0x0, %rax
je 0x36bd98
movl $0x0, 0x2c(%rsp)
movb $0x77, 0x35(%rsp)
cmpl $0x0, 0x2c(%rsp)
jge 0x36bdac
xorl %eax, %eax
movl %eax, 0x14(%rsp)
jmp 0x36bdb7
movl 0x2c(%rsp), %eax
addl $0x30, %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movb %al, 0x36(%rsp)
movb $0x0, 0x37(%rsp)
movq 0x40(%rsp), %rax
movl (%rax), %ecx
orl $0x1, %ecx
movl %ecx, (%rax)
movq 0x58(%rsp), %rdi
leaq 0x22f54b(%rip), %rsi    # 0x59b327
callq 0x19980
cmpl $0x0, %eax
je 0x36bdfc
movq 0x58(%rsp), %rdi
leaq 0x35(%rsp), %rsi
callq 0x4f6a10
movq %rax, 0x8(%rsp)
jmp 0x36be1c
movq 0x2d0135(%rip), %rax    # 0x63bf38
movq (%rax), %rdi
callq 0x197a0
movl %eax, %edi
leaq 0x35(%rsp), %rsi
callq 0x4f6e90
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x40(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x36be3b
jmp 0x36c070
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rsi
callq 0x365ac0
jmp 0x36c060
movq 0x58(%rsp), %rdi
leaq 0x22f4c4(%rip), %rsi    # 0x59b327
callq 0x19980
cmpl $0x0, %eax
je 0x36be84
movq 0x58(%rsp), %rdi
leaq 0x1fa04c(%rip), %rsi    # 0x565ec5
callq 0x19800
movq %rax, (%rsp)
jmp 0x36be92
movq 0x2d00ad(%rip), %rax    # 0x63bf38
movq (%rax), %rax
movq %rax, (%rsp)
movq (%rsp), %rcx
movq 0x40(%rsp), %rax
movq %rcx, 0x8(%rax)
movq 0x40(%rsp), %rax
cmpq $0x0, 0x8(%rax)
jne 0x36beb0
jmp 0x36c070
movq 0x50(%rsp), %rdi
movl $0x58, %esi
callq 0x19240
cmpq $0x0, %rax
je 0x36bed3
movq 0x40(%rsp), %rax
movl (%rax), %ecx
orl $0x8, %ecx
movl %ecx, (%rax)
jmp 0x36bf04
movq 0x50(%rsp), %rdi
movl $0x78, %esi
callq 0x19240
cmpq $0x0, %rax
je 0x36bef6
movq 0x40(%rsp), %rax
movl (%rax), %ecx
orl $0x4, %ecx
movl %ecx, (%rax)
jmp 0x36bf02
movq 0x40(%rsp), %rax
movl (%rax), %ecx
orl $0x0, %ecx
movl %ecx, (%rax)
jmp 0x36bf04
movq 0x50(%rsp), %rdi
movl $0x68, %esi
callq 0x19240
cmpq $0x0, %rax
je 0x36c05e
movb $0x0, %al
callq 0x3656f0
movq %rax, 0x20(%rsp)
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x30(%rax), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x30(%rax)
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x40(%rax), %rcx
movq 0x20(%rsp), %rax
movq %rcx, 0x40(%rax)
movq 0x20(%rsp), %rdi
callq 0x3687a0
movq 0x20(%rsp), %rax
movq $0x0, 0x30(%rax)
movq 0x20(%rsp), %rax
movq $0x0, 0x40(%rax)
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x40(%rax), %rdi
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x30(%rax), %rdx
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rcx
movl $0x1, %esi
callq 0x19ea0
movq 0x20(%rsp), %rax
cmpl $0x0, (%rax)
je 0x36bfea
movq 0x20(%rsp), %rax
movl (%rax), %eax
movq 0x40(%rsp), %rcx
movq 0x10(%rcx), %rcx
cmpl (%rcx), %eax
je 0x36bfe8
leaq 0x2d3195(%rip), %rax    # 0x63f160
cmpl $0x1, (%rax)
jl 0x36bfe8
movq 0x2cffc9(%rip), %rax    # 0x63bfa0
movq (%rax), %rdi
leaq 0x231ab6(%rip), %rsi    # 0x59da97
movb $0x0, %al
callq 0x199f0
jmp 0x36c054
movl $0x0, 0x28(%rsp)
movl 0x28(%rsp), %eax
movq 0x40(%rsp), %rcx
movq 0x10(%rcx), %rcx
cmpl (%rcx), %eax
jge 0x36c052
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x8(%rax), %rax
movslq 0x28(%rsp), %rcx
movq (%rax,%rcx,8), %rdx
movq 0x40(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rax
movslq 0x28(%rsp), %rcx
movl (%rax,%rcx,4), %ecx
leaq 0x231aa5(%rip), %rsi    # 0x59dae3
movb $0x0, %al
callq 0x199f0
movl 0x28(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
jmp 0x36bff2
jmp 0x36c054
movq 0x20(%rsp), %rdi
callq 0x365720
jmp 0x36c060
jmp 0x36c062
jmp 0x36c064
movq 0x40(%rsp), %rax
movq %rax, 0x60(%rsp)
jmp 0x36c083
movq 0x40(%rsp), %rdi
callq 0x19ef0
movq $0x0, 0x60(%rsp)
movq 0x60(%rsp), %rax
addq $0x68, %rsp
retq
nopl (%rax)