// File: /workspace/github_c_makefile_star2_O2/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 0xdebae
movl $0x448, %esi            # imm = 0x448
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r14
xorl %r15d, %r15d
pushq $0xd
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe08d2
movl %eax, (%r14)
leaq 0x78769(%rip), %rsi     # 0x12b1d5
movq %rbx, %rdi
xorl %edx, %edx
callq 0xdfae5
movb %al, 0x4(%r14)
leaq 0x6373e(%rip), %rsi     # 0x1161bf
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
leaq 0x28(%rsp), %r12
movq $0x4, (%r12)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3f2a
movb %al, 0x5(%r14)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3f2a
movb %al, 0x6(%r14)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3fc4
movq %rax, 0x8(%r14)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3fc4
movq %rax, 0x10(%r14)
movq 0x8(%r14), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xdf038
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0xe6be1
testl %eax, %eax
je 0xb2edf
movq 0x18(%rbx), %rsi
leaq 0x28(%rsp), %r15
movq (%r15), %rdx
movq %r14, %r9
addq $0x48, %r9
andl $0x0, 0x10(%rsp)
movl $0x2, 0x8(%rsp)
movq $0x100, (%rsp)          # imm = 0x100
pushq $0x3
popq %r8
movl $0x100, %ecx            # imm = 0x100
movq %rbx, %rdi
movq %r9, 0x40(%rsp)
callq 0xe79ca
addq $0x300, (%r15)          # imm = 0x300
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
movq %rax, 0x18(%r14)
leaq 0x78677(%rip), %rsi     # 0x12b1df
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq (%r15), %r15
leaq 0x20(%rsp), %rax
movq %r15, (%rax)
movq 0x18(%r14), %r12
addq %r15, %r12
leaq 0x7865c(%rip), %rsi     # 0x12b1f4
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0xde896
leaq 0x34(%rsp), %rsi
movq %rbx, %rdi
callq 0xdebae
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
leaq 0x2(%r15), %rax
cmpq %r12, %rax
jg 0xb2d3e
movq 0x18(%rbx), %rdi
leaq 0x20(%rsp), %rbp
movq %rbp, %rsi
callq 0xe3f2a
movl %eax, %r13d
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xe3f2a
movzbl %al, %ebp
movzbl %r13b, %r13d
movq %rbx, %rdi
leaq 0x7860c(%rip), %rsi     # 0x12b207
movq %r15, %rdx
movl %r13d, %ecx
movl %ebp, %r8d
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
cmpb $0x2, %r13b
je 0xb2ccb
cmpl $0x1, %r13d
je 0xb2c80
testl %r13d, %r13d
jne 0xb2cfd
movq 0x20(%rsp), %r15
movq %rbx, %rdi
callq 0xeac74
movq %rax, %r13
movq 0x18(%rbx), %rdi
movl (%r14), %r9d
movq %r15, %rsi
movq %rbp, %rdx
movq %rax, %rcx
xorl %r8d, %r8d
callq 0xe4c9a
movq %r13, %rdi
callq 0xeb877
movq %rbx, %rdi
leaq 0x54b8c(%rip), %rsi     # 0x1077f5
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq %r13, %rdi
callq 0xeaf3c
jmp 0xb2d21
cmpl $0x4, %ebp
jne 0xb2d21
movq 0x20(%rsp), %r15
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fa3
movq %rax, 0x28(%r14)
movq 0x18(%rbx), %rdi
addq $0x2, %r15
movq %r15, %rsi
callq 0xe3fa3
movq %rax, 0x30(%r14)
movl 0x28(%r14), %edx
movq %rbx, %rdi
leaq 0x78566(%rip), %rsi     # 0x12b226
movl %eax, %ecx
xorl %eax, %eax
callq 0xde896
jmp 0xb2d21
cmpl $0x1, %ebp
jne 0xb2d21
movq 0x20(%rsp), %rsi
movq 0x18(%rbx), %rdi
callq 0xe3eb4
movb %al, 0x39(%r14)
movb $0x1, 0x38(%r14)
movzbl %al, %edx
movq %rbx, %rdi
leaq 0x7854e(%rip), %rsi     # 0x12b242
xorl %eax, %eax
callq 0xde896
jmp 0xb2d21
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 0xded3a
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
movq 0x20(%rsp), %r15
addq %rbp, %r15
movq %r15, 0x20(%rsp)
jmp 0xb2bbd
movl 0x34(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
movq 0x18(%r14), %rax
addq %rax, 0x28(%rsp)
movq 0x10(%r14), %rsi
imulq 0x8(%r14), %rsi
movq %rsi, 0x20(%r14)
pushq $0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %edx
callq 0xe59cd
movq %rax, 0x40(%r14)
movq %rax, %rdi
callq 0xe589c
movq 0x28(%rsp), %r12
leaq 0x20(%rsp), %r13
movq %r12, (%r13)
leaq 0x784c7(%rip), %rsi     # 0x12b255
xorl %r15d, %r15d
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
movl %ebp, %esi
callq 0xdeba7
movq %r12, 0x38(%rsp)
cmpq 0x20(%r14), %r15
jge 0xb2e50
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %r12
jge 0xb2e50
movq %r13, %rsi
callq 0xe3f2a
cmpb 0x5(%r14), %al
jne 0xb2dfe
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3f2a
movl %eax, %r12d
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3f2a
movzbl %al, %ebp
movq 0x40(%r14), %rdi
movzbl %r12b, %esi
movq %rbp, %rdx
jmp 0xb2e2d
cmpb 0x6(%r14), %al
jne 0xb2e34
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3f2a
movl %eax, %r12d
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xe3f6e
movq %rax, %rbp
movq 0x40(%r14), %rdi
movzbl %r12b, %esi
movq %rax, %rdx
callq 0xe5e40
jmp 0xb2e43
movq 0x40(%r14), %rdi
movzbl %al, %esi
callq 0xe5bc5
pushq $0x1
popq %rbp
addq %rbp, %r15
movq 0x20(%rsp), %r12
jmp 0xb2dad
subq 0x38(%rsp), %r12
leaq 0x52e1c(%rip), %rsi     # 0x105c78
movq %rbx, %rdi
movq %r12, %rdx
movq %r15, %rcx
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x40(%r14), %rdi
callq 0xe58e8
pushq $0x3
popq %rcx
cmpb $0x0, 0x4(%r14)
je 0xb2ea2
cmpb $0x0, 0x38(%r14)
je 0xb2ea2
movzbl 0x39(%r14), %eax
andl $0xffffff, 0x48(%r14,%rax,4) # imm = 0xFFFFFF
pushq $0x4
popq %rcx
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0xe7437
movq %rax, %r15
movq 0x8(%r14), %rcx
movq 0x40(%r14), %rdi
andl $0x0, (%rsp)
pushq $0x8
popq %rdx
xorl %esi, %esi
movq 0x40(%rsp), %r8
movq %rax, %r9
callq 0xe7cbc
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe70b7
movq %r15, %rdi
callq 0xe7084
movq 0x40(%r14), %rdi
callq 0xe5591
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
movl 0x30(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq