// File: /workspace/github_c_makefile_star2_O1/samtools[P]htslib/cram/cram_io.c
// Function: cram_read_SAM_hdr
sam_hdr_t *cram_read_SAM_hdr(cram_fd *fd) {
    int32_t header_len;
    char *header;
    sam_hdr_t *hdr;

    /* 1.1 onwards stores the header in the first block of a container */
    if (CRAM_MAJOR_VERS(fd->version) == 1) {
        /* Length */
        if (-1 == int32_decode(fd, &header_len))
            return NULL;

#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
        if (header_len > FUZZ_ALLOC_LIMIT)
            return NULL;
#endif

        /* Alloc and read */
        if (header_len < 0 || NULL == (header = malloc((size_t) header_len+1)))
            return NULL;

        if (header_len != hread(fd->fp, header, header_len)) {
            free(header);
            return NULL;
        }
        header[header_len] = '\0';

        fd->first_container += 4 + header_len;
    } else {
        cram_container *c = cram_read_container(fd);
        cram_block *b;
        int i;
        int64_t len;

        if (!c)
            return NULL;

        fd->first_container += c->length + c->offset;
        fd->curr_position = fd->first_container;

        if (c->num_blocks < 1) {
            cram_free_container(c);
            return NULL;
        }

        if (!(b = cram_read_block(fd))) {
            cram_free_container(c);
            return NULL;
        }
        if (cram_uncompress_block(b) != 0) {
            cram_free_container(c);
            cram_free_block(b);
            return NULL;
        }

        len = b->comp_size + 2 + 4*(CRAM_MAJOR_VERS(fd->version) >= 3) +
            fd->vv.varint_size(b->content_id) +
            fd->vv.varint_size(b->uncomp_size) +
            fd->vv.varint_size(b->comp_size);

        /* Extract header from 1st block */
        if (-1 == int32_get_blk(b, &header_len) ||
            header_len < 0 || /* Spec. says signed...  why? */
            b->uncomp_size - 4 < header_len) {
            cram_free_container(c);
            cram_free_block(b);
            return NULL;
        }
        if (NULL == (header = malloc((size_t) header_len+1))) {
            cram_free_container(c);
            cram_free_block(b);
            return NULL;
        }
        memcpy(header, BLOCK_END(b), header_len);
        header[header_len] = '\0';
        cram_free_block(b);

        /* Consume any remaining blocks */
        for (i = 1; i < c->num_blocks; i++) {
            if (!(b = cram_read_block(fd))) {
                cram_free_container(c);
                free(header);
                return NULL;
            }
            len += b->comp_size + 2 + 4*(CRAM_MAJOR_VERS(fd->version) >= 3) +
                fd->vv.varint_size(b->content_id) +
                fd->vv.varint_size(b->uncomp_size) +
                fd->vv.varint_size(b->comp_size);
            cram_free_block(b);
        }

        if (c->length > 0 && len > 0 && c->length > len) {
            // Consume padding
            char *pads = malloc(c->length - len);
            if (!pads) {
                cram_free_container(c);
                free(header);
                return NULL;
            }

            if (c->length - len != hread(fd->fp, pads, c->length - len)) {
                cram_free_container(c);
                free(header);
                free(pads);
                return NULL;
            }
            free(pads);
        }

        cram_free_container(c);
    }

    /* Parse */
    hdr = sam_hdr_init();
    if (!hdr) {
        free(header);
        return NULL;
    }

    if (-1 == sam_hdr_add_lines(hdr, header, header_len)) {
        free(header);
        sam_hdr_destroy(hdr);
        return NULL;
    }

    hdr->l_text = header_len;
    hdr->text = header;

    return hdr;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movl $0xffffff00, %eax       # imm = 0xFFFFFF00
andl 0xc(%rdi), %eax
cmpl $0x100, %eax            # imm = 0x100
jne 0x54511
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0x4f08a
cmpl $-0x1, %eax
je 0x5464c
movl 0x8(%rsp), %r15d
testl %r15d, %r15d
js 0x5464c
leaq 0x1(%r15), %rdi
callq 0x4680
testq %rax, %rax
je 0x5464c
movq %rax, %r14
movq (%rbx), %r13
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rbp
subq %rsi, %rbp
cmpq %r15, %rbp
movq %r15, %r12
cmovbq %rbp, %r12
movq %rax, %rdi
movq %r12, %rdx
callq 0x43c0
addq %r12, 0x8(%r13)
cmpq %r15, %rbp
jae 0x544e9
testb $0x2, 0x30(%r13)
je 0x544e9
movq %r13, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0xf2cd
movq %rax, %r12
cmpq %r15, %r12
jne 0x54632
movb $0x0, (%r14,%r15)
movq 0x8880(%rbx), %rax
addq %r15, %rax
addq $0x4, %rax
movq %rax, 0x8880(%rbx)
jmp 0x54601
movq %rbx, %rdi
callq 0x52e66
testq %rax, %rax
je 0x545fb
movq %rax, %r15
movslq (%rax), %rax
addq 0x40(%r15), %rax
addq 0x8880(%rbx), %rax
movq %rax, 0x8880(%rbx)
movq %rax, 0x8888(%rbx)
cmpl $0x0, 0x2c(%r15)
jle 0x545f3
movq %rbx, %rdi
callq 0x4ee88
testq %rax, %rax
je 0x545f3
movq %rax, %r12
movq %rax, %rdi
callq 0x4f84e
testl %eax, %eax
jne 0x545e1
xorl %ebp, %ebp
cmpl $0x300, 0xc(%rbx)       # imm = 0x300
setge %bpl
shll $0x2, %ebp
addl 0x10(%r12), %ebp
movslq 0xc(%r12), %rdi
callq *0x8a08(%rbx)
movl %eax, 0xc(%rsp)
movslq 0x14(%r12), %rdi
callq *0x8a08(%rbx)
movl %eax, 0x10(%rsp)
movslq 0x10(%r12), %rdi
callq *0x8a08(%rbx)
movq %rax, 0x18(%rsp)
leaq 0x8(%rsp), %r14
movq %r12, %rdi
movq %r14, %rsi
callq 0x4eb72
cmpl $-0x1, %eax
sete %cl
movl (%r14), %eax
testl %eax, %eax
sets %dl
orb %cl, %dl
jne 0x545e1
movl %eax, %r13d
movl 0x14(%r12), %eax
addl $-0x4, %eax
cmpl %r13d, %eax
jge 0x54660
movq %r15, %rdi
callq 0x5295e
movq %r12, %rdi
callq 0x4f82d
jmp 0x545fb
movq %r15, %rdi
callq 0x5295e
xorl %eax, %eax
testb %al, %al
je 0x5464c
callq 0x22ac0
testq %rax, %rax
je 0x54632
movq %rax, %rbx
movslq 0x8(%rsp), %rdx
movq %rax, %rdi
movq %r14, %rsi
callq 0x87942
cmpl $-0x1, %eax
je 0x5463c
movslq 0x8(%rsp), %rax
movq %rax, 0x8(%rbx)
movq %r14, 0x28(%rbx)
jmp 0x5464e
movq %r14, %rdi
callq 0x45c0
jmp 0x5464c
movq %r14, %rdi
callq 0x45c0
movq %rbx, %rdi
callq 0x22ae6
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1(%r13), %rdi
callq 0x4680
testq %rax, %rax
je 0x54793
movq %rax, %r14
movl 0xc(%rsp), %eax
addl 0x10(%rsp), %eax
addl %eax, %ebp
movq 0x18(%rsp), %rax
addl %ebp, %eax
addl $0x2, %eax
cltq
movq %rax, 0x10(%rsp)
movq 0x20(%r12), %rsi
addq 0x30(%r12), %rsi
movq %r14, %rdi
movq %r13, %rdx
callq 0x43c0
movb $0x0, (%r14,%r13)
movq %r12, %rdi
callq 0x4f82d
cmpl $0x2, 0x2c(%r15)
jl 0x54742
movl $0x1, %ebp
movq %r14, 0x20(%rsp)
movq %rbx, %rdi
callq 0x4ee88
testq %rax, %rax
je 0x547ad
movq %rax, %r12
xorl %r14d, %r14d
cmpl $0x300, 0xc(%rbx)       # imm = 0x300
setge %r14b
shll $0x2, %r14d
addl 0x10(%rax), %r14d
movslq 0xc(%rax), %rdi
callq *0x8a08(%rbx)
movl %eax, 0xc(%rsp)
movslq 0x14(%r12), %rdi
callq *0x8a08(%rbx)
movl %eax, %r13d
addl 0xc(%rsp), %r13d
addl %r14d, %r13d
movq 0x20(%rsp), %r14
movslq 0x10(%r12), %rdi
callq *0x8a08(%rbx)
addl %r13d, %eax
addl $0x2, %eax
cltq
addq %rax, 0x10(%rsp)
movq %r12, %rdi
callq 0x4f82d
incl %ebp
cmpl 0x2c(%r15), %ebp
jl 0x546c7
movslq (%r15), %r12
testq %r12, %r12
movq 0x10(%rsp), %rax
jle 0x54784
testq %rax, %rax
jle 0x54784
subq %rax, %r12
jle 0x54784
movq %r12, %rdi
callq 0x4680
testq %rax, %rax
je 0x547ad
movq %rax, %r13
movq (%rbx), %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x4d96c
cmpq %rax, %r12
jne 0x547c4
movq %r13, %rdi
callq 0x45c0
movq %r15, %rdi
callq 0x5295e
movb $0x1, %al
jmp 0x545fd
movq %r15, %rdi
callq 0x5295e
movq %r12, %rdi
callq 0x4f82d
xorl %eax, %eax
xorl %r14d, %r14d
jmp 0x545fd
movq %r15, %rdi
callq 0x5295e
movq %r14, %rdi
callq 0x45c0
xorl %eax, %eax
jmp 0x545fd
movq %r15, %rdi
callq 0x5295e
movq %r14, %rdi
callq 0x45c0
movq %r13, %rdi
jmp 0x547b8