// File: karel-brinda[P]ococo/ext/htslib/cram/cram_io.c
// Function: cram_read_block
cram_block *cram_read_block(cram_fd *fd) {
    cram_block *b = malloc(sizeof(*b));
    unsigned char c;
    uint32_t crc = 0;
    if (!b)
	return NULL;

    //fprintf(stderr, "Block at %d\n", (int)ftell(fd->fp));

    if (-1 == (b->method      = hgetc(fd->fp))) { free(b); return NULL; }
    c = b->method; crc = crc32(crc, &c, 1);
    if (-1 == (b->content_type= hgetc(fd->fp))) { free(b); return NULL; }
    c = b->content_type; crc = crc32(crc, &c, 1);
    if (-1 == itf8_decode_crc(fd, &b->content_id, &crc))  { free(b); return NULL; }
    if (-1 == itf8_decode_crc(fd, &b->comp_size, &crc))   { free(b); return NULL; }
    if (-1 == itf8_decode_crc(fd, &b->uncomp_size, &crc)) { free(b); return NULL; }

    //    fprintf(stderr, "  method %d, ctype %d, cid %d, csize %d, ucsize %d\n",
    //	    b->method, b->content_type, b->content_id, b->comp_size, b->uncomp_size);

    if (b->method == RAW) {
	b->alloc = b->uncomp_size;
	if (!(b->data = malloc(b->uncomp_size))){ free(b); return NULL; }
	if (b->uncomp_size != hread(fd->fp, b->data, b->uncomp_size)) {
	    free(b->data);
	    free(b);
	    return NULL;
	}
    } else {
	b->alloc = b->comp_size;
	if (!(b->data = malloc(b->comp_size)))  { free(b); return NULL; }
	if (b->comp_size != hread(fd->fp, b->data, b->comp_size)) {
	    free(b->data);
	    free(b);
	    return NULL;
	}
    }

    if (CRAM_MAJOR_VERS(fd->version) >= 3) {
	if (-1 == int32_decode(fd, (int32_t *)&b->crc32)) {
	    free(b);
	    return NULL;
	}

	crc = crc32(crc, b->data ? b->data : (uc *)"", b->alloc);
	if (crc != b->crc32) {
	    fprintf(stderr, "Block CRC32 failure\n");
	    free(b->data);
	    free(b);
	    return NULL;
	}
    }

    b->orig_method = b->method;
    b->idx = 0;
    b->byte = 0;
    b->bit = 7; // MSB

    return b;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl $0x40, %edi
callq 0x47a0
movq %rax, -0x18(%rbp)
movl $0x0, -0x20(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x45bf5
movq $0x0, -0x8(%rbp)
jmp 0x45f8a
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x43130
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x45c28
movq -0x18(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x45f8a
movq -0x18(%rbp), %rax
movl (%rax), %eax
movb %al, -0x19(%rbp)
movl -0x20(%rbp), %eax
movl %eax, %edi
leaq -0x19(%rbp), %rsi
movl $0x1, %edx
callq 0x43d0
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
callq 0x43130
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x8(%rax)
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x45c7b
movq -0x18(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x45f8a
movq -0x18(%rbp), %rax
movl 0x8(%rax), %eax
movb %al, -0x19(%rbp)
movl -0x20(%rbp), %eax
movl %eax, %edi
leaq -0x19(%rbp), %rsi
movl $0x1, %edx
callq 0x43d0
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0xc, %rsi
leaq -0x20(%rbp), %rdx
callq 0x43180
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x45cd1
movq -0x18(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x45f8a
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x10, %rsi
leaq -0x20(%rbp), %rdx
callq 0x43180
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x45d07
movq -0x18(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x45f8a
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x14, %rsi
leaq -0x20(%rbp), %rdx
callq 0x43180
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x45d3d
movq -0x18(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x45f8a
movq -0x18(%rbp), %rax
cmpl $0x0, (%rax)
jne 0x45de7
movq -0x18(%rbp), %rax
movslq 0x14(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
movslq 0x14(%rax), %rdi
callq 0x47a0
movq -0x18(%rbp), %rcx
movq %rax, 0x20(%rcx)
cmpq $0x0, %rax
jne 0x45d8b
movq -0x18(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x45f8a
movq -0x18(%rbp), %rax
movslq 0x14(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x18(%rbp), %rax
movslq 0x14(%rax), %rdx
callq 0x45540
movq %rax, %rcx
movq -0x28(%rbp), %rax
cmpq %rcx, %rax
je 0x45de2
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x46a0
movq -0x18(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x45f8a
jmp 0x45e81
movq -0x18(%rbp), %rax
movslq 0x10(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x18(%rbp), %rax
movslq 0x10(%rax), %rdi
callq 0x47a0
movq -0x18(%rbp), %rcx
movq %rax, 0x20(%rcx)
cmpq $0x0, %rax
jne 0x45e28
movq -0x18(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x45f8a
movq -0x18(%rbp), %rax
movslq 0x10(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x18(%rbp), %rax
movslq 0x10(%rax), %rdx
callq 0x45540
movq %rax, %rcx
movq -0x30(%rbp), %rax
cmpq %rcx, %rax
je 0x45e7f
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x46a0
movq -0x18(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x45f8a
jmp 0x45e81
movq -0x10(%rbp), %rax
movl 0xc(%rax), %eax
sarl $0x8, %eax
cmpl $0x3, %eax
jl 0x45f53
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x18, %rsi
callq 0x454e0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x45ec6
movq -0x18(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x45f8a
movl -0x20(%rbp), %eax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x20(%rax)
je 0x45ee6
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x40(%rbp)
jmp 0x45ef3
leaq 0x365df(%rip), %rax     # 0x7c4cc
movq %rax, -0x40(%rbp)
jmp 0x45ef3
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %edx
callq 0x43d0
movl %eax, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x18(%rcx), %eax
je 0x45f51
movq 0x440b0(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x36df2(%rip), %rsi     # 0x7cd1c
movb $0x0, %al
callq 0x4830
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x46a0
movq -0x18(%rbp), %rdi
callq 0x46a0
movq $0x0, -0x8(%rbp)
jmp 0x45f8a
jmp 0x45f53
movq -0x18(%rbp), %rax
movl (%rax), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x18(%rbp), %rax
movl $0x0, 0x1c(%rax)
movq -0x18(%rbp), %rax
movq $0x0, 0x30(%rax)
movq -0x18(%rbp), %rax
movl $0x7, 0x38(%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)