// File: karel-brinda[P]ococo/ext/htslib/cram/cram_io.c
// Function: cram_read_container
cram_container *cram_read_container(cram_fd *fd) {
    cram_container c2, *c;
    int i, s;
    size_t rd = 0;
    uint32_t crc = 0;
    
    fd->err = 0;
    fd->eof = 0;

    memset(&c2, 0, sizeof(c2));
    if (CRAM_MAJOR_VERS(fd->version) == 1) {
	if ((s = itf8_decode_crc(fd, &c2.length, &crc)) == -1) {
	    fd->eof = fd->empty_container ? 1 : 2;
	    return NULL;
	} else {
	    rd+=s;
	}
    } else {
	uint32_t len;
	if ((s = int32_decode(fd, &c2.length)) == -1) {
	    if (CRAM_MAJOR_VERS(fd->version) == 2 &&
		CRAM_MINOR_VERS(fd->version) == 0)
		fd->eof = 1; // EOF blocks arrived in v2.1
	    else
		fd->eof = fd->empty_container ? 1 : 2;
	    return NULL;
	} else {
	    rd+=s;
	}
	len = le_int4(c2.length);
	crc = crc32(0L, (unsigned char *)&len, 4);
    }
    if ((s = itf8_decode_crc(fd, &c2.ref_seq_id, &crc))   == -1) return NULL; else rd+=s;
    if ((s = itf8_decode_crc(fd, &c2.ref_seq_start, &crc))== -1) return NULL; else rd+=s;
    if ((s = itf8_decode_crc(fd, &c2.ref_seq_span, &crc)) == -1) return NULL; else rd+=s;
    if ((s = itf8_decode_crc(fd, &c2.num_records, &crc))  == -1) return NULL; else rd+=s;

    if (CRAM_MAJOR_VERS(fd->version) == 1) {
	c2.record_counter = 0;
	c2.num_bases = 0;
    } else {
	if (CRAM_MAJOR_VERS(fd->version) >= 3) {
	    if ((s = ltf8_decode_crc(fd, &c2.record_counter, &crc)) == -1)
		return NULL;
	    else
		rd += s;
	} else {
	    int32_t i32;
	    if ((s = itf8_decode_crc(fd, &i32, &crc)) == -1)
		return NULL;
	    else
		rd += s;
	    c2.record_counter = i32;
	}

	if ((s = ltf8_decode_crc(fd, &c2.num_bases, &crc))== -1)
	    return NULL;
	else
	    rd += s;
    }
    if ((s = itf8_decode_crc(fd, &c2.num_blocks, &crc))   == -1) return NULL; else rd+=s;
    if ((s = itf8_decode_crc(fd, &c2.num_landmarks, &crc))== -1) return NULL; else rd+=s;

    if (!(c = calloc(1, sizeof(*c))))
	return NULL;

    *c = c2;

    if (!(c->landmark = malloc(c->num_landmarks * sizeof(int32_t))) &&
	c->num_landmarks) {
	fd->err = errno;
	cram_free_container(c);
	return NULL;
    }  
    for (i = 0; i < c->num_landmarks; i++) {
	if ((s = itf8_decode_crc(fd, &c->landmark[i], &crc)) == -1) {
	    cram_free_container(c);
	    return NULL;
	} else {
	    rd += s;
	}
    }

    if (CRAM_MAJOR_VERS(fd->version) >= 3) {
	if (-1 == int32_decode(fd, (int32_t *)&c->crc32))
	    return NULL;
	else
	    rd+=4;

	if (crc != c->crc32) {
	    fprintf(stderr, "Container header CRC32 failure\n");
	    cram_free_container(c);
	    return NULL;
	}
    }

    c->offset = rd;
    c->slices = NULL;
    c->curr_slice = 0;
    c->max_slice = c->num_landmarks;
    c->slice_rec = 0;
    c->curr_rec = 0;
    c->max_rec = 0;

    if (c->ref_seq_id == -2) {
	c->multi_seq = 1;
	fd->multi_seq = 1;
    }

    fd->empty_container =
	(c->num_records == 0 &&
	 c->ref_seq_id == -1 &&
	 c->ref_seq_start == 0x454f46 /* EOF */) ? 1 : 0;

    return c;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x290, %rsp            # imm = 0x290
movq %rdi, -0x10(%rbp)
movq $0x0, -0x278(%rbp)
movl $0x0, -0x27c(%rbp)
movq -0x10(%rbp), %rax
movl $0x0, 0x30(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x8848(%rax)
leaq -0x260(%rbp), %rdi
xorl %esi, %esi
movl $0x250, %edx            # imm = 0x250
callq 0x42d0
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
cmpl $0x1, %eax
jne 0x4bd1c
movq -0x10(%rbp), %rdi
leaq -0x260(%rbp), %rsi
leaq -0x27c(%rbp), %rdx
callq 0x43180
movl %eax, -0x270(%rbp)
cmpl $-0x1, %eax
jne 0x4bd02
movq -0x10(%rbp), %rax
movl 0x8858(%rax), %edx
movl $0x2, %ecx
movl $0x1, %eax
cmpl $0x0, %edx
cmovnel %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8848(%rax)
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
movslq -0x270(%rbp), %rax
addq -0x278(%rbp), %rax
movq %rax, -0x278(%rbp)
jmp 0x4bdd4
movq -0x10(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x454e0
movl %eax, -0x270(%rbp)
cmpl $-0x1, %eax
jne 0x4bd98
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
cmpl $0x2, %eax
jne 0x4bd67
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
andl $0xff, %eax
cmpl $0x0, %eax
jne 0x4bd67
movq -0x10(%rbp), %rax
movl $0x1, 0x8848(%rax)
jmp 0x4bd8b
movq -0x10(%rbp), %rax
movl 0x8858(%rax), %edx
movl $0x2, %ecx
movl $0x1, %eax
cmpl $0x0, %edx
cmovnel %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8848(%rax)
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
movslq -0x270(%rbp), %rax
addq -0x278(%rbp), %rax
movq %rax, -0x278(%rbp)
movl -0x260(%rbp), %eax
movl %eax, -0x280(%rbp)
xorl %eax, %eax
movl %eax, %edi
leaq -0x280(%rbp), %rsi
movl $0x4, %edx
callq 0x43d0
movl %eax, -0x27c(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x260(%rbp), %rsi
addq $0x4, %rsi
leaq -0x27c(%rbp), %rdx
callq 0x43180
movl %eax, -0x270(%rbp)
cmpl $-0x1, %eax
jne 0x4be07
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
movslq -0x270(%rbp), %rax
addq -0x278(%rbp), %rax
movq %rax, -0x278(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x260(%rbp), %rsi
addq $0x8, %rsi
leaq -0x27c(%rbp), %rdx
callq 0x43180
movl %eax, -0x270(%rbp)
cmpl $-0x1, %eax
jne 0x4be4f
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
movslq -0x270(%rbp), %rax
addq -0x278(%rbp), %rax
movq %rax, -0x278(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x260(%rbp), %rsi
addq $0xc, %rsi
leaq -0x27c(%rbp), %rdx
callq 0x43180
movl %eax, -0x270(%rbp)
cmpl $-0x1, %eax
jne 0x4be97
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
movslq -0x270(%rbp), %rax
addq -0x278(%rbp), %rax
movq %rax, -0x278(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x260(%rbp), %rsi
addq $0x20, %rsi
leaq -0x27c(%rbp), %rdx
callq 0x43180
movl %eax, -0x270(%rbp)
cmpl $-0x1, %eax
jne 0x4bedf
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
movslq -0x270(%rbp), %rax
addq -0x278(%rbp), %rax
movq %rax, -0x278(%rbp)
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
cmpl $0x1, %eax
jne 0x4bf1e
movq $0x0, -0x250(%rbp)
movq $0x0, -0x248(%rbp)
jmp 0x4c013
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
cmpl $0x3, %eax
jl 0x4bf77
movq -0x10(%rbp), %rdi
leaq -0x260(%rbp), %rsi
addq $0x10, %rsi
leaq -0x27c(%rbp), %rdx
callq 0x448f0
movl %eax, -0x270(%rbp)
cmpl $-0x1, %eax
jne 0x4bf60
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
movslq -0x270(%rbp), %rax
addq -0x278(%rbp), %rax
movq %rax, -0x278(%rbp)
jmp 0x4bfc9
movq -0x10(%rbp), %rdi
leaq -0x284(%rbp), %rsi
leaq -0x27c(%rbp), %rdx
callq 0x43180
movl %eax, -0x270(%rbp)
cmpl $-0x1, %eax
jne 0x4bfa6
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
movslq -0x270(%rbp), %rax
addq -0x278(%rbp), %rax
movq %rax, -0x278(%rbp)
movslq -0x284(%rbp), %rax
movq %rax, -0x250(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x260(%rbp), %rsi
addq $0x18, %rsi
leaq -0x27c(%rbp), %rdx
callq 0x448f0
movl %eax, -0x270(%rbp)
cmpl $-0x1, %eax
jne 0x4bffc
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
movslq -0x270(%rbp), %rax
addq -0x278(%rbp), %rax
movq %rax, -0x278(%rbp)
jmp 0x4c013
movq -0x10(%rbp), %rdi
leaq -0x260(%rbp), %rsi
addq $0x24, %rsi
leaq -0x27c(%rbp), %rdx
callq 0x43180
movl %eax, -0x270(%rbp)
cmpl $-0x1, %eax
jne 0x4c046
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
movslq -0x270(%rbp), %rax
addq -0x278(%rbp), %rax
movq %rax, -0x278(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x260(%rbp), %rsi
addq $0x28, %rsi
leaq -0x27c(%rbp), %rdx
callq 0x43180
movl %eax, -0x270(%rbp)
cmpl $-0x1, %eax
jne 0x4c08e
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
movslq -0x270(%rbp), %rax
addq -0x278(%rbp), %rax
movq %rax, -0x278(%rbp)
movl $0x1, %edi
movl $0x250, %esi            # imm = 0x250
callq 0x4370
movq %rax, -0x268(%rbp)
cmpq $0x0, %rax
jne 0x4c0cc
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
movq -0x268(%rbp), %rdi
leaq -0x260(%rbp), %rsi
movl $0x250, %edx            # imm = 0x250
callq 0x4460
movq -0x268(%rbp), %rax
movslq 0x28(%rax), %rdi
shlq $0x2, %rdi
callq 0x47a0
movq -0x268(%rbp), %rcx
movq %rax, 0x30(%rcx)
cmpq $0x0, %rax
jne 0x4c13d
movq -0x268(%rbp), %rax
cmpl $0x0, 0x28(%rax)
je 0x4c13d
callq 0x4070
movl (%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x268(%rbp), %rdi
callq 0x4b5e0
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
movl $0x0, -0x26c(%rbp)
movl -0x26c(%rbp), %eax
movq -0x268(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jge 0x4c1d1
movq -0x10(%rbp), %rdi
movq -0x268(%rbp), %rax
movq 0x30(%rax), %rsi
movslq -0x26c(%rbp), %rax
shlq $0x2, %rax
addq %rax, %rsi
leaq -0x27c(%rbp), %rdx
callq 0x43180
movl %eax, -0x270(%rbp)
cmpl $-0x1, %eax
jne 0x4c1a6
movq -0x268(%rbp), %rdi
callq 0x4b5e0
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
movslq -0x270(%rbp), %rax
addq -0x278(%rbp), %rax
movq %rax, -0x278(%rbp)
jmp 0x4c1bd
movl -0x26c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x26c(%rbp)
jmp 0x4c147
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
cmpl $0x3, %eax
jl 0x4c26d
movq -0x10(%rbp), %rdi
movq -0x268(%rbp), %rsi
addq $0x248, %rsi            # imm = 0x248
callq 0x454e0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x4c213
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
movq -0x278(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x278(%rbp)
movl -0x27c(%rbp), %eax
movq -0x268(%rbp), %rcx
cmpl 0x248(%rcx), %eax
je 0x4c26b
movq 0x3dd8f(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x30d78(%rip), %rsi     # 0x7cfc3
movb $0x0, %al
callq 0x4830
movq -0x268(%rbp), %rdi
callq 0x4b5e0
movq $0x0, -0x8(%rbp)
jmp 0x4c36f
jmp 0x4c26d
movq -0x278(%rbp), %rcx
movq -0x268(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x268(%rbp), %rax
movq $0x0, 0x78(%rax)
movq -0x268(%rbp), %rax
movl $0x0, 0x54(%rax)
movq -0x268(%rbp), %rax
movl 0x28(%rax), %ecx
movq -0x268(%rbp), %rax
movl %ecx, 0x50(%rax)
movq -0x268(%rbp), %rax
movl $0x0, 0x68(%rax)
movq -0x268(%rbp), %rax
movl $0x0, 0x5c(%rax)
movq -0x268(%rbp), %rax
movl $0x0, 0x58(%rax)
movq -0x268(%rbp), %rax
cmpl $-0x2, 0x4(%rax)
jne 0x4c306
movq -0x268(%rbp), %rax
movl $0x1, 0x94(%rax)
movq -0x10(%rbp), %rax
movl $0x1, 0x8850(%rax)
movq -0x268(%rbp), %rcx
xorl %eax, %eax
cmpl $0x0, 0x20(%rcx)
movb %al, -0x285(%rbp)
jne 0x4c347
movq -0x268(%rbp), %rcx
xorl %eax, %eax
cmpl $-0x1, 0x4(%rcx)
movb %al, -0x285(%rbp)
jne 0x4c347
movq -0x268(%rbp), %rax
cmpl $0x454f46, 0x8(%rax)    # imm = 0x454F46
sete %al
movb %al, -0x285(%rbp)
movb -0x285(%rbp), %dl
xorl %ecx, %ecx
movl $0x1, %eax
testb $0x1, %dl
cmovnel %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8858(%rax)
movq -0x268(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq
nopl (%rax)