// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/misc2.c
// Function: de_run_yp
static void de_run_yp(deark *c, de_module_params *mparams)
{
	struct fastgraph_ctx *d = NULL;
	de_bitmap *img = NULL;
	de_finfo *fi = NULL;
	dbuf *unc_pixels = NULL;

	d = de_malloc(c, sizeof(struct fastgraph_ctx));
	// TODO?: Maybe we should call do_fastgraph_internal() instead of
	// just decompress_fastgraph_ppr(), but it would be a bit messy.
	d->is_ppr = 1;

	if(has_yp_sig(c)) {
		d->w = (i64)de_getbyte(9) * 256;
		d->w += (i64)de_getbyte(12);
		d->h = (i64)de_getbyte(15) * 256;
		d->h += (i64)de_getbyte(18);
		d->img_startpos = 24; // (Not actually necessary)
	}
	else {
		d->w = 320;
		d->h = 200;
	}

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

	d->max_unc_bytes = d->w * d->h;
	unc_pixels = dbuf_create_membuf(c, d->max_unc_bytes, 0x1);
	dbuf_enable_wbuffer(unc_pixels);
	decompress_fastgraph_ppr(c, d, unc_pixels, d->img_startpos);

	fi = de_finfo_create(c);
	fi->density.code = DE_DENSITY_UNK_UNITS;
	fi->density.xdens = 6.0;
	fi->density.ydens = 5.0;

	de_copy_std_palette(DE_PALID_PC16, 0, 0, d->pal, 16, 0);
	img = de_bitmap_create(c, d->w, d->h, 3);
	de_convert_image_paletted(unc_pixels, 0, 8, d->w, d->pal, img, 0);
	de_bitmap_write_to_file_finfo(img, fi, DE_CREATEFLAG_FLIP_IMAGE |
		DE_CREATEFLAG_OPT_IMAGE);

done:
	dbuf_close(unc_pixels);
	de_bitmap_destroy(img);
	de_finfo_destroy(c, fi);
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movl $0x430, %esi            # imm = 0x430
callq 0xe969b
movq %rax, %r14
movb $0x1, (%rax)
movq 0x18(%rbx), %rdi
leaq 0xb9961(%rip), %rdx     # 0x124a34
movl $0x9, %ecx
xorl %esi, %esi
callq 0xf025e
testl %eax, %eax
je 0x6b0f7
movq $0x140, 0x8(%r14)       # imm = 0x140
movl $0xc8, %ecx
movl $0x10, %eax
jmp 0x6b15b
movq 0x18(%rbx), %rdi
movl $0x9, %esi
callq 0xef0d5
movzbl %al, %eax
shll $0x8, %eax
movq %rax, 0x8(%r14)
movq 0x18(%rbx), %rdi
movl $0xc, %esi
callq 0xef0d5
movzbl %al, %eax
addq %rax, 0x8(%r14)
movq 0x18(%rbx), %rdi
movl $0xf, %esi
callq 0xef0d5
movzbl %al, %eax
shll $0x8, %eax
movq %rax, 0x10(%r14)
movq 0x18(%rbx), %rdi
movl $0x12, %esi
callq 0xef0d5
movzbl %al, %eax
addq %rax, 0x10(%r14)
movl $0x18, %eax
movl $0x18, %ecx
movq %rcx, (%r14,%rax)
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0xe9b05
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0xf241d
testl %eax, %eax
je 0x6b25f
movq 0x10(%r14), %rsi
imulq 0x8(%r14), %rsi
movq %rsi, 0x20(%r14)
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf0faf
movq %rax, %r15
movq %rax, %rdi
callq 0xf0e3f
movq 0x18(%r14), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x6d47a
movq %rbx, %rdi
callq 0xea9a9
movq %rax, %r12
movl $0x1, 0x78(%rax)
movabsq $0x4018000000000000, %rax # imm = 0x4018000000000000
movq %rax, 0x80(%r12)
movabsq $0x4014000000000000, %rax # imm = 0x4014000000000000
movq %rax, 0x88(%r12)
movq %r14, %rbp
addq $0x28, %rbp
movl $0x10, %r8d
movl $0x14, %edi
xorl %esi, %esi
xorl %edx, %edx
movq %rbp, %rcx
xorl %r9d, %r9d
callq 0xecbbc
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
movl $0x3, %ecx
callq 0xf2cab
movq %rax, %r13
movq 0x8(%r14), %rcx
movl $0x0, (%rsp)
movl $0x8, %edx
movq %r15, %rdi
xorl %esi, %esi
movq %rbp, %r8
movq %rax, %r9
callq 0xf35b8
movq %r13, %rdi
movq %r12, %rsi
movl $0x6, %edx
callq 0xf2917
jmp 0x6b268
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r15, %rdi
callq 0xf0b13
movq %r13, %rdi
callq 0xf28d4
movq %rbx, %rdi
movq %r12, %rsi
callq 0xea9b3
movq %rbx, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9752