// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rdi
callq 0x9d7a0
movl %eax, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
jge 0xa5603
leaq 0x3ed44(%rip), %rdi     # 0xe4340
callq 0x6590
jmp 0xa5636
cmpl $0x0, -0x44(%rbp)
jne 0xa5634
leaq 0x619d0(%rip), %rax     # 0x106fe0
cmpl $0x2, (%rax)
jl 0xa5634
movq 0x5f9b4(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
leaq 0x3ced5(%rip), %rsi     # 0xe24fb
leaq 0x3ed34(%rip), %rdx     # 0xe4361
movb $0x0, %al
callq 0x6a80
jmp 0xa5636
movq -0x10(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
movl $0x4, %edx
callq 0x9c7b0
movl %eax, -0x20(%rbp)
cmpl $0x4, -0x20(%rbp)
jne 0xa566b
leaq -0x1c(%rbp), %rdi
leaq 0x3cc11(%rip), %rsi     # 0xe226d
movl $0x4, %edx
callq 0x6320
cmpl $0x0, %eax
je 0xa56a3
leaq 0x6196e(%rip), %rax     # 0x106fe0
cmpl $0x1, (%rax)
jl 0xa5696
movq 0x5f952(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
leaq 0x3ece6(%rip), %rsi     # 0xe436e
leaq 0x3ecd2(%rip), %rdx     # 0xe4361
movb $0x0, %al
callq 0x6a80
movq $0x0, -0x8(%rbp)
jmp 0xa5aef
callq 0xa5390
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0xa56b8
jmp 0xa5a1a
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
movl $0x4, %edx
callq 0x9c7b0
movq %rax, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
je 0xa56de
jmp 0xa5a4a
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x12, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0xa56fc
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
callq 0xa5b00
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jae 0xa5720
jmp 0xa5a1a
movq -0x38(%rbp), %rdi
callq 0x69c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0xa5744
jmp 0xa5a1a
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
movb $0x0, (%rax,%rcx)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, %edx
callq 0x9c7b0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
je 0xa578a
jmp 0xa5a4a
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x4, %edx
callq 0x9c7b0
movq %rax, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
je 0xa57ac
jmp 0xa5a4a
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x12, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0xa57c6
movq -0x18(%rbp), %rdi
callq 0xa5b00
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jge 0xa57d4
jmp 0xa5aa1
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jle 0xa5837
movq -0x18(%rbp), %rax
movslq (%rax), %rdi
movl $0x8, %esi
callq 0x6460
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0xa5809
jmp 0xa5a1a
movq -0x18(%rbp), %rax
movslq (%rax), %rdi
movl $0x4, %esi
callq 0x6460
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0xa5835
jmp 0xa5a1a
jmp 0xa584f
movq -0x18(%rbp), %rax
movq $0x0, 0x20(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x10(%rax)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
je 0xa5a0d
movq -0x10(%rbp), %rdi
leaq -0x28(%rbp), %rsi
movl $0x4, %edx
callq 0x9c7b0
movq %rax, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
je 0xa5887
jmp 0xa5a4a
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x12, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0xa58a1
leaq -0x28(%rbp), %rdi
callq 0xa5b00
cmpl $0x0, -0x28(%rbp)
jg 0xa58ac
jmp 0xa5aa1
movslq -0x28(%rbp), %rdi
callq 0x69c0
movq %rax, %rdx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x24(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x24(%rbp), %rcx
cmpq $0x0, (%rax,%rcx,8)
jne 0xa58e0
jmp 0xa5a1a
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movslq -0x28(%rbp), %rdx
callq 0x9c7b0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movslq -0x28(%rbp), %rcx
cmpq %rcx, %rax
je 0xa591c
jmp 0xa5a4a
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movl -0x28(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0xa59a6
cmpl $0x7fffffff, -0x28(%rbp) # imm = 0x7FFFFFFF
jne 0xa594c
jmp 0xa5aa1
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movl -0x28(%rbp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x6c80
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0xa597a
jmp 0xa5a1a
movq -0x50(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x24(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movslq -0x24(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movslq -0x28(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movslq -0x24(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movl $0x4, %edx
callq 0x9c7b0
movq %rax, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
je 0xa59d4
jmp 0xa5a4a
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x12, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0xa59fd
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x24(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0xa5b00
jmp 0xa59ff
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0xa5856
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0xa5aef
leaq 0x615bf(%rip), %rax     # 0x106fe0
cmpl $0x1, (%rax)
jl 0xa5a45
movq 0x5f5a3(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
leaq 0x3e95a(%rip), %rsi     # 0xe4391
leaq 0x3e923(%rip), %rdx     # 0xe4361
movb $0x0, %al
callq 0x6a80
jmp 0xa5ace
leaq 0x6158f(%rip), %rax     # 0x106fe0
cmpl $0x1, (%rax)
jl 0xa5a9f
cmpq $0x0, -0x40(%rbp)
jge 0xa5a7e
movq 0x5f56c(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
leaq 0x3e93a(%rip), %rsi     # 0xe43a8
leaq 0x3e8ec(%rip), %rdx     # 0xe4361
movb $0x0, %al
callq 0x6a80
jmp 0xa5a9d
movq 0x5f54b(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
leaq 0x3e93c(%rip), %rsi     # 0xe43cb
leaq 0x3e8cb(%rip), %rdx     # 0xe4361
movb $0x0, %al
callq 0x6a80
jmp 0xa5a9f
jmp 0xa5ace
leaq 0x61538(%rip), %rax     # 0x106fe0
cmpl $0x1, (%rax)
jl 0xa5acc
movq 0x5f51c(%rip), %rax     # 0x104fd0
movq (%rax), %rdi
leaq 0x3e8b0(%rip), %rsi     # 0xe436e
leaq 0x3e89c(%rip), %rdx     # 0xe4361
movb $0x0, %al
callq 0x6a80
jmp 0xa5ace
cmpq $0x0, -0x18(%rbp)
je 0xa5ae7
movl -0x2c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rdi
callq 0xa53b0
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)