// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rdi
callq 0x107e0
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x32393
leaq 0xbf7dc(%rip), %rdi     # 0xf1b68
callq 0x43c0
jmp 0x323b5
cmpl $0x0, -0x24(%rbp)
jne 0x323b3
movl $0x3, %edi
leaq 0xbf7e4(%rip), %rsi     # 0xf1b89
leaq 0xbe130(%rip), %rdx     # 0xf04dc
movb $0x0, %al
callq 0x1b030
jmp 0x323b5
movq -0x10(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
movl $0x4, %edx
callq 0xe620
movl %eax, -0x20(%rbp)
cmpl $0x4, -0x20(%rbp)
jne 0x323e6
leaq -0x1c(%rbp), %rax
movl (%rax), %eax
subl $0x14d4142, %eax        # imm = 0x14D4142
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x3240d
movl $0x1, %edi
leaq 0xbf797(%rip), %rsi     # 0xf1b89
leaq 0xbf79d(%rip), %rdx     # 0xf1b96
movb $0x0, %al
callq 0x1b030
movq $0x0, -0x8(%rbp)
jmp 0x3280a
callq 0x31e20
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x32422
jmp 0x32774
movq -0x10(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
movl $0x4, %edx
callq 0xe620
movq %rax, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
je 0x32444
jmp 0x32790
leaq -0x1c(%rbp), %rdi
callq 0x32820
movl %eax, %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jae 0x3247c
jmp 0x32774
movq -0x38(%rbp), %rdi
callq 0x4650
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x324a0
jmp 0x32774
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x18(%rbp), %rcx
movq 0x8(%rcx), %rcx
movb $0x0, (%rax,%rcx)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0xe620
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x8(%rcx), %rax
je 0x324e4
jmp 0x32790
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x4, %edx
callq 0xe620
movq %rax, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
je 0x32506
jmp 0x32790
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x13, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x32520
movq -0x18(%rbp), %rdi
callq 0x32830
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jge 0x3252e
jmp 0x327cf
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jle 0x32591
movq -0x18(%rbp), %rax
movslq (%rax), %rdi
movl $0x8, %esi
callq 0x42f0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x32563
jmp 0x32774
movq -0x18(%rbp), %rax
movslq (%rax), %rdi
movl $0x4, %esi
callq 0x42f0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x3258f
jmp 0x32774
jmp 0x325a9
movq -0x18(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x10(%rax)
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
je 0x32767
movq -0x10(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
movl $0x4, %edx
callq 0xe620
movq %rax, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
je 0x325e1
jmp 0x32790
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x13, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x325fb
leaq -0x2c(%rbp), %rdi
callq 0x32830
cmpl $0x0, -0x2c(%rbp)
jg 0x32606
jmp 0x327cf
movslq -0x2c(%rbp), %rdi
callq 0x4650
movq %rax, %rdx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x28(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x28(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0x3263a
jmp 0x32774
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movslq -0x2c(%rbp), %rdx
callq 0xe620
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
cmpq %rcx, %rax
je 0x32676
jmp 0x32790
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x32700
cmpl $0x7fffffff, -0x2c(%rbp) # imm = 0x7FFFFFFF
jne 0x326a6
jmp 0x327cf
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x4820
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x326d4
jmp 0x32774
movq -0x48(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x28(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x28(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x2c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x28(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movl $0x4, %edx
callq 0xe620
movq %rax, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
je 0x3272e
jmp 0x32790
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x13, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x32757
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x28(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x32830
jmp 0x32759
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x325b0
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3280a
movl $0x1, %edi
leaq 0xbf409(%rip), %rsi     # 0xf1b89
leaq 0xbf429(%rip), %rdx     # 0xf1bb0
movb $0x0, %al
callq 0x1b030
jmp 0x327e9
cmpq $0x0, -0x40(%rbp)
jge 0x327b3
movl $0x1, %edi
leaq 0xbf3e6(%rip), %rsi     # 0xf1b89
leaq 0xbf414(%rip), %rdx     # 0xf1bbe
movb $0x0, %al
callq 0x1b030
jmp 0x327cd
movl $0x1, %edi
leaq 0xbf3ca(%rip), %rsi     # 0xf1b89
leaq 0xbf412(%rip), %rdx     # 0xf1bd8
movb $0x0, %al
callq 0x1b030
jmp 0x327e9
movl $0x1, %edi
leaq 0xbf3ae(%rip), %rsi     # 0xf1b89
leaq 0xbf3b4(%rip), %rdx     # 0xf1b96
movb $0x0, %al
callq 0x1b030
cmpq $0x0, -0x18(%rbp)
je 0x32802
movl -0x30(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rdi
callq 0x31e70
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)