// File: /workspace/github_c_makefile_star2_O3/xiongxu[P]bam2bedGraph2/samtools-0.1.19/bam.c
// Function: bam_header_read
bam_header_t *bam_header_read(bamFile fp)
{
	bam_header_t *header;
	char buf[4];
	int magic_len;
	int32_t i = 1, name_len;
	// check EOF
	i = bgzf_check_EOF(fp);
	if (i < 0) {
		// If the file is a pipe, checking the EOF marker will *always* fail
		// with ESPIPE.  Suppress the error message in this case.
		if (errno != ESPIPE) perror("[bam_header_read] bgzf_check_EOF");
	}
	else if (i == 0) fprintf(stderr, "[bam_header_read] EOF marker is absent. The input is probably truncated.\n");
	// read "BAM1"
	magic_len = bam_read(fp, buf, 4);
	if (magic_len != 4 || strncmp(buf, "BAM\001", 4) != 0) {
		fprintf(stderr, "[bam_header_read] invalid BAM binary header (this is not a BAM file).\n");
		return 0;
	}
	header = bam_header_init();
	// read plain text and the number of reference sequences
	bam_read(fp, &header->l_text, 4);
	if (bam_is_be) bam_swap_endian_4p(&header->l_text);
	header->text = (char*)calloc(header->l_text + 1, 1);
	bam_read(fp, header->text, header->l_text);
	bam_read(fp, &header->n_targets, 4);
	if (bam_is_be) bam_swap_endian_4p(&header->n_targets);
	// read reference sequence names and lengths
	header->target_name = (char**)calloc(header->n_targets, sizeof(char*));
	header->target_len = (uint32_t*)calloc(header->n_targets, 4);
	for (i = 0; i != header->n_targets; ++i) {
		bam_read(fp, &name_len, 4);
		if (bam_is_be) bam_swap_endian_4p(&name_len);
		header->target_name[i] = (char*)calloc(name_len, 1);
		bam_read(fp, header->target_name[i], name_len);
		bam_read(fp, &header->target_len[i], 4);
		if (bam_is_be) bam_swap_endian_4p(&header->target_len[i]);
	}
	return header;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
callq 0x1894c
testl %eax, %eax
js 0x9c1b
jne 0x9c29
callq 0x365f
jmp 0x9c29
callq 0x3090
cmpl $0x1d, (%rax)
jne 0x9dbf
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
movq %rbx, %rdi
callq 0x180ad
cmpl $0x4, %eax
jne 0x9db5
cmpl $0x14d4142, 0x4(%rsp)   # imm = 0x14D4142
jne 0x9db5
movl $0x0, 0x19df4(%rip)     # 0x23a50
movl $0x1, %edi
movl $0x40, %esi
callq 0x32e0
movq %rax, %r14
movq %rax, %r15
addq $0x30, %r15
movl $0x4, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x180ad
cmpl $0x0, 0x19dc4(%rip)     # 0x23a50
je 0x9c99
movl (%r15), %r12d
bswapl %r12d
movl %r12d, (%r15)
jmp 0x9c9c
movl (%r15), %r12d
leal 0x1(%r12), %edi
movl $0x1, %esi
callq 0x32e0
movq %rax, 0x38(%r14)
movl %r12d, %edx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x180ad
movl $0x4, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x180ad
cmpl $0x0, 0x19d7c(%rip)     # 0x23a50
je 0x9ce0
movl (%r14), %eax
bswapl %eax
movl %eax, (%r14)
jmp 0x9ce3
movl (%r14), %eax
movslq %eax, %r15
movl $0x8, %esi
movq %r15, %rdi
callq 0x32e0
movq %rax, 0x8(%r14)
movl $0x4, %esi
movq %r15, %rdi
callq 0x32e0
movq %rax, 0x10(%r14)
testl %r15d, %r15d
je 0x9da3
xorl %r13d, %r13d
movq %rsp, %r15
xorl %ebp, %ebp
movl $0x4, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x180ad
cmpl $0x0, 0x19d20(%rip)     # 0x23a50
movl (%rsp), %eax
je 0x9d3a
bswapl %eax
movl %eax, (%rsp)
movslq %eax, %r12
movl $0x1, %esi
movq %r12, %rdi
callq 0x32e0
movq 0x8(%r14), %rcx
movq %rax, (%rcx,%r13,2)
movq 0x8(%r14), %rax
movq (%rax,%r13,2), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x180ad
movq 0x10(%r14), %rsi
addq %r13, %rsi
movl $0x4, %edx
movq %rbx, %rdi
callq 0x180ad
cmpl $0x0, 0x19cd0(%rip)     # 0x23a50
je 0x9d90
movq 0x10(%r14), %rax
movl (%rax,%r13), %ecx
bswapl %ecx
movl %ecx, (%rax,%r13)
incq %rbp
movl (%r14), %eax
addq $0x4, %r13
cmpq %rax, %rbp
jne 0x9d19
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x367b
xorl %r14d, %r14d
jmp 0x9da3
leaq 0x11df7(%rip), %rdi     # 0x1bbbd
callq 0x34d0
jmp 0x9c29