// File: /workspace/github_cpp_makefile_star2_O2/karel-brinda[P]ococo/ext/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 0x2c7a4
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0x277cc
cmpl $-0x1, %eax
je 0x2c7de
movl 0x10(%rsp), %r15d
testl %r15d, %r15d
js 0x2c7de
leaq 0x1(%r15), %rdi
callq 0x46f0
testq %rax, %rax
je 0x2c7de
movq %rax, %r12
movq (%rbx), %rdi
movq %rax, %rsi
movq %r15, %rdx
callq 0x277fd
cmpq %r15, %rax
jne 0x2c7de
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 0x338ce
movq %rax, %rbx
movq %r12, %rdi
callq 0x4600
movq %rbx, %rax
jmp 0x2c7e0
movq %rbx, %rdi
callq 0x2ad26
testq %rax, %rax
je 0x2c7de
movq %rax, %r14
cmpl $0x0, 0x24(%rax)
jle 0x2c7d6
movq %rbx, %rdi
callq 0x27b4b
testq %rax, %rax
je 0x2c7d6
movq %rax, %r13
movq %rax, %rdi
callq 0x28143
testl %eax, %eax
je 0x2c7ef
movq %r14, %rdi
callq 0x2a9b7
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 0x2c82e
cmpl $0x4000, %eax           # imm = 0x4000
jae 0x2c818
pushq $0x2
jmp 0x2c821
cmpl $0x200000, %eax         # imm = 0x200000
jae 0x2c824
pushq $0x3
popq %rsi
jmp 0x2c82e
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 0x2c860
cmpl $0x4000, %eax           # imm = 0x4000
jae 0x2c84a
pushq $0x2
jmp 0x2c853
cmpl $0x200000, %eax         # imm = 0x200000
jae 0x2c856
pushq $0x3
popq %rdi
jmp 0x2c860
cmpl $0x10000000, %eax       # imm = 0x10000000
movl %ecx, %edi
sbbl $0x0, %edi
cmpl $0x80, %ebp
jb 0x2c88c
cmpl $0x4000, %ebp           # imm = 0x4000
jae 0x2c874
pushq $0x2
jmp 0x2c87e
cmpl $0x200000, %ebp         # imm = 0x200000
jae 0x2c881
pushq $0x3
popq %rdx
jmp 0x2c88c
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 0x27885
cmpl $-0x1, %eax
sete %al
movl (%r15), %r15d
testl %r15d, %r15d
sets %cl
orb %al, %cl
jne 0x2ca1e
movl 0x14(%r13), %eax
addl $-0x4, %eax
cmpl %r15d, %eax
jl 0x2ca1e
movb %r12b, 0xf(%rsp)
movl %r15d, %eax
movq %rax, 0x20(%rsp)
leaq 0x1(%rax), %rdi
callq 0x46f0
testq %rax, %rax
je 0x2ca1e
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 0x4420
movq 0x20(%rsp), %rax
movb $0x0, (%r12,%rax)
movq %r13, %rdi
callq 0x28127
pushq $0x1
popq %r13
cmpl 0x24(%r14), %r13d
jge 0x2ca33
movq %rbx, %rdi
callq 0x27b4b
testq %rax, %rax
je 0x2c7d6
xorl %ecx, %ecx
cmpl $0x300, 0xc(%rbx)       # imm = 0x300
movl 0xc(%rax), %edi
setge %dl
pushq $0x1
popq %rsi
cmpl $0x80, %edi
jb 0x2c98b
pushq $0x2
popq %rsi
cmpl $0x4000, %edi           # imm = 0x4000
jb 0x2c98b
pushq $0x3
popq %rsi
cmpl $0x200000, %edi         # imm = 0x200000
jb 0x2c98b
cmpl $0x10000000, %edi       # imm = 0x10000000
pushq $0x5
popq %rsi
sbbl $0x0, %esi
movl 0x14(%rax), %r8d
pushq $0x1
popq %rdi
cmpl $0x80, %r8d
jb 0x2c9c0
pushq $0x2
popq %rdi
cmpl $0x4000, %r8d           # imm = 0x4000
jb 0x2c9c0
pushq $0x3
popq %rdi
cmpl $0x200000, %r8d         # imm = 0x200000
jb 0x2c9c0
cmpl $0x10000000, %r8d       # imm = 0x10000000
pushq $0x5
popq %rdi
sbbl $0x0, %edi
movl 0x10(%rax), %r9d
pushq $0x1
popq %r8
cmpl $0x80, %r9d
jb 0x2c9fa
pushq $0x2
popq %r8
cmpl $0x4000, %r9d           # imm = 0x4000
jb 0x2c9fa
pushq $0x3
popq %r8
cmpl $0x200000, %r9d         # imm = 0x200000
jb 0x2c9fa
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 0x28127
incl %r13d
jmp 0x2c934
movq %r14, %rdi
callq 0x2a9b7
movq %r13, %rdi
callq 0x28127
jmp 0x2c7de
movl (%r14), %eax
testl %eax, %eax
jle 0x2ca78
testl %ebp, %ebp
jle 0x2ca78
subl %ebp, %eax
jle 0x2ca78
movslq %eax, %r13
movq %r13, %rdi
callq 0x46f0
testq %rax, %rax
je 0x2c7d6
movq %rax, %rbp
movq (%rbx), %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x277fd
cmpq %r13, %rax
jne 0x2c7d6
movq %rbp, %rdi
callq 0x4600
movq %r14, %rdi
callq 0x2a9b7
jmp 0x2c789