// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/os2bootlogo.c
// Function: do_decompress_plane
static void do_decompress_plane(deark *c, lctx *d, int pn)
{
	struct plane_struct *pli;
	i64 orig_len;
	i64 num_unused_bytes;
	struct de_dfilter_in_params dcmpri;
	struct de_dfilter_out_params dcmpro;
	struct de_dfilter_results dres;

	pli = &d->pl[pn];
	de_dbg(c, "decompressing plane %d at %"I64_FMT, pn, pli->offset);
	de_dbg_indent(c, 1);

	dbuf_truncate(d->unc_pixels, (i64)pn*d->max_bytes_per_plane);
	dbuf_flush(d->unc_pixels);

	de_dfilter_init_objects(c, &dcmpri, &dcmpro, &dres);
	dcmpri.f = c->infile;
	dcmpri.pos = pli->offset;
	dcmpri.len = pli->len;
	dcmpro.f = d->unc_pixels;
	dcmpro.expected_len = d->max_bytes_per_plane;
	dcmpro.len_known = 1;

	orig_len = d->unc_pixels->len;
	decompress_exepack2(c, d, &dcmpri, &dcmpro, &dres);

	if(dres.errcode) {
		de_dbg(c, "Decompression failed (plane %d): %s",
			pn, de_dfilter_get_errmsg(c, &dres));
		d->fatal_errflag = 1;
		d->need_dcmpr_errmsg = 1;
		goto done;
	}

	pli->dcmpr_nbytes = d->unc_pixels->len - orig_len;
	de_dbg(c, "decompressed %"I64_FMT" to %"I64_FMT" bytes",
		dres.bytes_consumed, pli->dcmpr_nbytes);

	// There are third-party utilities that create bootlogo files, so
	// we don't want to be *too* strict.
	// But there are apparently some non-bootlogo formats that are hard
	// to distinguish from bootlogo. So we don't want it to be just
	// "anything goes".
	num_unused_bytes = pli->len - dres.bytes_consumed;
	if(num_unused_bytes) {
		de_dbg(c, "[unused bytes: %"I64_FMT"]", num_unused_bytes);
		if(num_unused_bytes>1024) {
			d->fatal_errflag = 1;
			d->need_dcmpr_errmsg = 1;
			goto done;
		}
	}
	if(pli->dcmpr_nbytes < 10000) {
		d->fatal_errflag = 1;
		d->need_dcmpr_errmsg = 1;
		goto done;
	}

done:
	de_dbg_indent(c, -1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x10(%rbp), %rax
movslq -0x14(%rbp), %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movl -0x14(%rbp), %edx
movq -0x20(%rbp), %rax
movq (%rax), %rcx
leaq 0xcd257(%rip), %rsi     # 0x1be745
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rdi
movslq -0x14(%rbp), %rsi
movq -0x10(%rbp), %rax
imulq 0x60(%rax), %rsi
callq 0x168900
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rdi
callq 0x167c30
movq -0x8(%rbp), %rdi
leaq -0x50(%rbp), %rsi
leaq -0x68(%rbp), %rdx
leaq -0xc8(%rbp), %rcx
callq 0x1773f0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq 0x60(%rax), %rax
movq %rax, -0x58(%rbp)
movb $0x1, -0x60(%rbp)
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x50(%rbp), %rdx
leaq -0x68(%rbp), %rcx
leaq -0xc8(%rbp), %r8
callq 0xf17f0
cmpl $0x0, -0xc8(%rbp)
je 0xf160d
movq -0x8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0xcc(%rbp)
movq -0x8(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x177360
movq -0xd8(%rbp), %rdi
movl -0xcc(%rbp), %edx
movq %rax, %rcx
leaq 0xcd172(%rip), %rsi     # 0x1be763
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movb $0x1, 0x69(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x6a(%rax)
jmp 0xf16b6
movq -0x10(%rbp), %rax
movq 0x70(%rax), %rax
movq 0x18(%rax), %rcx
subq -0x28(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0xc0(%rbp), %rdx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rcx
leaq 0xbbab9(%rip), %rsi     # 0x1ad0f8
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rax
subq -0xc0(%rbp), %rax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0xf1694
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0xcd118(%rip), %rsi     # 0x1be787
movb $0x0, %al
callq 0x15b0d0
cmpq $0x400, -0x30(%rbp)     # imm = 0x400
jle 0xf1692
movq -0x10(%rbp), %rax
movb $0x1, 0x69(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x6a(%rax)
jmp 0xf16b6
jmp 0xf1694
movq -0x20(%rbp), %rax
cmpq $0x2710, 0x10(%rax)     # imm = 0x2710
jge 0xf16b4
movq -0x10(%rbp), %rax
movb $0x1, 0x69(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x6a(%rax)
jmp 0xf16b6
jmp 0xf16b6
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax)