// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movq %rdi, %rbx
movl $0xffffff00, %eax       # imm = 0xFFFFFF00
andl 0xc(%rdi), %eax
cmpl $0x100, %eax            # imm = 0x100
jne 0x52727
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x4d834
cmpl $-0x1, %eax
je 0x52761
movl 0x10(%rsp), %r15d
testl %r15d, %r15d
js 0x52761
leaq 0x1(%r15), %rdi
callq 0x69d0
testq %rax, %rax
je 0x52761
movq %rax, %r12
movq (%rbx), %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x4d865
cmpq %r15, %rax
jne 0x52761
movb $0x0, (%r12,%r15)
movq 0x8840(%rbx), %rax
addq %r15, %rax
addq $0x4, %rax
movq %rax, 0x8840(%rbx)
movq %r12, %rdi
movl %r15d, %esi
callq 0x5969f
movq %rax, %rbx
movq %r12, %rdi
callq 0x68a0
movq %rbx, %rax
jmp 0x52763
movq %rbx, %rdi
callq 0x50ca9
testq %rax, %rax
je 0x52761
movq %rax, %r14
cmpl $0x0, 0x24(%rax)
jle 0x52759
movq %rbx, %rdi
callq 0x4dbb3
testq %rax, %rax
je 0x52759
movq %rax, %r13
movq %rax, %rdi
callq 0x4e1ab
testl %eax, %eax
je 0x52772
movq %r14, %rdi
callq 0x5093a
xorl %eax, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0xc(%r13), %eax
cmpl $0x300, 0xc(%rbx)       # imm = 0x300
setge %r12b
pushq $0x1
popq %rdx
pushq $0x5
popq %rcx
movl %edx, %esi
cmpl $0x80, %eax
jb 0x527b1
cmpl $0x4000, %eax           # imm = 0x4000
jae 0x5279b
pushq $0x2
jmp 0x527a4
cmpl $0x200000, %eax         # imm = 0x200000
jae 0x527a7
pushq $0x3
popq %rsi
jmp 0x527b1
cmpl $0x10000000, %eax       # imm = 0x10000000
movl %ecx, %esi
sbbl $0x0, %esi
movl 0x10(%r13), %ebp
movl 0x14(%r13), %eax
movl %edx, %edi
cmpl $0x80, %eax
jb 0x527e3
cmpl $0x4000, %eax           # imm = 0x4000
jae 0x527cd
pushq $0x2
jmp 0x527d6
cmpl $0x200000, %eax         # imm = 0x200000
jae 0x527d9
pushq $0x3
popq %rdi
jmp 0x527e3
cmpl $0x10000000, %eax       # imm = 0x10000000
movl %ecx, %edi
sbbl $0x0, %edi
cmpl $0x80, %ebp
jb 0x5280f
cmpl $0x4000, %ebp           # imm = 0x4000
jae 0x527f7
pushq $0x2
jmp 0x52801
cmpl $0x200000, %ebp         # imm = 0x200000
jae 0x52804
pushq $0x3
popq %rdx
jmp 0x5280f
cmpl $0x10000000, %ebp       # imm = 0x10000000
movl %ecx, %edx
sbbl $0x0, %edx
movl %edi, 0x14(%rsp)
movl %esi, 0x18(%rsp)
movl %edx, 0x1c(%rsp)
leaq 0x10(%rsp), %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x4d8ed
cmpl $-0x1, %eax
sete %al
movl (%r15), %r15d
testl %r15d, %r15d
sets %cl
orb %al, %cl
jne 0x529a1
movl 0x14(%r13), %eax
addl $-0x4, %eax
cmpl %r15d, %eax
jl 0x529a1
movb %r12b, 0xf(%rsp)
movl %r15d, %eax
movq %rax, 0x20(%rsp)
leaq 0x1(%rax), %rdi
callq 0x69d0
testq %rax, %rax
je 0x529a1
movq %rax, %r12
xorl %eax, %eax
movb 0xf(%rsp), %cl
movb %cl, %al
leal 0x2(%rbp,%rax,4), %ebp
addl 0x18(%rsp), %ebp
addl 0x14(%rsp), %ebp
addl 0x1c(%rsp), %ebp
movq 0x20(%r13), %rsi
addq 0x30(%r13), %rsi
movq %r12, %rdi
movq 0x20(%rsp), %rdx
callq 0x6570
movq 0x20(%rsp), %rax
movb $0x0, (%r12,%rax)
movq %r13, %rdi
callq 0x4e18f
pushq $0x1
popq %r13
cmpl 0x24(%r14), %r13d
jge 0x529b6
movq %rbx, %rdi
callq 0x4dbb3
testq %rax, %rax
je 0x52759
xorl %ecx, %ecx
cmpl $0x300, 0xc(%rbx)       # imm = 0x300
movl 0xc(%rax), %edi
setge %dl
pushq $0x1
popq %rsi
cmpl $0x80, %edi
jb 0x5290e
pushq $0x2
popq %rsi
cmpl $0x4000, %edi           # imm = 0x4000
jb 0x5290e
pushq $0x3
popq %rsi
cmpl $0x200000, %edi         # imm = 0x200000
jb 0x5290e
cmpl $0x10000000, %edi       # imm = 0x10000000
pushq $0x5
popq %rsi
sbbl $0x0, %esi
movl 0x14(%rax), %r8d
pushq $0x1
popq %rdi
cmpl $0x80, %r8d
jb 0x52943
pushq $0x2
popq %rdi
cmpl $0x4000, %r8d           # imm = 0x4000
jb 0x52943
pushq $0x3
popq %rdi
cmpl $0x200000, %r8d         # imm = 0x200000
jb 0x52943
cmpl $0x10000000, %r8d       # imm = 0x10000000
pushq $0x5
popq %rdi
sbbl $0x0, %edi
movl 0x10(%rax), %r9d
pushq $0x1
popq %r8
cmpl $0x80, %r9d
jb 0x5297d
pushq $0x2
popq %r8
cmpl $0x4000, %r9d           # imm = 0x4000
jb 0x5297d
pushq $0x3
popq %r8
cmpl $0x200000, %r9d         # imm = 0x200000
jb 0x5297d
cmpl $0x10000000, %r9d       # imm = 0x10000000
pushq $0x5
popq %r8
sbbl $0x0, %r8d
movb %dl, %cl
shll $0x2, %ecx
addl %r9d, %ebp
addl %esi, %ebp
addl %edi, %ebp
addl %ecx, %ebp
addl %r8d, %ebp
addl $0x2, %ebp
movq %rax, %rdi
callq 0x4e18f
incl %r13d
jmp 0x528b7
movq %r14, %rdi
callq 0x5093a
movq %r13, %rdi
callq 0x4e18f
jmp 0x52761
movl (%r14), %eax
testl %eax, %eax
jle 0x529fb
testl %ebp, %ebp
jle 0x529fb
subl %ebp, %eax
jle 0x529fb
movslq %eax, %r13
movq %r13, %rdi
callq 0x69d0
testq %rax, %rax
je 0x52759
movq %rax, %rbp
movq (%rbx), %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x4d865
cmpq %r13, %rax
jne 0x52759
movq %rbp, %rdi
callq 0x68a0
movq %r14, %rdi
callq 0x5093a
jmp 0x5270c