// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movl $0x4d8, %esi            # imm = 0x4D8
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
leaq 0x11f725(%rip), %rcx    # 0x1afa5a
movq %rcx, 0x50(%rax)
movq -0x8(%rbp), %rdi
movl $0xf0, %esi
callq 0x15b780
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x58(%rax)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rdx
callq 0x188930
movq -0x18(%rbp), %rax
movb $-0x37, 0x60(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rsi
movq -0x18(%rbp), %rdx
addq $0x60, %rdx
callq 0x189890
movq -0x18(%rbp), %rax
movzbl 0x61(%rax), %eax
cmpl $0xc9, %eax
je 0x903a7
movq -0x18(%rbp), %rax
movb $0x1, (%rax)
jmp 0x90673
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq 0x38(%rax), %rax
addq $0x9, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x10, %esi
callq 0x15ce90
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq 0x38(%rax), %rcx
movq -0x18(%rbp), %rax
addq 0x8(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
leaq 0x11f646(%rip), %rsi    # 0x1afa69
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rax
movl %eax, %edx
leaq 0x11f5ca(%rip), %rsi    # 0x1afa76
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %edx
leaq 0x11f5a6(%rip), %rsi    # 0x1afa8a
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x20(%rbp), %rsi
callq 0x1652c0
movl %eax, %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x4(%rax), %edx
leaq 0x1148b2(%rip), %rsi    # 0x1a4dc9
movb $0x0, %al
callq 0x15b0d0
movq -0x18(%rbp), %rax
cmpq $0x1, 0x10(%rax)
jl 0x90534
movq -0x18(%rbp), %rax
cmpq $0x8, 0x10(%rax)
jle 0x90540
movq -0x18(%rbp), %rax
movb $0x1, (%rax)
jmp 0x90673
movq -0x18(%rbp), %rax
cmpq $0x5, 0x10(%rax)
jl 0x90565
movq -0x18(%rbp), %rax
movq $0x8, 0x18(%rax)
movq -0x18(%rbp), %rax
movq $0x1, 0x30(%rax)
jmp 0x90585
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x18(%rbp), %rax
movq 0x40(%rax), %rcx
movq -0x18(%rbp), %rax
imulq 0x30(%rax), %rcx
movq -0x18(%rbp), %rax
imulq 0x28(%rax), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rax
movq 0x38(%rax), %rdx
addq $0x36, %rdx
movq -0x18(%rbp), %rax
movq 0x48(%rax), %rcx
movq -0x18(%rbp), %r9
addq $0xd8, %r9
movl $0x3, %r8d
xorl %eax, %eax
movq $0x100, (%rsp)          # imm = 0x100
movl $0x1, 0x8(%rsp)
movl $0x0, 0x10(%rsp)
callq 0x16c160
movq -0x18(%rbp), %rax
cmpl $0x1, 0x4(%rax)
je 0x90646
movq -0x18(%rbp), %rax
cmpl $0x2, 0x4(%rax)
je 0x90646
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdx
leaq 0x11f457(%rip), %rsi    # 0x1afa94
movb $0x0, %al
callq 0x15bfb0
jmp 0x90673
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x90666
jmp 0x90673
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x90e20
cmpq $0x0, -0x18(%rbp)
je 0x906bb
movq -0x18(%rbp), %rax
cmpb $0x0, (%rax)
je 0x9069d
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x50(%rax), %rdx
leaq 0x11f418(%rip), %rsi    # 0x1afaae
movb $0x0, %al
callq 0x15bfb0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x58(%rax), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)