// File: /workspace/github_c_makefile_star2_O3/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 $0x18, %rsp
movq %rdi, %rbx
movl $0x404, %esi            # imm = 0x404
callq 0xe8b2b
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
movl $0x9, %edx
callq 0xeb220
movl %eax, (%r14)
leaq 0x10(%rsp), %r15
movq $0x4, (%r15)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeeb4b
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeeb4b
movzbl %bpl, %edx
movzbl %al, %ecx
leaq 0xbfe24(%rip), %rsi     # 0x124130
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
callq 0xf6410
movq %rax, %r15
movq 0x18(%rbx), %rdi
movl (%r14), %r9d
movl $0x6, %esi
movl $0x7f, %edx
movq %rax, %rcx
movl $0x1, %r8d
callq 0xefbd7
cmpq $0x0, 0x10(%r15)
jle 0x64363
movq %r15, %rdi
callq 0xf70f6
leaq 0xbfdf2(%rip), %rsi     # 0x124148
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq %r15, %rdi
callq 0xf6423
movq 0x18(%rbx), %rdi
movl (%r14), %r9d
movl $0x87, %esi
movl $0x7f, %edx
movq %r15, %rcx
movl $0x1, %r8d
callq 0xefbd7
cmpq $0x0, 0x10(%r15)
jle 0x643ad
movq %r15, %rdi
callq 0xf70f6
leaq 0xbfdb7(%rip), %rsi     # 0x124157
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq $0x108, 0x10(%rsp)      # imm = 0x108
leaq 0xca453(%rip), %rsi     # 0x12e810
movl $0x108, %edx            # imm = 0x108
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
testb %bpl, %bpl
je 0x6443c
xorl %r13d, %r13d
movl %r13d, %esi
shrl $0x5, %esi
movl $0x3, %edi
callq 0xec741
movl %eax, %ebp
movl %r13d, %esi
shrl $0x2, %esi
andl $0x7, %esi
movl $0x3, %edi
callq 0xec741
movl %eax, %r12d
movl %r13d, %esi
andl $0x3, %esi
movl $0x2, %edi
callq 0xec741
movzbl %bpl, %ecx
shll $0x10, %ecx
movzbl %r12b, %edx
shll $0x8, %edx
orl %ecx, %edx
movzbl %al, %eax
orl %edx, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, 0x4(%r14,%r13,4)
incq %r13
cmpq $0x100, %r13            # imm = 0x100
jne 0x643d4
jmp 0x644d8
xorl %ecx, %ecx
movabsq $-0x5555555555555555, %rsi # imm = 0xAAAAAAAAAAAAAAAB
leaq 0xbfde1(%rip), %rdi     # 0x124230
leaq 0xbfde0(%rip), %r8      # 0x124236
movl %ecx, %eax
shrb %al
movzbl %al, %eax
imull $0x31, %eax, %r9d
shrl $0xa, %r9d
imull $-0x2a, %r9d, %r10d
movq %rcx, %rax
mulq %rsi
shrq %rdx
andq $-0x2, %rdx
leaq (%rdx,%rdx,2), %rax
movq %rdi, %rdx
subq %rax, %rdx
movzbl (%rdx,%rcx), %eax
shll $0x10, %eax
addl %ecx, %r10d
movzbl %r10b, %edx
imull $0xab, %edx, %edx
shrl $0xa, %edx
movzbl (%rdx,%r8), %edx
shll $0x8, %edx
orl %eax, %edx
movzbl (%r9,%rdi), %eax
orl %edx, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, 0x4(%r14,%rcx,4)
incq %rcx
cmpq $0xfb, %rcx
jne 0x64456
movaps 0xbfd7b(%rip), %xmm0    # 0x124240
movups %xmm0, 0x3f0(%r14)
movl $0xffffffff, 0x400(%r14) # imm = 0xFFFFFFFF
movl $0x60, %esi
movl $0x60, %edx
movq %rbx, %rdi
movl $0x3, %ecx
callq 0xf266a
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq 0x10(%rsp), %rsi
movq %r14, %r8
addq $0x4, %r8
movl $0x0, (%rsp)
movl $0x8, %edx
movl $0x60, %ecx
movq %rax, %r9
callq 0xf2f79
movq %r12, %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0xf22dc
movq %r12, %rdi
callq 0xf228d
movq %r15, %rdi
callq 0xf6753
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq