// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/gws.c
// Function: de_run_gws_exepic
static void de_run_gws_exepic(deark *c, de_module_params *mparams)
{
	struct gws_exepic_ctx *d = NULL;
	i64 pos;

	d = de_malloc(c, sizeof(struct gws_exepic_ctx));
	d->msgpfx = "[GWS picture] ";
	d->ei = de_malloc(c, sizeof(struct fmtutil_exe_info));

	fmtutil_collect_exe_info(c, c->infile, d->ei);

	d->edd.restrict_to_fmt = DE_SPECIALEXEFMT_GWS_EXEPIC;
	fmtutil_detect_specialexe(c, d->ei, &d->edd);
	if(d->edd.detected_fmt!=DE_SPECIALEXEFMT_GWS_EXEPIC) {
		d->need_errmsg = 1;
		goto done;
	}

	pos = d->ei->start_of_dos_code + 9;

	// My best guess as to how to calculate the image position.
	// The field at CS:9 is not actually used by the executable code. Instead
	// it's overwritten with a hardcoded value that is observed to be identical.
	// Then it is adjusted in some way.
	d->imgpos = de_getu16le_p(&pos);
	d->imgpos = de_pad_to_n(d->imgpos, 16);
	d->imgpos = d->ei->start_of_dos_code + d->imgpos;
	de_dbg(c, "img pos: %"I64_FMT, d->imgpos);

	d->w = de_getu16le_p(&pos);
	d->h = de_getu16le_p(&pos);
	de_dbg_dimensions(c, d->w, d->h);
	d->byprpp = de_getu16le_p(&pos);
	de_dbg(c, "bytes/row/plane: %u", (UI)d->byprpp);
	d->depth_raw = de_getu16le_p(&pos);
	de_dbg(c, "depth: %u", (UI)d->depth_raw);
	d->cmpr_meth = (UI)de_getu16le_p(&pos);
	de_dbg(c, "cmpr meth: %u", d->cmpr_meth);
	if(d->depth_raw<1 || d->depth_raw>8) {
		d->need_errmsg = 1;
		goto done;
	}
	if(d->depth_raw>=5) {
		d->depth_adj = 8;
		d->nplanes = 1;
	}
	else {
		d->depth_adj = d->depth_raw;
		d->nplanes = d->depth_raw;
	}

	d->unc_image_size = d->byprpp * d->nplanes * d->h;
	d->ncolors = (i64)1 << d->depth_raw;
	de_read_simple_palette(c, c->infile, d->ei->start_of_dos_code+54,
		d->ncolors, 3, d->pal, 256, DE_RDPALTYPE_24BIT, 0);

	if(d->cmpr_meth!=1 && d->cmpr_meth!=2) {
		de_err(c, "%sUnsupported compression", d->msgpfx);
		goto done;
	}

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

	do_gwsexe_image(c, d);

done:
	if(d) {
		if(d->need_errmsg) {
			de_err(c, "%sBad or unsupported GWS EXE picture", d->msgpfx);
		}
		de_free(c, d->ei);
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movl $0x4d8, %esi            # imm = 0x4D8
callq 0xe969b
movq %rax, %r14
leaq 0xbf7b1(%rip), %rax     # 0x123f4a
movq %rax, 0x50(%r14)
movl $0xf0, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, 0x58(%r14)
movq 0x18(%rbx), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x1053ee
leaq 0x60(%r14), %rdx
movb $-0x37, 0x60(%r14)
movq 0x58(%r14), %rsi
movq %rbx, %rdi
callq 0x105ccc
cmpb $-0x37, 0x61(%r14)
jne 0x648d8
movq 0x58(%r14), %rax
movq 0x38(%rax), %rax
addq $0x9, %rax
leaq 0x10(%rsp), %r15
movq %rax, (%r15)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x8(%r14)
movl $0x10, %esi
movq %rax, %rdi
callq 0xea80b
movq %rax, 0x8(%r14)
movq 0x58(%r14), %rcx
addq 0x38(%rcx), %rax
movq %rax, 0x8(%r14)
leaq 0xbf734(%rip), %rsi     # 0x123f59
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x20(%r14)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x28(%r14)
movq 0x20(%r14), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xe9b05
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x40(%r14)
leaq 0xbf6ee(%rip), %rsi     # 0x123f66
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x10(%r14)
leaq 0xbf6df(%rip), %rsi     # 0x123f7a
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movl %eax, 0x4(%r14)
leaq 0xb474b(%rip), %rsi     # 0x119009
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x10(%r14), %rcx
leaq -0x9(%rcx), %rax
cmpq $-0x9, %rax
ja 0x64922
movb $0x1, (%r14)
testq %r14, %r14
je 0x64913
cmpb $0x0, (%r14)
je 0x648fc
movq 0x50(%r14), %rdx
leaq 0xbf6ac(%rip), %rsi     # 0x123f9e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq 0x58(%r14), %rsi
movq %rbx, %rdi
callq 0xe9752
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x5, %rcx
movl $0x8, %edx
cmovlq %rcx, %rdx
movl $0x1, %eax
movq %rcx, %rsi
cmovgeq %rax, %rsi
movq %rdx, 0x18(%r14)
movq %rsi, 0x30(%r14)
imulq 0x40(%r14), %rsi
imulq 0x28(%r14), %rsi
shlq %cl, %rax
movq %rsi, 0x38(%r14)
movq %rax, 0x48(%r14)
movq 0x18(%rbx), %rsi
movq 0x58(%r14), %rcx
movq 0x38(%rcx), %rdx
addq $0x36, %rdx
leaq 0xd8(%r14), %r9
subq $0x8, %rsp
movl $0x3, %r8d
movq %rbx, %rdi
movq %rax, %rcx
pushq $0x0
pushq $0x1
pushq $0x100                  # imm = 0x100
callq 0xf32f2
addq $0x20, %rsp
movl 0x4(%r14), %eax
decl %eax
cmpl $0x2, %eax
jae 0x64a80
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq %rbx, %rdi
callq 0xf241d
testl %eax, %eax
je 0x648dc
movq 0x8(%r14), %rdx
leaq 0xc9b55(%rip), %rsi     # 0x12e518
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
cmpl $0x2, 0x4(%r14)
jne 0x64a9a
movq 0x38(%r14), %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf0faf
movq %rax, %r15
movq %rax, %rdi
callq 0xf0e3f
movq 0x8(%r14), %rax
leaq 0x8(%rsp), %r12
movq %rax, (%r12)
xorl %ebp, %ebp
movq 0x8(%rsp), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0x64a39
cmpq 0x38(%r14), %rbp
jge 0x64a39
movq %r12, %rsi
callq 0xef14d
cmpb $-0x40, %al
jae 0x64a3d
movl $0x1, %r13d
jmp 0x64a4f
xorl %eax, %eax
jmp 0x64a62
andb $0x3f, %al
movzbl %al, %r13d
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef14d
movzbl %al, %esi
movq %r15, %rdi
movq %r13, %rdx
callq 0xf14bd
addq %r13, %rbp
movb $0x1, %al
testb %al, %al
jne 0x64a10
movq %r15, %rdi
callq 0xf0ea4
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x64aca
jmp 0x64ab0
movq 0x50(%r14), %rdx
leaq 0xbf4f9(%rip), %rsi     # 0x123f84
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x648dc
movq 0x18(%rbx), %rdx
movq 0x8(%r14), %rcx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x64aca
xorl %r15d, %r15d
movq %r15, %rdi
callq 0xf0b13
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
jmp 0x648dc