// File: 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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rdi
callq 0x65d60
movl %eax, -0x24(%rbp)
cmpl $0x0, -0x24(%rbp)
jge 0x1aca3
leaq 0x606e4(%rip), %rdi     # 0x7b380
callq 0x4480
jmp 0x1acd6
cmpl $0x0, -0x24(%rbp)
jne 0x1acd4
leaq 0x6f930(%rip), %rax     # 0x8a5e0
cmpl $0x2, (%rax)
jl 0x1acd4
movq 0x6f314(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x6030f(%rip), %rsi     # 0x7afd5
leaq 0x606d4(%rip), %rdx     # 0x7b3a1
movb $0x0, %al
callq 0x4830
jmp 0x1acd6
movq -0x10(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
movl $0x4, %edx
callq 0x64bb0
movl %eax, -0x20(%rbp)
cmpl $0x4, -0x20(%rbp)
jne 0x1ad0b
leaq -0x1c(%rbp), %rdi
leaq 0x60010(%rip), %rsi     # 0x7ad0c
movl $0x4, %edx
callq 0x4290
cmpl $0x0, %eax
je 0x1ad43
leaq 0x6f8ce(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jl 0x1ad36
movq 0x6f2b2(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x60686(%rip), %rsi     # 0x7b3ae
leaq 0x60672(%rip), %rdx     # 0x7b3a1
movb $0x0, %al
callq 0x4830
movq $0x0, -0x8(%rbp)
jmp 0x1b18f
callq 0x1aa30
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x1ad58
jmp 0x1b0ba
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x8, %rsi
movl $0x4, %edx
callq 0x64bb0
movq %rax, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
je 0x1ad7e
jmp 0x1b0ea
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x12, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x1ad9c
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
callq 0x1b1a0
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 0x1adc0
jmp 0x1b0ba
movq -0x38(%rbp), %rdi
callq 0x47a0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x1ade4
jmp 0x1b0ba
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 0x64bb0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
je 0x1ae2a
jmp 0x1b0ea
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x4, %edx
callq 0x64bb0
movq %rax, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
je 0x1ae4c
jmp 0x1b0ea
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x12, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x1ae66
movq -0x18(%rbp), %rdi
callq 0x1b1a0
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jge 0x1ae74
jmp 0x1b141
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jle 0x1aed7
movq -0x18(%rbp), %rax
movslq (%rax), %rdi
movl $0x8, %esi
callq 0x4370
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x1aea9
jmp 0x1b0ba
movq -0x18(%rbp), %rax
movslq (%rax), %rdi
movl $0x4, %esi
callq 0x4370
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x1aed5
jmp 0x1b0ba
jmp 0x1aeef
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 0x1b0ad
movq -0x10(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
movl $0x4, %edx
callq 0x64bb0
movq %rax, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
je 0x1af27
jmp 0x1b0ea
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x12, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x1af41
leaq -0x2c(%rbp), %rdi
callq 0x1b1a0
cmpl $0x0, -0x2c(%rbp)
jg 0x1af4c
jmp 0x1b141
movslq -0x2c(%rbp), %rdi
callq 0x47a0
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 0x1af80
jmp 0x1b0ba
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 0x64bb0
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movslq -0x2c(%rbp), %rcx
cmpq %rcx, %rax
je 0x1afbc
jmp 0x1b0ea
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 0x1b046
cmpl $0x7fffffff, -0x2c(%rbp) # imm = 0x7FFFFFFF
jne 0x1afec
jmp 0x1b141
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 0x4990
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x1b01a
jmp 0x1b0ba
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 0x64bb0
movq %rax, -0x40(%rbp)
cmpq $0x4, -0x40(%rbp)
je 0x1b074
jmp 0x1b0ea
movq -0x10(%rbp), %rax
movl (%rax), %eax
shrl $0x12, %eax
andl $0x3, %eax
cmpl $0x0, %eax
je 0x1b09d
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdi
movslq -0x28(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rdi
callq 0x1b1a0
jmp 0x1b09f
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1aef6
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x1b18f
leaq 0x6f51f(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jl 0x1b0e5
movq 0x6ef03(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x602fa(%rip), %rsi     # 0x7b3d1
leaq 0x602c3(%rip), %rdx     # 0x7b3a1
movb $0x0, %al
callq 0x4830
jmp 0x1b16e
leaq 0x6f4ef(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jl 0x1b13f
cmpq $0x0, -0x40(%rbp)
jge 0x1b11e
movq 0x6eecc(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x602da(%rip), %rsi     # 0x7b3e8
leaq 0x6028c(%rip), %rdx     # 0x7b3a1
movb $0x0, %al
callq 0x4830
jmp 0x1b13d
movq 0x6eeab(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x602dc(%rip), %rsi     # 0x7b40b
leaq 0x6026b(%rip), %rdx     # 0x7b3a1
movb $0x0, %al
callq 0x4830
jmp 0x1b13f
jmp 0x1b16e
leaq 0x6f498(%rip), %rax     # 0x8a5e0
cmpl $0x1, (%rax)
jl 0x1b16c
movq 0x6ee7c(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x60250(%rip), %rsi     # 0x7b3ae
leaq 0x6023c(%rip), %rdx     # 0x7b3a1
movb $0x0, %al
callq 0x4830
jmp 0x1b16e
cmpq $0x0, -0x18(%rbp)
je 0x1b187
movl -0x30(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x18(%rbp), %rdi
callq 0x1aa50
movq $0x0, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)