// File: /workspace/github_c_makefile_star2_O0/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
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl $0x1, -0x24(%rbp)
movq -0x10(%rbp), %rdi
callq 0x66d0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x3edd
callq 0x3050
cmpl $0x1d, (%rax)
je 0x3edb
leaq 0xe1ce(%rip), %rdi      # 0x120a4
callq 0x32e0
jmp 0x3efd
cmpl $0x0, -0x24(%rbp)
jne 0x3efb
movq 0x130e6(%rip), %rax     # 0x16fd0
movq (%rax), %rdi
leaq 0xe1d1(%rip), %rsi      # 0x120c5
movb $0x0, %al
callq 0x3590
jmp 0x3efd
movq -0x10(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
movl $0x4, %edx
callq 0x6780
movl %eax, -0x20(%rbp)
cmpl $0x4, -0x20(%rbp)
jne 0x3f32
leaq -0x1c(%rbp), %rdi
leaq 0xe1ec(%rip), %rsi      # 0x1210f
movl $0x4, %edx
callq 0x3180
cmpl $0x0, %eax
je 0x3f57
movq 0x13097(%rip), %rax     # 0x16fd0
movq (%rax), %rdi
leaq 0xe1d1(%rip), %rsi      # 0x12114
movb $0x0, %al
callq 0x3590
movq $0x0, -0x8(%rbp)
jmp 0x40f7
callq 0x6630
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x30, %rsi
movl $0x4, %edx
callq 0x6780
cmpl $0x0, 0x1398f(%rip)     # 0x1790c
je 0x3f8c
movq -0x18(%rbp), %rdi
addq $0x30, %rdi
callq 0x6920
movq -0x18(%rbp), %rax
movl 0x30(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
movl $0x1, %esi
callq 0x3210
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x18(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, %edx
callq 0x6780
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x4, %edx
callq 0x6780
cmpl $0x0, 0x1392a(%rip)     # 0x1790c
je 0x3fed
movq -0x18(%rbp), %rdi
callq 0x6920
movq -0x18(%rbp), %rax
movslq (%rax), %rdi
movl $0x8, %esi
callq 0x3210
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movslq (%rax), %rdi
movl $0x4, %esi
callq 0x3210
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
je 0x40ef
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
movl $0x4, %edx
callq 0x6780
cmpl $0x0, 0x138b8(%rip)     # 0x1790c
je 0x405f
leaq -0x28(%rbp), %rdi
callq 0x6920
movslq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0x3210
movq %rax, %rdx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x24(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movslq -0x28(%rbp), %rdx
callq 0x6780
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x24(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movl $0x4, %edx
callq 0x6780
cmpl $0x0, 0x13847(%rip)     # 0x1790c
je 0x40df
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x24(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x6920
jmp 0x40e1
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x402c
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)