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

	de_dbg_indent_save(c, &saved_indent_level);

	de_declare_fmt(c, "PCPaint PIC");

	d->fi = de_finfo_create(c);

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

	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);
	d->palette_flag = de_getbyte(11);

	de_dbg(c, "plane info: 0x%02x", (int)d->plane_info);
	de_dbg(c, "palette flag: 0x%02x", (int)d->palette_flag);

	if(d->palette_flag==0xff) {
		d->ver = 2;
	}
	else if(d->palette_flag==0 && d->plane_info==1) {
		d->ver = 1;
	}

	if(d->ver!=1 && d->ver!=2) {
		de_err(c, "This version of PCPaint PIC is not supported");
		goto done;
	}
	if(d->ver!=2) {
		de_warn(c, "This version of PCPaint PIC might not be supported correctly");
	}

	if(d->ver==1) {
		// V1 support is based on the behavior of Iconvert (Infinity Engineering Services)
		d->video_mode = 0;
		d->pal_info_mainfile.edesc = 0;
		d->pal_info_mainfile.esize = 10;
	}
	else {
		d->video_mode = de_getbyte(12);
		de_dbg(c, "video mode: 0x%02x", (int)d->video_mode);

		do_read_palette_data(c, d, c->infile, &d->pal_info_mainfile);
		de_dbg(c, "edesc: %u", (UI)d->pal_info_mainfile.edesc);
		de_dbg(c, "esize: %d", (int)d->pal_info_mainfile.esize);
	}

	if(d->pal_info_mainfile.esize>0) {
		de_dbg(c, "palette or other info at %d", 17);
	}

	set_density(c, d);

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

	d->num_rle_blocks = de_getu16le(17+d->pal_info_mainfile.esize);

	d->header_size = 17 + d->pal_info_mainfile.esize + 2;

	de_dbg(c, "num rle blocks: %d", (int)d->num_rle_blocks);
	de_dbg_indent(c, -1);

	de_dbg(c, "image data at %d", (int)d->header_size);
	de_dbg_indent(c, 1);
	if(!do_set_up_decoder(c, d)) goto done;
	if(d->screen_mode_type==SCREENMODETYPE_BITMAP) {
		if(!de_good_image_dimensions(c, d->npwidth, d->height)) goto done;
	}

	if(d->num_rle_blocks>0) {
		// Image is compressed.
		decompress_pic_pixels(c, d);
	}
	else {
		// Image is not compressed.
		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 $0x20, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x1c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
leaq 0xbfc3a(%rip), %rsi     # 0x1c1916
callq 0x15e380
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
leaq 0xc328e(%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
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 -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xb, %esi
callq 0x165060
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x39(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x38(%rax), %edx
leaq 0xbf859(%rip), %rsi     # 0x1c161c
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x39(%rax), %edx
leaq 0xbfb45(%rip), %rsi     # 0x1c1922
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movzbl 0x39(%rax), %eax
cmpl $0xff, %eax
jne 0x101e00
movq -0x10(%rbp), %rax
movl $0x2, 0x4(%rax)
jmp 0x101e27
movq -0x10(%rbp), %rax
movzbl 0x39(%rax), %eax
cmpl $0x0, %eax
jne 0x101e25
movq -0x10(%rbp), %rax
movzbl 0x38(%rax), %eax
cmpl $0x1, %eax
jne 0x101e25
movq -0x10(%rbp), %rax
movl $0x1, 0x4(%rax)
jmp 0x101e27
movq -0x10(%rbp), %rax
cmpl $0x1, 0x4(%rax)
je 0x101e52
movq -0x10(%rbp), %rax
cmpl $0x2, 0x4(%rax)
je 0x101e52
movq -0x8(%rbp), %rdi
leaq 0xbfaf1(%rip), %rsi     # 0x1c1937
movb $0x0, %al
callq 0x15bfb0
jmp 0x1020be
movq -0x10(%rbp), %rax
cmpl $0x2, 0x4(%rax)
je 0x101e6e
movq -0x8(%rbp), %rdi
leaq 0xbfafd(%rip), %rsi     # 0x1c1964
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
cmpl $0x1, 0x4(%rax)
jne 0x101e9c
movq -0x10(%rbp), %rax
movb $0x0, 0x3a(%rax)
movq -0x10(%rbp), %rax
movl $0x0, 0x48(%rax)
movq -0x10(%rbp), %rax
movq $0xa, 0x50(%rax)
jmp 0x101f23
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xc, %esi
callq 0x165060
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x3a(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movzbl 0x3a(%rax), %edx
leaq 0xbfad7(%rip), %rsi     # 0x1c19a1
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x10(%rbp), %rcx
addq $0x48, %rcx
callq 0x102660
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x48(%rax), %edx
leaq 0xbfab4(%rip), %rsi     # 0x1c19b4
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movl %eax, %edx
leaq 0xa4207(%rip), %rsi     # 0x1a6123
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpq $0x0, 0x50(%rax)
jle 0x101f45
movq -0x8(%rbp), %rdi
leaq 0xbfa85(%rip), %rsi     # 0x1c19be
movl $0x11, %edx
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1037d0
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 0x101f79
jmp 0x1020be
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rsi
addq $0x11, %rsi
callq 0x165280
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x80(%rax)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rcx
addq $0x11, %rcx
addq $0x2, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x80(%rax), %rax
movl %eax, %edx
leaq 0xbfa0a(%rip), %rsi     # 0x1c19da
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq 0xa9b0f(%rip), %rsi     # 0x1abb09
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 0x102026
jmp 0x1020be
movq -0x10(%rbp), %rax
cmpl $0x1, 0x98(%rax)
jne 0x102055
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x102053
jmp 0x1020be
jmp 0x102055
movq -0x10(%rbp), %rax
cmpq $0x0, 0x80(%rax)
jle 0x102072
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x103980
jmp 0x1020a9
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 -0x1c(%rbp), %esi
callq 0x15b5a0
addq $0x20, %rsp
popq %rbp
retq