// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/gemras.c
// Function: de_run_gemraster
static void de_run_gemraster(deark *c, de_module_params *mparams)
{
	i64 ver;
	i64 ext_word0 = 0;
	lctx *d = NULL;
	int need_format_warning = 0;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	d = de_malloc(c, sizeof(lctx));

	de_dbg(c, "header (base part) at %d", 0);
	de_dbg_indent(c, 1);
	ver = de_getu16be(0);
	de_dbg(c, "version: %d", (int)ver);
	d->header_size_in_words = de_getu16be(2);
	d->header_size_in_bytes = d->header_size_in_words*2;
	de_dbg(c, "header size: %d words (%d bytes)", (int)d->header_size_in_words,
		(int)d->header_size_in_bytes);
	d->nplanes = de_getu16be(4);
	de_dbg(c, "planes: %d", (int)d->nplanes);

	if(d->header_size_in_words>=10) {
		u32 sig16;

		sig16 = (UI)de_getu32be(16);
		if(sig16==CODE_XIMG) d->is_ximg = 1;
	}

	d->patlen = de_getu16be(6);
	de_dbg(c, "pattern def len: %d", (int)d->patlen);
	d->pixwidth = de_getu16be(8);
	d->pixheight = de_getu16be(10);
	d->dens_x = microns_to_dpi(d->pixwidth);
	d->dens_y = microns_to_dpi(d->pixheight);
	de_dbg(c, "pixel size: %d"DE_CHAR_TIMES"%d microns (%.1f"DE_CHAR_TIMES"%.1f dpi)",
		(int)d->pixwidth, (int)d->pixheight, d->dens_x, d->dens_y);
	d->npwidth = de_getu16be(12);
	d->pdwidth = de_pad_to_n(d->npwidth, 8);
	d->h = de_getu16be(14);
	de_dbg_dimensions(c, d->npwidth, d->h);
	de_dbg_indent(c, -1);

	if(d->header_size_in_words>=9) {
		// This may help to detect the image format.
		ext_word0 = de_getu16be(16);
	}

	if(ver>2) {
		de_err(c, "This version of GEM Raster (%d) is not supported.", (int)ver);
		goto done;
	}

	if(d->is_ximg) {
		de_declare_fmt(c, "GEM VDI Bit Image, XIMG extension");
	}
	else if(d->header_size_in_words==25 && d->patlen==2 && ext_word0==0x0080) {
		de_declare_fmt(c, "GEM VDI Bit Image, Hyperpaint extension");
	}
	else if(d->header_size_in_words==8 && d->nplanes==1) {
		;
	}
	else if(d->header_size_in_words==8 && (d->nplanes>=2 && d->nplanes<=8)) {
		need_format_warning = 1;
	}
	else if(d->header_size_in_words==9 && (d->nplanes>=1 && d->nplanes<=8)) {
		need_format_warning = 1;
	}
	else {
		if(d->header_size_in_words==27 && ext_word0==0x5354) {
			de_declare_fmt(c, "GEM VDI Bit Image, STTT extension");
		}
		de_err(c, "This version of GEM Raster is not supported.");
		goto done;
	}

	if(d->patlen>16) {
		de_err(c, "Bad or unsupported file");
	}

	if(need_format_warning) {
		de_warn(c, "This type of GEM Raster image is not very portable, and might "
			"not be handled correctly.");
	}

	if(!de_good_image_dimensions(c, d->npwidth, d->h)) goto done;

	d->rowspan_per_plane = d->pdwidth/8;
	d->rowspan_total = d->rowspan_per_plane * d->nplanes;

	// If we haven't declared the format yet, do so.
	de_declare_fmt(c, "GEM VDI Bit Image");

	if(d->is_ximg) {
		do_gem_ximg(c, d);
	}
	else if(d->header_size_in_words==25) {
		do_gem_ximg(c, d);
	}
	else {
		do_gem_img(c, d);
	}

done:
	de_dbg_indent_restore(c, saved_indent_level);
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
leaq 0xc(%rsp), %rsi
callq 0xe958c
movl $0x470, %esi            # imm = 0x470
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r15
movq %rax, %r14
leaq 0xcf63b(%rip), %rsi     # 0x11f52e
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xef169
movq %rax, %r12
leaq 0xe44b0(%rip), %rsi     # 0x1343d1
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0x2, %esi
callq 0xef169
movq %rax, 0x60(%r15)
leaq (%rax,%rax), %rcx
movq %rcx, 0x68(%r15)
leaq 0xcf5f8(%rip), %rsi     # 0x11f547
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xef169
movq %rax, 0x20(%r15)
leaq 0xeb183(%rip), %rsi     # 0x13b0f7
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
cmpq $0xa, 0x60(%r15)
jl 0x4ffa1
movq 0x18(%rbx), %rdi
movl $0x10, %esi
callq 0xef32a
cmpl $0x58494d47, %eax       # imm = 0x58494D47
jne 0x4ffa1
movb $0x1, 0x1(%r14)
movq 0x18(%rbx), %rdi
movl $0x6, %esi
callq 0xef169
movq %rax, 0x28(%r14)
leaq 0xcf5ae(%rip), %rsi     # 0x11f568
xorl %r13d, %r13d
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0x8, %esi
callq 0xef169
movq %rax, 0x40(%r14)
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xef169
movq %rax, 0x48(%r14)
movq 0x40(%r14), %rdx
movsd 0xcf4f7(%rip), %xmm2    # 0x11f4f0
xorpd %xmm1, %xmm1
testq %rdx, %rdx
jg 0x50008
xorpd %xmm0, %xmm0
jmp 0x50015
cvtsi2sd %rdx, %xmm3
movapd %xmm2, %xmm0
divsd %xmm3, %xmm0
movsd %xmm0, 0x50(%r14)
testq %rax, %rax
jle 0x50030
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm2
movapd %xmm2, %xmm1
movsd %xmm1, 0x58(%r14)
leaq 0xcf53f(%rip), %rsi     # 0x11f57c
movq %rbx, %rdi
movl %eax, %ecx
movb $0x2, %al
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0xc, %esi
callq 0xef169
movq %rax, 0x8(%r14)
movl $0x8, %esi
movq %rax, %rdi
callq 0xea80b
movq %rax, 0x18(%r14)
movq 0x18(%rbx), %rdi
movl $0xe, %esi
callq 0xef169
movq %rax, 0x10(%r14)
movq 0x8(%r14), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xe9b05
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpq $0x9, 0x60(%r14)
jl 0x500b2
movq 0x18(%rbx), %rdi
movl $0x10, %esi
callq 0xef169
movq %rax, %r13
cmpq $0x3, %r12
jl 0x500d1
leaq 0xcf4e9(%rip), %rsi     # 0x11f5a8
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9df7
jmp 0x5047f
cmpb $0x0, 0x1(%r14)
je 0x5017a
leaq 0xcf4f7(%rip), %rsi     # 0x11f5da
movq %rbx, %rdi
callq 0xeb6d6
movb $0x1, %bpl
cmpq $0x11, 0x28(%r14)
jl 0x50106
leaq 0xc8d73(%rip), %rsi     # 0x118e6f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
testb %bpl, %bpl
jne 0x5011c
leaq 0xcf561(%rip), %rsi     # 0x11f673
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0xf241d
testl %eax, %eax
je 0x5047f
movq 0x18(%r14), %rax
leaq 0x7(%rax), %rcx
testq %rax, %rax
cmovnsq %rax, %rcx
sarq $0x3, %rcx
movq %rcx, 0x30(%r14)
imulq 0x20(%r14), %rcx
movq %rcx, 0x38(%r14)
leaq 0xcf570(%rip), %rsi     # 0x11f6cb
movq %rbx, %rdi
callq 0xeb6d6
cmpb $0x0, 0x1(%r14)
je 0x501e9
movq %rbx, %rdi
movq %r14, %rsi
callq 0x506c2
jmp 0x5047f
movq 0x60(%r14), %rax
cmpq $0x19, %rax
jne 0x501a7
movq 0x28(%r14), %rcx
xorq $0x2, %rcx
movq %r13, %rdx
xorq $0x80, %rdx
orq %rcx, %rdx
jne 0x501a7
leaq 0xcf45a(%rip), %rsi     # 0x11f5fc
jmp 0x500e3
cmpq $0x8, %rax
jne 0x501cf
movb $0x1, %bpl
cmpq $0x1, 0x20(%r14)
je 0x500ee
cmpq $0x8, %rax
jne 0x501cf
movq 0x20(%r14), %rcx
addq $-0x2, %rcx
cmpq $0x7, %rcx
jb 0x501e2
cmpq $0x9, %rax
jne 0x50202
movq 0x20(%r14), %rcx
decq %rcx
cmpq $0x8, %rcx
jae 0x50202
xorl %ebp, %ebp
jmp 0x500ee
movq 0x60(%r14), %rax
movb $0x1, %bpl
cmpq $0x9, %rax
je 0x50237
cmpq $0x19, %rax
je 0x5016a
jmp 0x5025a
xorq $0x1b, %rax
xorq $0x5354, %r13           # imm = 0x5354
orq %rax, %r13
jne 0x50221
leaq 0xcf40b(%rip), %rsi     # 0x11f624
movq %rbx, %rdi
callq 0xeb6d6
leaq 0xcf41e(%rip), %rsi     # 0x11f646
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x5047f
movq 0x20(%r14), %rax
addq $-0x3, %rax
cmpq $0x1, %rax
ja 0x5025a
movq 0x18(%rbx), %rdi
movl $0x10, %esi
callq 0xef169
testq %rax, %rax
setne %bpl
movq 0x68(%r14), %rdx
leaq 0xde2b3(%rip), %rsi     # 0x12e518
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x10(%r14), %rsi
imulq 0x38(%r14), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf0faf
movq %rax, %r12
movq 0x68(%r14), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x50aed
cmpb $0x0, (%r14)
je 0x502a8
xorl %r15d, %r15d
jmp 0x50464
movq 0x18(%r14), %rdx
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rcx
movl %ebp, %eax
xorb $0x1, %al
movzbl %al, %eax
leal 0x1(,%rax,2), %r8d
movq %rbx, %rdi
callq 0xf2ce6
movq %rax, %r13
movq %rbx, %rdi
callq 0xea9a9
movq %rax, 0x10(%rsp)
cmpq $0x2, 0x40(%r14)
jl 0x50311
cmpq $0x2, 0x48(%r14)
jl 0x50311
movq 0x10(%rsp), %rax
movl $0x2, 0x78(%rax)
movsd 0x50(%r14), %xmm0
movsd %xmm0, 0x80(%rax)
movsd 0x58(%r14), %xmm0
movsd %xmm0, 0x88(%rax)
movq 0x20(%r14), %rcx
cmpq $0x1, %rcx
jne 0x5033c
movq 0x30(%r14), %rdx
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rcx
movl $0x1, %r8d
callq 0xf3081
movl $0xa, %edx
jmp 0x50454
cmpq $0x3, %rcx
setne %al
orb %bpl, %al
je 0x503b8
cmpq $0x4, %rcx
setne %al
orb %al, %bpl
addq $0x70, %r15
testb %bpl, %bpl
je 0x503e8
cmpq $0x8, %rcx
jne 0x5042c
xorl %ebp, %ebp
movl %ebp, %eax
notb %al
movl $0x8, %ecx
xorl %edx, %edx
movl %edx, %esi
addb %dl, %sil
movl %eax, %edx
andb $0x1, %dl
orb %sil, %dl
shrb %al
decl %ecx
jne 0x50376
movzbl %dl, %eax
imull $0x10101, %eax, %edx    # imm = 0x10101
orl $0xff000000, %edx       # imm = 0xFF000000
movl %edx, (%r15,%rbp,4)
movq %rbx, %rdi
movq %rbp, %rsi
callq 0xe9d68
incq %rbp
cmpq $0x100, %rbp            # imm = 0x100
jne 0x5036b
jmp 0x50441
leaq 0xcf54f(%rip), %rsi     # 0x11f90e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movaps 0xcf5a0(%rip), %xmm0    # 0x11f970
movups %xmm0, 0x80(%r14)
movapd 0xcf580(%rip), %xmm0    # 0x11f960
movupd %xmm0, 0x70(%r14)
jmp 0x50441
leaq 0xcf53f(%rip), %rsi     # 0x11f92e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movaps 0xcf5b0(%rip), %xmm0    # 0x11f9b0
movups %xmm0, 0x30(%r15)
movaps 0xcf594(%rip), %xmm0    # 0x11f9a0
movups %xmm0, 0x20(%r15)
movaps 0xcf578(%rip), %xmm0    # 0x11f990
movups %xmm0, 0x10(%r15)
movapd 0xcf55b(%rip), %xmm0    # 0x11f980
movupd %xmm0, (%r15)
jmp 0x50441
movl $0x1, %esi
shlq %cl, %rsi
movq %r15, %rdi
movl $0x1, %edx
callq 0xf41f4
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x50e86
movl $0x2, %edx
movq %r13, %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0xf2917
movq %r13, %rdi
callq 0xf28d4
movq %rbx, %rdi
movq %r15, %rsi
callq 0xea9b3
movq %r12, %rdi
callq 0xf0b13
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq