// File: /workspace/github_c_makefile_star2_O3/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
subq $0x18, %rsp
movq %rdi, %r14
callq 0xd6bc
testl %eax, %eax
js 0x2255f
jne 0x22444
leaq 0x75238(%rip), %rsi     # 0x97669
leaq 0x73e6c(%rip), %rdx     # 0x962a4
movl $0x3, %edi
xorl %eax, %eax
callq 0x13de5
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
movq %r14, %rdi
callq 0xbefc
cmpl $0x4, %eax
jne 0x22518
cmpl $0x14d4142, 0xc(%rsp)   # imm = 0x14D4142
jne 0x22518
movl $0x1, %edi
movl $0x48, %esi
callq 0x4300
testq %rax, %rax
je 0x22528
movq %rax, %rbx
leaq 0x74f81(%rip), %rax     # 0x97410
movq %rax, 0x18(%rbx)
leaq 0xc(%rsp), %rsi
movl $0x4, %edx
movq %r14, %rdi
callq 0xbefc
cmpq $0x4, %rax
jne 0x22546
movl 0xc(%rsp), %r15d
movq %r15, 0x8(%rbx)
leaq 0x1(%r15), %rdi
callq 0x4680
movq %rax, 0x28(%rbx)
leaq 0x751c9(%rip), %r13     # 0x97695
testq %rax, %rax
je 0x22570
movb $0x0, (%rax,%r15)
movq %r14, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0xbefc
xorl %ebp, %ebp
cmpq 0x8(%rbx), %rax
jne 0x22548
movl $0x4, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xbefc
cmpq $0x4, %rax
jne 0x22548
testb $0x8, 0x2(%r14)
jne 0x225a9
movl (%rbx), %eax
jmp 0x225af
leaq 0x7514a(%rip), %rsi     # 0x97669
leaq 0x75155(%rip), %rdx     # 0x9767b
jmp 0x22536
leaq 0x7513a(%rip), %rsi     # 0x97669
leaq 0x7515f(%rip), %rdx     # 0x97695
xorl %ebx, %ebx
movl $0x1, %edi
xorl %eax, %eax
callq 0x13de5
jmp 0x22597
xorl %ebp, %ebp
testq %rax, %rax
leaq 0x75151(%rip), %rax     # 0x976a3
leaq 0x75164(%rip), %r15     # 0x976bd
cmovsq %rax, %r15
jmp 0x22575
leaq 0x750e2(%rip), %rdi     # 0x97648
callq 0x43d0
jmp 0x22444
xorl %ebp, %ebp
movq %r13, %r15
leaq 0x750ed(%rip), %rsi     # 0x97669
movl $0x1, %edi
movq %r15, %rdx
xorl %eax, %eax
callq 0x13de5
movl %ebp, (%rbx)
movq %rbx, %rdi
callq 0x22102
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl (%rbx), %eax
bswapl %eax
movl %eax, (%rbx)
xorl %ebp, %ebp
leaq 0x750c3(%rip), %r15     # 0x9767b
testl %eax, %eax
js 0x22575
je 0x22703
movl %eax, %r12d
movl $0x8, %esi
movq %r12, %rdi
callq 0x4300
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x22572
movl $0x4, %esi
movq %r12, %rdi
callq 0x4300
movq %rax, 0x10(%rbx)
testq %rax, %rax
je 0x22572
xorl %ebp, %ebp
movl $0x4, %edx
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
callq 0xbefc
cmpq $0x4, %rax
jne 0x22548
testb $0x8, 0x2(%r14)
movl 0x8(%rsp), %eax
je 0x22620
bswapl %eax
movl %eax, 0x8(%rsp)
testl %eax, %eax
jle 0x22575
movq %r13, %r12
movl %eax, %r13d
movq %r13, %rdi
callq 0x4680
movq 0x20(%rbx), %rcx
movq %rax, (%rcx,%rbp,8)
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rsi
testq %rsi, %rsi
je 0x2271c
leaq 0x1(%rbp), %rax
movq %rax, 0x10(%rsp)
movq %r14, %rdi
movq %r13, %rdx
callq 0xbefc
movslq 0x8(%rsp), %rsi
cmpq %rsi, %rax
jne 0x22712
movq 0x20(%rbx), %rcx
movq (%rcx,%rbp,8), %rdi
cmpb $0x0, -0x1(%rdi,%rax)
movq %r12, %r13
je 0x226b9
cmpl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
je 0x22724
incq %rsi
callq 0x4840
testq %rax, %rax
je 0x2272e
movq 0x20(%rbx), %rcx
movq %rax, (%rcx,%rbp,8)
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rax
movslq 0x8(%rsp), %rcx
movb $0x0, (%rax,%rcx)
leaq (,%rbp,4), %rsi
addq 0x10(%rbx), %rsi
movl $0x4, %edx
movq %r14, %rdi
callq 0xbefc
cmpq $0x4, %rax
jne 0x22712
testb $0x8, 0x2(%r14)
je 0x226eb
movq 0x10(%rbx), %rax
movl (%rax,%rbp,4), %ecx
bswapl %ecx
movl %ecx, (%rax,%rbp,4)
movl (%rbx), %eax
movq 0x10(%rsp), %rcx
movq %rcx, %rbp
cmpq %rax, %rcx
jne 0x225f3
jmp 0x22597
xorl %eax, %eax
movq %rax, 0x20(%rbx)
movq %rax, 0x10(%rbx)
jmp 0x22597
movq 0x10(%rsp), %rbp
jmp 0x22548
movq %r12, %r13
jmp 0x22572
movq 0x10(%rsp), %rbp
jmp 0x22575
movq 0x10(%rsp), %rbp
jmp 0x22572