// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/pkm.c
// Function: de_run_pkm
static void de_run_pkm(deark *c, de_module_params *mparams)
{
	struct pkm_ctx *d = NULL;
	de_bitmap *img = NULL;
	i64 pos = 0;
	int bypp;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	d = de_malloc(c, sizeof(struct pkm_ctx));
	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_CP437);
	d->respect_trns = (u8)de_get_ext_option_bool(c, "pkm:trans", 0);

	de_dbg(c, "header at %"I64_FMT, pos);
	de_dbg_indent(c, 1);
	pos += 3; // signature
	pos += 1; // version
	d->pack_byte = de_getbyte_p(&pos);
	d->pack_word = de_getbyte_p(&pos);

	d->w = de_getu16le_p(&pos);
	d->h = de_getu16le_p(&pos);
	de_dbg_dimensions(c, d->w, d->h);
	if(!de_good_image_dimensions(c, d->w, d->h)) goto done;

	de_read_simple_palette(c, c->infile, pos, 256, 3, d->pal, 256, DE_RDPALTYPE_VGA18BIT, 0);
	pos += 3*256;

	d->ph_size = de_getu16le_p(&pos);
	de_dbg(c, "post-header size: %u", (UI)d->ph_size);
	de_dbg_indent(c, -1);

	pkm_read_postheader(c, d, pos);
	pos += d->ph_size;

	d->unc_image_size = d->w * d->h;
	d->unc_pixels = dbuf_create_membuf(c, d->unc_image_size, 0x1);
	dbuf_enable_wbuffer(d->unc_pixels);
	pkm_decompress_image(c, d, pos);
	dbuf_flush(d->unc_pixels);

	if(d->respect_trns && d->has_back_clr) {
		bypp = 4;
		// TODO: Understand the "background/transparent" color field better.
		// Making it transparent messes up some images.
		d->pal[(UI)d->back_clr] = DE_SET_ALPHA(d->pal[(UI)d->back_clr], 0);
	}
	else {
		bypp = 3;
	}
	img = de_bitmap_create(c, d->w, d->h, bypp);
	de_convert_image_paletted(d->unc_pixels, 0, 8, d->w, d->pal, img, 0);
	de_bitmap_write_to_file(img, NULL, 0);

done:
	de_bitmap_destroy(img);
	if(d) {
		dbuf_close(d->unc_pixels);
		de_free(c, d);
	}
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
leaq 0x30(%rsp), %rsi
callq 0xe8a1c
movl $0x448, %esi            # imm = 0x448
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r13
movq %rax, %r14
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0xeb220
movl %eax, (%r13)
leaq 0x7e79f(%rip), %rsi     # 0x1384d5
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe9bd0
movb %al, 0x4(%r13)
leaq 0x69634(%rip), %rsi     # 0x12337f
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
leaq 0x28(%rsp), %r15
movq $0x4, (%r15)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeeb4b
movb %al, 0x5(%r13)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeeb4b
movb %al, 0x6(%r13)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x8(%r13)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x10(%r13)
movq 0x8(%r13), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xe8f88
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rdx
movq %rbx, %rdi
callq 0xf1db1
testl %eax, %eax
je 0xba1ea
movq 0x18(%rbx), %rsi
leaq 0x28(%rsp), %r15
movq (%r15), %rdx
addq $0x48, %r13
movl $0x0, 0x10(%rsp)
movl $0x2, 0x8(%rsp)
movq $0x100, (%rsp)          # imm = 0x100
movl $0x100, %ecx            # imm = 0x100
movl $0x3, %r8d
movq %rbx, %rdi
movq %r13, 0x40(%rsp)
movq %r13, %r9
callq 0xf2cb1
addq $0x300, (%r15)          # imm = 0x300
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x18(%r14)
leaq 0x7e6a7(%rip), %rsi     # 0x1384df
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq (%r15), %r15
movq %r15, 0x20(%rsp)
movq 0x18(%r14), %r12
leaq 0x7e690(%rip), %rsi     # 0x1384f4
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe8617
leaq 0x34(%rsp), %rsi
movq %rbx, %rdi
callq 0xe8a1c
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
cmpq $0x2, %r12
jl 0xba01c
addq %r15, %r12
movq 0x18(%rbx), %rdi
leaq 0x20(%rsp), %rbp
movq %rbp, %rsi
callq 0xeeb4b
movl %eax, %r13d
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xeeb4b
movzbl %al, %ebp
movzbl %r13b, %r13d
movq %rbx, %rdi
leaq 0x7e63e(%rip), %rsi     # 0x138507
movq %r15, %rdx
movl %r13d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
cmpb $0x2, %r13b
je 0xb9f9b
cmpl $0x1, %r13d
je 0xb9f50
testl %r13d, %r13d
jne 0xb9fcd
movq 0x20(%rsp), %r15
movq %rbx, %rdi
callq 0xf6410
movq %rax, %r13
movq 0x18(%rbx), %rdi
movl (%r14), %r9d
movq %r15, %rsi
movq %rbp, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0xefbd7
movq %r13, %rdi
callq 0xf70f6
movq %rbx, %rdi
leaq 0x5a947(%rip), %rsi     # 0x114880
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq %r13, %rdi
callq 0xf6753
jmp 0xb9ff1
cmpl $0x4, %ebp
jne 0xb9ff1
movq 0x20(%rsp), %r15
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebc8
movq %rax, 0x28(%r14)
movq 0x18(%rbx), %rdi
addq $0x2, %r15
movq %r15, %rsi
callq 0xeebc8
movq %rax, 0x30(%r14)
movl 0x28(%r14), %edx
movq %rbx, %rdi
leaq 0x7e596(%rip), %rsi     # 0x138526
movl %eax, %ecx
xorl %eax, %eax
callq 0xe8617
jmp 0xb9ff1
cmpl $0x1, %ebp
jne 0xb9ff1
movq 0x20(%rsp), %rsi
movq 0x18(%rbx), %rdi
callq 0xeead3
movb %al, 0x39(%r14)
movb $0x1, 0x38(%r14)
movzbl %al, %edx
movq %rbx, %rdi
leaq 0x7e57e(%rip), %rsi     # 0x138542
xorl %eax, %eax
callq 0xe8617
jmp 0xb9ff1
movq 0x18(%rbx), %rsi
movq 0x20(%rsp), %rdx
movl $0x1, (%rsp)
movl $0x100, %r8d            # imm = 0x100
movq %rbx, %rdi
movq %rbp, %rcx
xorl %r9d, %r9d
callq 0xe8bea
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x20(%rsp), %rax
leaq (%rax,%rbp), %r15
movq %r15, 0x20(%rsp)
addq %rbp, %rax
addq $0x2, %rax
cmpq %r12, %rax
jle 0xb9e98
movl 0x34(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
movq 0x18(%r14), %rax
addq %rax, 0x28(%rsp)
movq 0x10(%r14), %rsi
imulq 0x8(%r14), %rsi
movq %rsi, 0x20(%r14)
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf09c5
movq %rax, 0x40(%r14)
movq %rax, %rdi
callq 0xf0855
movq 0x28(%rsp), %r15
movq %r15, 0x20(%rsp)
leaq 0x7e4ed(%rip), %rsi     # 0x138555
xorl %ebp, %ebp
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
cmpq $0x0, 0x20(%r14)
jle 0xba145
movq %r15, 0x38(%rsp)
xorl %ebp, %ebp
leaq 0x20(%rsp), %r13
movq %rbp, %r12
movq 0x20(%rsp), %rdx
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rdx
jge 0xba14a
movq %r13, %rsi
callq 0xeeb4b
cmpb 0x5(%r14), %al
jne 0xba0ea
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeeb4b
movl %eax, %r15d
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeeb4b
movzbl %al, %ebp
movq 0x40(%r14), %rdi
movzbl %r15b, %esi
movq %rbp, %rdx
jmp 0xba119
cmpb 0x6(%r14), %al
jne 0xba120
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeeb4b
movl %eax, %r15d
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeeb91
movq %rax, %rbp
movq 0x40(%r14), %rdi
movzbl %r15b, %esi
movq %rax, %rdx
callq 0xf0eba
jmp 0xba131
movq 0x40(%r14), %rdi
movzbl %al, %esi
callq 0xf0bd6
movl $0x1, %ebp
addq %r12, %rbp
cmpq 0x20(%r14), %rbp
jl 0xba09b
movq 0x20(%rsp), %rdx
jmp 0xba14d
movq %r15, %rdx
jmp 0xba152
movq %r12, %rbp
movq 0x38(%rsp), %r15
subq %r15, %rdx
leaq 0x58b3c(%rip), %rsi     # 0x112c98
movq %rbx, %rdi
movq %rbp, %rcx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x40(%r14), %rdi
callq 0xf08ba
movl $0x3, %ecx
cmpb $0x0, 0x4(%r14)
movq 0x40(%rsp), %r15
je 0xba1aa
cmpb $0x0, 0x38(%r14)
je 0xba1aa
movzbl 0x39(%r14), %eax
andl $0xffffff, 0x48(%r14,%rax,4) # imm = 0xFFFFFF
movl $0x4, %ecx
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0xf266a
movq %rax, %r12
movq 0x8(%r14), %rcx
movq 0x40(%r14), %rdi
movl $0x0, (%rsp)
movl $0x8, %edx
xorl %esi, %esi
movq %r15, %r8
movq %rax, %r9
callq 0xf2f79
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf22dc
movq %r12, %rdi
callq 0xf228d
movq 0x40(%r14), %rdi
callq 0xf052b
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
movl 0x30(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq