// File: /workspace/github_c_makefile_star2_O1/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 0x4083
movl (%rbx), %r13d
movl %r13d, %eax
orl $0x2, %eax
movl %eax, (%rbx)
movq %r12, %rdi
movl $0x62, %esi
callq 0x3100
testq %rax, %rax
je 0x409e
orl $0x3, %r13d
movl %r13d, (%rbx)
leaq 0x9141(%rip), %rsi      # 0xd1a0
movq %r14, %rdi
callq 0x3460
testl %eax, %eax
je 0x40ef
leaq 0x9390(%rip), %rsi      # 0xd406
movq %r14, %rdi
callq 0x4229
jmp 0x410c
movq %r12, %rdi
movl $0x77, %esi
callq 0x3100
testq %rax, %rax
je 0x4217
callq 0x36fa
movq %r14, %rdi
callq 0x45bf
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x420d
movq %rax, %rdi
callq 0x4674
movq %rax, 0x10(%rbx)
movl (%rax), %edx
testl %edx, %edx
je 0x412a
cmpl $0x2, 0xd264(%rip)      # 0x11330
jl 0x4217
movq 0xcef7(%rip), %rax      # 0x10fd0
movq (%rax), %rdi
leaq 0x90e6(%rip), %rsi      # 0xd1c9
xorl %eax, %eax
callq 0x3480
jmp 0x4217
movq 0xceca(%rip), %rax      # 0x10fc0
movq (%rax), %rdi
callq 0x3330
leaq 0x9301(%rip), %rsi      # 0xd406
movl %eax, %edi
callq 0x4318
movq %rax, 0x8(%rbx)
testq %rax, %rax
je 0x420d
movq %rax, %rdi
callq 0x4407
movq %rax, 0x10(%rbx)
jmp 0x4217
testq %r15, %r15
je 0x41f4
movq %rax, %r14
movq %r15, %rdi
callq 0x4753
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x420d
movq %rax, %r15
movq 0x38(%r14), %r12
testq %r12, %r12
je 0x41e7
movslq 0x30(%r14), %r13
movl 0x30(%r15), %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 0x41c1
incl %eax
movq 0x38(%r15), %rdi
movslq %eax, %rsi
callq 0x3560
movq %rax, 0x38(%r15)
movl 0x30(%r15), %edi
addq 0x38(%r15), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x3150
movl 0x30(%r15), %eax
addl %r13d, %eax
movl %eax, 0x30(%r15)
movq 0x38(%r15), %rcx
movb $0x0, (%rcx,%rax)
movq %r14, %rdi
callq 0x47cb
testq %r15, %r15
je 0x420d
movq 0x10(%rbx), %rax
cmpl $0x0, (%rax)
jne 0x4217
cmpl $0x0, 0xd12c(%rip)      # 0x11330
jle 0x4217
callq 0x36cb
jmp 0x4217
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