// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r14
movb $0x1, (%rax)
movq 0x18(%rbx), %rdi
leaq 0xb9de0(%rip), %rdx     # 0x124c54
movl $0x9, %ecx
xorl %esi, %esi
callq 0xefc85
testl %eax, %eax
je 0x6aea0
movq $0x140, 0x8(%r14)       # imm = 0x140
movq $0xc8, 0x10(%r14)
movl $0x140, %esi            # imm = 0x140
movl $0xc8, %edx
jmp 0x6af0a
movq 0x18(%rbx), %rdi
movl $0x9, %esi
callq 0xeead3
movzbl %al, %eax
shll $0x8, %eax
movq %rax, 0x8(%r14)
movq 0x18(%rbx), %rdi
movl $0xc, %esi
callq 0xeead3
movzbl %al, %eax
addq %rax, 0x8(%r14)
movq 0x18(%rbx), %rdi
movl $0xf, %esi
callq 0xeead3
movzbl %al, %eax
shll $0x8, %eax
movq %rax, 0x10(%r14)
movq 0x18(%rbx), %rdi
movl $0x12, %esi
callq 0xeead3
movzbl %al, %edx
addq 0x10(%r14), %rdx
movq %rdx, 0x10(%r14)
movq $0x18, 0x18(%r14)
movq 0x8(%r14), %rsi
movq %rbx, %rdi
callq 0xe8f88
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0xf1db1
testl %eax, %eax
je 0x6afec
movq 0x10(%r14), %rsi
imulq 0x8(%r14), %rsi
movq %rsi, 0x20(%r14)
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf09c5
movq %rax, %r15
movq %rax, %rdi
callq 0xf0855
movq 0x18(%r14), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x6d234
movq %rbx, %rdi
callq 0xea504
movq %rax, %r12
movl $0x1, 0x78(%rax)
movaps 0xb7eb6(%rip), %xmm0    # 0x122e30
movups %xmm0, 0x80(%rax)
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 0xec55a
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
movl $0x3, %ecx
callq 0xf266a
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 0xf2f79
movq %r13, %rdi
movq %r12, %rsi
movl $0x6, %edx
callq 0xf22d0
jmp 0x6aff5
xorl %r13d, %r13d
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %r15, %rdi
callq 0xf052b
movq %r13, %rdi
callq 0xf228d
movq %rbx, %rdi
movq %r12, %rsi
callq 0xea50e
movq %rbx, %rdi
movq %r14, %rsi
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe8be2