// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl $0x1, -0x28(%rbp)
movq -0x10(%rbp), %rdi
callq 0x5d00
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0xc879
callq 0x2060
cmpl $0x1d, (%rax)
je 0xc877
leaq 0xa9ca(%rip), %rax      # 0x17230
cmpl $0x2, (%rax)
jl 0xc877
leaq 0x6b58(%rip), %rdi      # 0x133ca
callq 0x2380
jmp 0xc8ac
cmpl $0x0, -0x24(%rbp)
jne 0xc8aa
leaq 0xa9aa(%rip), %rax      # 0x17230
cmpl $0x2, (%rax)
jl 0xc8aa
movq 0xa766(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
leaq 0x6b4f(%rip), %rsi      # 0x133eb
leaq 0x6b88(%rip), %rdx      # 0x1342b
movb $0x0, %al
callq 0x2230
jmp 0xc8ac
movq -0x10(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
movl $0x4, %edx
callq 0x4fb0
movl %eax, -0x20(%rbp)
cmpl $0x4, -0x20(%rbp)
jne 0xc8e1
leaq -0x1c(%rbp), %rdi
leaq 0x6b66(%rip), %rsi      # 0x13438
movl $0x4, %edx
callq 0x2080
cmpl $0x0, %eax
je 0xc919
leaq 0xa948(%rip), %rax      # 0x17230
cmpl $0x1, (%rax)
jl 0xc90c
movq 0xa704(%rip), %rax      # 0x16ff8
movq (%rax), %rdi
leaq 0x6b3f(%rip), %rsi      # 0x1343d
leaq 0x6b26(%rip), %rdx      # 0x1342b
movb $0x0, %al
callq 0x2230
movq $0x0, -0x8(%rbp)
jmp 0xcae7
callq 0xc760
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
movl $0x4, %edx
callq 0x4fb0
movq -0x10(%rbp), %rax
movl (%rax), %eax
shll $0xc, %eax
sarl $0x1e, %eax
cmpl $0x0, %eax
je 0xc956
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
callq 0xcb00
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %edi
callq 0x22b0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
movb $0x0, (%rax,%rcx)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %edx
callq 0x4fb0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x4, %edx
callq 0x4fb0
movq -0x10(%rbp), %rax
movl (%rax), %eax
shll $0xc, %eax
sarl $0x1e, %eax
cmpl $0x0, %eax
je 0xc9cd
movq -0x18(%rbp), %rdi
callq 0xcb00
movq -0x18(%rbp), %rax
movslq (%rax), %rdi
movl $0x8, %esi
callq 0x2200
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rax
movslq (%rax), %rdi
movl $0x4, %esi
callq 0x2200
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
je 0xcadf
movq -0x10(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
movl $0x4, %edx
callq 0x4fb0
movq -0x10(%rbp), %rax
movl (%rax), %eax
shll $0xc, %eax
sarl $0x1e, %eax
cmpl $0x0, %eax
je 0xca47
leaq -0x2c(%rbp), %rdi
callq 0xcb00
movslq -0x2c(%rbp), %rdi
movl $0x1, %esi
callq 0x2200
movq %rax, %rdx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x28(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movslq -0x2c(%rbp), %rdx
callq 0x4fb0
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x28(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movl $0x4, %edx
callq 0x4fb0
movq -0x10(%rbp), %rax
movl (%rax), %eax
shll $0xc, %eax
sarl $0x1e, %eax
cmpl $0x0, %eax
je 0xcacf
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x28(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0xcb00
jmp 0xcad1
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0xca0c
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)