// File: /workspace/github_c_makefile_star2_O1/icebert[P]pblat/htslib/cram/cram_io.c
// Function: cram_read_SAM_hdr
SAM_hdr *cram_read_SAM_hdr(cram_fd *fd) {
    int32_t header_len;
    char *header;
    SAM_hdr *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;

	/* 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))
	    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, len;

	if (!c)
	    return NULL;

	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);
	    return NULL;
	}

	len = b->comp_size + 2 + 4*(CRAM_MAJOR_VERS(fd->version) >= 3) +
	    itf8_size(b->content_id) + 
	    itf8_size(b->uncomp_size) + 
	    itf8_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);
		return NULL;
	    }
	    len += b->comp_size + 2 + 4*(CRAM_MAJOR_VERS(fd->version) >= 3) +
		itf8_size(b->content_id) + 
		itf8_size(b->uncomp_size) + 
		itf8_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);
		return NULL;
	    }

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

	cram_free_container(c);
    }

    /* Parse */
    hdr = sam_hdr_parse_(header, header_len);
    free(header);

    return hdr;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl $0xffffff00, %eax       # imm = 0xFFFFFF00
andl 0xc(%rdi), %eax
cmpl $0x100, %eax            # imm = 0x100
jne 0x5b536
leaq 0x4(%rsp), %rsi
movq %rbx, %rdi
callq 0x55adf
cmpl $-0x1, %eax
je 0x5b592
movl 0x4(%rsp), %r14d
testl %r14d, %r14d
js 0x5b592
leaq 0x1(%r14), %rdi
callq 0x69d0
testq %rax, %rax
je 0x5b592
movq %rax, %r15
movq (%rbx), %r13
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rbp
subq %rsi, %rbp
cmpq %r14, %rbp
movq %r14, %r12
cmovbq %rbp, %r12
movq %rax, %rdi
movq %r12, %rdx
callq 0x6570
addq %r12, 0x8(%r13)
cmpq %r14, %rbp
jae 0x5b515
movq %r13, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x6b053
movq %rax, %r12
cmpq %r14, %r12
jne 0x5b592
movb $0x0, (%r15,%r14)
movq 0x8840(%rbx), %rax
addq %r14, %rax
addq $0x4, %rax
movq %rax, 0x8840(%rbx)
jmp 0x5b576
movq %rbx, %rdi
callq 0x597db
testq %rax, %rax
je 0x5b570
movq %rax, %r14
cmpl $0x0, 0x24(%rax)
jle 0x5b568
movq %rbx, %rdi
callq 0x55f49
testq %rax, %rax
je 0x5b568
movq %rax, %r12
movq %rax, %rdi
callq 0x567e1
testl %eax, %eax
je 0x5b5a3
movq %r14, %rdi
callq 0x593d4
xorl %eax, %eax
testb %al, %al
je 0x5b592
movl 0x4(%rsp), %esi
movq %r15, %rdi
callq 0x63bd2
movq %rax, %rbx
movq %r15, %rdi
callq 0x68a0
movq %rbx, %rax
jmp 0x5b594
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%r12), %eax
cmpl $0x300, 0xc(%rbx)       # imm = 0x300
setge 0x3(%rsp)
movl $0x1, %ecx
movl $0x1, %edx
cmpl $0x80, %eax
jb 0x5b5ea
movl $0x2, %edx
cmpl $0x4000, %eax           # imm = 0x4000
jb 0x5b5ea
movl $0x3, %edx
cmpl $0x200000, %eax         # imm = 0x200000
jb 0x5b5ea
cmpl $0x10000000, %eax       # imm = 0x10000000
movl $0x5, %edx
sbbl $0x0, %edx
movl 0x10(%r12), %ebp
movl 0x14(%r12), %eax
cmpl $0x80, %eax
jb 0x5b620
movl $0x2, %ecx
cmpl $0x4000, %eax           # imm = 0x4000
jb 0x5b620
movl $0x3, %ecx
cmpl $0x200000, %eax         # imm = 0x200000
jb 0x5b620
cmpl $0x10000000, %eax       # imm = 0x10000000
movl $0x5, %ecx
sbbl $0x0, %ecx
movl %edx, 0x8(%rsp)
movl %ecx, 0xc(%rsp)
movl $0x1, %r13d
cmpl $0x80, %ebp
jb 0x5b662
movl $0x2, %r13d
cmpl $0x4000, %ebp           # imm = 0x4000
jb 0x5b662
movl $0x3, %r13d
cmpl $0x200000, %ebp         # imm = 0x200000
jb 0x5b662
cmpl $0x10000000, %ebp       # imm = 0x10000000
movl $0x5, %r13d
sbbl $0x0, %r13d
leaq 0x4(%rsp), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x55c36
cmpl $-0x1, %eax
sete %cl
movl (%r15), %eax
testl %eax, %eax
sets %dl
orb %cl, %dl
jne 0x5b692
movl %eax, %ecx
movl 0x14(%r12), %eax
addl $-0x4, %eax
cmpl %ecx, %eax
jge 0x5b6a7
movq %r14, %rdi
callq 0x593d4
movq %r12, %rdi
callq 0x567c0
jmp 0x5b570
movq %rcx, 0x10(%rsp)
leaq 0x1(%rcx), %rdi
callq 0x69d0
testq %rax, %rax
je 0x5b860
movq %rax, %r15
xorl %eax, %eax
movb 0x3(%rsp), %cl
movb %cl, %al
leal 0x2(%rbp,%rax,4), %ebp
addl 0x8(%rsp), %ebp
addl 0xc(%rsp), %ebp
addl %r13d, %ebp
movq 0x20(%r12), %rsi
addq 0x30(%r12), %rsi
movq %r15, %rdi
movq 0x10(%rsp), %r13
movq %r13, %rdx
callq 0x6570
movb $0x0, (%r15,%r13)
movq %r12, %rdi
callq 0x567c0
cmpl $0x2, 0x24(%r14)
jl 0x5b814
movl $0x1, %r12d
movq %rbx, %rdi
callq 0x55f49
testq %rax, %rax
je 0x5b87a
movl 0xc(%rax), %r8d
xorl %ecx, %ecx
cmpl $0x300, 0xc(%rbx)       # imm = 0x300
setge %sil
movl $0x1, %edx
movl $0x1, %edi
cmpl $0x80, %r8d
jb 0x5b770
movl $0x2, %edi
cmpl $0x4000, %r8d           # imm = 0x4000
jb 0x5b770
movl $0x3, %edi
cmpl $0x200000, %r8d         # imm = 0x200000
jb 0x5b770
cmpl $0x10000000, %r8d       # imm = 0x10000000
movl $0x5, %edi
sbbl $0x0, %edi
movl 0x14(%rax), %r8d
cmpl $0x80, %r8d
jb 0x5b7a8
movl $0x2, %edx
cmpl $0x4000, %r8d           # imm = 0x4000
jb 0x5b7a8
movl $0x3, %edx
cmpl $0x200000, %r8d         # imm = 0x200000
jb 0x5b7a8
cmpl $0x10000000, %r8d       # imm = 0x10000000
movl $0x5, %edx
sbbl $0x0, %edx
movl 0x10(%rax), %r9d
movl $0x1, %r8d
cmpl $0x80, %r9d
jb 0x5b7ea
movl $0x2, %r8d
cmpl $0x4000, %r9d           # imm = 0x4000
jb 0x5b7ea
movl $0x3, %r8d
cmpl $0x200000, %r9d         # imm = 0x200000
jb 0x5b7ea
cmpl $0x10000000, %r9d       # imm = 0x10000000
movl $0x5, %r8d
sbbl $0x0, %r8d
movb %sil, %cl
shll $0x2, %ecx
addl %r9d, %ebp
addl %edi, %ebp
addl %edx, %ebp
addl %ecx, %ebp
addl %r8d, %ebp
addl $0x2, %ebp
movq %rax, %rdi
callq 0x567c0
incl %r12d
cmpl 0x24(%r14), %r12d
jl 0x5b710
movl (%r14), %eax
testl %eax, %eax
jle 0x5b851
testl %ebp, %ebp
jle 0x5b851
subl %ebp, %eax
jle 0x5b851
movslq %eax, %r12
movq %r12, %rdi
callq 0x69d0
testq %rax, %rax
je 0x5b87a
movq %rax, %r13
movq (%rbx), %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x55b64
cmpq %r12, %rax
jne 0x5b87a
movq %r13, %rdi
callq 0x68a0
movq %r14, %rdi
callq 0x593d4
movb $0x1, %al
jmp 0x5b572
movq %r14, %rdi
callq 0x593d4
movq %r12, %rdi
callq 0x567c0
xorl %eax, %eax
xorl %r15d, %r15d
jmp 0x5b572
movq %r14, %rdi
callq 0x593d4
xorl %eax, %eax
jmp 0x5b572