// File: /workspace/github_c_makefile_star2_O2/c-zhou[P]yahs/bamlite.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;
    // 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 NULL;
    }
    header = bam_header_init();
    // read plain text and the number of reference sequences
    if (bam_read(fp, &header->l_text, 4) != 4) goto fail; 
    if (bam_is_be) bam_swap_endian_4p(&header->l_text);
    header->text = (char*)calloc(header->l_text + 1, 1);
    if (bam_read(fp, header->text, header->l_text) != header->l_text) goto fail;
    if (bam_read(fp, &header->n_targets, 4) != 4) goto fail;
    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) {
        if (bam_read(fp, &name_len, 4) != 4) goto fail;
        if (bam_is_be) bam_swap_endian_4p(&name_len);
        header->target_name[i] = (char*)calloc(name_len, 1);
        if (bam_read(fp, header->target_name[i], name_len) != name_len) {
            goto fail;
        }
        if (bam_read(fp, &header->target_len[i], 4) != 4) goto fail;
        if (bam_is_be) bam_swap_endian_4p(&header->target_len[i]);
    }
    return header;
 fail:
    bam_header_destroy(header);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
callq 0x2f4b
cmpl $0x4, %eax
jne 0x2e47
cmpl $0x14d4142, 0x4(%rsp)   # imm = 0x14D4142
jne 0x2e47
callq 0x2d18
movq %rax, %r14
movq %rax, %r15
addq $0x18, %r15
pushq $0x4
popq %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2f4b
cmpl $0x4, %eax
jne 0x2f2e
cmpl $0x0, 0x25830(%rip)     # 0x28614
je 0x2dee
movl (%r15), %eax
bswapl %eax
movl %eax, (%r15)
movq 0x18(%r14), %r15
leaq 0x1(%r15), %rdi
pushq $0x1
popq %rsi
callq 0x2290
movq %rax, 0x28(%r14)
movq %rbx, %rdi
movq %rax, %rsi
movl %r15d, %edx
callq 0x2f4b
cltq
cmpq %rax, 0x18(%r14)
jne 0x2f2e
pushq $0x4
popq %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2f4b
cmpl $0x4, %eax
jne 0x2f2e
cmpl $0x0, 0x257da(%rip)     # 0x28614
je 0x2e68
movl (%r14), %r13d
bswapl %r13d
movl %r13d, (%r14)
jmp 0x2e6b
movq 0x251aa(%rip), %rax     # 0x27ff8
movq (%rax), %rcx
leaq 0x1b281(%rip), %rdi     # 0x1e0d9
pushq $0x46
popq %rsi
pushq $0x1
popq %rdx
callq 0x24d0
jmp 0x2f36
movl (%r14), %r13d
movslq %r13d, %r15
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x2290
movq %rax, 0x8(%r14)
pushq $0x4
popq %rsi
movq %r15, %rdi
callq 0x2290
movq %rax, 0x10(%r14)
xorl %r12d, %r12d
pushq $0x4
popq %rbp
xorl %r15d, %r15d
movl %r13d, %eax
cmpq %rax, %r15
je 0x2f39
movq %rbx, %rdi
movq %rsp, %rsi
movl %ebp, %edx
callq 0x2f4b
cmpl $0x4, %eax
jne 0x2f2e
cmpl $0x0, 0x2575a(%rip)     # 0x28614
movl (%rsp), %eax
je 0x2ec4
bswapl %eax
movl %eax, (%rsp)
movslq %eax, %r13
movq %r13, %rdi
pushq $0x1
popq %rsi
callq 0x2290
movq 0x8(%r14), %rcx
movq %rax, (%rcx,%r12,2)
movq 0x8(%r14), %rax
movq (%rax,%r12,2), %rsi
movq %rbx, %rdi
movl %r13d, %edx
callq 0x2f4b
cmpl (%rsp), %eax
jne 0x2f2e
movq 0x10(%r14), %rsi
addq %r12, %rsi
movq %rbx, %rdi
movl %ebp, %edx
callq 0x2f4b
cmpl $0x4, %eax
jne 0x2f2e
cmpl $0x0, 0x25705(%rip)     # 0x28614
je 0x2f1f
movq 0x10(%r14), %rax
movl (%rax,%r12), %ecx
bswapl %ecx
movl %ecx, (%rax,%r12)
incq %r15
movl (%r14), %r13d
addq $0x4, %r12
jmp 0x2e95
movq %r14, %rdi
callq 0x2d2a
xorl %r14d, %r14d
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq