// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%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
leaq 0x13a046(%rip), %rsi    # 0x1ab35d
movl $0x10, %edx
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x10(%rbp), %rax
cmpq $0x1, 0x20(%rax)
jl 0x71349
movq -0x10(%rbp), %rax
cmpq $0x8, 0x20(%rax)
jg 0x71349
jmp 0x71392
movq -0x10(%rbp), %rax
cmpb $0x0, 0x1(%rax)
je 0x71371
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %edx
leaq 0x13a00e(%rip), %rsi    # 0x1ab376
movb $0x0, %al
callq 0x15bfb0
jmp 0x7138d
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %edx
leaq 0x13a017(%rip), %rsi    # 0x1ab39d
movb $0x0, %al
callq 0x15bfb0
jmp 0x715a6
movq -0x10(%rbp), %rax
cmpq $0x19, 0x60(%rax)
jne 0x7143a
movq -0x10(%rbp), %rax
cmpb $0x0, 0x1(%rax)
jne 0x7143a
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rax
subq $0x20, %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movl %eax, %edx
leaq 0x14ab80(%rip), %rsi    # 0x1bbf50
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x38(%rbp), %rdx
movq -0x10(%rbp), %rax
addq $0x70, %rax
movq %rax, -0x40(%rbp)
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rcx
movl $0x1, %r9d
shlq %cl, %r9
movq -0x40(%rbp), %rcx
movl $0x10, %r8d
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x174460
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
jmp 0x71447
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x718e0
movq -0x10(%rbp), %rax
cmpq $0x1, 0x20(%rax)
jne 0x7148a
movq -0x10(%rbp), %rax
movl 0x70(%rax), %eax
movq -0x10(%rbp), %rcx
cmpl 0x74(%rcx), %eax
jne 0x7148a
movq -0x8(%rbp), %rdi
leaq 0x139f5d(%rip), %rsi    # 0x1ab3ca
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movl $0xffffffff, 0x70(%rax) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rax
movl $0xff000000, 0x74(%rax) # imm = 0xFF000000
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rdx
leaq 0x148e83(%rip), %rsi    # 0x1ba32e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
movq -0x10(%rbp), %rax
imulq 0x10(%rax), %rsi
xorl %edx, %edx
callq 0x168020
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rcx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %r8
movq -0x10(%rbp), %rax
subq 0x68(%rax), %r8
callq 0x71b30
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0x7150d
jmp 0x715a6
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movl $0x3, %r8d
callq 0x16b7f0
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x71e00
movq -0x10(%rbp), %rax
cmpq $0x8, 0x20(%rax)
jle 0x71578
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x71e60
jmp 0x7158d
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x71e80
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl $0x2, %edx
callq 0x16afa0
movl $0x1, -0x2c(%rbp)
movq -0x20(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x15d260
movq -0x18(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movl -0x30(%rbp), %esi
callq 0x15b5a0
movl -0x2c(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)