// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0x448, %esi            # imm = 0x448
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0xd, %edx
callq 0x15e4a0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rdi
leaq 0xb15dd(%rip), %rsi     # 0x1c3d89
xorl %edx, %edx
callq 0x15cbc0
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x4(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
leaq 0x9c598(%rip), %rsi     # 0x1aed63
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x28(%rbp), %rax
addq $0x3, %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x5(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x165170
movb %al, %cl
movq -0x18(%rbp), %rax
movb %cl, 0x6(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x1128a0
jmp 0x112a75
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x28(%rbp), %rdx
movq -0x18(%rbp), %r9
addq $0x48, %r9
movl $0x100, %ecx            # imm = 0x100
movl $0x3, %r8d
xorl %eax, %eax
movq $0x100, (%rsp)          # imm = 0x100
movl $0x2, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x16c160
movq -0x28(%rbp), %rax
addq $0x300, %rax            # imm = 0x300
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %edx
leaq 0xb1472(%rip), %rsi     # 0x1c3d93
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x112b40
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
imulq 0x10(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x112ce0
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x167c30
movq -0x18(%rbp), %rax
movzbl 0x4(%rax), %eax
cmpl $0x0, %eax
je 0x112a0c
movq -0x18(%rbp), %rax
movzbl 0x38(%rax), %eax
cmpl $0x0, %eax
je 0x112a0c
movl $0x4, -0x2c(%rbp)
movq -0x18(%rbp), %rax
movq -0x18(%rbp), %rcx
movzbl 0x39(%rcx), %ecx
movl %ecx, %ecx
movl 0x48(%rax,%rcx,4), %edx
andl $0xffffff, %edx         # imm = 0xFFFFFF
orl $0x0, %edx
movq -0x18(%rbp), %rax
movq -0x18(%rbp), %rcx
movzbl 0x39(%rcx), %ecx
movl %ecx, %ecx
movl %edx, 0x48(%rax,%rcx,4)
jmp 0x112a13
movl $0x3, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
movl -0x2c(%rbp), %ecx
callq 0x16b750
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x18(%rbp), %r8
addq $0x48, %r8
movq -0x20(%rbp), %r9
xorl %eax, %eax
movl %eax, %esi
movl $0x8, %edx
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x16afd0
movq -0x20(%rbp), %rdi
callq 0x16af60
cmpq $0x0, -0x18(%rbp)
je 0x112a9f
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x15b5a0
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)