// File: /workspace/github_c_makefile_star2_O1/icebert[P]pblat/htslib/sam.c
// Function: bam_hdr_read
bam_hdr_t *bam_hdr_read(BGZF *fp)
{
    bam_hdr_t *h;
    char buf[4];
    int magic_len;
    int32_t i, name_len, num_names = 0;
    size_t bufsize;
    ssize_t bytes;
#ifndef UCSC_CRAM
    // check EOF
    int has_EOF = bgzf_check_EOF(fp);
    if (has_EOF < 0) {
        perror("[W::bam_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__);
#endif
    // 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();
    if (!h) goto nomem;

    // read plain text and the number of reference sequences
    bytes = bgzf_read(fp, &h->l_text, 4);
    if (bytes != 4) goto read_err;
    if (fp->is_be) ed_swap_4p(&h->l_text);

    bufsize = ((size_t) h->l_text) + 1;
    if (bufsize < h->l_text) goto nomem; // so large that adding 1 overflowed
    h->text = (char*)malloc(bufsize);
    if (!h->text) goto nomem;
    h->text[h->l_text] = 0; // make sure it is NULL terminated
    bytes = bgzf_read(fp, h->text, h->l_text);
    if (bytes != h->l_text) goto read_err;

    bytes = bgzf_read(fp, &h->n_targets, 4);
    if (bytes != 4) goto read_err;
    if (fp->is_be) ed_swap_4p(&h->n_targets);

    if (h->n_targets < 0) goto invalid;

    // read reference sequence names and lengths
    if (h->n_targets > 0) {
        h->target_name = (char**)calloc(h->n_targets, sizeof(char*));
        if (!h->target_name) goto nomem;
        h->target_len = (uint32_t*)calloc(h->n_targets, sizeof(uint32_t));
        if (!h->target_len) goto nomem;
    }
    else {
        h->target_name = NULL;
        h->target_len = NULL;
    }

    for (i = 0; i != h->n_targets; ++i) {
        bytes = bgzf_read(fp, &name_len, 4);
        if (bytes != 4) goto read_err;
        if (fp->is_be) ed_swap_4p(&name_len);
        if (name_len <= 0) goto invalid;

        h->target_name[i] = (char*)malloc(name_len);
        if (!h->target_name[i]) goto nomem;
        num_names++;

        bytes = bgzf_read(fp, h->target_name[i], name_len);
        if (bytes != name_len) goto read_err;

        if (h->target_name[i][name_len - 1] != '\0') {
            /* Fix missing NUL-termination.  Is this being too nice?
               We could alternatively bail out with an error. */
            char *new_name;
            if (name_len == INT32_MAX) goto invalid;
            new_name = realloc(h->target_name[i], name_len + 1);
            if (new_name == NULL) goto nomem;
            h->target_name[i] = new_name;
            h->target_name[i][name_len] = '\0';
        }

        bytes = bgzf_read(fp, &h->target_len[i], 4);
        if (bytes != 4) goto read_err;
        if (fp->is_be) ed_swap_4p(&h->target_len[i]);
    }
    return h;

 nomem:
    if (hts_verbose >= 1) fprintf(stderr, "[E::%s] out of memory\n", __func__);
    goto clean;

 read_err:
    if (hts_verbose >= 1) {
        if (bytes < 0) {
            fprintf(stderr, "[E::%s] error reading BGZF stream\n", __func__);
        } else {
            fprintf(stderr, "[E::%s] truncated bam header\n", __func__);
        }
    }
    goto clean;

 invalid:
    if (hts_verbose >= 1) {
        fprintf(stderr, "[E::%s] invalid BAM binary header\n", __func__);
    }

 clean:
    if (h != NULL) {
        h->n_targets = num_names; // ensure we free only allocated target_names
        bam_hdr_destroy(h);
    }
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r14
callq 0x68451
testl %eax, %eax
js 0x6cf7c
jne 0x6ce2b
leaq 0x53fa6(%rip), %rax     # 0xc0dc8
cmpl $0x2, (%rax)
jge 0x6cfb5
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
movq %r14, %rdi
callq 0x67976
cmpl $0x4, %eax
jne 0x6ce4c
cmpl $0x14d4142, 0x4(%rsp)   # imm = 0x14D4142
je 0x6ce70
leaq 0x53f75(%rip), %rax     # 0xc0dc8
cmpl $0x0, (%rax)
jg 0x6cfbf
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %edi
movl $0x38, %esi
callq 0x6470
movq %rax, %rbx
testq %rax, %rax
je 0x6cf66
leaq 0x8(%rbx), %r15
movl $0x4, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x67976
cmpq $0x4, %rax
jne 0x6cf8d
testb $0xc, 0x2(%r14)
je 0x6ceb8
movl (%r15), %eax
bswapl %eax
movl %eax, (%r15)
movl 0x8(%rbx), %r12d
leaq 0x1(%r12), %rdi
callq 0x69d0
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x6cf66
movb $0x0, (%rax,%r12)
movq %r14, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x67976
movl (%r15), %ecx
xorl %r13d, %r13d
cmpq %rcx, %rax
jne 0x6cf90
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x67976
cmpq $0x4, %rax
jne 0x6cf90
testb $0xc, 0x2(%r14)
je 0x6cf1c
movl (%rbx), %eax
bswapl %eax
movl %eax, (%rbx)
movslq (%rbx), %r15
testq %r15, %r15
js 0x6cfe1
testl %r15d, %r15d
je 0x6d00e
movl $0x8, %esi
movq %r15, %rdi
callq 0x6470
movq %rax, 0x20(%rbx)
xorl %r13d, %r13d
testq %rax, %rax
je 0x6cf69
movl $0x4, %esi
movq %r15, %rdi
callq 0x6470
movq %rax, 0x10(%rbx)
testq %rax, %rax
jne 0x6d018
jmp 0x6cf69
xorl %r13d, %r13d
leaq 0x53e58(%rip), %rax     # 0xc0dc8
cmpl $0x0, (%rax)
jle 0x6cf9c
callq 0x7dcd
jmp 0x6cf9c
leaq 0x2d41d(%rip), %rdi     # 0x9a3a0
callq 0x6590
jmp 0x6ce2b
xorl %r13d, %r13d
leaq 0x53e31(%rip), %rcx     # 0xc0dc8
cmpl $0x0, (%rcx)
jg 0x6cfc9
testq %rbx, %rbx
je 0x6ce5c
movl %r13d, (%rbx)
movq %rbx, %rdi
callq 0x6cca3
jmp 0x6ce5c
callq 0x7d64
jmp 0x6ce2b
callq 0x7d83
jmp 0x6ce5c
movq 0x52008(%rip), %rcx     # 0xbefd8
movq (%rcx), %rdi
testq %rax, %rax
jns 0x6cff7
leaq 0x2d42e(%rip), %rsi     # 0x9a40d
jmp 0x6cffe
xorl %r13d, %r13d
leaq 0x53ddd(%rip), %rax     # 0xc0dc8
cmpl $0x0, (%rax)
jle 0x6cf9c
callq 0x7da8
jmp 0x6cf9c
leaq 0x2d432(%rip), %rsi     # 0x9a430
leaq 0x2d3bc(%rip), %rdx     # 0x9a3c1
xorl %eax, %eax
callq 0x6a90
jmp 0x6cf9c
xorl %eax, %eax
movq %rax, 0x20(%rbx)
movq %rax, 0x10(%rbx)
cmpl $0x0, (%rbx)
je 0x6ce5e
xorl %ebp, %ebp
movq %rsp, %r15
xorl %r13d, %r13d
movl $0x4, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x67976
cmpq $0x4, %rax
jne 0x6cf90
testb $0xc, 0x2(%r14)
je 0x6d052
movl (%rsp), %eax
bswapl %eax
movl %eax, (%rsp)
movslq (%rsp), %r12
testq %r12, %r12
jle 0x6cfe4
movq %r12, %rdi
callq 0x69d0
movq 0x20(%rbx), %rcx
movq %rax, (%rcx,%rbp,2)
movq 0x20(%rbx), %rax
movq (%rax,%rbp,2), %rsi
testq %rsi, %rsi
je 0x6cf69
movq %r14, %rdi
movq %r12, %rdx
callq 0x67976
movslq (%rsp), %rcx
cmpq %rcx, %rax
jne 0x6d128
movq 0x20(%rbx), %rax
movq (%rax,%rbp,2), %rdi
cmpb $0x0, -0x1(%rdi,%rcx)
je 0x6d0e4
movl $0x4, %eax
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
je 0x6d0e0
incl %ecx
movslq %ecx, %rsi
callq 0x6c80
testq %rax, %rax
je 0x6d0db
movq 0x20(%rbx), %rcx
movq %rax, (%rcx,%rbp,2)
movq 0x20(%rbx), %rax
movq (%rax,%rbp,2), %rax
movslq (%rsp), %rcx
movb $0x0, (%rax,%rcx)
xorl %eax, %eax
jmp 0x6d0e0
movl $0x2, %eax
testl %eax, %eax
jne 0x6d130
movq 0x10(%rbx), %rsi
addq %rbp, %rsi
movl $0x4, %edx
movq %r14, %rdi
callq 0x67976
cmpq $0x4, %rax
jne 0x6d128
testb $0xc, 0x2(%r14)
je 0x6d111
movq 0x10(%rbx), %rax
movl (%rax,%rbp), %ecx
bswapl %ecx
movl %ecx, (%rax,%rbp)
incq %r13
movl (%rbx), %eax
addq $0x4, %rbp
cmpq %rax, %r13
jne 0x6d029
jmp 0x6ce5e
incl %r13d
jmp 0x6cf90
incl %r13d
cmpl $0x2, %eax
je 0x6cf69
jmp 0x6cfe4