// File: /workspace/github_c_makefile_star2_O2/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 0x14(%rsp), %rsi
callq 0xdebae
movl $0x470, %esi            # imm = 0x470
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r15
movq %rax, %r14
leaq 0xc4ecf(%rip), %rsi     # 0x112586
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xe3f46
movq %rax, %r13
leaq 0xd9e0e(%rip), %rsi     # 0x1274f1
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
pushq $0x2
popq %rsi
callq 0xe3f46
movq %rax, 0x60(%r15)
leaq (%rax,%rax), %rcx
movq %rcx, 0x68(%r15)
leaq 0xc4e90(%rip), %rsi     # 0x11259f
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
pushq $0x4
popq %rsi
callq 0xe3f46
movq %rax, 0x20(%r15)
leaq 0xe0a8d(%rip), %rsi     # 0x12e1bf
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
cmpq $0xa, 0x60(%r15)
jl 0x4d75d
movq 0x18(%rbx), %rdi
pushq $0x10
popq %rsi
callq 0xe404f
cmpl $0x58494d47, %eax       # imm = 0x58494D47
jne 0x4d75d
movb $0x1, 0x1(%r14)
movq 0x18(%rbx), %rdi
pushq $0x6
popq %rsi
callq 0xe3f46
movq %rax, 0x28(%r14)
leaq 0xc4e4c(%rip), %rsi     # 0x1125c0
xorl %ebp, %ebp
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
pushq $0x8
popq %r12
movq %r12, %rsi
callq 0xe3f46
movq %rax, 0x40(%r14)
movq 0x18(%rbx), %rdi
pushq $0xa
popq %rsi
callq 0xe3f46
movq %rax, 0x48(%r14)
movq 0x40(%r14), %rdx
movq %rax, %xmm0
movq %rdx, %xmm1
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movdqa 0xc4d70(%rip), %xmm0    # 0x112530
movdqa %xmm1, %xmm2
pxor %xmm0, %xmm2
movdqa %xmm2, %xmm3
pcmpgtd %xmm0, %xmm3
pshufd $0xa0, %xmm3, %xmm4     # xmm4 = xmm3[0,0,2,2]
pcmpeqd %xmm0, %xmm2
pshufd $0xf5, %xmm2, %xmm0     # xmm0 = xmm2[1,1,3,3]
pand %xmm4, %xmm0
pshufd $0xf5, %xmm3, %xmm2     # xmm2 = xmm3[1,1,3,3]
movdqa 0xbcef1(%rip), %xmm3    # 0x10a6e0
pand %xmm1, %xmm3
por 0xbcef5(%rip), %xmm3    # 0x10a6f0
por %xmm0, %xmm2
psrlq $0x20, %xmm1
por 0xbcef4(%rip), %xmm1    # 0x10a700
subpd 0xbcefc(%rip), %xmm1    # 0x10a710
addpd %xmm3, %xmm1
movapd 0xc4d20(%rip), %xmm0    # 0x112540
divpd %xmm1, %xmm0
andpd %xmm2, %xmm0
movupd %xmm0, 0x50(%r14)
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
leaq 0xc4d9a(%rip), %rsi     # 0x1125d4
movq %rbx, %rdi
movl %eax, %ecx
movb $0x2, %al
callq 0xde896
movq 0x18(%rbx), %rdi
pushq $0xc
popq %rsi
callq 0xe3f46
movq %rax, 0x8(%r14)
movq %rax, %rdi
movq %r12, %rsi
callq 0xdfbb6
movq %rax, 0x18(%r14)
movq 0x18(%rbx), %rdi
pushq $0xe
popq %rsi
callq 0xe3f46
movq %rax, 0x10(%r14)
movq 0x8(%r14), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xdf038
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
cmpq $0x9, 0x60(%r14)
jl 0x4d8a5
movq 0x18(%rbx), %rdi
pushq $0x10
popq %rsi
callq 0xe3f46
movq %rax, %rbp
cmpq $0x3, %r13
jl 0x4d8c4
leaq 0xc4d4e(%rip), %rsi     # 0x112600
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xdf2a5
jmp 0x4dc0c
cmpb $0x0, 0x1(%r14)
je 0x4d963
leaq 0xc4d5c(%rip), %rsi     # 0x112632
movq %rbx, %rdi
callq 0xe07f7
movb $0x1, %bpl
cmpq $0x11, 0x28(%r14)
jl 0x4d8f9
leaq 0xbe5b0(%rip), %rsi     # 0x10be9f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
testb %bpl, %bpl
jne 0x4d90f
leaq 0xc4dc6(%rip), %rsi     # 0x1126cb
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0xe6be1
testl %eax, %eax
je 0x4dc0c
movq 0x18(%r14), %rax
cqto
idivq %r12
movq %rax, 0x30(%r14)
imulq 0x20(%r14), %rax
movq %rax, 0x38(%r14)
leaq 0xc4ddf(%rip), %rsi     # 0x112723
movq %rbx, %rdi
callq 0xe07f7
cmpb $0x0, 0x1(%r14)
je 0x4d999
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4de41
jmp 0x4dc0c
movq 0x60(%r14), %rax
cmpq $0x8, %rax
je 0x4d9c0
cmpq $0x9, %rax
je 0x4d9b1
cmpq $0x19, %rax
jne 0x4d9e2
movq 0x28(%r14), %rax
xorq $0x2, %rax
xorq $0x80, %rbp
orq %rax, %rbp
jne 0x4da01
leaq 0xc4cc0(%rip), %rsi     # 0x112654
jmp 0x4d8d6
movq 0x60(%r14), %rax
movb $0x1, %r13b
cmpq $0x9, %rax
je 0x4da17
cmpq $0x19, %rax
je 0x4d953
jmp 0x4da38
movq 0x20(%r14), %rax
decq %rax
cmpq $0x8, %rax
jb 0x4d9db
jmp 0x4da01
movq 0x20(%r14), %rax
movb $0x1, %bpl
cmpq $0x1, %rax
je 0x4d8e1
addq $-0x2, %rax
cmpq $0x7, %rax
jae 0x4da01
xorl %ebp, %ebp
jmp 0x4d8e1
xorq $0x1b, %rax
xorq $0x5354, %rbp           # imm = 0x5354
orq %rax, %rbp
jne 0x4da01
leaq 0xc4c83(%rip), %rsi     # 0x11267c
movq %rbx, %rdi
callq 0xe07f7
leaq 0xc4c96(%rip), %rsi     # 0x11269e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x4dc0c
movq 0x20(%r14), %rax
addq $-0x3, %rax
cmpq $0x1, %rax
ja 0x4da38
movq 0x18(%rbx), %rdi
pushq $0x10
popq %rsi
callq 0xe3f46
testq %rax, %rax
setne %r13b
movq 0x68(%r14), %rdx
leaq 0xd3b5d(%rip), %rsi     # 0x1215a0
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x10(%r14), %rsi
imulq 0x38(%r14), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, %r12
movq 0x68(%r14), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x4e254
cmpb $0x0, (%r14)
je 0x4da85
xorl %r13d, %r13d
xorl %ebp, %ebp
jmp 0x4dbf1
movq 0x18(%r14), %rdx
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rcx
movl %r13d, %eax
xorb $0x1, %al
movzbl %al, %eax
leal 0x1(,%rax,2), %r8d
movq %rbx, %rdi
callq 0xe746f
movq %rax, %rbp
movq %rbx, %rdi
callq 0xdfd1a
movq %r14, %rdi
movq %rax, 0x8(%rsp)
movq %rax, %rsi
callq 0x4e5a7
movq 0x20(%r14), %rcx
cmpq $0x1, %rcx
jne 0x4daf0
movq 0x30(%r14), %rdx
pushq $0x1
popq %r8
movq %r12, %rdi
xorl %esi, %esi
movq %rbp, %rcx
callq 0xe77b7
pushq $0xa
popq %rdx
movq 0x8(%rsp), %r13
jmp 0x4dbe6
cmpq $0x3, %rcx
setne %al
orb %r13b, %al
je 0x4db6b
cmpq $0x4, %rcx
setne %al
orb %al, %r13b
je 0x4dba0
addq $0x70, %r15
cmpq $0x8, %rcx
jne 0x4dc32
xorl %r13d, %r13d
cmpq $0x100, %r13            # imm = 0x100
je 0x4dc43
movl %r13d, %eax
notb %al
xorl %ecx, %ecx
pushq $0x8
popq %rdx
subl $0x1, %edx
jb 0x4db48
addb %cl, %cl
movl %eax, %esi
andb $0x1, %sil
orb %sil, %cl
shrb %al
jmp 0x4db34
movzbl %cl, %eax
imull $0x10101, %eax, %edx    # imm = 0x10101
orl $0xff000000, %edx       # imm = 0xFF000000
movl %edx, (%r15,%r13,4)
movq %rbx, %rdi
movq %r13, %rsi
callq 0xdf23b
incq %r13
jmp 0x4db1d
leaq 0xc4df4(%rip), %rsi     # 0x112966
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
leaq 0xc4e2a(%rip), %rax     # 0x1129b0
movq 0x8(%rsp), %r13
cmpq $0x20, %r15
je 0x4dbd5
movl (%r15,%rax), %ecx
movl %ecx, 0x70(%r14,%r15)
addq $0x4, %r15
jmp 0x4db8b
leaq 0xc4ddf(%rip), %rsi     # 0x112986
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
leaq 0xc4e15(%rip), %rax     # 0x1129d0
movq 0x8(%rsp), %r13
cmpq $0x40, %r15
je 0x4dbd5
movl (%r15,%rax), %ecx
movl %ecx, 0x70(%r14,%r15)
addq $0x4, %r15
jmp 0x4dbc0
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x4e5c8
pushq $0x2
popq %rdx
movq %rbp, %rdi
movq %r13, %rsi
callq 0xe70ab
movq %rbp, %rdi
callq 0xe7084
movq %rbx, %rdi
movq %r13, %rsi
callq 0xdfd24
movq %r12, %rdi
callq 0xe5591
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rsi
shlq %cl, %rsi
pushq $0x1
popq %rdx
movq %r15, %rdi
callq 0xe87a8
movq %r14, %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x4e5c8
pushq $0x2
jmp 0x4dae5