// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
cmpl $0x1, %eax
jne 0x7783f
movq -0x10(%rbp), %rdi
leaq -0x14(%rbp), %rsi
callq 0x6f2b0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x777a7
movq $0x0, -0x8(%rbp)
jmp 0x77c87
cmpl $0x0, -0x14(%rbp)
jl 0x777c8
movslq -0x14(%rbp), %rdi
addq $0x1, %rdi
callq 0x4650
movq %rax, %rcx
movq %rcx, -0x20(%rbp)
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x777d5
movq $0x0, -0x8(%rbp)
jmp 0x77c87
movslq -0x14(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
movslq -0x14(%rbp), %rdx
callq 0x6cee0
movq %rax, %rcx
movq -0x58(%rbp), %rax
cmpq %rcx, %rax
je 0x77813
movq -0x20(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
jmp 0x77c87
movq -0x20(%rbp), %rax
movslq -0x14(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl -0x14(%rbp), %eax
addl $0x4, %eax
movslq %eax, %rcx
movq -0x10(%rbp), %rax
addq 0x8880(%rax), %rcx
movq %rcx, 0x8880(%rax)
jmp 0x77c0c
movq -0x10(%rbp), %rdi
callq 0x75260
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x77860
movq $0x0, -0x8(%rbp)
jmp 0x77c87
movq -0x30(%rbp), %rax
movslq (%rax), %rcx
movq -0x30(%rbp), %rax
addq 0x40(%rax), %rcx
movq -0x10(%rbp), %rax
addq 0x8880(%rax), %rcx
movq %rcx, 0x8880(%rax)
movq -0x10(%rbp), %rax
movq 0x8880(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x8888(%rax)
movq -0x30(%rbp), %rax
cmpl $0x1, 0x2c(%rax)
jge 0x778b7
movq -0x30(%rbp), %rdi
callq 0x74a70
movq $0x0, -0x8(%rbp)
jmp 0x77c87
movq -0x10(%rbp), %rdi
callq 0x6eec0
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x778e0
movq -0x30(%rbp), %rdi
callq 0x74a70
movq $0x0, -0x8(%rbp)
jmp 0x77c87
movq -0x38(%rbp), %rdi
callq 0x6f980
cmpl $0x0, %eax
je 0x7790d
movq -0x30(%rbp), %rdi
callq 0x74a70
movq -0x38(%rbp), %rdi
callq 0x6f940
movq $0x0, -0x8(%rbp)
jmp 0x77c87
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
addl $0x2, %eax
movq -0x10(%rbp), %rcx
movl 0xc(%rcx), %ecx
sarl $0x8, %ecx
cmpl $0x3, %ecx
setge %cl
andb $0x1, %cl
movzbl %cl, %ecx
shll $0x2, %ecx
addl %ecx, %eax
movl %eax, -0x64(%rbp)
movq -0x10(%rbp), %rax
movq 0x8a08(%rax), %rax
movq -0x38(%rbp), %rcx
movslq 0xc(%rcx), %rdi
callq *%rax
movl %eax, %ecx
movl -0x64(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x8a08(%rax), %rax
movq -0x38(%rbp), %rcx
movslq 0x14(%rcx), %rdi
callq *%rax
movl %eax, %ecx
movl -0x60(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x5c(%rbp)
movq -0x10(%rbp), %rax
movq 0x8a08(%rax), %rax
movq -0x38(%rbp), %rcx
movslq 0x10(%rcx), %rdi
callq *%rax
movl %eax, %ecx
movl -0x5c(%rbp), %eax
addl %ecx, %eax
cltq
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rdi
leaq -0x14(%rbp), %rsi
callq 0x6e9a0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
je 0x779c2
cmpl $0x0, -0x14(%rbp)
jl 0x779c2
movq -0x38(%rbp), %rax
movl 0x14(%rax), %eax
subl $0x4, %eax
cmpl -0x14(%rbp), %eax
jge 0x779e1
movq -0x30(%rbp), %rdi
callq 0x74a70
movq -0x38(%rbp), %rdi
callq 0x6f940
movq $0x0, -0x8(%rbp)
jmp 0x77c87
movslq -0x14(%rbp), %rdi
addq $0x1, %rdi
callq 0x4650
movq %rax, %rcx
movq %rcx, -0x20(%rbp)
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x77a1b
movq -0x30(%rbp), %rdi
callq 0x74a70
movq -0x38(%rbp), %rdi
callq 0x6f940
movq $0x0, -0x8(%rbp)
jmp 0x77c87
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x38(%rbp), %rax
addq 0x30(%rax), %rsi
movslq -0x14(%rbp), %rdx
callq 0x43b0
movq -0x20(%rbp), %rax
movslq -0x14(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x38(%rbp), %rdi
callq 0x6f940
movl $0x1, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x2c(%rcx), %eax
jge 0x77b39
movq -0x10(%rbp), %rdi
callq 0x6eec0
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x77a96
movq -0x30(%rbp), %rdi
callq 0x74a70
movq -0x20(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
jmp 0x77c87
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
addl $0x2, %eax
movq -0x10(%rbp), %rcx
movl 0xc(%rcx), %ecx
sarl $0x8, %ecx
cmpl $0x3, %ecx
setge %cl
andb $0x1, %cl
movzbl %cl, %ecx
shll $0x2, %ecx
addl %ecx, %eax
movl %eax, -0x70(%rbp)
movq -0x10(%rbp), %rax
movq 0x8a08(%rax), %rax
movq -0x38(%rbp), %rcx
movslq 0xc(%rcx), %rdi
callq *%rax
movl %eax, %ecx
movl -0x70(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x6c(%rbp)
movq -0x10(%rbp), %rax
movq 0x8a08(%rax), %rax
movq -0x38(%rbp), %rcx
movslq 0x14(%rcx), %rdi
callq *%rax
movl %eax, %ecx
movl -0x6c(%rbp), %eax
addl %ecx, %eax
movl %eax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq 0x8a08(%rax), %rax
movq -0x38(%rbp), %rcx
movslq 0x10(%rcx), %rdi
callq *%rax
movl %eax, %ecx
movl -0x68(%rbp), %eax
addl %ecx, %eax
cltq
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x38(%rbp), %rdi
callq 0x6f940
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x77a54
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
jle 0x77c03
cmpq $0x0, -0x48(%rbp)
jle 0x77c03
movq -0x30(%rbp), %rax
movslq (%rax), %rax
cmpq -0x48(%rbp), %rax
jle 0x77c03
movq -0x30(%rbp), %rax
movslq (%rax), %rdi
subq -0x48(%rbp), %rdi
callq 0x4650
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x77b9c
movq -0x30(%rbp), %rdi
callq 0x74a70
movq -0x20(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
jmp 0x77c87
movq -0x30(%rbp), %rax
movslq (%rax), %rax
subq -0x48(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rsi
movq -0x30(%rbp), %rax
movslq (%rax), %rdx
subq -0x48(%rbp), %rdx
callq 0x6cee0
movq %rax, %rcx
movq -0x78(%rbp), %rax
cmpq %rcx, %rax
je 0x77bfa
movq -0x30(%rbp), %rdi
callq 0x74a70
movq -0x20(%rbp), %rdi
callq 0x45a0
movq -0x50(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
jmp 0x77c87
movq -0x50(%rbp), %rdi
callq 0x45a0
movq -0x30(%rbp), %rdi
callq 0x74a70
callq 0x31e20
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x77c2f
movq -0x20(%rbp), %rdi
callq 0x45a0
movq $0x0, -0x8(%rbp)
jmp 0x77c87
movq -0x28(%rbp), %rdi
movq -0x20(%rbp), %rsi
movslq -0x14(%rbp), %rdx
callq 0xd8d30
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x77c67
movq -0x20(%rbp), %rdi
callq 0x45a0
movq -0x28(%rbp), %rdi
callq 0x31e70
movq $0x0, -0x8(%rbp)
jmp 0x77c87
movslq -0x14(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x80, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)