// File: /workspace/github_c_makefile_star2_O2/skoren[P]bacValidation/bamcat.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
    fprintf(stderr, "Write not supported here.\n");
    exit(1);
  }

	return fp;

open_err_ret:
	free(fp);
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
pushq $0x1
popq %rdi
pushq $0x18
popq %rsi
callq 0x31c0
movq %rax, %rbx
pushq $0x72
popq %rsi
movq %r12, %rdi
callq 0x3100
testq %rax, %rax
je 0x3920
movl $0x2, (%rbx)
pushq $0x62
popq %rsi
movq %r12, %rdi
callq 0x3100
testq %rax, %rax
je 0x3958
movl $0x3, (%rbx)
cmpb $0x2d, (%r14)
jne 0x38f3
cmpb $0x0, 0x1(%r14)
je 0x39a9
leaq 0x7aa8(%rip), %rsi      # 0xb3a2
movq %r14, %rdi
callq 0x3ae5
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x3a64
movq %rax, %rdi
callq 0x3c8b
movq %rax, 0x10(%rbx)
jmp 0x3a6e
pushq $0x77
popq %rsi
movq %r12, %rdi
callq 0x3100
testq %rax, %rax
je 0x3a6e
movq 0xb695(%rip), %rax      # 0xefd0
movq (%rax), %rcx
leaq 0x7852(%rip), %rdi      # 0xb197
pushq $0x1a
popq %rsi
pushq $0x1
popq %rdx
callq 0x3660
pushq $0x1
popq %rdi
callq 0x33f0
movq %r14, %rdi
callq 0x3e49
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x3a64
movq %rax, %rdi
callq 0x3ed4
movq %rax, 0x10(%rbx)
movl (%rax), %edx
testl %edx, %edx
je 0x39cb
cmpl $0x2, 0xb9ca(%rip)      # 0xf350
jl 0x3a6e
movq 0xb63d(%rip), %rax      # 0xefd0
movq (%rax), %rdi
leaq 0x77ca(%rip), %rsi      # 0xb167
xorl %eax, %eax
callq 0x34c0
jmp 0x3a6e
movq 0xb610(%rip), %rax      # 0xefc0
movq (%rax), %rdi
callq 0x3360
leaq 0x79e3(%rip), %rsi      # 0xb3a2
movl %eax, %edi
callq 0x3bb8
jmp 0x3902
testq %r15, %r15
je 0x3a80
movq %rax, %r14
movq %r15, %rdi
callq 0x3f89
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x3a64
movq %rax, %r15
movq 0x38(%r14), %r13
testq %r13, %r13
je 0x3aab
movslq 0x30(%r14), %r12
movl 0x30(%r15), %ecx
leal (%rcx,%r12), %eax
movl %ecx, %edx
sarl %edx
orl %ecx, %edx
movl %edx, %esi
sarl $0x2, %esi
orl %edx, %esi
movl %esi, %edx
sarl $0x4, %edx
orl %esi, %edx
movl %edx, %esi
sarl $0x8, %esi
orl %edx, %esi
movl %esi, %edx
sarl $0x10, %edx
orl %esi, %edx
movl %eax, %esi
sarl %esi
orl %eax, %esi
movl %esi, %eax
sarl $0x2, %eax
orl %esi, %eax
movl %eax, %esi
sarl $0x4, %esi
orl %eax, %esi
movl %esi, %edi
sarl $0x8, %edi
orl %esi, %edi
movl %edi, %eax
sarl $0x10, %eax
orl %edi, %eax
cmpl %eax, %edx
jge 0x3a84
incl %eax
movq 0x38(%r15), %rdi
movslq %eax, %rsi
callq 0x3590
movq %rax, 0x38(%r15)
movl 0x30(%r15), %ecx
jmp 0x3a88
movq %rbx, %rdi
callq 0x33d0
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %al
jmp 0x3aba
movq 0x38(%r15), %rax
movl %ecx, %edi
addq %rax, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x3160
movl 0x30(%r15), %eax
addl %r12d, %eax
movl %eax, 0x30(%r15)
movq 0x38(%r15), %rcx
movb $0x0, (%rcx,%rax)
movq %r14, %rdi
callq 0x46bd
cmpl $0x0, (%r15)
sete %al
testb %al, %al
je 0x3a6e
cmpl $0x0, 0xb88b(%rip)      # 0xf350
jle 0x3a6e
movq 0xb502(%rip), %rax      # 0xefd0
movq (%rax), %rcx
leaq 0x7668(%rip), %rdi      # 0xb140
pushq $0x26
popq %rsi
pushq $0x1
popq %rdx
callq 0x3660
jmp 0x3a6e