// File: /workspace/github_c_makefile_star2_O1/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 0xe969b
movq %rax, %r14
movq %rbx, %rdi
xorl %esi, %esi
movl $0x9, %edx
callq 0xeb7d1
movl %eax, (%r14)
leaq 0x10(%rsp), %r15
movq $0x4, (%r15)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef14d
movl %eax, %ebp
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef14d
movzbl %bpl, %edx
movzbl %al, %ecx
leaq 0xbfa36(%rip), %rsi     # 0x123f0e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
callq 0xf6870
movq %rax, %r15
movq 0x18(%rbx), %rdi
movl (%r14), %r9d
movl $0x6, %esi
movl $0x7f, %edx
movq %rax, %rcx
movl $0x1, %r8d
callq 0xf01b0
cmpq $0x0, 0x10(%r15)
jle 0x6452f
movq %r15, %rdi
callq 0xf7557
leaq 0xbfa04(%rip), %rsi     # 0x123f26
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq %r15, %rdi
callq 0xf6883
movq 0x18(%rbx), %rdi
movl (%r14), %r9d
movl $0x87, %esi
movl $0x7f, %edx
movq %r15, %rcx
movl $0x1, %r8d
callq 0xf01b0
cmpq $0x0, 0x10(%r15)
jle 0x64579
movq %r15, %rdi
callq 0xf7557
leaq 0xbf9c9(%rip), %rsi     # 0x123f35
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq $0x108, 0x10(%rsp)      # imm = 0x108
leaq 0xc9f8f(%rip), %rsi     # 0x12e518
movl $0x108, %edx            # imm = 0x108
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
testb %bpl, %bpl
je 0x64608
xorl %r13d, %r13d
movl %r13d, %esi
shrl $0x5, %esi
movl $0x3, %edi
callq 0xecd9a
movl %eax, %ebp
movl %r13d, %esi
shrl $0x2, %esi
andl $0x7, %esi
movl $0x3, %edi
callq 0xecd9a
movl %eax, %r12d
movl %r13d, %esi
andl $0x3, %esi
movl $0x2, %edi
callq 0xecd9a
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 0x645a0
jmp 0x646a5
xorl %ecx, %ecx
movabsq $-0x5555555555555555, %rsi # imm = 0xAAAAAAAAAAAAAAAB
leaq 0xbf9f5(%rip), %rdi     # 0x124010
movl $0xaaaaaaab, %r8d       # imm = 0xAAAAAAAB
leaq 0xbf9ee(%rip), %r9      # 0x124016
movl %ecx, %eax
shrl %eax
imulq $0x30c30c31, %rax, %r10 # imm = 0x30C30C31
shrq $0x22, %r10
imull $-0x2a, %r10d, %r11d
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, %r11d
imulq %r8, %r11
shrq $0x22, %r11
movzbl (%r11,%r9), %edx
shll $0x8, %edx
orl %eax, %edx
movzbl (%r10,%rdi), %eax
orl %edx, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, 0x4(%r14,%rcx,4)
incq %rcx
cmpq $0xfb, %rcx
jne 0x64628
movaps 0xbf98e(%rip), %xmm0    # 0x124020
movups %xmm0, 0x3f0(%r14)
movl $0xffffffff, 0x400(%r14) # imm = 0xFFFFFFFF
movl $0x60, %esi
movl $0x60, %edx
movq %rbx, %rdi
movl $0x3, %ecx
callq 0xf2cab
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 0xf35b8
movq %r12, %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0xf2923
movq %r12, %rdi
callq 0xf28d4
movq %r15, %rdi
callq 0xf6bb0
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq