// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/pcpaint.c
// Function: de_run_pcpaint
static void de_run_pcpaint(deark *c, de_module_params *mparams)
{
	// 0=unknown, 1=pic, 2=clp
	const char *pcpaintfmt;
	u8 buf[16];
	lctx *d;

	d = de_malloc(c, sizeof(lctx));

	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_CP437_G);

	pcpaintfmt = de_get_ext_option(c, "pcpaint:fmt");
	if(pcpaintfmt) {
		if(!de_strcmp(pcpaintfmt, "pic")) {
			d->file_fmt = FMT_PIC;
		}
		else if(!de_strcmp(pcpaintfmt, "clp")) {
			d->file_fmt = FMT_CLP;
		}
		else if(!de_strcmp(pcpaintfmt, "clip")) {
			d->file_fmt = FMT_CLP;
		}
	}

	if(!d->file_fmt) {
		// File subtype not given by user. Try to detect it.
		de_read(buf, 0, 16);
		if(buf[0]==0x34 && buf[1]==0x12) {
			if(c->infile->len==0x1234) {
				// Pathological case where both formats could start with 0x1234.
				if(buf[10]==0xff) { // definitely a compressed CLP
					d->file_fmt = FMT_CLP;
				}
				else {
					de_warn(c, "Format can't be reliably identified. Try \"-opt pcpaint:fmt=clp\" if necessary.");
					d->file_fmt = FMT_PIC;
				}
			}
			else {
				d->file_fmt = FMT_PIC;
			}
		}
		else {
			d->file_fmt = FMT_CLP;
		}
	}

	d->opt_keep_invis_chars = de_get_ext_option_bool(c, "pcpaint:invistext", 0);

	if(d->file_fmt==FMT_CLP) {
		de_run_pcpaint_clp(c, d, mparams);
	}
	else {
		de_run_pcpaint_pic(c, d, mparams);
	}

	destroy_lctx(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movl $0x4a0, %esi            # imm = 0x4A0
callq 0xe8b2b
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0xeb220
movl %eax, 0x8(%r14)
leaq 0x86f4a(%rip), %rsi     # 0x135bdd
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0xaece5
movq %rax, %r15
leaq 0x874bb(%rip), %rsi     # 0x136165
movq %rax, %rdi
callq 0x181a0
testl %eax, %eax
je 0xaed3b
leaq 0x7462b(%rip), %rsi     # 0x1232ec
movq %r15, %rdi
callq 0x181a0
movl $0x2, %ebp
testl %eax, %eax
je 0xaed40
leaq 0x86f10(%rip), %rsi     # 0x135be9
movq %r15, %rdi
callq 0x181a0
testl %eax, %eax
je 0xaed40
cmpl $0x0, (%r14)
jne 0xaed43
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %r15
movl $0x10, %ecx
movq %r15, %rsi
xorl %edx, %edx
callq 0xee8d4
cmpb $0x34, (%r15)
movl $0x2, %ebp
jne 0xaed40
cmpb $0x12, 0x11(%rsp)
jne 0xaed40
movq 0x18(%rbx), %rax
cmpq $0x1234, 0x18(%rax)     # imm = 0x1234
jne 0xaed3b
cmpb $-0x1, 0x1a(%rsp)
je 0xaed40
leaq 0x86ebd(%rip), %rsi     # 0x135bee
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movl $0x1, %ebp
movl %ebp, (%r14)
leaq 0x86ef2(%rip), %rsi     # 0x135c3c
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe9bd0
movl %eax, 0xc(%r14)
cmpl $0x2, (%r14)
jne 0xaedf4
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0xe8a1c
leaq 0x86ed8(%rip), %rsi     # 0x135c4e
movq %rbx, %rdi
callq 0xeb125
leaq 0x8a969(%rip), %rsi     # 0x1396ee
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xeebc8
movq %rax, %r15
leaq 0x6ca46(%rip), %rsi     # 0x11b7f9
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rcx
cmpq %rcx, %r15
je 0xaf1ad
cmpq $0x1234, %r15           # imm = 0x1234
jne 0xaf199
leaq 0x86e75(%rip), %rsi     # 0x135c5a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
jmp 0xaf1ad
leaq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0xe8a1c
leaq 0x871d6(%rip), %rsi     # 0x135fde
movq %rbx, %rdi
callq 0xeb125
movq %rbx, %rdi
callq 0xea504
movq %rax, 0x10(%r14)
leaq 0x8a8cb(%rip), %rsi     # 0x1396ee
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
movl $0x2, %esi
callq 0xeebc8
movq %rax, 0x20(%r14)
movq %rax, 0x30(%r14)
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xeebc8
movq %rax, 0x28(%r14)
movq 0x20(%r14), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xe8f88
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xeead3
movb %al, 0x38(%r14)
movq 0x18(%rbx), %rdi
movl $0xb, %esi
callq 0xeead3
movb %al, 0x39(%r14)
movzbl 0x38(%r14), %edx
leaq 0x86e41(%rip), %rsi     # 0x135ce4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movzbl 0x39(%r14), %edx
leaq 0x87131(%rip), %rsi     # 0x135fea
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movzbl 0x39(%r14), %eax
cmpl $0xff, %eax
je 0xaeee1
testl %eax, %eax
jne 0xaeeea
movl $0x1, %eax
cmpb $0x1, 0x38(%r14)
je 0xaeee6
jmp 0xaeeea
movl $0x2, %eax
movl %eax, 0x4(%r14)
movl 0x4(%r14), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0xaef34
cmpl $0x2, %eax
je 0xaef4a
leaq 0x8712a(%rip), %rsi     # 0x13602c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
cmpl $0x1, 0x4(%r14)
jne 0xaef4a
movb $0x0, 0x3a(%r14)
movq %r14, %r15
addq $0x48, %r15
movl $0x0, 0x48(%r14)
movq $0xa, 0x50(%r14)
jmp 0xaefb7
leaq 0x870c4(%rip), %rsi     # 0x135fff
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0xaf3d6
movq 0x18(%rbx), %rdi
movl $0xc, %esi
callq 0xeead3
movb %al, 0x3a(%r14)
movzbl %al, %edx
leaq 0x87103(%rip), %rsi     # 0x136069
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rsi
movq %r14, %r15
addq $0x48, %r15
movq %rbx, %rdi
movq %r15, %rdx
callq 0xaf9a5
movl 0x48(%r14), %edx
leaq 0x870eb(%rip), %rsi     # 0x13607c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movl 0x50(%r14), %edx
leaq 0x6b51d(%rip), %rsi     # 0x11a4c3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
cmpq $0x0, 0x50(%r14)
jle 0xaefcd
leaq 0x870c8(%rip), %rsi     # 0x136086
movq %rbx, %rdi
movl $0x11, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x10(%r14), %rax
testq %rax, %rax
je 0xaf028
cmpl $0x2, 0x4(%r14)
jl 0xaf028
movb 0x3a(%r14), %cl
addb $-0x41, %cl
cmpb $0x11, %cl
ja 0xaf028
movzbl %cl, %ecx
movl $0x22fff, %edx          # imm = 0x22FFF
btl %ecx, %edx
jae 0xaf028
shll $0x3, %ecx
leaq 0x86a78(%rip), %rdx     # 0x135a78
movsd (%rcx,%rdx), %xmm0
leaq 0x86afc(%rip), %rdx     # 0x135b08
movsd (%rcx,%rdx), %xmm1
movl $0x1, 0x78(%rax)
movsd %xmm0, 0x80(%rax)
movsd %xmm1, 0x88(%rax)
movq %r15, 0x78(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xaf6b9
testl %eax, %eax
je 0xaf3d6
movq 0x18(%rbx), %rdi
movq 0x50(%r14), %rsi
addq $0x11, %rsi
callq 0xeebc8
movq %rax, 0x80(%r14)
movq 0x50(%r14), %rcx
addq $0x13, %rcx
movq %rcx, 0x18(%r14)
leaq 0x87038(%rip), %rsi     # 0x1360a2
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movl 0x18(%r14), %edx
leaq 0x70f13(%rip), %rsi     # 0x11ffa1
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
movq %r14, %rsi
callq 0xaf796
testl %eax, %eax
je 0xaf3d6
cmpl $0x1, 0x98(%r14)
jne 0xaf0da
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq %rbx, %rdi
callq 0xf1db1
testl %eax, %eax
je 0xaf3d6
cmpq $0x0, 0x80(%r14)
jle 0xaf3ae
leaq 0x8(%rsp), %rsi
movq %rbx, %rdi
callq 0xe8a1c
cmpq $0x0, 0x80(%r14)
jle 0xaf188
movl $0x4000, %esi           # imm = 0x4000
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, 0x88(%r14)
movq 0x30(%r14), %rsi
addq $0x7, %rsi
imulq 0x28(%r14), %rsi
movq %rax, %rdi
callq 0xf0a7c
leaq 0x86bd5(%rip), %rsi     # 0x135d0a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%r14), %rdx
movq %rbx, %rdi
movq %r14, %rsi
callq 0xb04e7
testl %eax, %eax
je 0xaf188
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x88(%r14), %rax
movq 0x18(%rax), %rdx
leaq 0x64748(%rip), %rsi     # 0x1138c6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movl 0x8(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
jmp 0xaf3c9
leaq 0x86ae9(%rip), %rsi     # 0x135c89
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe8faa
movq 0x18(%rbx), %rdi
movl $0x2, %esi
callq 0xeebc8
movq %rax, 0x20(%r14)
movq %rax, 0x30(%r14)
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xeebc8
movq %rax, 0x28(%r14)
movq 0x20(%r14), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xe8f88
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xeead3
movl %eax, %ebp
movb %al, 0x38(%r14)
xorl %r15d, %r15d
cmpb $-0x1, %al
sete %r15b
je 0xaf20d
movq $0xb, 0x18(%r14)
jmp 0xaf227
movq $0xd, 0x18(%r14)
movq 0x18(%rbx), %rdi
movl $0xb, %esi
callq 0xeead3
movb %al, 0x38(%r14)
leaq 0x88f9e(%rip), %rsi     # 0x1381cc
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movzbl 0x38(%r14), %edx
leaq 0x86a9d(%rip), %rsi     # 0x135ce4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movb $0x0, 0x3a(%r14)
movq %r14, %rax
addq $0x48, %rax
movl $0x0, 0x48(%r14)
movq $0x0, 0x50(%r14)
movq %rax, 0x78(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq 0xaf6b9
testl %eax, %eax
je 0xaf3a8
movq 0x18(%r14), %rdx
leaq 0x8be1b(%rip), %rsi     # 0x13b0b7
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, %rdi
movq %r14, %rsi
callq 0xaf796
testl %eax, %eax
je 0xaf3a8
movq 0x18(%rbx), %rdi
cmpb $-0x1, %bpl
je 0xaf2ec
movq 0x18(%r14), %rsi
movq 0x18(%rdi), %rdx
subq %rsi, %rdx
callq 0xf1388
movq %rax, 0x88(%r14)
jmp 0xaf39b
movl $0xc, %esi
callq 0xeead3
movzbl %al, %ebp
leaq 0x869f7(%rip), %rsi     # 0x135cf7
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe889f
leaq 0x869f7(%rip), %rsi     # 0x135d0a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movl $0x4000, %esi           # imm = 0x4000
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, 0x88(%r14)
movq 0x30(%r14), %rsi
addq $0x7, %rsi
imulq 0x28(%r14), %rsi
movq %rax, %rdi
callq 0xf0a7c
movq 0x18(%r14), %rdx
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rcx
subq %rdx, %rcx
movq %rbx, %rdi
movq %r14, %rsi
movl %ebp, %r8d
callq 0xaf8f4
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x88(%r14), %rax
movq 0x18(%rax), %rdx
leaq 0x64535(%rip), %rsi     # 0x1138c6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movq %r14, %rsi
callq *0x90(%r14)
movl 0x8(%rsp), %esi
jmp 0xaf3da
movq 0x18(%rbx), %rdi
movq 0x18(%r14), %rsi
movq 0x18(%rdi), %rdx
subq %rsi, %rdx
callq 0xf1388
movq %rax, 0x88(%r14)
movq %rbx, %rdi
movq %r14, %rsi
callq *0x90(%r14)
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
movq %rbx, %rdi
movq %r14, %rsi
callq 0xaf662
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq