// File: /workspace/github_c_makefile_star2_O2/skoren[P]bacValidation/bamcat.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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
callq 0x59ae
testl %eax, %eax
je 0x3e28
leaq -0x30(%rbp), %rsi
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x5a33
cmpl $0x4, %eax
jne 0x3e08
cmpl $0x14d4142, -0x30(%rbp) # imm = 0x14D4142
jne 0x3e08
callq 0x58dd
movq %rax, %r12
movq %rax, %r14
addq $0x30, %r14
pushq $0x4
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x5a33
cmpl $0x0, 0xbba7(%rip)      # 0xf89c
je 0x3d02
movl (%r14), %r15d
bswapl %r15d
movl %r15d, (%r14)
jmp 0x3d05
movl (%r14), %r15d
leal 0x1(%r15), %edi
pushq $0x1
popq %rsi
callq 0x31c0
movq %rax, 0x38(%r12)
movl %r15d, %edx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x5a33
pushq $0x4
popq %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x5a33
cmpl $0x0, 0xbb63(%rip)      # 0xf89c
je 0x3d47
movl (%r12), %eax
bswapl %eax
movl %eax, (%r12)
jmp 0x3d4b
movl (%r12), %eax
movslq %eax, %r14
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x31c0
movq %rax, -0x40(%rbp)
movq %rax, 0x8(%r12)
pushq $0x4
popq %rsi
movq %r14, %rdi
callq 0x31c0
movq %rax, %r13
movq %r12, -0x38(%rbp)
movq %rax, 0x10(%r12)
movl %r14d, %r15d
shlq $0x2, %r15
xorl %r12d, %r12d
cmpq %r12, %r15
je 0x3df5
movq %rbx, %rdi
leaq -0x2c(%rbp), %rsi
pushq $0x4
popq %rdx
callq 0x5a33
cmpl $0x0, 0xbafe(%rip)      # 0xf89c
movl -0x2c(%rbp), %eax
je 0x3da8
bswapl %eax
movl %eax, -0x2c(%rbp)
movslq %eax, %r14
movq %r14, %rdi
pushq $0x1
popq %rsi
callq 0x31c0
movq -0x40(%rbp), %rcx
movq %rax, (%rcx,%r12,2)
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x5a33
leaq (%r12,%r13), %r14
movq %rbx, %rdi
movq %r14, %rsi
pushq $0x4
popq %rdx
callq 0x5a33
cmpl $0x0, 0xbab7(%rip)      # 0xf89c
je 0x3def
movl (%r14), %eax
bswapl %eax
movl %eax, (%r14)
addq $0x4, %r12
jmp 0x3d83
movq -0x38(%rbp), %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb1c1(%rip), %rax      # 0xefd0
movq (%rax), %rcx
leaq 0x73e8(%rip), %rdi      # 0xb201
pushq $0x46
popq %rsi
pushq $0x1
popq %rdx
callq 0x3660
xorl %eax, %eax
jmp 0x3df9
movq 0xb1a1(%rip), %rax      # 0xefd0
movq (%rax), %rcx
leaq 0x7379(%rip), %rdi      # 0xb1b2
pushq $0x49
popq %rsi
pushq $0x1
popq %rdx
callq 0x3660
jmp 0x3cac