// File: /workspace/github_c_makefile_star2_O2/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, %r12
movq %rcx, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq %rsi, %rbp
movq %rdi, %rbx
leaq 0x4d2e1(%rip), %rsi     # 0x13d791
xorl %r13d, %r13d
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r15
pushq $0x30
popq %rsi
movq %rbx, 0x30(%rsp)
movq %rbx, %rdi
callq 0xdecaf
movq %rbp, (%rax)
movq %r12, 0x28(%rsp)
movq %r12, 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), %r12
movq 0x10(%rsp), %rax
movq %rax, (%r12)
movq %rbp, %rdi
callq 0xe3f2a
movl %eax, %ebx
movq %rbp, %rdi
movq %r12, %rsi
callq 0xe3f2a
movl %eax, %r12d
movq 0x70(%rsp), %rax
testq %rax, %rax
cmovgq %rax, %r13
subq $0x1, %r13
jb 0xf059e
cmpl $0x0, 0x4(%r15)
jne 0xf0573
movq (%rsp), %rdi
pushq $0x1
popq %rsi
callq 0xe6a4a
testb %al, %al
je 0xf0567
movq %rbp, %rdi
leaq 0x18(%rsp), %rsi
callq 0xe3f2a
movl %eax, %r12d
movzbl %r12b, %esi
movq %r15, %rdi
callq 0xe69c4
movq %r15, %rdi
pushq $0x1
popq %rsi
callq 0xe69f9
testb %al, %al
je 0xf0591
movq %rbp, %rdi
leaq 0x20(%rsp), %rsi
callq 0xe3f2a
movl %eax, %ebx
movzbl %bl, %esi
movq %r14, %rdi
callq 0xe5bc5
jmp 0xf053a
movq %r14, %rdi
callq 0xe58e8
movq 0x20(%rsp), %r12
subq 0x8(%rsp), %r12
movq 0x18(%rsp), %rbx
subq 0x10(%rsp), %rbx
movq (%rsp), %r13
movq %r13, %rdi
callq 0xe6abe
movq 0x8(%r13), %r13
subq 0x28(%rsp), %r13
leaq 0x4d1d1(%rip), %rsi     # 0x13d7a7
movq 0x30(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
leaq 0x4d1d2(%rip), %rsi     # 0x13d7c1
movq %rbp, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0xde896
leaq 0x4d1d8(%rip), %rsi     # 0x13d7db
movq %rbp, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xde896
addq %r12, %rbx
addq %r13, %rbx
movq 0x18(%r14), %rcx
leaq 0x23e3e(%rip), %rsi     # 0x11445f
movq %rbp, %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0xde896
movq %rbp, %rdi
movq %r15, %rsi
callq 0xded32
movq %rbp, %rdi
movq (%rsp), %rsi
callq 0xded32
pushq $-0x1
popq %rsi
movq %rbp, %rdi
callq 0xdeba7
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq