// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/gemras.c
// Function: do_gem_ximg
static int do_gem_ximg(deark *c, lctx *d)
{
	dbuf *unc_pixels = NULL;
	de_bitmap *img = NULL;
	de_finfo *fi = NULL;
	int retval = 0;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);

	de_dbg(c, "header (continued) at %d", 8*2);
	de_dbg_indent(c, 1);

	if((d->nplanes>=1 && d->nplanes<=8) /* || d->nplanes==24 */) {
		;
	}
	else {
		if(d->is_ximg)
			de_err(c, "%d-plane XIMG images are not supported", (int)d->nplanes);
		else
			de_err(c, "This type of %d-plane image is not supported", (int)d->nplanes);
		goto done;
	}

	if(d->header_size_in_words==25 && !d->is_ximg) {
		i64 pal_pos = d->header_size_in_bytes-32;
		de_dbg(c, "palette at %d", (int)pal_pos);
		de_dbg_indent(c, 1);
		fmtutil_read_atari_palette(c, c->infile, pal_pos,
			d->pal, 16, ((i64)1)<<d->nplanes, 0);
		de_dbg_indent(c, -1);
	}
	else {
		read_palette_ximg(c, d);
	}

	if(d->nplanes==1 && d->pal[0]==d->pal[1]) {
		de_dbg(c, "Palette doesn't seem to be present. Using a default palette.");
		d->pal[0] = DE_STOCKCOLOR_WHITE;
		d->pal[1] = DE_STOCKCOLOR_BLACK;
	}

	de_dbg_indent(c, -1);

	de_dbg(c, "image at %"I64_FMT, d->header_size_in_bytes);

	unc_pixels = dbuf_create_membuf(c, d->rowspan_total*d->h, 0);
	decompress_pixels(c, d, unc_pixels, d->header_size_in_bytes, c->infile->len-d->header_size_in_bytes);
	if(d->errflag) goto done;

	img = de_bitmap_create2(c, d->npwidth, d->pdwidth, d->h, 3);

	fi = de_finfo_create(c);
	set_density(c, d, fi);

	if(d->nplanes>8) {
		read_rgb_image(c, d, unc_pixels, img);
	}
	else {
		read_paletted_image(c, d, unc_pixels, img);
	}

	de_bitmap_write_to_file_finfo(img, fi, DE_CREATEFLAG_OPT_IMAGE);

	retval = 1;

done:
	de_bitmap_destroy(img);
	de_finfo_destroy(c, fi);
	dbuf_close(unc_pixels);
	de_dbg_indent_restore(c, saved_indent_level);
	return retval;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x24(%rsp), %rsi
callq 0xdebae
leaq 0xc48cc(%rip), %rsi     # 0x112735
pushq $0x10
popq %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %r14, 0x10(%rsp)
movq %r14, %rdi
callq 0xdeba7
movq %rbx, 0x8(%rsp)
movq 0x20(%rbx), %rcx
leaq -0x1(%rcx), %rax
cmpq $0x8, %rax
jae 0x4e06c
movq 0x8(%rsp), %rax
cmpq $0x19, 0x60(%rax)
jne 0x4deb8
movq 0x8(%rsp), %rax
cmpb $0x0, 0x1(%rax)
je 0x4e0af
movq 0x8(%rsp), %rax
movq 0x68(%rax), %rax
cmpq $0x19, %rax
jl 0x4e113
addq $-0x16, %rax
xorl %ebx, %ebx
pushq $0x3
popq %rsi
xorl %edx, %edx
divq %rsi
movq %rax, %r14
movq %rcx, %rdi
callq 0xdfba1
cmpq %r14, %rax
cmovlq %rax, %r14
movl $0x100, %eax            # imm = 0x100
cmpq %rax, %r14
cmovgeq %rax, %r14
leaq 0xd52ca(%rip), %rsi     # 0x1231c8
pushq $0x16
popq %rdx
movq 0x10(%rsp), %r15
movq %r15, %rdi
xorl %eax, %eax
callq 0xde896
movq %r15, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
testq %r14, %r14
cmovleq %rbx, %r14
movq %r14, 0x28(%rsp)
pushq $0x1a
popq %r14
xorl %r15d, %r15d
xorl %ecx, %ecx
movq 0x10(%rsp), %rbp
cmpq %r15, 0x28(%rsp)
je 0x4e080
movq 0x18(%rbp), %rdi
leaq -0x4(%r14), %rsi
movl %ecx, 0x20(%rsp)
callq 0xe3f46
movq %rax, %rbx
movq %rax, 0x30(%rsp)
movq 0x18(%rbp), %rdi
leaq -0x2(%r14), %rsi
callq 0xe3f46
movq %rax, %r13
movq %rax, 0x18(%rsp)
movq 0x18(%rbp), %rdi
movq %r14, 0x38(%rsp)
movq %r14, %rsi
callq 0xe3f46
movq %rax, %r14
movq %rbx, %rdi
callq 0xe1d11
movl %eax, %ebx
movq %r13, %rdi
callq 0xe1d11
movl %eax, %r13d
movq %r14, %rdi
callq 0xe1d11
movzbl %bl, %ecx
movq %r15, %rbx
movq 0x8(%rsp), %r15
shll $0x10, %ecx
movzbl %r13b, %edx
shll $0x8, %edx
orl %ecx, %edx
movzbl %al, %eax
orl %edx, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, 0x70(%r15,%rbx,4)
leaq 0x40(%rsp), %r12
movq %r12, %rdi
pushq $0x40
popq %rsi
leaq 0xb88a9(%rip), %rdx     # 0x106880
movq 0x30(%rsp), %rcx
movq %rcx, %r13
movq 0x18(%rsp), %r8
movl %r14d, %r9d
xorl %eax, %eax
callq 0xde40c
movl 0x70(%r15,%rbx,4), %edx
movq %rbx, %r15
movq %rbp, %rdi
movq %rbx, %rsi
movq %r12, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xdf153
cmpl $0x0, 0x20(%rsp)
pushq $0x1
popq %rcx
jne 0x4e05b
movl $0x3e9, %edx            # imm = 0x3E9
cmpq %rdx, %r13
setge %al
cmpq %rdx, 0x18(%rsp)
setge %cl
orb %al, %cl
cmpq %rdx, %r14
setge %al
orb %cl, %al
xorl %ecx, %ecx
cmpb $0x1, %al
jne 0x4e05b
movq 0x10(%rsp), %rdi
leaq 0xc479c(%rip), %rsi     # 0x1127df
movl %r15d, %edx
movl %r13d, %ecx
movq 0x18(%rsp), %r8
movl %r14d, %r9d
xorl %eax, %eax
callq 0xdf05a
pushq $0x1
popq %rcx
incq %r15
movq 0x38(%rsp), %r14
addq $0x6, %r14
jmp 0x4df35
movq 0x8(%rsp), %rax
cmpb $0x0, 0x1(%rax)
je 0x4e08d
leaq 0xc46d0(%rip), %rsi     # 0x11274e
jmp 0x4e094
pushq $-0x1
popq %rsi
movq 0x10(%rsp), %rdi
jmp 0x4e10e
leaq 0xc46e1(%rip), %rsi     # 0x112775
xorl %ebx, %ebx
movq 0x10(%rsp), %rdi
movl %ecx, %edx
xorl %eax, %eax
callq 0xdf2a5
xorl %r12d, %r12d
xorl %r14d, %r14d
jmp 0x4e216
movq 0x8(%rsp), %rbx
movq 0x68(%rbx), %r14
addq $-0x20, %r14
leaq 0xd5105(%rip), %rsi     # 0x1231c8
movq 0x10(%rsp), %r15
movq %r15, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0xdeba7
movq 0x18(%r15), %rsi
leaq 0x70(%rbx), %rax
movb 0x20(%rbx), %cl
pushq $0x1
popq %r9
shlq %cl, %r9
andl $0x0, (%rsp)
pushq $0x10
popq %r8
movq %r15, %rdi
movq %r14, %rdx
movq %rax, %rcx
callq 0xecea5
pushq $-0x1
popq %rsi
movq %r15, %rdi
callq 0xdeba7
movq 0x8(%rsp), %rax
cmpq $0x1, 0x20(%rax)
jne 0x4e152
movq 0x8(%rsp), %rcx
movl 0x70(%rcx), %eax
cmpl 0x74(%rcx), %eax
jne 0x4e152
leaq 0xc466f(%rip), %rsi     # 0x1127a2
movq 0x10(%rsp), %rdi
xorl %eax, %eax
callq 0xde896
movabsq $-0xffffff00000001, %rax # imm = 0xFF000000FFFFFFFF
movq 0x8(%rsp), %rcx
movq %rax, 0x70(%rcx)
pushq $-0x1
popq %rsi
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0xdeba7
movq 0x8(%rsp), %r12
movq 0x68(%r12), %rdx
leaq 0xd342d(%rip), %rsi     # 0x1215a0
xorl %ebx, %ebx
movq %r15, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x10(%r12), %rsi
imulq 0x38(%r12), %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, %r14
movq 0x68(%r12), %rcx
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x4e254
cmpb $0x0, (%r12)
je 0x4e1b6
xorl %r12d, %r12d
jmp 0x4e216
movq 0x8(%rsp), %r15
movq 0x18(%r15), %rdx
movq 0x8(%r15), %rsi
movq 0x10(%r15), %rcx
pushq $0x3
popq %r8
movq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0xe746f
movq %rax, %rbx
movq %r12, %rdi
callq 0xdfd1a
movq %rax, %r12
movq %r15, %rdi
movq %rax, %rsi
callq 0x4e5a7
cmpq $0x8, 0x20(%r15)
jg 0x4e208
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4e5c8
pushq $0x2
popq %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe70ab
movq %rbx, %rdi
callq 0xe7084
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xdfd24
movq %r14, %rdi
callq 0xe5591
movl 0x24(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq