// File: /workspace/github_c_makefile_star2_O1/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, %r13
movq %rdi, %rbx
leaq 0x1c(%rsp), %rsi
callq 0xe958c
leaq 0xceff3(%rip), %rsi     # 0x11f6dd
movq %rbx, %rdi
movl $0x10, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %r12
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x20(%r13), %rcx
leaq -0x1(%rcx), %rax
cmpq $0x8, %rax
jae 0x50910
cmpq $0x19, 0x60(%r13)
jne 0x5072d
cmpb $0x0, 0x1(%r13)
je 0x50940
movq 0x68(%r13), %rax
cmpq $0x19, %rax
jl 0x509a7
movq %r13, 0x20(%rsp)
addq $-0x16, %rax
movabsq $0x5555555555555556, %rdx # imm = 0x5555555555555556
imulq %rdx
movq %r12, 0x8(%rsp)
movq %rdx, %rbx
shrq $0x3f, %rbx
addq %rdx, %rbx
movq %rcx, %rdi
callq 0xea7f2
cmpq %rbx, %rax
cmovlq %rax, %rbx
movl $0x100, %eax            # imm = 0x100
cmpq %rax, %rbx
cmovlq %rbx, %rax
movq %rax, 0x30(%rsp)
leaq 0xdf9e9(%rip), %rsi     # 0x130170
movq 0x8(%rsp), %rdi
movl $0x16, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x8(%rsp), %rdi
movl $0x1, %esi
callq 0xe9585
testq %rbx, %rbx
movq 0x8(%rsp), %r12
jle 0x508f9
movl $0x1a, %r15d
xorl %eax, %eax
xorl %ebp, %ebp
movl %eax, 0x18(%rsp)
movq 0x18(%r12), %rdi
leaq -0x4(%r15), %rsi
callq 0xef169
movq %rax, %rbx
movq %rax, 0x28(%rsp)
movq 0x18(%r12), %rdi
leaq -0x2(%r15), %rsi
callq 0xef169
movq %rax, %r13
movq %rax, 0x10(%rsp)
movq 0x18(%r12), %rdi
movq %r15, %rsi
callq 0xef169
movq %rax, %r14
movq %rbx, %rdi
callq 0xece08
movl %eax, %ebx
movq %r13, %rdi
callq 0xece08
movl %eax, %r12d
movq %r14, %rdi
callq 0xece08
movzbl %bl, %ecx
movq 0x8(%rsp), %r13
shll $0x10, %ecx
movzbl %r12b, %edx
shll $0x8, %edx
orl %ecx, %edx
movzbl %al, %eax
orl %edx, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movq 0x20(%rsp), %r12
movl %eax, 0x70(%r12,%rbp,4)
movl $0x40, %esi
leaq 0x40(%rsp), %rbx
movq %rbx, %rdi
leaq 0xc2fd8(%rip), %rdx     # 0x113830
movq 0x28(%rsp), %rcx
movq 0x10(%rsp), %r8
movq %r14, 0x38(%rsp)
movl %r14d, %r9d
xorl %eax, %eax
callq 0xe8d67
movl 0x70(%r12,%rbp,4), %edx
movq %r13, %r12
movq %r13, %rdi
movq %rbp, %rsi
movq %rbx, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe9c1e
movl $0x1, %eax
cmpl $0x0, 0x18(%rsp)
jne 0x508e7
movl $0x3e9, %edx            # imm = 0x3E9
movq 0x28(%rsp), %rcx
cmpq %rdx, %rcx
setge %al
cmpq %rdx, 0x10(%rsp)
setge %sil
orb %al, %sil
movq 0x38(%rsp), %r9
cmpq %rdx, %r9
setge %dl
orb %sil, %dl
xorl %eax, %eax
cmpb $0x1, %dl
jne 0x508e7
movq %r12, %rdi
leaq 0xceeb3(%rip), %rsi     # 0x11f787
movl %ebp, %edx
movq 0x10(%rsp), %r8
xorl %eax, %eax
callq 0xe9b27
movl $0x1, %eax
incq %rbp
addq $0x6, %r15
cmpq %rbp, 0x30(%rsp)
jne 0x507bf
movq %r12, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x20(%rsp), %r13
jmp 0x509a7
cmpb $0x0, 0x1(%r13)
je 0x50920
leaq 0xcedd8(%rip), %rsi     # 0x11f6f6
jmp 0x50927
leaq 0xcedf6(%rip), %rsi     # 0x11f71d
xorl %r15d, %r15d
movq %r12, %rdi
movl %ecx, %edx
xorl %eax, %eax
callq 0xe9df7
xorl %r14d, %r14d
xorl %ebx, %ebx
jmp 0x50ab4
movq 0x68(%r13), %r14
addq $-0x20, %r14
leaq 0xdf821(%rip), %rsi     # 0x130170
movq %r12, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
movq %r12, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%r12), %rsi
leaq 0x70(%r13), %rax
movb 0x20(%r13), %cl
movl $0x1, %r9d
shlq %cl, %r9
movl $0x0, (%rsp)
movl $0x10, %r8d
movq %r12, %rdi
movq %r14, %rdx
movq %rax, %rcx
callq 0xf8c62
movq %r12, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpq $0x1, 0x20(%r13)
jne 0x509d7
movl 0x70(%r13), %eax
cmpl 0x74(%r13), %eax
jne 0x509d7
leaq 0xced8b(%rip), %rsi     # 0x11f74a
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9226
movabsq $-0xffffff00000001, %rax # imm = 0xFF000000FFFFFFFF
movq %rax, 0x70(%r13)
movq %r12, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x68(%r13), %rdx
leaq 0xddb29(%rip), %rsi     # 0x12e518
xorl %r15d, %r15d
movq %r12, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x10(%r13), %rsi
imulq 0x38(%r13), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0xf0faf
movq %rax, %rbx
movq 0x68(%r13), %rcx
movq %r12, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x50aed
cmpb $0x0, (%r13)
je 0x50a33
xorl %r14d, %r14d
jmp 0x50ab4
movq 0x18(%r13), %rdx
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rcx
movq %r12, %rdi
movl $0x3, %r8d
callq 0xf2ce6
movq %rax, %r15
movq %r12, %rdi
callq 0xea9a9
movq %rax, %r14
cmpq $0x2, 0x40(%r13)
jl 0x50a8f
cmpq $0x2, 0x48(%r13)
jl 0x50a8f
movl $0x2, 0x78(%r14)
movsd 0x50(%r13), %xmm0
movsd %xmm0, 0x80(%r14)
movsd 0x58(%r13), %xmm0
movsd %xmm0, 0x88(%r14)
cmpq $0x8, 0x20(%r13)
jg 0x50aa4
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x50e86
movq %r15, %rdi
movq %r14, %rsi
movl $0x2, %edx
callq 0xf2917
movq %r15, %rdi
callq 0xf28d4
movq %r12, %rdi
movq %r14, %rsi
callq 0xea9b3
movq %rbx, %rdi
callq 0xf0b13
movl 0x1c(%rsp), %esi
movq %r12, %rdi
callq 0xe9595
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq