// File: /workspace/github_c_makefile_star2_O3/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
movl $0x1, %edi
movl $0x18, %esi
callq 0x31b0
movq %rax, %rbx
movq %r12, %rdi
movl $0x72, %esi
callq 0x3100
testq %rax, %rax
je 0x4350
movl $0x2, (%rbx)
movq %r12, %rdi
movl $0x62, %esi
callq 0x3100
testq %rax, %rax
je 0x436b
movl $0x3, (%rbx)
cmpb $0x2d, (%r14)
jne 0x4323
cmpb $0x0, 0x1(%r14)
je 0x43bc
leaq 0x80d8(%rip), %rsi      # 0xc402
movq %r14, %rdi
callq 0x44e1
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x4477
movq %rax, %rdi
callq 0x4697
movq %rax, 0x10(%rbx)
jmp 0x4481
movq %r12, %rdi
movl $0x77, %esi
callq 0x3100
testq %rax, %rax
je 0x4481
callq 0x36fa
movq %r14, %rdi
callq 0x4856
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x4477
movq %rax, %rdi
callq 0x4903
movq %rax, 0x10(%rbx)
movl (%rax), %edx
testl %edx, %edx
je 0x43de
cmpl $0x2, 0xbf97(%rip)      # 0x10330
jl 0x4481
movq 0xbc2a(%rip), %rax      # 0xffd0
movq (%rax), %rdi
leaq 0x7e17(%rip), %rsi      # 0xc1c7
xorl %eax, %eax
callq 0x3480
jmp 0x4481
movq 0xbbfd(%rip), %rax      # 0xffc0
movq (%rax), %rdi
callq 0x3330
leaq 0x8030(%rip), %rsi      # 0xc402
movl %eax, %edi
callq 0x45bc
jmp 0x4332
testq %r15, %r15
je 0x4493
movq %rax, %r14
movq %r15, %rdi
callq 0x49e2
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x4477
movq %rax, %r15
movq 0x38(%r14), %r13
testq %r13, %r13
je 0x44be
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 0x4497
incl %eax
movq 0x38(%r15), %rdi
movslq %eax, %rsi
callq 0x3560
movq %rax, 0x38(%r15)
movl 0x30(%r15), %ecx
jmp 0x449b
movq %rbx, %rdi
callq 0x33b0
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 0x44cd
movq 0x38(%r15), %rax
movl %ecx, %edi
addq %rax, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x3150
movl 0x30(%r15), %eax
addl %r12d, %eax
movl %eax, 0x30(%r15)
movq 0x38(%r15), %rcx
movb $0x0, (%rcx,%rax)
movq %r14, %rdi
callq 0x4a4f
cmpl $0x0, (%r15)
sete %al
testb %al, %al
je 0x4481
cmpl $0x0, 0xbe58(%rip)      # 0x10330
jle 0x4481
callq 0x36cb
jmp 0x4481