// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/gemras.c
// Function: do_gem_img
static void do_gem_img(deark *c, lctx *d)
{
	dbuf *unc_pixels = NULL;
	de_bitmap *img = NULL;
	de_finfo *fi = NULL;
	int is_color = 0;
	UI createflags = 0;
	i64 k;

	if(d->header_size_in_words==9 && (d->nplanes==3 || d->nplanes==4)) {
		i64 x;
		x = de_getu16be(8*2);
		if(x==0) {
			is_color = 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, is_color?3:1);

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

	if(d->nplanes==1) {
		de_convert_image_bilevel(unc_pixels, 0, d->rowspan_per_plane, img, DE_CVTF_WHITEISZERO);
		createflags |= DE_CREATEFLAG_IS_BWIMG;
	}
	else if(is_color && d->nplanes==3) {
		de_dbg(c, "[using default 8-color palette]");
		for(k=0; k<8; k++) {
			d->pal[k] = pal3bit[k];
		}
		read_paletted_image(c, d, unc_pixels, img);
	}
	else if(is_color && d->nplanes==4) {
		de_dbg(c, "[using default 16-color palette]");
		for(k=0; k<16; k++) {
			d->pal[k] = pal4bit[k];
		}
		read_paletted_image(c, d, unc_pixels, img);
	}
	else {
		if(d->nplanes==8) {
			make_pal256_revbits(c, d);
		}
		else {
			de_make_grayscale_palette(d->pal, ((i64)1)<<((UI)d->nplanes), 1);
		}
		read_paletted_image(c, d, unc_pixels, img);
	}

	de_bitmap_write_to_file_finfo(img, fi, DE_CREATEFLAG_OPT_IMAGE | createflags);

done:
	de_bitmap_destroy(img);
	de_finfo_destroy(c, fi);
	dbuf_close(unc_pixels);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %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)
movl $0x0, -0x30(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x9, 0x60(%rax)
jne 0x7165d
movq -0x10(%rbp), %rax
cmpq $0x3, 0x20(%rax)
je 0x71637
movq -0x10(%rbp), %rax
cmpq $0x4, 0x20(%rax)
jne 0x7165d
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x10, %esi
callq 0x1651d0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x7165b
movl $0x1, -0x2c(%rbp)
jmp 0x7165d
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x68(%rax), %rdx
leaq 0x148cbe(%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 0x716d2
jmp 0x718b5
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 -0x2c(%rbp), %r9d
movl $0x1, %r8d
movl $0x3, %eax
cmpl $0x0, %r9d
cmovnel %eax, %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 $0x1, 0x20(%rax)
jne 0x71764
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x30(%rax), %rdx
movq -0x20(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %r8d
callq 0x16bee0
movl -0x30(%rbp), %eax
orl $0x8, %eax
movl %eax, -0x30(%rbp)
jmp 0x718a2
cmpl $0x0, -0x2c(%rbp)
je 0x717d8
movq -0x10(%rbp), %rax
cmpq $0x3, 0x20(%rax)
jne 0x717d8
movq -0x8(%rbp), %rdi
leaq 0x139e0e(%rip), %rsi    # 0x1ab58e
movb $0x0, %al
callq 0x15b0d0
movq $0x0, -0x38(%rbp)
cmpq $0x8, -0x38(%rbp)
jge 0x717be
movq -0x38(%rbp), %rcx
leaq 0x139e3f(%rip), %rax    # 0x1ab5e0
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movl %edx, 0x70(%rax,%rcx,4)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x7178f
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x71e80
jmp 0x718a0
cmpl $0x0, -0x2c(%rbp)
je 0x71849
movq -0x10(%rbp), %rax
cmpq $0x4, 0x20(%rax)
jne 0x71849
movq -0x8(%rbp), %rdi
leaq 0x139dba(%rip), %rsi    # 0x1ab5ae
movb $0x0, %al
callq 0x15b0d0
movq $0x0, -0x38(%rbp)
cmpq $0x10, -0x38(%rbp)
jge 0x71832
movq -0x38(%rbp), %rcx
leaq 0x139deb(%rip), %rax    # 0x1ab600
movl (%rax,%rcx,4), %edx
movq -0x10(%rbp), %rax
movq -0x38(%rbp), %rcx
movl %edx, 0x70(%rax,%rcx,4)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x71803
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x71e80
jmp 0x7189e
movq -0x10(%rbp), %rax
cmpq $0x8, 0x20(%rax)
jne 0x71863
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x72240
jmp 0x71889
movq -0x10(%rbp), %rdi
addq $0x70, %rdi
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl %eax, %eax
movl %eax, %ecx
movl $0x1, %esi
shlq %cl, %rsi
movl $0x1, %edx
callq 0x16d580
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
callq 0x71e80
jmp 0x718a0
jmp 0x718a2
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl -0x30(%rbp), %edx
orl $0x2, %edx
callq 0x16afa0
movq -0x20(%rbp), %rdi
callq 0x16af60
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x15d260
movq -0x18(%rbp), %rdi
callq 0x1676a0
addq $0x40, %rsp
popq %rbp
retq
nopw (%rax,%rax)