// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0x470, %esi            # imm = 0x470
callq 0x15b780
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
leaq 0x13a60d(%rip), %rsi    # 0x1ab1ae
xorl %edx, %edx
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1651d0
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0x14f632(%rip), %rsi    # 0x1c0210
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x2, %esi
callq 0x1651d0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x60(%rax)
movq -0x28(%rbp), %rax
movq 0x60(%rax), %rcx
shlq %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x60(%rax), %rax
movl %eax, %edx
movq -0x28(%rbp), %rax
movq 0x68(%rax), %rax
movl %eax, %ecx
leaq 0x13a593(%rip), %rsi    # 0x1ab1c7
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x4, %esi
callq 0x1651d0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %edx
leaq 0x156092(%rip), %rsi    # 0x1c6cff
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rax
cmpq $0xa, 0x60(%rax)
jl 0x70ca7
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x10, %esi
callq 0x165470
movl %eax, -0x34(%rbp)
cmpl $0x58494d47, -0x34(%rbp) # imm = 0x58494D47
jne 0x70ca5
movq -0x28(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x70ca7
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x6, %esi
callq 0x1651d0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %edx
leaq 0x13a50f(%rip), %rsi    # 0x1ab1e8
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x8, %esi
callq 0x1651d0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xa, %esi
callq 0x1651d0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x48(%rax)
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x71290
movq -0x28(%rbp), %rax
movsd %xmm0, 0x50(%rax)
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rdi
callq 0x71290
movq -0x28(%rbp), %rax
movsd %xmm0, 0x58(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x40(%rax), %rax
movl %eax, %edx
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
movl %eax, %ecx
movq -0x28(%rbp), %rax
movsd 0x50(%rax), %xmm0
movq -0x28(%rbp), %rax
movsd 0x58(%rax), %xmm1
leaq 0x13a485(%rip), %rsi    # 0x1ab1fc
movb $0x2, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xc, %esi
callq 0x1651d0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x8, %esi
callq 0x15ce90
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xe, %esi
callq 0x1651d0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x28(%rbp), %rax
cmpq $0x9, 0x60(%rax)
jl 0x70e1d
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x10, %esi
callq 0x1651d0
movq %rax, -0x20(%rbp)
cmpq $0x2, -0x18(%rbp)
jle 0x70e41
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0x13a3f3(%rip), %rsi    # 0x1ab228
movb $0x0, %al
callq 0x15bfb0
jmp 0x7102a
movq -0x28(%rbp), %rax
cmpb $0x0, 0x1(%rax)
je 0x70e60
movq -0x8(%rbp), %rdi
leaq 0x13a404(%rip), %rsi    # 0x1ab25a
callq 0x15e380
jmp 0x70f48
movq -0x28(%rbp), %rax
cmpq $0x19, 0x60(%rax)
jne 0x70e95
movq -0x28(%rbp), %rax
cmpq $0x2, 0x28(%rax)
jne 0x70e95
cmpq $0x80, -0x20(%rbp)
jne 0x70e95
movq -0x8(%rbp), %rdi
leaq 0x13a3f1(%rip), %rsi    # 0x1ab27c
callq 0x15e380
jmp 0x70f46
movq -0x28(%rbp), %rax
cmpq $0x8, 0x60(%rax)
jne 0x70eb0
movq -0x28(%rbp), %rax
cmpq $0x1, 0x20(%rax)
jne 0x70eb0
jmp 0x70f44
movq -0x28(%rbp), %rax
cmpq $0x8, 0x60(%rax)
jne 0x70eda
movq -0x28(%rbp), %rax
cmpq $0x2, 0x20(%rax)
jl 0x70eda
movq -0x28(%rbp), %rax
cmpq $0x8, 0x20(%rax)
jg 0x70eda
movl $0x1, -0x2c(%rbp)
jmp 0x70f42
movq -0x28(%rbp), %rax
cmpq $0x9, 0x60(%rax)
jne 0x70f04
movq -0x28(%rbp), %rax
cmpq $0x1, 0x20(%rax)
jl 0x70f04
movq -0x28(%rbp), %rax
cmpq $0x8, 0x20(%rax)
jg 0x70f04
movl $0x1, -0x2c(%rbp)
jmp 0x70f40
movq -0x28(%rbp), %rax
cmpq $0x1b, 0x60(%rax)
jne 0x70f29
cmpq $0x5354, -0x20(%rbp)    # imm = 0x5354
jne 0x70f29
movq -0x8(%rbp), %rdi
leaq 0x13a380(%rip), %rsi    # 0x1ab2a4
callq 0x15e380
movq -0x8(%rbp), %rdi
leaq 0x13a392(%rip), %rsi    # 0x1ab2c6
movb $0x0, %al
callq 0x15bfb0
jmp 0x7102a
jmp 0x70f42
jmp 0x70f44
jmp 0x70f46
jmp 0x70f48
movq -0x28(%rbp), %rax
cmpq $0x10, 0x28(%rax)
jle 0x70f65
movq -0x8(%rbp), %rdi
leaq 0x133cd1(%rip), %rsi    # 0x1a4c2f
movb $0x0, %al
callq 0x15bfb0
cmpl $0x0, -0x2c(%rbp)
je 0x70f7d
movq -0x8(%rbp), %rdi
leaq 0x13a37d(%rip), %rsi    # 0x1ab2f3
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x70fa0
jmp 0x7102a
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x28(%rbp), %rax
imulq 0x20(%rax), %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
leaq 0x13a36a(%rip), %rsi    # 0x1ab34b
callq 0x15e380
movq -0x28(%rbp), %rax
cmpb $0x0, 0x1(%rax)
je 0x70fff
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x712d0
jmp 0x71028
movq -0x28(%rbp), %rax
cmpq $0x19, 0x60(%rax)
jne 0x71019
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x712d0
jmp 0x71026
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x715e0
jmp 0x71028
jmp 0x7102a
movq -0x8(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x15b5a0
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x15b820
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)