// File: /workspace/github_c_makefile_star2_O3/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 0xe8a1c
movl $0x470, %esi            # imm = 0x470
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r15
movq %rax, %r14
leaq 0xcf87b(%rip), %rsi     # 0x11f656
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xeeb67
movq %rax, %r12
leaq 0xe49c0(%rip), %rsi     # 0x1347c9
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0x2, %esi
callq 0xeeb67
movq %rax, 0x60(%r15)
leaq (%rax,%rax), %rcx
movq %rcx, 0x68(%r15)
leaq 0xcf838(%rip), %rsi     # 0x11f66f
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xeeb67
movq %rax, 0x20(%r15)
leaq 0xeb653(%rip), %rsi     # 0x13b4af
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
cmpq $0xa, 0x60(%r15)
jl 0x4fe89
movq 0x18(%rbx), %rdi
movl $0x10, %esi
callq 0xeed0d
cmpl $0x58494d47, %eax       # imm = 0x58494D47
jne 0x4fe89
movb $0x1, 0x1(%r14)
movq 0x18(%rbx), %rdi
movl $0x6, %esi
callq 0xeeb67
movq %rax, 0x28(%r14)
leaq 0xcf7ee(%rip), %rsi     # 0x11f690
xorl %r13d, %r13d
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0x8, %esi
callq 0xeeb67
movq %rax, 0x40(%r14)
movq 0x18(%rbx), %rdi
movl $0xa, %esi
callq 0xeeb67
movq %rax, 0x48(%r14)
movq 0x40(%r14), %rdx
movq %rax, %xmm0
movq %rdx, %xmm1
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movdqa 0xcf711(%rip), %xmm0    # 0x11f600
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 0xc7852(%rip), %xmm3    # 0x117770
pand %xmm1, %xmm3
por 0xc7856(%rip), %xmm3    # 0x117780
por %xmm0, %xmm2
psrlq $0x20, %xmm1
por 0xc7855(%rip), %xmm1    # 0x117790
subpd 0xc785d(%rip), %xmm1    # 0x1177a0
addpd %xmm3, %xmm1
movapd 0xcf6c1(%rip), %xmm0    # 0x11f610
divpd %xmm1, %xmm0
andpd %xmm2, %xmm0
movupd %xmm0, 0x50(%r14)
pshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
leaq 0xcf73b(%rip), %rsi     # 0x11f6a4
movq %rbx, %rdi
movl %eax, %ecx
movb $0x2, %al
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0xc, %esi
callq 0xeeb67
movq %rax, 0x8(%r14)
movl $0x8, %esi
movq %rax, %rdi
callq 0xe9cb4
movq %rax, 0x18(%r14)
movq 0x18(%rbx), %rdi
movl $0xe, %esi
callq 0xeeb67
movq %rax, 0x10(%r14)
movq 0x8(%r14), %rsi
movq %rbx, %rdi
movq %rax, %rdx
callq 0xe8f88
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpq $0x9, 0x60(%r14)
jl 0x4ffde
movq 0x18(%rbx), %rdi
movl $0x10, %esi
callq 0xeeb67
movq %rax, %r13
cmpq $0x3, %r12
jl 0x4fffd
leaq 0xcf6e5(%rip), %rsi     # 0x11f6d0
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9295
jmp 0x50394
cmpb $0x0, 0x1(%r14)
je 0x500a6
leaq 0xcf6f3(%rip), %rsi     # 0x11f702
movq %rbx, %rdi
callq 0xeb125
movb $0x1, %bpl
cmpq $0x11, 0x28(%r14)
jl 0x50032
leaq 0xc8f67(%rip), %rsi     # 0x118f8f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
testb %bpl, %bpl
jne 0x50048
leaq 0xcf75d(%rip), %rsi     # 0x11f79b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq 0x8(%r14), %rsi
movq 0x10(%r14), %rdx
movq %rbx, %rdi
callq 0xf1db1
testl %eax, %eax
je 0x50394
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 0xcf76c(%rip), %rsi     # 0x11f7f3
movq %rbx, %rdi
callq 0xeb125
cmpb $0x0, 0x1(%r14)
je 0x500dc
movq %rbx, %rdi
movq %r14, %rsi
callq 0x505ee
jmp 0x50394
movq 0x60(%r14), %rax
cmpq $0x8, %rax
je 0x50103
cmpq $0x9, %rax
je 0x500f4
cmpq $0x19, %rax
jne 0x50125
movq 0x28(%r14), %rax
xorq $0x2, %rax
xorq $0x80, %r13
orq %rax, %r13
jne 0x50144
leaq 0xcf64d(%rip), %rsi     # 0x11f724
jmp 0x5000f
movq 0x60(%r14), %rax
movb $0x1, %bpl
cmpq $0x9, %rax
je 0x5015a
cmpq $0x19, %rax
je 0x50096
jmp 0x5017d
movq 0x20(%r14), %rax
decq %rax
cmpq $0x8, %rax
jb 0x5011e
jmp 0x50144
movq 0x20(%r14), %rax
movb $0x1, %bpl
cmpq $0x1, %rax
je 0x5001a
addq $-0x2, %rax
cmpq $0x7, %rax
jae 0x50144
xorl %ebp, %ebp
jmp 0x5001a
xorq $0x1b, %rax
xorq $0x5354, %r13           # imm = 0x5354
orq %rax, %r13
jne 0x50144
leaq 0xcf610(%rip), %rsi     # 0x11f74c
movq %rbx, %rdi
callq 0xeb125
leaq 0xcf623(%rip), %rsi     # 0x11f76e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x50394
movq 0x20(%r14), %rax
addq $-0x3, %rax
cmpq $0x1, %rax
ja 0x5017d
movq 0x18(%rbx), %rdi
movl $0x10, %esi
callq 0xeeb67
testq %rax, %rax
setne %bpl
movq 0x68(%r14), %rdx
leaq 0xde688(%rip), %rsi     # 0x12e810
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x10(%r14), %rsi
imulq 0x38(%r14), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %r12
movq 0x68(%r14), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x50a1e
cmpb $0x0, (%r14)
je 0x501cb
xorl %r15d, %r15d
jmp 0x50379
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 0xf26a5
movq %rax, %r13
movq %rbx, %rdi
callq 0xea504
movq %rax, 0x10(%rsp)
cmpq $0x2, 0x40(%r14)
jl 0x50226
cmpq $0x2, 0x48(%r14)
jl 0x50226
movq 0x10(%rsp), %rax
movl $0x2, 0x78(%rax)
movupd 0x50(%r14), %xmm0
movupd %xmm0, 0x80(%rax)
movq 0x20(%r14), %rcx
cmpq $0x1, %rcx
jne 0x50251
movq 0x30(%r14), %rdx
movq %r12, %rdi
xorl %esi, %esi
movq %r13, %rcx
movl $0x1, %r8d
callq 0xf2a40
movl $0xa, %edx
jmp 0x50369
cmpq $0x3, %rcx
setne %al
orb %bpl, %al
je 0x502cd
cmpq $0x4, %rcx
setne %al
orb %al, %bpl
addq $0x70, %r15
testb %bpl, %bpl
je 0x502fd
cmpq $0x8, %rcx
jne 0x50341
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 0x5028b
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 0xe91fc
incq %rbp
cmpq $0x100, %rbp            # imm = 0x100
jne 0x50280
jmp 0x50356
leaq 0xcf762(%rip), %rsi     # 0x11fa36
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movaps 0xcf7ab(%rip), %xmm0    # 0x11fa90
movups %xmm0, 0x80(%r14)
movapd 0xcf78b(%rip), %xmm0    # 0x11fa80
movupd %xmm0, 0x70(%r14)
jmp 0x50356
leaq 0xcf752(%rip), %rsi     # 0x11fa56
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movaps 0xcf7bb(%rip), %xmm0    # 0x11fad0
movups %xmm0, 0x30(%r15)
movaps 0xcf79f(%rip), %xmm0    # 0x11fac0
movups %xmm0, 0x20(%r15)
movaps 0xcf783(%rip), %xmm0    # 0x11fab0
movups %xmm0, 0x10(%r15)
movapd 0xcf766(%rip), %xmm0    # 0x11faa0
movupd %xmm0, (%r15)
jmp 0x50356
movl $0x1, %esi
shlq %cl, %rsi
movq %r15, %rdi
movl $0x1, %edx
callq 0xf3bdb
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x50d92
movl $0x2, %edx
movq %r13, %rdi
movq 0x10(%rsp), %r15
movq %r15, %rsi
callq 0xf22d0
movq %r13, %rdi
callq 0xf228d
movq %rbx, %rdi
movq %r15, %rsi
callq 0xea50e
movq %r12, %rdi
callq 0xf052b
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq