// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/pcpaint.c
// Function: de_run_pcpaint_clp
static void de_run_pcpaint_clp(deark *c, lctx *d, de_module_params *mparams)
{
	i64 file_size;
	u8 run_marker;
	int is_compressed;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);

	de_declare_fmt(c, "PCPaint CLP");

	de_dbg(c, "header at %d", 0);
	de_dbg_indent(c, 1);

	file_size = de_getu16le(0);
	de_dbg(c, "reported file size: %"I64_FMT, file_size);
	if(file_size != c->infile->len) {
		if(file_size==0x1234) {
			de_warn(c, "This is probably a .PIC file, not a CLIP file.");
		}
		else {
			de_warn(c, "Reported file size (%"I64_FMT") does not equal actual file size (%"I64_FMT"). "
				"Format may not be correct.", file_size, c->infile->len);
		}
	}

	d->npwidth = de_getu16le(2);
	d->pdwidth = d->npwidth; // default
	d->height = de_getu16le(4);
	de_dbg_dimensions(c, d->npwidth, d->height);

	d->plane_info = de_getbyte(10);

	is_compressed = (d->plane_info==0xff);

	if(is_compressed) {
		d->header_size = 13;
		d->plane_info = de_getbyte(11);
	}
	else {
		d->header_size = 11;
	}
	de_dbg(c, "compressed: %d", (int)is_compressed);
	de_dbg(c, "plane info: 0x%02x", (int)d->plane_info);

	de_dbg_indent(c, -1);

	// The colors probably won't be right, but we have no way to tell what palette
	// is used by a CLP image.
	d->video_mode = 0;
	d->pal_info_mainfile.edesc = 0;
	d->pal_info_mainfile.esize = 0;

	d->pal_info_to_use = &d->pal_info_mainfile; // tentative
	if(!do_read_alt_palette_file(c, d)) goto done;

	de_dbg(c, "image data at %"I64_FMT, d->header_size);
	de_dbg_indent(c, 1);
	if(!do_set_up_decoder(c, d)) goto done;

	if(is_compressed) {
		run_marker = de_getbyte(12);
		de_dbg3(c, "run marker: 0x%02x", (UI)run_marker);

		de_dbg(c, "decompressing image");
		de_dbg_indent(c, 1);
		d->unc_pixels = dbuf_create_membuf(c, 16384, 0);
		dbuf_set_length_limit(d->unc_pixels, (d->pdwidth+7) * d->height);

		if(!decompress_block(c, d, d->header_size,
			c->infile->len - d->header_size, run_marker))
		{
			goto done;
		}
		de_dbg_indent(c, -1);
		de_dbg(c, "decompressed to %"I64_FMT" bytes", d->unc_pixels->len);
	}
	else {
		d->unc_pixels = dbuf_open_input_subfile(c->infile,
			d->header_size, c->infile->len-d->header_size);
	}

	d->decoder_fn(c, d);

done:
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
leaq 0xbfc7a(%rip), %rsi     # 0x1c1586
callq 0x15e380
movq -0x8(%rbp), %rdi
leaq 0xc3672(%rip), %rsi     # 0x1c4f8e
xorl %edx, %edx
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x165280
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0xa5aee(%rip), %rsi     # 0x1a7445
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
je 0x1019b2
cmpq $0x1234, -0x20(%rbp)    # imm = 0x1234
jne 0x10198e
movq -0x8(%rbp), %rdi
leaq 0xbfc0d(%rip), %rsi     # 0x1c1592
movb $0x0, %al
callq 0x15bc10
jmp 0x1019b0
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
leaq 0xbfc18(%rip), %rsi     # 0x1c15c1
movb $0x0, %al
callq 0x15bc10
jmp 0x1019b2
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x2, %esi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xa, %esi
callq 0x165060
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x38(%rax)
movq -0x10(%rbp), %rax
movzbl 0x38(%rax), %eax
cmpl $0xff, %eax
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x28(%rbp)
cmpl $0x0, -0x28(%rbp)
je 0x101a77
movq -0x10(%rbp), %rax
movq $0xd, 0x18(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xb, %esi
callq 0x165060
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x38(%rax)
jmp 0x101a83
movq -0x10(%rbp), %rax
movq $0xb, 0x18(%rax)
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %edx
leaq 0xc1fef(%rip), %rsi     # 0x1c3a80
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x38(%rax), %edx
leaq 0xbfb71(%rip), %rsi     # 0x1c161c
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x10(%rbp), %rax
movb $0x0, 0x3a(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x48(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x50(%rax)
movq -0x10(%rbp), %rcx
addq $0x48, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x78(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x102150
cmpl $0x0, %eax
jne 0x101b06
jmp 0x101c8f
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
leaq 0xc4f4c(%rip), %rsi     # 0x1c6a65
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x102280
cmpl $0x0, %eax
jne 0x101b45
jmp 0x101c8f
cmpl $0x0, -0x28(%rbp)
je 0x101c43
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xc, %esi
callq 0x165060
movb %al, -0x21(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x21(%rbp), %edx
leaq 0xbfabc(%rip), %rsi     # 0x1c162f
movb $0x0, %al
callq 0x15b400
movq -0x8(%rbp), %rdi
leaq 0xbfabd(%rip), %rsi     # 0x1c1642
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0x4000, %esi           # imm = 0x4000
xorl %edx, %edx
callq 0x168020
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x88(%rax)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rsi
addq $0x7, %rsi
movq -0x10(%rbp), %rax
imulq 0x28(%rax), %rsi
callq 0x168120
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
subq 0x18(%rax), %rcx
movzbl -0x21(%rbp), %r8d
callq 0x102540
cmpl $0x0, %eax
jne 0x101c12
jmp 0x101c8f
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movq 0x18(%rax), %rdx
leaq 0x9dbbc(%rip), %rsi     # 0x19f7f6
movb $0x0, %al
callq 0x15b0d0
jmp 0x101c7a
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rdx
movq -0x10(%rbp), %rax
subq 0x18(%rax), %rdx
callq 0x1691b0
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x88(%rax)
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rax
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq *%rax
movq -0x8(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x15b5a0
addq $0x30, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)