// File: 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
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
cmpl $0x1, %eax
jne 0x4ff46
movq -0x10(%rbp), %rdi
leaq -0x14(%rbp), %rsi
callq 0x454e0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x4feb7
movq $0x0, -0x8(%rbp)
jmp 0x505c8
cmpl $0x0, -0x14(%rbp)
jl 0x4fed8
movslq -0x14(%rbp), %rdi
addq $0x1, %rdi
callq 0x47a0
movq %rax, %rcx
movq %rcx, -0x20(%rbp)
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x4fee5
movq $0x0, -0x8(%rbp)
jmp 0x505c8
movslq -0x14(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x20(%rbp), %rsi
movslq -0x14(%rbp), %rdx
callq 0x45540
movq %rax, %rcx
movq -0x50(%rbp), %rax
cmpq %rcx, %rax
je 0x4ff1a
movq $0x0, -0x8(%rbp)
jmp 0x505c8
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 0x8840(%rax), %rcx
movq %rcx, 0x8840(%rax)
jmp 0x505a7
movq -0x10(%rbp), %rdi
callq 0x4bc50
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x4ff67
movq $0x0, -0x8(%rbp)
jmp 0x505c8
movq -0x30(%rbp), %rax
cmpl $0x1, 0x24(%rax)
jge 0x4ff87
movq -0x30(%rbp), %rdi
callq 0x4b5e0
movq $0x0, -0x8(%rbp)
jmp 0x505c8
movq -0x10(%rbp), %rdi
callq 0x45bc0
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x4ffb0
movq -0x30(%rbp), %rdi
callq 0x4b5e0
movq $0x0, -0x8(%rbp)
jmp 0x505c8
movq -0x38(%rbp), %rdi
callq 0x47100
cmpl $0x0, %eax
je 0x4ffd4
movq -0x30(%rbp), %rdi
callq 0x4b5e0
movq $0x0, -0x8(%rbp)
jmp 0x505c8
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, -0x54(%rbp)
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
andl $-0x80, %eax
cmpl $0x0, %eax
jne 0x50015
movl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0x5007f
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xffffc000, %eax       # imm = 0xFFFFC000
cmpl $0x0, %eax
jne 0x50030
movl $0x2, %eax
movl %eax, -0x5c(%rbp)
jmp 0x50079
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xffe00000, %eax       # imm = 0xFFE00000
cmpl $0x0, %eax
jne 0x5004b
movl $0x3, %eax
movl %eax, -0x60(%rbp)
jmp 0x50073
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xf0000000, %eax       # imm = 0xF0000000
cmpl $0x0, %eax
setne %dl
xorb $-0x1, %dl
movl $0x5, %eax
movl $0x4, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x60(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x54(%rbp), %eax
movl -0x58(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x64(%rbp)
movq -0x38(%rbp), %rax
movl 0x14(%rax), %eax
andl $-0x80, %eax
cmpl $0x0, %eax
jne 0x500a3
movl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x5010d
movq -0x38(%rbp), %rax
movl 0x14(%rax), %eax
andl $0xffffc000, %eax       # imm = 0xFFFFC000
cmpl $0x0, %eax
jne 0x500be
movl $0x2, %eax
movl %eax, -0x6c(%rbp)
jmp 0x50107
movq -0x38(%rbp), %rax
movl 0x14(%rax), %eax
andl $0xffe00000, %eax       # imm = 0xFFE00000
cmpl $0x0, %eax
jne 0x500d9
movl $0x3, %eax
movl %eax, -0x70(%rbp)
jmp 0x50101
movq -0x38(%rbp), %rax
movl 0x14(%rax), %eax
andl $0xf0000000, %eax       # imm = 0xF0000000
cmpl $0x0, %eax
setne %dl
xorb $-0x1, %dl
movl $0x5, %eax
movl $0x4, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x70(%rbp)
movl -0x70(%rbp), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x64(%rbp), %eax
movl -0x68(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x74(%rbp)
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
andl $-0x80, %eax
cmpl $0x0, %eax
jne 0x50131
movl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x5019b
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
andl $0xffffc000, %eax       # imm = 0xFFFFC000
cmpl $0x0, %eax
jne 0x5014c
movl $0x2, %eax
movl %eax, -0x7c(%rbp)
jmp 0x50195
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
andl $0xffe00000, %eax       # imm = 0xFFE00000
cmpl $0x0, %eax
jne 0x50167
movl $0x3, %eax
movl %eax, -0x80(%rbp)
jmp 0x5018f
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
andl $0xf0000000, %eax       # imm = 0xF0000000
cmpl $0x0, %eax
setne %dl
xorb $-0x1, %dl
movl $0x5, %eax
movl $0x4, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x78(%rbp)
movl -0x74(%rbp), %eax
movl -0x78(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
leaq -0x14(%rbp), %rsi
callq 0x45630
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
je 0x501d3
cmpl $0x0, -0x14(%rbp)
jl 0x501d3
movq -0x38(%rbp), %rax
movl 0x14(%rax), %eax
subl $0x4, %eax
cmpl -0x14(%rbp), %eax
jge 0x501f2
movq -0x30(%rbp), %rdi
callq 0x4b5e0
movq -0x38(%rbp), %rdi
callq 0x470c0
movq $0x0, -0x8(%rbp)
jmp 0x505c8
movslq -0x14(%rbp), %rdi
addq $0x1, %rdi
callq 0x47a0
movq %rax, %rcx
movq %rcx, -0x20(%rbp)
xorl %eax, %eax
cmpq %rcx, %rax
jne 0x5022c
movq -0x30(%rbp), %rdi
callq 0x4b5e0
movq -0x38(%rbp), %rdi
callq 0x470c0
movq $0x0, -0x8(%rbp)
jmp 0x505c8
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 0x4460
movq -0x20(%rbp), %rax
movslq -0x14(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movq -0x38(%rbp), %rdi
callq 0x470c0
movl $0x1, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x30(%rbp), %rcx
cmpl 0x24(%rcx), %eax
jge 0x504ed
movq -0x10(%rbp), %rdi
callq 0x45bc0
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x5029e
movq -0x30(%rbp), %rdi
callq 0x4b5e0
movq $0x0, -0x8(%rbp)
jmp 0x505c8
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, -0x84(%rbp)
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
andl $-0x80, %eax
cmpl $0x0, %eax
jne 0x502e5
movl $0x1, %eax
movl %eax, -0x88(%rbp)
jmp 0x50364
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xffffc000, %eax       # imm = 0xFFFFC000
cmpl $0x0, %eax
jne 0x50303
movl $0x2, %eax
movl %eax, -0x8c(%rbp)
jmp 0x50358
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xffe00000, %eax       # imm = 0xFFE00000
cmpl $0x0, %eax
jne 0x50321
movl $0x3, %eax
movl %eax, -0x90(%rbp)
jmp 0x5034c
movq -0x38(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xf0000000, %eax       # imm = 0xF0000000
cmpl $0x0, %eax
setne %dl
xorb $-0x1, %dl
movl $0x5, %eax
movl $0x4, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0x90(%rbp)
movl -0x90(%rbp), %eax
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movl %eax, -0x88(%rbp)
movl -0x84(%rbp), %eax
movl -0x88(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x94(%rbp)
movq -0x38(%rbp), %rax
movl 0x14(%rax), %eax
andl $-0x80, %eax
cmpl $0x0, %eax
jne 0x50394
movl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x50413
movq -0x38(%rbp), %rax
movl 0x14(%rax), %eax
andl $0xffffc000, %eax       # imm = 0xFFFFC000
cmpl $0x0, %eax
jne 0x503b2
movl $0x2, %eax
movl %eax, -0x9c(%rbp)
jmp 0x50407
movq -0x38(%rbp), %rax
movl 0x14(%rax), %eax
andl $0xffe00000, %eax       # imm = 0xFFE00000
cmpl $0x0, %eax
jne 0x503d0
movl $0x3, %eax
movl %eax, -0xa0(%rbp)
jmp 0x503fb
movq -0x38(%rbp), %rax
movl 0x14(%rax), %eax
andl $0xf0000000, %eax       # imm = 0xF0000000
cmpl $0x0, %eax
setne %dl
xorb $-0x1, %dl
movl $0x5, %eax
movl $0x4, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0xa0(%rbp)
movl -0xa0(%rbp), %eax
movl %eax, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movl %eax, -0x98(%rbp)
movl -0x94(%rbp), %eax
movl -0x98(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0xa4(%rbp)
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
andl $-0x80, %eax
cmpl $0x0, %eax
jne 0x50443
movl $0x1, %eax
movl %eax, -0xa8(%rbp)
jmp 0x504c2
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
andl $0xffffc000, %eax       # imm = 0xFFFFC000
cmpl $0x0, %eax
jne 0x50461
movl $0x2, %eax
movl %eax, -0xac(%rbp)
jmp 0x504b6
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
andl $0xffe00000, %eax       # imm = 0xFFE00000
cmpl $0x0, %eax
jne 0x5047f
movl $0x3, %eax
movl %eax, -0xb0(%rbp)
jmp 0x504aa
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
andl $0xf0000000, %eax       # imm = 0xF0000000
cmpl $0x0, %eax
setne %dl
xorb $-0x1, %dl
movl $0x5, %eax
movl $0x4, %ecx
testb $0x1, %dl
cmovnel %ecx, %eax
movl %eax, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
movl %eax, -0xac(%rbp)
movl -0xac(%rbp), %eax
movl %eax, -0xa8(%rbp)
movl -0xa4(%rbp), %eax
movl -0xa8(%rbp), %ecx
addl %ecx, %eax
addl -0x40(%rbp), %eax
movl %eax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
callq 0x470c0
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x50265
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
jle 0x5059e
cmpl $0x0, -0x40(%rbp)
jle 0x5059e
movq -0x30(%rbp), %rax
movl (%rax), %eax
cmpl -0x40(%rbp), %eax
jle 0x5059e
movq -0x30(%rbp), %rax
movl (%rax), %eax
subl -0x40(%rbp), %eax
movslq %eax, %rdi
callq 0x47a0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x50545
movq -0x30(%rbp), %rdi
callq 0x4b5e0
movq $0x0, -0x8(%rbp)
jmp 0x505c8
movq -0x30(%rbp), %rax
movl (%rax), %eax
subl -0x40(%rbp), %eax
cltq
movq %rax, -0xb8(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x48(%rbp), %rsi
movq -0x30(%rbp), %rax
movl (%rax), %eax
subl -0x40(%rbp), %eax
movslq %eax, %rdx
callq 0x45540
movq %rax, %rcx
movq -0xb8(%rbp), %rax
cmpq %rcx, %rax
je 0x50595
movq -0x30(%rbp), %rdi
callq 0x4b5e0
movq $0x0, -0x8(%rbp)
jmp 0x505c8
movq -0x48(%rbp), %rdi
callq 0x46a0
movq -0x30(%rbp), %rdi
callq 0x4b5e0
movq -0x20(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x5e300
movq %rax, -0x28(%rbp)
movq -0x20(%rbp), %rdi
callq 0x46a0
movq -0x28(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xc0, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)