// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/palmpdb.c
// Function: do_imgview_image
static void do_imgview_image(deark *c, lctx *d, i64 pos1, i64 len)
{
	u8 imgver;
	u8 imgtype;
	unsigned int cmpr_meth;
	i64 x0, x1;
	i64 pos = pos1;
	i64 num_raw_image_bytes;
	de_ucstring *iname = NULL;
	struct img_gen_info *igi = NULL;

	igi = de_malloc(c, sizeof(struct img_gen_info));
	igi->fi = de_finfo_create(c);

	de_dbg(c, "image record at %d", (int)pos1);
	de_dbg_indent(c, 1);

	igi->fi->internal_mod_time = d->mod_time;

	iname = ucstring_create(c);
	dbuf_read_to_ucstring(c->infile, pos, 32, iname, DE_CONVFLAG_STOP_AT_NUL, DE_ENCODING_PALM);
	de_dbg(c, "name: \"%s\"", ucstring_getpsz(iname));
	if(iname->len>0 && c->filenames_from_file) {
		de_finfo_set_name_from_ucstring(c, igi->fi, iname, 0);
	}
	pos += 32;

	imgver = de_getbyte(pos++);
	de_dbg(c, "version: 0x%02x", (unsigned int)imgver);
	cmpr_meth = (unsigned int)(imgver&0x07);
	de_dbg_indent(c, 1);
	de_dbg(c, "compression method: %u", cmpr_meth);
	de_dbg_indent(c, -1);
	if(imgver>0x01) {
		de_warn(c, "This version of ImageViewer format (0x%02x) might not be supported correctly.",
			(unsigned int)imgver);
	}

	imgtype = de_getbyte(pos++);
	de_dbg(c, "type: 0x%02x", (unsigned int)imgtype);
	de_dbg_indent(c, 1);
	switch(imgtype) {
	case 0: igi->bitsperpixel = 2; break;
	case 2: igi->bitsperpixel = 4; break;
	default: igi->bitsperpixel = 1;
	}
	de_dbg(c, "bits/pixel: %d", (int)igi->bitsperpixel);
	de_dbg_indent(c, -1);

	x0 = de_getu32be(pos);
	de_dbg(c, "reserved1: 0x%08x", (unsigned int)x0);
	pos += 4;

	x0 = de_getu32be(pos);
	de_dbg(c, "note: 0x%08x", (unsigned int)x0);
	pos += 4;

	x0 = de_getu16be(pos);
	pos += 2;
	x1 = de_getu16be(pos);
	pos += 2;
	de_dbg(c, "last: (%d,%d)", (int)x0, (int)x1);

	x0 = de_getu32be(pos);
	de_dbg(c, "reserved2: 0x%08x", (unsigned int)x0);
	pos += 4;

	// TODO: Is the anchor signed or unsigned?
	x0 = de_getu16be(pos);
	pos += 2;
	x1 = de_getu16be(pos);
	pos += 2;
	de_dbg(c, "anchor: (%d,%d)", (int)x0, (int)x1);

	igi->w = de_getu16be(pos);
	pos += 2;
	igi->h = de_getu16be(pos);
	pos += 2;
	de_dbg_dimensions(c, igi->w, igi->h);
	if(!de_good_image_dimensions(c, igi->w, igi->h)) goto done;

	igi->rowbytes = (igi->w*igi->bitsperpixel + 7)/8;
	num_raw_image_bytes = pos1+len-pos;

	de_dbg(c, "image data at %d", (int)pos);
	de_dbg_indent(c, 1);
	do_generate_image(c, d, c->infile, pos, num_raw_image_bytes,
		cmpr_meth, igi);
	de_dbg_indent(c, -1);

done:
	de_dbg_indent(c, -1);
	ucstring_destroy(iname);
	if(igi) {
		de_finfo_destroy(c, igi->fi);
		de_free(c, igi);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movl $0x30, %esi
callq 0x15b780
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
leaq 0xc0b74(%rip), %rsi     # 0x1c127b
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x80(%rcx), %rdx
movq %rdx, 0x68(%rax)
movq 0x88(%rcx), %rcx
movq %rcx, 0x70(%rax)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x50(%rbp), %rcx
movl $0x20, %edx
movl $0x1, %r8d
movl $0x13, %r9d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x50(%rbp), %rdi
callq 0x172500
movq -0x60(%rbp), %rdi
movq %rax, %rdx
leaq 0xada40(%rip), %rsi     # 0x1ae1d0
movb $0x0, %al
callq 0x15b0d0
movq -0x50(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jle 0x1007c9
movq -0x8(%rbp), %rax
movzbl 0xd0(%rax), %eax
cmpl $0x0, %eax
je 0x1007c9
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x50(%rbp), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
movq -0x40(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
callq 0x165060
movb %al, -0x21(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x21(%rbp), %edx
leaq 0xbd500(%rip), %rsi     # 0x1bdd03
movb $0x0, %al
callq 0x15b0d0
movzbl -0x21(%rbp), %eax
andl $0x7, %eax
movl %eax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movl -0x28(%rbp), %edx
leaq 0xb797f(%rip), %rsi     # 0x1b81af
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movzbl -0x21(%rbp), %eax
cmpl $0x1, %eax
jle 0x100864
movq -0x8(%rbp), %rdi
movzbl -0x21(%rbp), %edx
leaq 0xc0a31(%rip), %rsi     # 0x1c128e
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movq %rsi, %rax
incq %rax
movq %rax, -0x40(%rbp)
callq 0x165060
movb %al, -0x22(%rbp)
movq -0x8(%rbp), %rdi
movzbl -0x22(%rbp), %edx
leaq 0xb169a(%rip), %rsi     # 0x1b1f2b
xorl %eax, %eax
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movzbl -0x22(%rbp), %eax
movl %eax, -0x64(%rbp)
testl %eax, %eax
je 0x1008bd
jmp 0x1008b3
movl -0x64(%rbp), %eax
subl $0x2, %eax
je 0x1008cb
jmp 0x1008d9
movq -0x58(%rbp), %rax
movq $0x2, 0x10(%rax)
jmp 0x1008e5
movq -0x58(%rbp), %rax
movq $0x4, 0x10(%rax)
jmp 0x1008e5
movq -0x58(%rbp), %rax
movq $0x1, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %edx
leaq 0xd12fa(%rip), %rsi     # 0x1d1bf4
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x165470
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
leaq 0xc09a7(%rip), %rsi     # 0x1c12dc
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x165470
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
leaq 0xc0980(%rip), %rsi     # 0x1c12ee
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x1651d0
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x1651d0
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x38(%rbp), %rax
movl %eax, %ecx
leaq 0xc0921(%rip), %rsi     # 0x1c12fb
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x165470
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
leaq 0xc0902(%rip), %rsi     # 0x1c1309
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x1651d0
movq %rax, -0x30(%rbp)
movq -0x40(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x1651d0
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x38(%rbp), %rax
movl %eax, %ecx
leaq 0xc08a8(%rip), %rsi     # 0x1c131b
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x1651d0
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
callq 0x1651d0
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movq (%rax), %rsi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movq (%rax), %rsi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x100b03
jmp 0x100b99
movq -0x58(%rbp), %rax
movq (%rax), %rax
movq -0x58(%rbp), %rcx
imulq 0x10(%rcx), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x18(%rbp), %rax
addq -0x20(%rbp), %rax
subq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movl %eax, %edx
leaq 0xaafbc(%rip), %rsi     # 0x1abb09
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %r8
movl -0x28(%rbp), %r9d
movq -0x58(%rbp), %rax
movq %rax, (%rsp)
callq 0x100c90
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x50(%rbp), %rdi
callq 0x171750
cmpq $0x0, -0x58(%rbp)
je 0x100bd5
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x15d260
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x15b820
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)