// File: /workspace/github_c_makefile_star2_O2/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 0xbedd
testl %eax, %eax
js 0x1da5f
jne 0x1d927
leaq 0x5bca3(%rip), %rsi     # 0x795b9
leaq 0x5a978(%rip), %rdx     # 0x78295
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x110c6
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0xab95
cmpl $0x4, %eax
jne 0x1d9d6
cmpl $0x14d4142, 0x4(%rsp)   # imm = 0x14D4142
jne 0x1d9d6
callq 0x1d600
movq %rax, %rbx
testq %rax, %rax
je 0x1d9f2
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0xab95
cmpq $0x4, %rax
jne 0x1da14
movl 0x4(%rsp), %r15d
movq %r15, 0x8(%rbx)
leaq 0x1(%r15), %rdi
callq 0x4680
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x1d9f2
movb $0x0, (%rax,%r15)
movq %r14, %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0xab95
xorl %r13d, %r13d
cmpq 0x8(%rbx), %rax
jne 0x1da17
pushq $0x4
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0xab95
cmpq $0x4, %rax
jne 0x1da17
testb $0x8, 0x2(%r14)
jne 0x1da70
movl (%rbx), %ebp
jmp 0x1da76
leaq 0x5bbdc(%rip), %rsi     # 0x795b9
leaq 0x5bbe7(%rip), %rdx     # 0x795cb
xorl %ebx, %ebx
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
jmp 0x1da4d
xorl %r13d, %r13d
leaq 0x5bbbd(%rip), %rsi     # 0x795b9
leaq 0x5bbe2(%rip), %rdx     # 0x795e5
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x110c6
testq %rbx, %rbx
jne 0x1da40
jmp 0x1da4b
xorl %r13d, %r13d
testq %rax, %rax
leaq 0x5bbd2(%rip), %rax     # 0x795f3
leaq 0x5bbe5(%rip), %r15     # 0x7960d
cmovsq %rax, %r15
leaq 0x5bb86(%rip), %rsi     # 0x795b9
pushq $0x1
popq %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x110c6
movl %r13d, (%rbx)
movq %rbx, %rdi
callq 0x1d61e
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x5bb32(%rip), %rdi     # 0x79598
callq 0x43d0
jmp 0x1d927
movl (%rbx), %ebp
bswapl %ebp
movl %ebp, (%rbx)
leaq 0x5bb4e(%rip), %r15     # 0x795cb
testl %ebp, %ebp
js 0x1daba
je 0x1dac2
movl %ebp, %r12d
pushq $0x8
popq %rsi
movq %r12, %rdi
callq 0x4300
movq %rax, 0x20(%rbx)
xorl %r13d, %r13d
testq %rax, %rax
je 0x1d9f5
pushq $0x4
popq %rsi
movq %r12, %rdi
callq 0x4300
movq %rax, 0x10(%rbx)
testq %rax, %rax
jne 0x1dacc
jmp 0x1d9f5
xorl %r13d, %r13d
jmp 0x1da2c
andq $0x0, 0x20(%rbx)
andq $0x0, 0x10(%rbx)
xorl %r13d, %r13d
movl %ebp, %eax
cmpq %rax, %r13
je 0x1da4d
movq %r14, %rdi
movq %rsp, %rsi
pushq $0x4
popq %rdx
callq 0xab95
cmpq $0x4, %rax
jne 0x1da17
testb $0x8, 0x2(%r14)
movl (%rsp), %eax
je 0x1db01
bswapl %eax
movl %eax, (%rsp)
testl %eax, %eax
jle 0x1da2c
movl %eax, %ebp
movq %rbp, %rdi
callq 0x4680
movq 0x20(%rbx), %rcx
movq %rax, (%rcx,%r13,8)
movq 0x20(%rbx), %rax
movq (%rax,%r13,8), %rsi
testq %rsi, %rsi
je 0x1d9f5
leaq 0x1(%r13), %r12
movq %r14, %rdi
movq %rbp, %rdx
callq 0xab95
movslq (%rsp), %rsi
cmpq %rsi, %rax
jne 0x1dbbc
movq 0x20(%rbx), %rcx
movq (%rcx,%r13,8), %rdi
cmpb $0x0, -0x1(%rdi,%rax)
je 0x1db80
cmpl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
je 0x1dbc4
incq %rsi
callq 0x4840
testq %rax, %rax
je 0x1dbcc
movq 0x20(%rbx), %rcx
movq %rax, (%rcx,%r13,8)
movq 0x20(%rbx), %rax
movq (%rax,%r13,8), %rax
movslq (%rsp), %rcx
movb $0x0, (%rax,%rcx)
leaq (,%r13,4), %rsi
addq 0x10(%rbx), %rsi
movq %r14, %rdi
pushq $0x4
popq %rdx
callq 0xab95
cmpq $0x4, %rax
jne 0x1dbbc
testb $0x8, 0x2(%r14)
je 0x1dbb2
movq 0x10(%rbx), %rax
movl (%rax,%r13,4), %ecx
bswapl %ecx
movl %ecx, (%rax,%r13,4)
movl (%rbx), %ebp
movq %r12, %r13
jmp 0x1dacf
movq %r12, %r13
jmp 0x1da17
movq %r12, %r13
jmp 0x1da2c
movq %r12, %r13
jmp 0x1d9f5