// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/sam.c
// Function: bam_hdr_read
sam_hdr_t *bam_hdr_read(BGZF *fp)
{
    sam_hdr_t *h;
    uint8_t buf[4];
    int magic_len, has_EOF;
    int32_t i, name_len, num_names = 0;
    size_t bufsize;
    ssize_t bytes;
    // check EOF
    has_EOF = bgzf_check_EOF(fp);
    if (has_EOF < 0) {
        perror("[W::bam_hdr_read] bgzf_check_EOF");
    } else if (has_EOF == 0) {
        hts_log_warning("EOF marker is absent. The input is probably truncated");
    }
    // read "BAM1"
    magic_len = bgzf_read(fp, buf, 4);
    if (magic_len != 4 || memcmp(buf, "BAM\1", 4)) {
        hts_log_error("Invalid BAM binary header");
        return 0;
    }
    h = sam_hdr_init();
    if (!h) goto nomem;

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

    bufsize = h->l_text + 1;
    if (bufsize < h->l_text) goto nomem; // so large that adding 1 overflowed
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
    if (bufsize > FUZZ_ALLOC_LIMIT) goto nomem;
#endif
    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
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
    if (h->n_targets > (FUZZ_ALLOC_LIMIT - bufsize)/(sizeof(char*)+sizeof(uint32_t)))
        goto nomem;
#endif
    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:
    hts_log_error("Out of memory");
    goto clean;

 read_err:
    if (bytes < 0) {
        hts_log_error("Error reading BGZF stream");
    } else {
        hts_log_error("Truncated BAM header");
    }
    goto clean;

 invalid:
    hts_log_error("Invalid BAM binary header");

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r14
callq 0xd79a
testl %eax, %eax
js 0x22fbb
jne 0x22e62
leaq 0x7780a(%rip), %rsi     # 0x9a659
leaq 0x763ae(%rip), %rdx     # 0x99204
movl $0x3, %edi
xorl %eax, %eax
callq 0x14378
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
movq %r14, %rdi
callq 0xbf5d
cmpl $0x4, %eax
jne 0x22ea9
cmpl $0x14d4142, 0x4(%rsp)   # imm = 0x14D4142
jne 0x22ea9
movl $0x1, %edi
movl $0x48, %esi
callq 0x4300
xorl %ebp, %ebp
testq %rax, %rax
je 0x22eca
movq %rax, %rbx
leaq 0x7755d(%rip), %rax     # 0x9a400
movq %rax, 0x18(%rbx)
jmp 0x22ecc
leaq 0x777a9(%rip), %rsi     # 0x9a659
leaq 0x777b4(%rip), %rdx     # 0x9a66b
xorl %ebx, %ebx
movl $0x1, %edi
xorl %eax, %eax
callq 0x14378
jmp 0x22ff5
xorl %ebx, %ebx
leaq 0x777b2(%rip), %r15     # 0x9a685
testq %rbx, %rbx
je 0x22fce
leaq 0x4(%rsp), %rsi
movl $0x4, %edx
movq %r14, %rdi
callq 0xbf5d
cmpq $0x4, %rax
jne 0x22fa2
movl 0x4(%rsp), %r12d
movq %r12, 0x8(%rbx)
leaq 0x1(%r12), %rdi
callq 0x4680
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x22fcc
movb $0x0, (%rax,%r12)
movq %r14, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0xbf5d
xorl %ebp, %ebp
cmpq 0x8(%rbx), %rax
jne 0x22fa4
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xbf5d
cmpq $0x4, %rax
jne 0x22fa4
testb $0x8, 0x2(%r14)
je 0x22f56
movl (%rbx), %eax
bswapl %eax
movl %eax, (%rbx)
movslq (%rbx), %r12
xorl %ebp, %ebp
leaq 0x77709(%rip), %rax     # 0x9a66b
testq %r12, %r12
js 0x23007
testl %r12d, %r12d
je 0x2300c
movl $0x8, %esi
movq %r12, %rdi
callq 0x4300
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x22fce
movl $0x4, %esi
movq %r12, %rdi
callq 0x4300
movq %rax, 0x10(%rbx)
testq %rax, %rax
jne 0x23016
jmp 0x22fce
xorl %ebp, %ebp
testq %rax, %rax
leaq 0x776e5(%rip), %rax     # 0x9a693
leaq 0x776f8(%rip), %r15     # 0x9a6ad
cmovsq %rax, %r15
jmp 0x22fce
leaq 0x77676(%rip), %rdi     # 0x9a638
callq 0x43d0
jmp 0x22e62
xorl %ebp, %ebp
leaq 0x77684(%rip), %rsi     # 0x9a659
movl $0x1, %edi
movq %r15, %rdx
xorl %eax, %eax
callq 0x14378
testq %rbx, %rbx
je 0x22ff3
movl %ebp, (%rbx)
movq %rbx, %rdi
callq 0x22ae6
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
jmp 0x22fce
xorl %eax, %eax
movq %rax, 0x20(%rbx)
movq %rax, 0x10(%rbx)
cmpl $0x0, (%rbx)
je 0x22ff5
xorl %r12d, %r12d
xorl %ebp, %ebp
movl $0x4, %edx
movq %r14, %rdi
movq %rsp, %rsi
callq 0xbf5d
cmpq $0x4, %rax
jne 0x22fa4
testb $0x8, 0x2(%r14)
je 0x23049
movl (%rsp), %eax
bswapl %eax
movl %eax, (%rsp)
movslq (%rsp), %r13
testq %r13, %r13
jle 0x23137
movq %r13, %rdi
callq 0x4680
movq 0x20(%rbx), %rcx
movq %rax, (%rcx,%r12,2)
movq 0x20(%rbx), %rax
movq (%rax,%r12,2), %rsi
testq %rsi, %rsi
je 0x22fce
movq %r14, %rdi
movq %r13, %rdx
callq 0xbf5d
movslq (%rsp), %rcx
cmpq %rcx, %rax
jne 0x23125
movq 0x20(%rbx), %rax
movq (%rax,%r12,2), %rdi
cmpb $0x0, -0x1(%rdi,%rcx)
je 0x230df
movl $0x4, %eax
cmpl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
je 0x230db
incl %ecx
movslq %ecx, %rsi
callq 0x4840
testq %rax, %rax
je 0x230d6
movq 0x20(%rbx), %rcx
movq %rax, (%rcx,%r12,2)
movq 0x20(%rbx), %rax
movq (%rax,%r12,2), %rax
movslq (%rsp), %rcx
movb $0x0, (%rax,%rcx)
xorl %eax, %eax
jmp 0x230db
movl $0x2, %eax
testl %eax, %eax
jne 0x2312c
movq 0x10(%rbx), %rsi
addq %r12, %rsi
movl $0x4, %edx
movq %r14, %rdi
callq 0xbf5d
cmpq $0x4, %rax
jne 0x23125
testb $0x8, 0x2(%r14)
je 0x2310e
movq 0x10(%rbx), %rax
movl (%rax,%r12), %ecx
bswapl %ecx
movl %ecx, (%rax,%r12)
incq %rbp
movl (%rbx), %eax
addq $0x4, %r12
cmpq %rax, %rbp
jne 0x23020
jmp 0x22ff5
incl %ebp
jmp 0x22fa4
incl %ebp
cmpl $0x2, %eax
je 0x22fce
leaq 0x7752d(%rip), %r15     # 0x9a66b
jmp 0x22fce