// File: /workspace/github_c_makefile_star2_O1/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 0x28(%rsp), %r13
movq $0x0, (%r13)
leaq 0x30(%rsp), %rsi
callq 0xe958c
movl $0x448, %esi            # imm = 0x448
movq %rbx, %rdi
callq 0xe969b
movq %rax, %rbp
movq %rax, %r14
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %esi, %esi
movl $0xd, %edx
callq 0xeb7d1
movl %eax, (%rbp)
leaq 0x7d863(%rip), %rsi     # 0x138149
movq %rbx, %rdi
xorl %edx, %edx
callq 0xea727
movb %al, 0x4(%rbp)
movq (%r13), %rdx
leaq 0x688a1(%rip), %rsi     # 0x12319f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
addq $0x4, (%r13)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef14d
movb %al, 0x5(%rbp)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef14d
movb %al, 0x6(%rbp)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef1ed
movq %rax, 0x8(%rbp)
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef1ed
movq %rax, 0x10(%rbp)
movq 0x8(%rbp), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xe9b05
movq 0x8(%rbp), %rsi
movq 0x10(%rbp), %rdx
movq %rbx, %rdi
callq 0xf241d
testl %eax, %eax
je 0xbad82
movq 0x18(%rbx), %rsi
leaq 0x28(%rsp), %r15
movq (%r15), %rdx
addq $0x48, %rbp
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 %rbp, 0x38(%rsp)
movq %rbp, %r9
callq 0xf32f2
addq $0x300, (%r15)          # imm = 0x300
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x18(%r14)
leaq 0x7d773(%rip), %rsi     # 0x138153
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq (%r15), %rdx
movq %rdx, 0x20(%rsp)
movq 0x18(%r14), %r12
addq %rdx, %r12
leaq 0x7d759(%rip), %rsi     # 0x138168
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
leaq 0x34(%rsp), %rsi
movq %rbx, %rdi
callq 0xe958c
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x20(%rsp), %r15
leaq 0x2(%r15), %rax
cmpq %r12, %rax
jg 0xbabc5
movq 0x18(%rbx), %rdi
leaq 0x20(%rsp), %rbp
movq %rbp, %rsi
callq 0xef14d
movl %eax, %r13d
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xef14d
movzbl %al, %ebp
movzbl %r13b, %r13d
movq %rbx, %rdi
leaq 0x7d705(%rip), %rsi     # 0x13817b
movq %r15, %rdx
movl %r13d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
cmpb $0x2, %r13b
je 0xbab48
cmpl $0x1, %r13d
je 0xbaafd
testl %r13d, %r13d
jne 0xbab7a
movq 0x20(%rsp), %r15
movq %rbx, %rdi
callq 0xf6870
movq %rax, %r13
movq 0x18(%rbx), %rdi
movl (%r14), %r9d
movq %r15, %rsi
movq %rbp, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0xf01b0
movq %r13, %rdi
callq 0xf7557
movq %rbx, %rdi
leaq 0x59cbf(%rip), %rsi     # 0x1147a5
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq %r13, %rdi
callq 0xf6bb0
jmp 0xbab9e
cmpl $0x4, %ebp
jne 0xbab9e
movq 0x20(%rsp), %r15
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ca
movq %rax, 0x28(%r14)
movq 0x18(%rbx), %rdi
addq $0x2, %r15
movq %r15, %rsi
callq 0xef1ca
movq %rax, 0x30(%r14)
movl 0x28(%r14), %edx
movq %rbx, %rdi
leaq 0x7d65d(%rip), %rsi     # 0x13819a
movl %eax, %ecx
xorl %eax, %eax
callq 0xe9226
jmp 0xbab9e
cmpl $0x1, %ebp
jne 0xbab9e
movq 0x20(%rsp), %rsi
movq 0x18(%rbx), %rdi
callq 0xef0d5
movb %al, 0x39(%r14)
movb $0x1, 0x38(%r14)
movzbl %al, %edx
movq %rbx, %rdi
leaq 0x7d645(%rip), %rsi     # 0x1381b6
xorl %eax, %eax
callq 0xe9226
jmp 0xbab9e
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 0xe975a
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x20(%rsp), %rax
leaq (%rax,%rbp), %r15
movq %r15, 0x20(%rsp)
addq %rbp, %rax
addq $0x2, %rax
jmp 0xbaa3c
movl 0x34(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
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 0xf0faf
movq %rax, 0x40(%r14)
movq %rax, %rdi
callq 0xf0e3f
movq 0x28(%rsp), %rdx
leaq 0x20(%rsp), %rbp
movq %rdx, (%rbp)
leaq 0x7d5b4(%rip), %rsi     # 0x1381c9
xorl %r12d, %r12d
movq %rbx, %rdi
movq %rdx, 0x40(%rsp)
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
cmpq 0x20(%r14), %r12
jge 0xbac83
movq 0x20(%rsp), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0xbac83
movq %rbp, %rsi
callq 0xef14d
cmpb 0x5(%r14), %al
jne 0xbac87
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xef14d
movl %eax, %r13d
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xef14d
movzbl %al, %r15d
movq 0x40(%r14), %rdi
movzbl %r13b, %esi
movq %r15, %rdx
jmp 0xbacb6
xorl %eax, %eax
jmp 0xbacd4
cmpb 0x6(%r14), %al
jne 0xbacbd
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xef14d
movl %eax, %r13d
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xef193
movq %rax, %r15
movq 0x40(%r14), %rdi
movzbl %r13b, %esi
movq %rax, %rdx
callq 0xf14bd
jmp 0xbaccf
movq 0x40(%r14), %rdi
movzbl %al, %esi
callq 0xf11be
movl $0x1, %r15d
addq %r15, %r12
movb $0x1, %al
testb %al, %al
jne 0xbac34
movq 0x20(%rsp), %rdx
subq 0x40(%rsp), %rdx
leaq 0x57f9b(%rip), %rsi     # 0x112c88
movq %rbx, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x40(%r14), %rdi
callq 0xf0ea4
movl $0x3, %ecx
cmpb $0x0, 0x4(%r14)
je 0xbad3d
cmpb $0x0, 0x38(%r14)
movq 0x38(%rsp), %r15
je 0xbad42
movzbl 0x39(%r14), %eax
andl $0xffffff, 0x48(%r14,%rax,4) # imm = 0xFFFFFF
movl $0x4, %ecx
jmp 0xbad42
movq 0x38(%rsp), %r15
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0xf2cab
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 0xf35b8
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf2923
movq %r12, %rdi
callq 0xf28d4
movq 0x40(%r14), %rdi
callq 0xf0b13
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
movl 0x30(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq