// File: /workspace/github_cpp_makefile_star2_O2/karel-brinda[P]ococo/ext/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, 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_verbose >= 2)
        fprintf(stderr, "[W::%s] EOF marker is absent. The input is probably truncated.\n", __func__);
    // 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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r14
callq 0x37aed
testl %eax, %eax
js 0x10d25
jne 0x10ca8
leaq 0x468a9(%rip), %rax     # 0x57548
cmpl $0x2, (%rax)
jge 0x10d3d
leaq 0x4(%rsp), %rsi
pushq $0x4
popq %rdx
movq %r14, %rdi
callq 0x37049
cmpl $0x4, %eax
jne 0x10cc7
cmpl $0x14d4142, 0x4(%rsp)   # imm = 0x14D4142
je 0x10ceb
leaq 0x4687a(%rip), %rax     # 0x57548
cmpl $0x0, (%rax)
jg 0x10d61
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x10b38
movq %rax, %rbx
testq %rax, %rax
je 0x10deb
leaq 0x8(%rbx), %r15
pushq $0x4
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x37049
cmpq $0x4, %rax
jne 0x10d36
testb $0xc, 0x2(%r14)
je 0x10d87
movl (%r15), %eax
bswapl %eax
movl %eax, (%r15)
jmp 0x10d8a
leaq 0x33514(%rip), %rdi     # 0x44240
callq 0x4440
jmp 0x10ca8
xorl %ebp, %ebp
jmp 0x10f8e
movq 0x4628c(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0x33140(%rip), %rsi     # 0x43e8e
leaq 0x3350c(%rip), %rdx     # 0x44261
xorl %eax, %eax
callq 0x4770
jmp 0x10ca8
movq 0x46268(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0x33501(%rip), %rsi     # 0x44273
leaq 0x334e8(%rip), %rdx     # 0x44261
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x4770
jmp 0x10cd9
movl (%r15), %eax
movl %eax, %r12d
leaq 0x1(%r12), %rdi
callq 0x46f0
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x10deb
movb $0x0, (%rax,%r12)
movq %r14, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x37049
movl (%r15), %ecx
xorl %ebp, %ebp
cmpq %rcx, %rax
jne 0x10f8e
pushq $0x4
popq %rdx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x37049
cmpq $0x4, %rax
jne 0x10f8e
testb $0xc, 0x2(%r14)
je 0x10e28
movl (%rbx), %r13d
bswapl %r13d
movl %r13d, (%rbx)
jmp 0x10e2b
xorl %ebp, %ebp
leaq 0x46754(%rip), %rax     # 0x57548
cmpl $0x0, (%rax)
jg 0x10e07
testq %rbx, %rbx
jne 0x10f9a
jmp 0x10cd7
movq 0x461c2(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0x3347e(%rip), %rsi     # 0x44296
leaq 0x33442(%rip), %rdx     # 0x44261
xorl %eax, %eax
callq 0x4770
jmp 0x10df9
movl (%rbx), %r13d
testl %r13d, %r13d
js 0x10e61
je 0x10e89
movl %r13d, %r15d
pushq $0x8
popq %rsi
movq %r15, %rdi
callq 0x4370
movq %rax, 0x20(%rbx)
xorl %ebp, %ebp
testq %rax, %rax
je 0x10ded
pushq $0x4
popq %rsi
movq %r15, %rdi
callq 0x4370
movq %rax, 0x10(%rbx)
testq %rax, %rax
jne 0x10e93
jmp 0x10ded
xorl %ebp, %ebp
leaq 0x466de(%rip), %rax     # 0x57548
cmpl $0x0, (%rax)
jle 0x10f9a
movq 0x46156(%rip), %rax     # 0x56fd0
movq (%rax), %rdi
leaq 0x333ef(%rip), %rsi     # 0x44273
jmp 0x10fc8
andq $0x0, 0x20(%rbx)
andq $0x0, 0x10(%rbx)
xorl %ebp, %ebp
pushq $0x4
popq %r12
movl %r13d, %eax
cmpq %rax, %rbp
je 0x10cd9
movq %r14, %rdi
movq %rsp, %rsi
movq %r12, %rdx
callq 0x37049
cmpq $0x4, %rax
jne 0x10f8e
testb $0xc, 0x2(%r14)
movl (%rsp), %eax
je 0x10ecc
bswapl %eax
movl %eax, (%rsp)
testl %eax, %eax
jle 0x10e63
movl %eax, %r13d
movq %r13, %rdi
callq 0x46f0
movq 0x20(%rbx), %rcx
movq %rax, (%rcx,%rbp,8)
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rsi
testq %rsi, %rsi
je 0x10ded
leaq 0x1(%rbp), %r15
movq %r14, %rdi
movq %r13, %rdx
callq 0x37049
movslq (%rsp), %rsi
cmpq %rsi, %rax
jne 0x10f8b
movq 0x20(%rbx), %rcx
movq (%rcx,%rbp,8), %rdi
cmpb $0x0, -0x1(%rdi,%rax)
je 0x10f50
cmpl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
je 0x10fd8
incq %rsi
callq 0x48c0
testq %rax, %rax
je 0x10fe0
movq 0x20(%rbx), %rcx
movq %rax, (%rcx,%rbp,8)
movq 0x20(%rbx), %rax
movq (%rax,%rbp,8), %rax
movslq (%rsp), %rcx
movb $0x0, (%rax,%rcx)
leaq (,%rbp,4), %rsi
addq 0x10(%rbx), %rsi
movq %r14, %rdi
movq %r12, %rdx
callq 0x37049
cmpq $0x4, %rax
jne 0x10f8b
testb $0xc, 0x2(%r14)
je 0x10f80
movq 0x10(%rbx), %rax
movl (%rax,%rbp,4), %ecx
bswapl %ecx
movl %ecx, (%rax,%rbp,4)
movl (%rbx), %r13d
movq %r15, %rbp
jmp 0x10e99
movq %r15, %rbp
leaq 0x465b3(%rip), %rcx     # 0x57548
cmpl $0x0, (%rcx)
jg 0x10fa9
movl %ebp, (%rbx)
movq %rbx, %rdi
callq 0x10b43
jmp 0x10cd7
movq 0x46020(%rip), %rcx     # 0x56fd0
movq (%rcx), %rdi
testq %rax, %rax
jns 0x10fc1
leaq 0x332ee(%rip), %rsi     # 0x442ad
jmp 0x10fc8
leaq 0x33308(%rip), %rsi     # 0x442d0
leaq 0x33292(%rip), %rdx     # 0x44261
xorl %eax, %eax
callq 0x4770
jmp 0x10f9a
movq %r15, %rbp
jmp 0x10e63
movq %r15, %rbp
jmp 0x10ded