// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x5c26b(%rip), %rsi     # 0x1d66dd
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0x10, %esi
callq 0x15b780
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
movl $0x30, %esi
callq 0x15b780
movq %rax, -0x70(%rbp)
movq -0x10(%rbp), %rcx
movq -0x70(%rbp), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x70(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165170
movb %al, -0x41(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movb %al, -0x42(%rbp)
movq $0x0, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq 0x10(%rbp), %rax
jge 0x17a59b
movq -0x68(%rbp), %rax
cmpl $0x0, 0x4(%rax)
jne 0x17a556
movq -0x70(%rbp), %rdi
movl $0x1, %esi
callq 0x16a390
movb %al, -0x79(%rbp)
cmpb $0x0, -0x79(%rbp)
je 0x17a549
movq -0x10(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movb %al, -0x42(%rbp)
movq -0x68(%rbp), %rdi
movzbl -0x42(%rbp), %esi
callq 0x16a230
movq -0x68(%rbp), %rdi
movl $0x1, %esi
callq 0x16a2a0
movb %al, -0x43(%rbp)
cmpb $0x0, -0x43(%rbp)
je 0x17a57d
movq -0x10(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165170
movb %al, -0x41(%rbp)
movq -0x30(%rbp), %rdi
movzbl -0x41(%rbp), %esi
callq 0x168380
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x17a50a
movq -0x30(%rbp), %rdi
callq 0x167c30
movq -0x38(%rbp), %rax
subq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
subq -0x20(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x70(%rbp), %rdi
callq 0x16a460
movq -0x70(%rbp), %rax
movq 0x8(%rax), %rax
subq -0x28(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rdx
leaq 0x5c10f(%rip), %rsi     # 0x1d66f3
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rdx
leaq 0x5c113(%rip), %rsi     # 0x1d670d
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x60(%rbp), %rdx
leaq 0x5c117(%rip), %rsi     # 0x1d6727
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rdx
addq -0x58(%rbp), %rdx
addq -0x60(%rbp), %rdx
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rcx
leaq 0x32ac2(%rip), %rsi     # 0x1ad0f8
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
addq $0x80, %rsp
popq %rbp
retq
nop