// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/gws.c
// Function: de_run_gws_thn
static void de_run_gws_thn(deark *c, de_module_params *mparams)
{
	struct gws_thn_ctx *d = NULL;
	de_bitmap *img = NULL;
	u8 v1, v2;
	i64 w, h;
	i64 pos;
	de_ucstring *s = NULL;

	d = de_malloc(c, sizeof(struct gws_thn_ctx));

	// This code is based on reverse engineering, and may be incorrect.
	d->input_encoding = de_get_input_encoding(c, NULL, DE_ENCODING_WINDOWS1252);
	pos = 4;
	v1 = de_getbyte_p(&pos);
	v2 = de_getbyte_p(&pos);
	de_dbg(c, "version?: 0x%02x 0x%02x", (UI)v1, (UI)v2);

	s = ucstring_create(c);
	// For the text fields, the field size appears to be 129, but the software
	// only properly supports up to 127 non-NUL bytes.
	dbuf_read_to_ucstring(c->infile, 6, 127, s, DE_CONVFLAG_STOP_AT_NUL, d->input_encoding);
	if(s->len>0) de_dbg(c, "comments: \"%s\"", ucstring_getpsz_d(s));
	ucstring_empty(s);
	dbuf_read_to_ucstring(c->infile, 135, 127, s, DE_CONVFLAG_STOP_AT_NUL, d->input_encoding);
	if(s->len>0) de_dbg(c, "key words: \"%s\"", ucstring_getpsz_d(s));

	pos = 264;
	de_dbg(c, "image at %"I64_FMT, pos);
	w = 96;
	h = 96;

	// Set up the palette. There are two possible fixed palettes.
	if(v1==0) {
		gwsthn_makepal_orig(c, d);
	}
	else {
		gwsthn_makepal_new(c, d);
	}

	img = de_bitmap_create(c, w, h, 3);
	de_convert_image_paletted(c->infile, pos, 8, w, d->pal, img, 0);
	de_bitmap_write_to_file(img, NULL, DE_CREATEFLAG_FLIP_IMAGE);

	de_bitmap_destroy(img);
	ucstring_destroy(s);
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movl $0x404, %esi            # imm = 0x404
callq 0xdecaf
movq %rax, %r14
pushq $0x9
popq %rdx
movq %rbx, %rdi
xorl %esi, %esi
callq 0xe08d2
movl %eax, (%r14)
leaq 0x10(%rsp), %r15
movq $0x4, (%r15)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3f2a
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3f2a
movzbl %bpl, %edx
movzbl %al, %ecx
leaq 0xb6343(%rip), %rsi     # 0x116f2e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
callq 0xeac74
movq %rax, %r15
movq 0x18(%rbx), %rdi
movl (%r14), %r9d
pushq $0x6
popq %r12
pushq $0x7f
popq %rdx
pushq $0x1
popq %r8
movq %r12, %rsi
movq %rax, %rcx
callq 0xe4c9a
cmpq $0x0, 0x10(%r15)
jle 0x60c40
movq %r15, %rdi
callq 0xeb877
leaq 0xb6313(%rip), %rsi     # 0x116f46
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq %r15, %rdi
callq 0xeac85
movq 0x18(%rbx), %rdi
movl (%r14), %r9d
pushq $0x7f
popq %rdx
pushq $0x1
popq %r8
movl $0x87, %esi
movq %r15, %rcx
callq 0xe4c9a
cmpq $0x0, 0x10(%r15)
jle 0x60c86
movq %r15, %rdi
callq 0xeb877
leaq 0xb62dc(%rip), %rsi     # 0x116f55
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq $0x108, 0x10(%rsp)      # imm = 0x108
leaq 0xc090a(%rip), %rsi     # 0x1215a0
movl $0x108, %edx            # imm = 0x108
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
testb %bpl, %bpl
movq %rbx, 0x20(%rsp)
movq %r15, 0x18(%rsp)
je 0x60d1e
xorl %ebx, %ebx
pushq $0x3
popq %r12
pushq $0x2
popq %r13
cmpq $0x100, %rbx            # imm = 0x100
je 0x60dbb
movl %ebx, %esi
shrl $0x5, %esi
movq %r12, %rdi
callq 0xe1ca5
movl %eax, %ebp
movl %ebx, %esi
shrl $0x2, %esi
andl $0x7, %esi
movq %r12, %rdi
callq 0xe1ca5
movl %eax, %r15d
movl %ebx, %esi
andl $0x3, %esi
movq %r13, %rdi
callq 0xe1ca5
movzbl %bpl, %ecx
shll $0x10, %ecx
movzbl %r15b, %edx
shll $0x8, %edx
orl %ecx, %edx
movzbl %al, %eax
orl %edx, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, 0x4(%r14,%rbx,4)
incq %rbx
jmp 0x60cbe
xorl %ecx, %ecx
movb $0x2a, %dil
leaq 0xb6306(%rip), %r8      # 0x117030
movb $0x6, %r9b
leaq 0xb6302(%rip), %r10     # 0x117036
movzbl %cl, %eax
divb %dil
movl %eax, %esi
movq %rcx, %rax
xorl %edx, %edx
divq %r12
cmpq $0xfb, %rcx
je 0x60d9b
movzbl %sil, %edx
imull $-0x2a, %edx, %edx
movzbl %dl, %edx
imulq $-0x6, %rax, %rax
addq %rcx, %rax
movzbl (%r8,%rax), %esi
shll $0x10, %esi
addl %ecx, %edx
movzbl %dl, %eax
divb %r9b
movzbl %al, %eax
movzbl (%rax,%r10), %edx
shll $0x8, %edx
movzbl %cl, %eax
divb %dil
movzbl %al, %eax
movzbl (%rax,%r8), %eax
orl %esi, %edx
orl %eax, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
movl %edx, 0x4(%r14,%rcx,4)
incq %rcx
jmp 0x60d34
xorl %eax, %eax
leaq 0xb629c(%rip), %rcx     # 0x117040
cmpq $0x14, %rax
je 0x60dbb
movl (%rax,%rcx), %edx
movl %edx, 0x3f0(%r14,%rax)
addq $0x4, %rax
jmp 0x60da4
pushq $0x60
popq %r15
pushq $0x3
popq %rcx
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %r15, %rdx
callq 0xe7437
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq 0x10(%rsp), %rsi
movq %r14, %r8
addq $0x4, %r8
andl $0x0, (%rsp)
pushq $0x8
popq %rdx
movq %r15, %rcx
movq %rax, %r9
callq 0xe7cbc
pushq $0x4
popq %rdx
movq %r12, %rdi
xorl %esi, %esi
callq 0xe70b7
movq %r12, %rdi
callq 0xe7084
movq 0x18(%rsp), %rdi
callq 0xeaf3c
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq