// File: /workspace/github_c_makefile_star2_O2/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 0xdecaf
movq %rax, %r14
leaq 0xb60c3(%rip), %rax     # 0x116f6a
movq %rax, 0x50(%r14)
movl $0xf0, %esi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, 0x58(%r14)
movq 0x18(%rbx), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xf8849
leaq 0x60(%r14), %rdx
movb $-0x37, 0x60(%r14)
movq 0x58(%r14), %rsi
movq %rbx, %rdi
callq 0xf90d3
cmpb $-0x37, 0x61(%r14)
jne 0x60fe0
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 0xe3fc4
movq %rax, 0x8(%r14)
pushq $0x10
popq %rsi
movq %rax, %rdi
callq 0xdfbb6
movq 0x58(%r14), %rcx
addq 0x38(%rcx), %rax
movq %rax, 0x8(%r14)
leaq 0xb604c(%rip), %rsi     # 0x116f79
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
movq %rax, 0x20(%r14)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
movq %rax, 0x28(%r14)
movq 0x20(%r14), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xdf038
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
movq %rax, 0x40(%r14)
leaq 0xb6006(%rip), %rsi     # 0x116f86
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
movq %rax, 0x10(%r14)
leaq 0xb5ff7(%rip), %rsi     # 0x116f9a
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
movl %eax, 0x4(%r14)
leaq 0xab073(%rip), %rsi     # 0x10c039
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x10(%r14), %rcx
leaq -0x9(%rcx), %rax
cmpq $-0x8, %rax
jae 0x6101f
movb $0x1, (%r14)
movq 0x50(%r14), %rdx
leaq 0xb5fcf(%rip), %rsi     # 0x116fbe
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq 0x58(%r14), %rsi
movq %rbx, %rdi
callq 0xded32
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x5, %rcx
pushq $0x8
popq %rax
cmovbq %rcx, %rax
pushq $0x1
popq %rsi
movq %rcx, %rdx
cmovaeq %rsi, %rdx
movq %rax, 0x18(%r14)
movq %rdx, 0x30(%r14)
imulq 0x40(%r14), %rdx
imulq 0x28(%r14), %rdx
movq %rsi, %rax
shlq %cl, %rax
movq %rdx, 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
pushq $0x3
popq %r8
movq %rbx, %rdi
movq %rax, %rcx
pushq $0x0
pushq $0x1
pushq $0x100                  # imm = 0x100
callq 0xe79ca
addq $0x20, %rsp
movl 0x4(%r14), %eax
decl %eax
cmpl $0x2, %eax
jae 0x61150
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq %rbx, %rdi
callq 0xe6be1
testl %eax, %eax
je 0x611b3
movq 0x8(%r14), %rdx
leaq 0xc04e3(%rip), %rsi     # 0x1215a0
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
cmpl $0x2, 0x4(%r14)
jne 0x61167
movq 0x38(%r14), %rsi
pushq $0x1
popq %rdx
movq %rbx, %rdi
callq 0xe59cd
movq %rax, %r15
movq %rax, %rdi
callq 0xe589c
movq 0x8(%r14), %rax
leaq 0x8(%rsp), %r12
movq %rax, (%r12)
xorl %ebp, %ebp
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0x6117f
cmpq 0x38(%r14), %rbp
jge 0x6117f
movq %r12, %rsi
callq 0xe3f2a
pushq $0x1
popq %r13
cmpb $-0x40, %al
jb 0x61138
andb $0x3f, %al
movzbl %al, %r13d
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3f2a
movzbl %al, %esi
movq %r15, %rdi
movq %r13, %rdx
callq 0xe5e40
addq %r13, %rbp
movq 0x8(%rsp), %rax
jmp 0x61106
movq 0x50(%r14), %rdx
leaq 0xb5e49(%rip), %rsi     # 0x116fa4
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x611b3
movq 0x18(%rbx), %rdx
movq 0x8(%r14), %rcx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x61205
xorl %r15d, %r15d
jmp 0x61197
movq %r15, %rdi
callq 0xe58e8
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x61205
movq %r15, %rdi
callq 0xe5591
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
testq %r14, %r14
je 0x61010
cmpb $0x0, (%r14)
jne 0x60fe4
jmp 0x60ff9