// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movl $0x1, -0x24(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
movl $0x4, %edx
callq 0x38a0
movl %eax, -0x20(%rbp)
cmpl $0x4, -0x20(%rbp)
jne 0x3678
leaq -0x1c(%rbp), %rdi
leaq 0x2da50(%rip), %rsi     # 0x310b9
movl $0x4, %edx
callq 0x20a0
cmpl $0x0, %eax
je 0x369d
movq 0x35979(%rip), %rax     # 0x38ff8
movq (%rax), %rdi
leaq 0x2da35(%rip), %rsi     # 0x310be
movb $0x0, %al
callq 0x22c0
movq $0x0, -0x8(%rbp)
jmp 0x388b
callq 0x3550
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x18, %rsi
movl $0x4, %edx
callq 0x38a0
cmpl $0x4, %eax
je 0x36c6
jmp 0x387a
cmpl $0x0, 0x366e7(%rip)     # 0x39db4
je 0x36dc
movq -0x18(%rbp), %rdi
addq $0x18, %rdi
callq 0x3940
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
addq $0x1, %rdi
movl $0x1, %esi
callq 0x2290
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
callq 0x38a0
cltq
movq -0x18(%rbp), %rcx
cmpq 0x18(%rcx), %rax
je 0x3729
jmp 0x387a
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x4, %edx
callq 0x38a0
cmpl $0x4, %eax
je 0x3745
jmp 0x387a
cmpl $0x0, 0x36668(%rip)     # 0x39db4
je 0x3757
movq -0x18(%rbp), %rdi
callq 0x3940
movq -0x18(%rbp), %rax
movslq (%rax), %rdi
movl $0x8, %esi
callq 0x2290
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movslq (%rax), %rdi
movl $0x4, %esi
callq 0x2290
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
je 0x3870
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
movl $0x4, %edx
callq 0x38a0
cmpl $0x4, %eax
je 0x37c1
jmp 0x387a
cmpl $0x0, 0x365ec(%rip)     # 0x39db4
je 0x37d3
leaq -0x28(%rbp), %rdi
callq 0x3940
movslq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0x2290
movq %rax, %rdx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x24(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movl -0x28(%rbp), %edx
callq 0x38a0
cmpl -0x28(%rbp), %eax
je 0x3817
jmp 0x387a
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x24(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movl $0x4, %edx
callq 0x38a0
cmpl $0x4, %eax
je 0x383f
jmp 0x387a
cmpl $0x0, 0x3656e(%rip)     # 0x39db4
je 0x3860
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x24(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x3940
jmp 0x3862
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x3796
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x388b
movq -0x18(%rbp), %rdi
callq 0x3570
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)