// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/xwd.c
// Function: do_xwd_image
static void do_xwd_image(deark *c, lctx *d)
{
	int bypp;
	de_bitmap *img = NULL;
	dbuf *unc_pixels = NULL;
	dbuf *inf = c->infile; // This is a copy -- do not close
	i64 inf_pos = d->imgpos;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	de_dbg(c, "image at %"I64_FMT, d->imgpos);
	de_dbg_indent(c, 1);
	if(!de_good_image_dimensions(c, d->width, d->height)) goto done;

	detect_compression(c, d);

	if(d->cmpr_meth!=0) {
		unc_pixels = dbuf_create_membuf(c, d->expected_image_size, 0x1);
		dbuf_enable_wbuffer(unc_pixels);
		decompress_pvwave_rle(c, d, unc_pixels);
		inf = unc_pixels;
		inf_pos = 0;
	}

	if(d->cmpr_meth==0) {
		if(d->imgpos + d->expected_image_size > c->infile->len+16) {
			de_err(c, "Bad or truncated XWD file");
			d->errflag = 1;
			goto done;
		}
	}

	if(d->imgtype==XWD_IMGTYPE_RGB &&
		(d->bytes_per_pixel==2 || d->bytes_per_pixel==3 || d->bytes_per_pixel==4))
	{
		;
	}
	else if(d->imgtype==XWD_IMGTYPE_PAL_OR_GRAY &&
		d->bits_per_pixel>0)
	{
		;
	}
	else {
		d->errflag = 1;
		d->need_errmsg = 1;
		goto done;
	}

	bypp = 3;
	if(d->imgtype==XWD_IMGTYPE_PAL_OR_GRAY) {
		if(de_is_grayscale_palette(d->pal, 256)) {
			bypp = 1;
		}
	}
	img = de_bitmap_create(c, d->width, d->height, bypp);

	if(d->imgtype==XWD_IMGTYPE_RGB) {
		read_image_rgb(c, d, inf, inf_pos, img);
	}
	else if(d->imgtype==XWD_IMGTYPE_PAL_OR_GRAY) {
		read_image_colormapped(c, d, inf, inf_pos, img);
	}

	if(d->errflag) goto done;

	de_bitmap_write_to_file(img, NULL, DE_CREATEFLAG_OPT_IMAGE);

done:
	if(d->need_errmsg) {
		de_err(c, "Unsupported image type");
		d->need_errmsg = 0;
	}
	de_bitmap_destroy(img);
	dbuf_close(unc_pixels);
	de_dbg_indent_restore(c, saved_indent_level);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x3c(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rdx
leaq 0x609b0(%rip), %rsi     # 0x1ba32e
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xb8(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0xc0(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x1599bc
jmp 0x159bab
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x15a390
movq -0x10(%rbp), %rax
cmpl $0x0, 0xd8(%rax)
je 0x159a1d
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x90(%rax), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x167b90
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x15a4b0
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0xd8(%rax)
jne 0x159a75
movq -0x10(%rbp), %rax
movq 0x88(%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x90(%rcx), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
movq 0x18(%rcx), %rcx
addq $0x10, %rcx
cmpq %rcx, %rax
jle 0x159a73
movq -0x8(%rbp), %rdi
leaq 0x781e4(%rip), %rsi     # 0x1d1c44
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
jmp 0x159bab
jmp 0x159a75
movq -0x10(%rbp), %rax
cmpl $0x3, 0xdc(%rax)
jne 0x159aae
movq -0x10(%rbp), %rax
cmpq $0x2, 0xa8(%rax)
je 0x159aac
movq -0x10(%rbp), %rax
cmpq $0x3, 0xa8(%rax)
je 0x159aac
movq -0x10(%rbp), %rax
cmpq $0x4, 0xa8(%rax)
jne 0x159aae
jmp 0x159ae1
movq -0x10(%rbp), %rax
cmpl $0x1, 0xdc(%rax)
jne 0x159acb
movq -0x10(%rbp), %rax
cmpq $0x0, 0xb0(%rax)
jle 0x159acb
jmp 0x159adf
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x1(%rax)
jmp 0x159bab
jmp 0x159ae1
movl $0x3, -0x14(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x1, 0xdc(%rax)
jne 0x159b18
movq -0x10(%rbp), %rdi
addq $0x108, %rdi            # imm = 0x108
movl $0x100, %esi            # imm = 0x100
callq 0x16aa20
cmpl $0x0, %eax
je 0x159b16
movl $0x1, -0x14(%rbp)
jmp 0x159b18
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xb8(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0xc0(%rax), %rdx
movl -0x14(%rbp), %ecx
callq 0x16b750
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x3, 0xdc(%rax)
jne 0x159b66
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %r8
callq 0x15a620
jmp 0x159b8e
movq -0x10(%rbp), %rax
cmpl $0x1, 0xdc(%rax)
jne 0x159b8c
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x20(%rbp), %r8
callq 0x15a800
jmp 0x159b8e
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0x159b99
jmp 0x159bab
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x16afd0
movq -0x10(%rbp), %rax
cmpb $0x0, 0x1(%rax)
je 0x159bcf
movq -0x8(%rbp), %rdi
leaq 0x49900(%rip), %rsi     # 0x1a34c0
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x0, 0x1(%rax)
movq -0x20(%rbp), %rdi
callq 0x16af60
movq -0x28(%rbp), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movl -0x3c(%rbp), %esi
callq 0x15b5a0
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)