// File: /workspace/github_c_makefile_star2_O3/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, %r14
movq %rdi, %r15
leaq 0x1c(%rsp), %rsi
callq 0xe8a1c
leaq 0xcf1ef(%rip), %rsi     # 0x11f805
movq %r15, %rdi
movl $0x10, %edx
xorl %eax, %eax
callq 0xe8617
movq %r15, %rbx
movq %r15, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %r14, 0x10(%rsp)
movq 0x20(%r14), %rcx
leaq -0x1(%rcx), %rax
cmpq $0x8, %rax
jae 0x5081f
movq 0x10(%rsp), %rax
cmpq $0x19, 0x60(%rax)
jne 0x50667
movq 0x10(%rsp), %rax
cmpb $0x0, 0x1(%rax)
je 0x50854
movq 0x10(%rsp), %rax
movq 0x68(%rax), %rax
cmpq $0x19, %rax
jl 0x508c2
addq $-0x16, %rax
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
mulq %rdx
movq %rdx, %r14
shrq %r14
movq %rcx, %rdi
callq 0xe9c9b
cmpq %r14, %rax
cmovlq %rax, %r14
movl $0x100, %eax            # imm = 0x100
cmpq %rax, %r14
cmovlq %r14, %rax
movq %rax, 0x28(%rsp)
leaq 0xdfdb1(%rip), %rsi     # 0x130469
movq %rbx, %rdi
movl $0x16, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
testq %r14, %r14
jle 0x508b5
movl $0x1a, %r15d
xorl %eax, %eax
xorl %ebp, %ebp
movq %rbx, 0x20(%rsp)
movl %eax, 0x18(%rsp)
movq %rbx, %r14
movq 0x18(%rbx), %rdi
leaq -0x4(%r15), %rsi
callq 0xeeb67
movq %rax, %rbx
movq 0x18(%r14), %rdi
leaq -0x2(%r15), %rsi
callq 0xeeb67
movq %rax, %r13
movq 0x18(%r14), %rdi
movq %r15, 0x38(%rsp)
movq %r15, %rsi
callq 0xeeb67
movq %rax, %r15
movq %rbx, %rdi
movq %rbx, %r14
callq 0xec7af
movl %eax, %ebx
movq %r13, %rdi
callq 0xec7af
movl %eax, %r12d
movq %r15, %rdi
callq 0xec7af
movzbl %bl, %ecx
movq 0x10(%rsp), %rbx
shll $0x10, %ecx
movzbl %r12b, %edx
movq %r14, %r12
shll $0x8, %edx
orl %ecx, %edx
movzbl %al, %eax
orl %edx, %eax
orl $0xff000000, %eax       # imm = 0xFF000000
movl %eax, 0x70(%rbx,%rbp,4)
movl $0x40, %esi
leaq 0x40(%rsp), %r14
movq %r14, %rdi
leaq 0xc3160(%rip), %rdx     # 0x1138e0
movl %r12d, %ecx
movq %r13, 0x30(%rsp)
movl %r13d, %r8d
movl %r15d, %r9d
xorl %eax, %eax
callq 0xe8193
movl 0x70(%rbx,%rbp,4), %edx
movq 0x20(%rsp), %rdi
movq %rdi, %rbx
movq %rbp, %rsi
movq %r14, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe90a3
movl $0x1, %eax
cmpl $0x0, 0x18(%rsp)
jne 0x50803
movl $0x3e9, %edx            # imm = 0x3E9
cmpq %rdx, %r12
setge %al
movq 0x30(%rsp), %r8
cmpq %rdx, %r8
setge %cl
orb %al, %cl
cmpq %rdx, %r15
setge %dl
orb %cl, %dl
xorl %eax, %eax
cmpb $0x1, %dl
jne 0x50803
movq %rbx, %rdi
leaq 0xcf0c0(%rip), %rsi     # 0x11f8af
movl %ebp, %edx
movl %r12d, %ecx
movl %r15d, %r9d
xorl %eax, %eax
callq 0xe8faa
movl $0x1, %eax
incq %rbp
movq 0x38(%rsp), %r15
addq $0x6, %r15
cmpq %rbp, 0x28(%rsp)
jne 0x506ec
jmp 0x508b5
movq 0x10(%rsp), %rax
cmpb $0x0, 0x1(%rax)
je 0x50833
leaq 0xcefed(%rip), %rsi     # 0x11f81e
jmp 0x5083a
leaq 0xcf00b(%rip), %rsi     # 0x11f845
xorl %r15d, %r15d
movq %rbx, %rdi
movl %ecx, %edx
xorl %eax, %eax
callq 0xe9295
xorl %r14d, %r14d
xorl %r12d, %r12d
jmp 0x509e5
movq 0x10(%rsp), %r12
movq 0x68(%r12), %r14
addq $-0x20, %r14
leaq 0xdfc00(%rip), %rsi     # 0x130469
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%rbx), %rsi
leaq 0x70(%r12), %rax
movb 0x20(%r12), %cl
movl $0x1, %r9d
shlq %cl, %r9
movl $0x0, (%rsp)
movl $0x10, %r8d
movq %rbx, %rdi
movq %r14, %rdx
movq %rax, %rcx
callq 0xf876e
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x10(%rsp), %rax
cmpq $0x1, 0x20(%rax)
jne 0x508ff
movq 0x10(%rsp), %rcx
movl 0x70(%rcx), %eax
cmpl 0x74(%rcx), %eax
jne 0x508ff
leaq 0xcef90(%rip), %rsi     # 0x11f872
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movabsq $-0xffffff00000001, %rax # imm = 0xFF000000FFFFFFFF
movq 0x10(%rsp), %rcx
movq %rax, 0x70(%rcx)
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x10(%rsp), %r13
movq 0x68(%r13), %rdx
leaq 0xddef4(%rip), %rsi     # 0x12e810
xorl %r15d, %r15d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x10(%r13), %rsi
imulq 0x38(%r13), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, %r12
movq 0x68(%r13), %rcx
movq %rbx, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x50a1e
cmpb $0x0, (%r13)
je 0x50960
xorl %r14d, %r14d
jmp 0x509e5
movq 0x10(%rsp), %r13
movq 0x18(%r13), %rdx
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rcx
movq %rbx, %rdi
movl $0x3, %r8d
callq 0xf26a5
movq %rax, %r15
movq %rbx, %rdi
callq 0xea504
movq %rax, %r14
cmpq $0x2, 0x40(%r13)
jl 0x509b9
movq 0x10(%rsp), %rax
cmpq $0x2, 0x48(%rax)
jl 0x509b9
movl $0x2, 0x78(%r14)
movq 0x10(%rsp), %rax
movups 0x50(%rax), %xmm0
movups %xmm0, 0x80(%r14)
movq 0x10(%rsp), %rax
cmpq $0x8, 0x20(%rax)
jg 0x509d5
movq 0x10(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x50d92
movq %r15, %rdi
movq %r14, %rsi
movl $0x2, %edx
callq 0xf22d0
movq %r15, %rdi
callq 0xf228d
movq %rbx, %rdi
movq %r14, %rsi
callq 0xea50e
movq %r12, %rdi
callq 0xf052b
movl 0x1c(%rsp), %esi
movq %rbx, %rdi
callq 0xe8a25
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq