// File: /workspace/github_c_makefile_star2_O1/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 0x45d5
testl %eax, %eax
js 0x89cc
jne 0x89e6
leaq 0x9860(%rip), %rax      # 0x12220
cmpl $0x2, (%rax)
jl 0x89e6
callq 0x2501
jmp 0x89e6
callq 0x2070
cmpl $0x1d, (%rax)
je 0x89e6
leaq 0x9843(%rip), %rax      # 0x12220
cmpl $0x2, (%rax)
jge 0x8b70
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
movq %rbx, %rdi
callq 0x3e92
cmpl $0x4, %eax
jne 0x8a07
cmpl $0x14d4142, 0x4(%rsp)   # imm = 0x14D4142
je 0x8a2c
leaq 0x9812(%rip), %rax      # 0x12220
cmpl $0x0, (%rax)
jg 0x8b66
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 0x3e92
testb $0xc, 0x2(%rbx)
je 0x8a63
movl (%r15), %eax
bswapl %eax
movl %eax, (%r15)
movl 0x8(%r14), %r15d
leal 0x1(%r15), %edi
callq 0x22a0
movq %rax, 0x28(%r14)
movb $0x0, (%rax,%r15)
movq %rbx, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x3e92
movl $0x4, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3e92
testb $0xc, 0x2(%rbx)
je 0x8aa5
movl (%r14), %eax
bswapl %eax
movl %eax, (%r14)
movslq (%r14), %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)
cmpl $0x0, (%r14)
je 0x8a1a
xorl %r13d, %r13d
movq %rsp, %r15
xorl %ebp, %ebp
movl $0x4, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3e92
testb $0xc, 0x2(%rbx)
je 0x8afa
movl (%rsp), %eax
bswapl %eax
movl %eax, (%rsp)
movslq (%rsp), %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 0x3e92
movq 0x10(%r14), %rsi
addq %r13, %rsi
movl $0x4, %edx
movq %rbx, %rdi
callq 0x3e92
testb $0xc, 0x2(%rbx)
je 0x8b4e
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 0x8adc
jmp 0x8a1a
callq 0x2520
jmp 0x8a17
leaq 0x5915(%rip), %rdi      # 0xe48c
callq 0x2370
jmp 0x89e6