// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r14
leaq 0xbfba0(%rip), %rax     # 0x12416c
movq %rax, 0x50(%r14)
movl $0xf0, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, 0x58(%r14)
movq 0x18(%rbx), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0x104f96
leaq 0x60(%r14), %rdx
movb $-0x37, 0x60(%r14)
movq 0x58(%r14), %rsi
movq %rbx, %rdi
callq 0x105864
cmpb $-0x37, 0x61(%r14)
jne 0x64707
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 0xeebeb
movq %rax, 0x8(%r14)
movl $0x10, %esi
movq %rax, %rdi
callq 0xe9cb4
movq 0x58(%r14), %rcx
addq 0x38(%rcx), %rax
movq %rax, 0x8(%r14)
leaq 0xbfb27(%rip), %rsi     # 0x12417b
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x20(%r14)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x28(%r14)
movq 0x20(%r14), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xe8f88
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x40(%r14)
leaq 0xbfae1(%rip), %rsi     # 0x124188
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x10(%r14)
leaq 0xbfad2(%rip), %rsi     # 0x12419c
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movl %eax, 0x4(%r14)
leaq 0xb4a3c(%rip), %rsi     # 0x119129
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x10(%r14), %rcx
leaq -0x9(%rcx), %rax
cmpq $-0x8, %rax
jae 0x64746
movb $0x1, (%r14)
movq 0x50(%r14), %rdx
leaq 0xbfaaa(%rip), %rsi     # 0x1241c0
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq 0x58(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x5, %rcx
movl $0x8, %edx
cmovbq %rcx, %rdx
movl $0x1, %eax
movq %rcx, %rsi
cmovaeq %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
movq %r14, %r9
addq $0xd8, %r9
subq $0x8, %rsp
movl $0x3, %r8d
movq %rbx, %rdi
movq %rax, %rcx
pushq $0x0
pushq $0x1
pushq $0x100                  # imm = 0x100
callq 0xf2cb1
addq $0x20, %rsp
movl 0x4(%r14), %eax
decl %eax
cmpl $0x2, %eax
jae 0x648a8
movq 0x20(%r14), %rsi
movq 0x28(%r14), %rdx
movq %rbx, %rdi
callq 0xf1db1
testl %eax, %eax
je 0x648ea
movq 0x8(%r14), %rdx
leaq 0xca026(%rip), %rsi     # 0x12e810
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
cmpl $0x2, 0x4(%r14)
jne 0x648bf
movq 0x38(%r14), %rsi
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf09c5
movq %rax, %r15
movq %rax, %rdi
callq 0xf0855
movq 0x8(%r14), %rax
movq %rax, 0x8(%rsp)
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0x6488e
xorl %ebp, %ebp
leaq 0x8(%rsp), %r12
cmpq 0x38(%r14), %rbp
jge 0x6488e
movq %r12, %rsi
callq 0xeeb4b
cmpb $-0x40, %al
jae 0x6485c
movl $0x1, %r13d
jmp 0x6486e
andb $0x3f, %al
movzbl %al, %r13d
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeeb4b
movzbl %al, %esi
movq %r15, %rdi
movq %r13, %rdx
callq 0xf0eba
addq %r13, %rbp
movq 0x8(%rsp), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jl 0x64842
movq %r15, %rdi
callq 0xf08ba
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x648f9
jmp 0x648d5
movq 0x50(%r14), %rdx
leaq 0xbf8f3(%rip), %rsi     # 0x1241a6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x648ea
movq 0x18(%rbx), %rdx
movq 0x8(%r14), %rcx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x648f9
xorl %r15d, %r15d
movq %r15, %rdi
callq 0xf052b
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpb $0x0, (%r14)
jne 0x6470b
jmp 0x64720