// File: karel-brinda[P]ococo/ext/htslib/cram/cram_io.c
// Function: cram_uncompress_block
int cram_uncompress_block(cram_block *b) {
    char *uncomp;
    size_t uncomp_size = 0;

    if (b->uncomp_size == 0) {
	// blank block
	b->method = RAW;
	return 0;
    }

    switch (b->method) {
    case RAW:
	return 0;

    case GZIP:
	uncomp = zlib_mem_inflate((char *)b->data, b->comp_size, &uncomp_size);
	if (!uncomp)
	    return -1;
	if ((int)uncomp_size != b->uncomp_size) {
	    free(uncomp);
	    return -1;
	}
	free(b->data);
	b->data = (unsigned char *)uncomp;
	b->alloc = uncomp_size;
	b->method = RAW;
	break;

#ifdef HAVE_LIBBZ2
    case BZIP2: {
	unsigned int usize = b->uncomp_size;
	if (!(uncomp = malloc(usize)))
	    return -1;
	if (BZ_OK != BZ2_bzBuffToBuffDecompress(uncomp, &usize,
						(char *)b->data, b->comp_size,
						0, 0)) {
	    free(uncomp);
	    return -1;
	}
	free(b->data);
	b->data = (unsigned char *)uncomp;
	b->alloc = usize;
	b->method = RAW;
	b->uncomp_size = usize; // Just incase it differs
	break;
    }
#else
    case BZIP2:
	fprintf(stderr, "Bzip2 compression is not compiled into this "
		"version.\nPlease rebuild and try again.\n");
	return -1;
#endif

#ifdef HAVE_LIBLZMA
    case LZMA:
	uncomp = lzma_mem_inflate((char *)b->data, b->comp_size, &uncomp_size);
	if (!uncomp)
	    return -1;
	if ((int)uncomp_size != b->uncomp_size)
	    return -1;
	free(b->data);
	b->data = (unsigned char *)uncomp;
	b->alloc = uncomp_size;
	b->method = RAW;
	break;
#else
    case LZMA:
	fprintf(stderr, "Lzma compression is not compiled into this "
		"version.\nPlease rebuild and try again.\n");
	return -1;
	break;
#endif

    case RANS: {
	unsigned int usize = b->uncomp_size, usize2;
	uncomp = (char *)rans_uncompress(b->data, b->comp_size, &usize2);
	if (!uncomp || usize != usize2)
 	    return -1;
	free(b->data);
	b->data = (unsigned char *)uncomp;
	b->alloc = usize2;
	b->method = RAW;
	b->uncomp_size = usize2; // Just incase it differs
	//fprintf(stderr, "Expanded %d to %d\n", b->comp_size, b->uncomp_size);
	break;
    }

    default:
	return -1;
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x14(%rax)
jne 0x47134
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x4(%rbp)
jmp 0x472c0
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x30(%rbp)
subq $0x4, %rax
ja 0x472b0
movq -0x30(%rbp), %rax
leaq 0x35845(%rip), %rcx     # 0x7c998
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0x0, -0x4(%rbp)
jmp 0x472c0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movslq 0x10(%rax), %rsi
leaq -0x20(%rbp), %rdx
callq 0x45840
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x47198
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x472c0
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpl 0x14(%rcx), %eax
je 0x471ba
movq -0x18(%rbp), %rdi
callq 0x46a0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x472c0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x46a0
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
jmp 0x472b9
movq 0x42ddb(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x35ba4(%rip), %rsi     # 0x7cda3
movb $0x0, %al
callq 0x4830
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x472c0
movq 0x42db7(%rip), %rax     # 0x89fd0
movq (%rax), %rdi
leaq 0x35bd4(%rip), %rsi     # 0x7cdf7
movb $0x0, %al
callq 0x4830
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x472c0
movq -0x10(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x24(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x10(%rax), %esi
leaq -0x28(%rbp), %rdx
callq 0x5aef0
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
je 0x4726b
movl -0x24(%rbp), %eax
cmpl -0x28(%rbp), %eax
je 0x47274
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x472c0
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x46a0
movq -0x18(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movl -0x28(%rbp), %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x10(%rbp), %rax
movl $0x0, (%rax)
movl -0x28(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
jmp 0x472b9
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x472c0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)