// File: /workspace/github_c_makefile_star2_O3/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 0x4685
testl %eax, %eax
js 0x8bde
jne 0x8bf8
leaq 0x964e(%rip), %rax      # 0x12220
cmpl $0x2, (%rax)
jl 0x8bf8
callq 0x2501
jmp 0x8bf8
callq 0x2070
cmpl $0x1d, (%rax)
je 0x8bf8
leaq 0x9631(%rip), %rax      # 0x12220
cmpl $0x2, (%rax)
jge 0x8d83
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
movq %rbx, %rdi
callq 0x3e73
cmpl $0x4, %eax
jne 0x8c19
cmpl $0x14d4142, 0x4(%rsp)   # imm = 0x14D4142
je 0x8c3e
leaq 0x9600(%rip), %rax      # 0x12220
cmpl $0x0, (%rax)
jg 0x8d79
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
movl $0x38, %esi
callq 0x21f0
movq %rax, %r14
movq %rax, %r15
addq $0x8, %r15
movl $0x4, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3e73
testb $0xc, 0x2(%rbx)
je 0x8c78
movl (%r15), %r12d
bswapl %r12d
movl %r12d, (%r15)
jmp 0x8c7b
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 0x3e73
movl $0x4, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3e73
testb $0xc, 0x2(%rbx)
je 0x8cbb
movl (%r14), %eax
bswapl %eax
movl %eax, (%r14)
jmp 0x8cbe
movl (%r14), %eax
movslq %eax, %r15
movl $0x8, %esi
movq %r15, %rdi
callq 0x21f0
movq %rax, 0x20(%r14)
movl $0x4, %esi
movq %r15, %rdi
callq 0x21f0
movq %rax, 0x10(%r14)
testl %r15d, %r15d
je 0x8c2c
xorl %r13d, %r13d
movq %rsp, %r15
xorl %ebp, %ebp
movl $0x4, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3e73
testb $0xc, 0x2(%rbx)
movl (%rsp), %eax
je 0x8d12
bswapl %eax
movl %eax, (%rsp)
movslq %eax, %r12
movl $0x1, %esi
movq %r12, %rdi
callq 0x21f0
movq 0x20(%r14), %rcx
movq %rax, (%rcx,%r13,2)
movq 0x20(%r14), %rax
movq (%rax,%r13,2), %rsi
movq %rbx, %rdi
movq %r12, %rdx
callq 0x3e73
movq 0x10(%r14), %rsi
addq %r13, %rsi
movl $0x4, %edx
movq %rbx, %rdi
callq 0x3e73
testb $0xc, 0x2(%rbx)
je 0x8d65
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 0x8cf4
jmp 0x8c2c
callq 0x2520
jmp 0x8c29
leaq 0x5786(%rip), %rdi      # 0xe510
callq 0x2370
jmp 0x8bf8