// File: /workspace/github_c_makefile_star2_O2/DReichLab[P]adna/sam.c
// Function: bam_hdr_read
bam_hdr_t *bam_hdr_read(BGZF *fp)
{
	bam_hdr_t *h;
	char buf[4];
	int magic_len, has_EOF;
	int32_t i = 1, name_len;
	// check EOF
	has_EOF = bgzf_check_EOF(fp);
	if (has_EOF < 0) {
		if (errno != ESPIPE && hts_verbose >= 2) perror("[W::sam_hdr_read] bgzf_check_EOF");
	} else if (has_EOF == 0 && hts_verbose >= 2)
		fprintf(stderr, "[W::%s] EOF marker is absent. The input is probably truncated.\n", __func__);
	// read "BAM1"
	magic_len = bgzf_read(fp, buf, 4);
	if (magic_len != 4 || strncmp(buf, "BAM\1", 4)) {
		if (hts_verbose >= 1) fprintf(stderr, "[E::%s] invalid BAM binary header\n", __func__);
		return 0;
	}
	h = bam_hdr_init();
	// read plain text and the number of reference sequences
	bgzf_read(fp, &h->l_text, 4);
	if (fp->is_be) ed_swap_4p(&h->l_text);
	h->text = (char*)malloc(h->l_text + 1);
	h->text[h->l_text] = 0; // make sure it is NULL terminated
	bgzf_read(fp, h->text, h->l_text);
	bgzf_read(fp, &h->n_targets, 4);
	if (fp->is_be) ed_swap_4p(&h->n_targets);
	// read reference sequence names and lengths
	h->target_name = (char**)calloc(h->n_targets, sizeof(char*));
	h->target_len = (uint32_t*)calloc(h->n_targets, 4);
	for (i = 0; i != h->n_targets; ++i) {
		bgzf_read(fp, &name_len, 4);
		if (fp->is_be) ed_swap_4p(&name_len);
		h->target_name[i] = (char*)calloc(name_len, 1);
		bgzf_read(fp, h->target_name[i], name_len);
		bgzf_read(fp, &h->target_len[i], 4);
		if (fp->is_be) ed_swap_4p(&h->target_len[i]);
	}
	return h;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
callq 0x4148
testl %eax, %eax
js 0x7b97
jne 0x7bb1
leaq 0x96bf(%rip), %rax      # 0x11230
cmpl $0x2, (%rax)
jl 0x7bb1
movq 0x947b(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x491a(%rip), %rsi      # 0xc4a1
leaq 0x4953(%rip), %rdx      # 0xc4e1
xorl %eax, %eax
callq 0x2220
jmp 0x7bb1
callq 0x2070
cmpl $0x1d, (%rax)
je 0x7bb1
leaq 0x9688(%rip), %rax      # 0x11230
cmpl $0x2, (%rax)
jge 0x7d3e
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0x3a53
cmpl $0x4, %eax
jne 0x7bd0
cmpl $0x14d4142, 0x4(%rsp)   # imm = 0x14D4142
je 0x7bf5
leaq 0x9659(%rip), %rax      # 0x11230
cmpl $0x0, (%rax)
jg 0x7d17
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x7ad8
movq %rax, %r14
movq %rax, %r15
addq $0x8, %r15
pushq $0x4
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3a53
testb $0xc, 0x2(%rbx)
je 0x7c23
movl (%r15), %r12d
bswapl %r12d
movl %r12d, (%r15)
jmp 0x7c26
movl (%r15), %r12d
leal 0x1(%r12), %edi
callq 0x22a0
movq %rax, 0x28(%r14)
movl %r12d, %edx
movb $0x0, (%rax,%rdx)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x3a53
pushq $0x4
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3a53
testb $0xc, 0x2(%rbx)
je 0x7c64
movl (%r14), %ebp
bswapl %ebp
movl %ebp, (%r14)
jmp 0x7c67
movl (%r14), %ebp
movslq %ebp, %r12
pushq $0x8
popq %rsi
movq %r12, %rdi
callq 0x21f0
movq %rax, 0x20(%r14)
pushq $0x4
popq %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x21f0
movq %rax, 0x10(%r14)
xorl %r12d, %r12d
xorl %r13d, %r13d
movl %ebp, %eax
cmpq %rax, %r13
je 0x7be3
movq %rbx, %rdi
movq %rsp, %rsi
movq %r15, %rdx
callq 0x3a53
testb $0xc, 0x2(%rbx)
movl (%rsp), %eax
je 0x7cb9
bswapl %eax
movl %eax, (%rsp)
movslq %eax, %rbp
movq %rbp, %rdi
pushq $0x1
popq %rsi
callq 0x21f0
movq 0x20(%r14), %rcx
movq %rax, (%rcx,%r12,2)
movq 0x20(%r14), %rax
movq (%rax,%r12,2), %rsi
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x3a53
movq 0x10(%r14), %rsi
addq %r12, %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0x3a53
testb $0xc, 0x2(%rbx)
je 0x7d08
movq 0x10(%r14), %rax
movl (%rax,%r12), %ecx
bswapl %ecx
movl %ecx, (%rax,%r12)
incq %r13
movl (%r14), %ebp
addq $0x4, %r12
jmp 0x7c92
movq 0x92da(%rip), %rax      # 0x10ff8
movq (%rax), %rdi
leaq 0x47cb(%rip), %rsi      # 0xc4f3
leaq 0x47b2(%rip), %rdx      # 0xc4e1
xorl %r14d, %r14d
xorl %eax, %eax
callq 0x2220
jmp 0x7be3
leaq 0x473b(%rip), %rdi      # 0xc480
callq 0x2380
jmp 0x7bb1