// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/src/fmtutil-cmpr.c
// Function: fmtutil_decompress_stos_pictbank
void fmtutil_decompress_stos_pictbank(deark *c, dbuf *inf,
	i64 picdatapos1, i64 rledatapos1, i64 pointspos1,
	dbuf *unc_pixels, i64 unc_image_size)
{
	i64 picdatapos;
	i64 rledatapos;
	u8 picbyte;
	u8 rlebyte;
	u8 rlebit;
	i64 cmpr_pic_bytes, cmpr_rle_bytes, points_bytes;
	struct de_bitbuf_lowlevel *bbll_r = NULL;
	struct de_bitreader *bitrd_p = NULL;
	i64 t;

	de_dbg(c, "decompressing picture");
	de_dbg_indent(c, 1);

	bbll_r = de_malloc(c, sizeof(struct de_bitbuf_lowlevel));
	bitrd_p = de_malloc(c, sizeof(struct de_bitreader));
	bitrd_p->f = inf;
	bitrd_p->curpos = pointspos1;
	bitrd_p->endpos = inf->len;

	picdatapos = picdatapos1;
	rledatapos = rledatapos1;

	// Note that the first picbyte and/or rlebyte can potentially be overwritten
	// before being used. That's just the way it is, apparently.
	picbyte = dbuf_getbyte_p(inf, &picdatapos);
	rlebyte = dbuf_getbyte_p(inf, &rledatapos);

	for(t=0; t<unc_image_size; t++) {
		// We'll need an rle bit.
		// If we've run out of them, read a "points" bit to decide whether
		// to repeat the previous 8 rle bits (stored in rlebyte), or to read
		// a new set of 8 rle bits.
		if(bbll_r->nbits_in_bitbuf==0) {
			u8 pointsbit;

			pointsbit = (u8)de_bitreader_getbits(bitrd_p, 1);
			if(pointsbit) {
				rlebyte = dbuf_getbyte_p(inf, &rledatapos);
			}

			de_bitbuf_lowlevel_add_byte(bbll_r, rlebyte);
		}

		rlebit = (u8)de_bitbuf_lowlevel_get_bits(bbll_r, 1);
		if(rlebit) {
			picbyte = dbuf_getbyte_p(inf, &picdatapos);
		}

		dbuf_writebyte(unc_pixels, picbyte);
	}

	dbuf_flush(unc_pixels);
	cmpr_pic_bytes = picdatapos - picdatapos1;
	cmpr_rle_bytes = rledatapos - rledatapos1;
	de_bitreader_skip_to_byte_boundary(bitrd_p);
	points_bytes = bitrd_p->curpos - pointspos1;
	de_dbg(c, "compressed pic bytes: %"I64_FMT, cmpr_pic_bytes);
	de_dbg(c, "compressed rle bytes: %"I64_FMT, cmpr_rle_bytes);
	de_dbg(c, "points bytes: %"I64_FMT, points_bytes);
	de_dbg(c, "decompressed %"I64_FMT" to %"I64_FMT" bytes",
		cmpr_pic_bytes + cmpr_rle_bytes + points_bytes, unc_pixels->len);

	de_free(c, bbll_r);
	de_free(c, bitrd_p);
	de_dbg_indent(c, -1);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, %r14
movq %r8, %r13
movq %rcx, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, %rbp
movq %rdi, %rbx
movq 0x70(%rsp), %r12
leaq 0x4eaed(%rip), %rsi     # 0x14ac61
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movl $0x10, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r15
movl $0x30, %esi
movq %rbx, 0x30(%rsp)
movq %rbx, %rdi
callq 0xe8b2b
movq %rbp, (%rax)
movq %r13, 0x28(%rsp)
movq %r13, 0x8(%rax)
movq 0x18(%rbp), %rcx
movq %rax, (%rsp)
movq %rcx, 0x10(%rax)
leaq 0x20(%rsp), %rsi
movq 0x8(%rsp), %rax
movq %rax, (%rsi)
leaq 0x18(%rsp), %rbx
movq 0x10(%rsp), %rax
movq %rax, (%rbx)
movq %rbp, %rdi
callq 0xeeb4b
movl %eax, %r13d
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xeeb4b
testq %r12, %r12
jle 0xfc25e
movl %eax, %ebx
cmpl $0x0, 0x4(%r15)
jne 0xfc22c
movq (%rsp), %rdi
movl $0x1, %esi
callq 0xf1bed
testb %al, %al
je 0xfc221
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
callq 0xeeb4b
movl %eax, %ebx
movzbl %bl, %esi
movq %r15, %rdi
callq 0xf1b5e
movq %r15, %rdi
movl $0x1, %esi
callq 0xf1b93
testb %al, %al
je 0xfc24d
movq %rbp, %rdi
leaq 0x20(%rsp), %rsi
callq 0xeeb4b
movl %eax, %r13d
movzbl %r13b, %esi
movq %r14, %rdi
callq 0xf0bd6
decq %r12
jne 0xfc1f9
movq %r14, %rdi
callq 0xf08ba
movq 0x20(%rsp), %r13
subq 0x8(%rsp), %r13
movq 0x18(%rsp), %rbx
subq 0x10(%rsp), %rbx
movq (%rsp), %r12
movq %r12, %rdi
callq 0xf1c84
movq 0x8(%r12), %rbp
subq 0x28(%rsp), %rbp
leaq 0x4e9e0(%rip), %rsi     # 0x14ac77
movq 0x30(%rsp), %r12
movq %r12, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe8617
leaq 0x4e9e1(%rip), %rsi     # 0x14ac91
movq %r12, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0xe8617
leaq 0x4e9e7(%rip), %rsi     # 0x14acab
movq %r12, %rdi
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe8617
addq %r13, %rbx
addq %rbp, %rbx
movq 0x18(%r14), %rcx
leaq 0x2526d(%rip), %rsi     # 0x12154f
movq %r12, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0xe8617
movq %r12, %rdi
movq %r15, %rsi
callq 0xe8be2
movq %r12, %rdi
movq (%rsp), %rsi
callq 0xe8be2
movq %r12, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq