// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/gws.c
// Function: do_imggal_member
static int do_imggal_member(deark *c, struct imggal_ctx *d, i64 idx, i64 pos1)
{
	int saved_indent_level;
	int retval = 1; // Default to no-fatal-error
	i64 pos = pos1;
	struct imggal_member *md = NULL;
	de_bitmap *img = NULL;
	de_finfo *fi = NULL;
	i64 t;
	static const de_color alchpal[16] = {
		0xff000000U,0xffff0000U,0xff00ff00U,0xffffff00U,
		0xff0000ffU,0xffff00ffU,0xff00ffffU,0xffd3a292U,
		0xffa26159U,0xff929292U,0xff7d0000U,0xff007d00U,
		0xff00007dU,0xffd3d3d3U,0xff515151U,0xffffffffU };

	de_dbg_indent_save(c, &saved_indent_level);
	md = de_malloc(c, sizeof(struct imggal_member));
	de_dbg(c, "item at %"I64_FMT, pos1);
	de_dbg_indent(c, 1);

	t = de_geti32le_p(&pos);
	de_unix_time_to_timestamp(t, &md->create_dt, 0x1);
	datetime_dbgmsg(c, &md->create_dt, "create time");
	t = de_geti32le_p(&pos);
	de_unix_time_to_timestamp(t, &md->mod_dt, 0x1);
	datetime_dbgmsg(c, &md->mod_dt, "mod time");

	pos += 28; // various fields

	md->orig_name = ucstring_create(c);
	dbuf_read_to_ucstring(c->infile, pos, 80, md->orig_name, DE_CONVFLAG_STOP_AT_NUL, d->input_encoding);
	de_dbg(c, "orig name: \"%s\"", ucstring_getpsz_d(md->orig_name));
	pos += 81;
	md->name = ucstring_create(c);
	get_base_filename(md->orig_name, md->name);

	pos += 14; // ?
	pos += 257; // description
	pos += 257; // keywords

	md->npwidth = 1 + de_getu16le_p(&pos);
	md->height = 1 + de_getu16le_p(&pos);
	de_dbg_dimensions(c, md->npwidth, md->height);
	if(!de_good_image_dimensions(c, md->npwidth, md->height)) goto done;

	md->bytes_per_row_per_plane = (md->npwidth + 7)/8;
	md->rowspan = md->bytes_per_row_per_plane * d->bpp;
	md->pdwidth = md->bytes_per_row_per_plane * 8;

	if(pos+(md->rowspan*md->height) > pos1+d->item_size) {
		retval = 0;
		d->need_errmsg = 1;
		goto done;
	}

	img = de_bitmap_create2(c, md->npwidth, md->pdwidth, md->height, (d->is_color?3:1));
	fi = de_finfo_create(c);

	if(d->is_color) {
		de_memcpy(md->pal, alchpal, sizeof(alchpal));
	}
	else if(d->bpp==1) {
		md->pal[0] = DE_STOCKCOLOR_BLACK;
		md->pal[1] = DE_MAKE_GRAY(243);
	}
	else {
		UI k;

		// Note: The Image Gallery software, in grayscale mode, uses a palette
		// that doesn't go all the way to white. The whitest color is 243 when
		// emulated by DOSBox.
		// We'll respect that, though whether we should do so is debatable.
		for(k=0; k<16; k++) {
			md->pal[k] = DE_MAKE_GRAY((u8)((k * 65) / 4));
		}
	}
	de_convert_image_paletted_planar(c->infile, pos, d->bpp,
		md->bytes_per_row_per_plane * d->bpp, md->bytes_per_row_per_plane,
		md->pal, img, 0);

	if(c->filenames_from_file) {
		de_finfo_set_name_from_ucstring(c, fi, md->name, 0);
	}
	fi->timestamp[DE_TIMESTAMPIDX_MODIFY] = md->mod_dt;
	fi->timestamp[DE_TIMESTAMPIDX_CREATE] = md->create_dt;
	de_bitmap_write_to_file_finfo(img, fi, DE_CREATEFLAG_OPT_IMAGE);

done:
	de_dbg_indent_restore(c, saved_indent_level);
	de_finfo_destroy(c, fi);
	de_bitmap_destroy(img);
	if(md) {
		ucstring_destroy(md->orig_name);
		ucstring_destroy(md->name);
		de_free(c, md);
	}
	return retval;
}

// 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)
movl $0x1, -0x28(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x8(%rbp), %rdi
leaq -0x24(%rbp), %rsi
callq 0x15b580
movq -0x8(%rbp), %rdi
movl $0x458, %esi            # imm = 0x458
callq 0x15b780
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x12c03d(%rip), %rsi    # 0x1bc77a
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165670
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x38, %rsi
movl $0x1, %edx
callq 0x15d530
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x38, %rsi
leaq 0x12a78c(%rip), %rdx    # 0x1baf1c
callq 0x90bb0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x165670
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x48, %rsi
movl $0x1, %edx
callq 0x15d530
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x48, %rsi
leaq 0x12939f(%rip), %rdx    # 0x1b9b72
callq 0x90bb0
movq -0x30(%rbp), %rax
addq $0x1c, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x10(%rbp), %rax
movl (%rax), %r9d
movl $0x50, %edx
movl $0x1, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x172650
movq -0x60(%rbp), %rdi
movq %rax, %rdx
leaq 0x11f1bf(%rip), %rsi    # 0x1afa05
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
addq $0x51, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0x90c00
movq -0x30(%rbp), %rax
addq $0xe, %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0x101, %rax            # imm = 0x101
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0x101, %rax            # imm = 0x101
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
addq $0x1, %rcx
movq -0x38(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1652c0
movq %rax, %rcx
addq $0x1, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rsi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x15bba0
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rsi
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x16a850
cmpl $0x0, %eax
jne 0x90923
jmp 0x90b4a
movq -0x38(%rbp), %rax
movq (%rax), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
imulq 0x18(%rax), %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rcx
shlq $0x3, %rcx
movq -0x38(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq -0x38(%rbp), %rdx
imulq 0x10(%rdx), %rcx
addq %rcx, %rax
movq -0x20(%rbp), %rcx
movq -0x10(%rbp), %rdx
addq 0x10(%rdx), %rcx
cmpq %rcx, %rax
jle 0x909ad
movl $0x0, -0x28(%rbp)
movq -0x10(%rbp), %rax
movb $0x1, 0x5(%rax)
jmp 0x90b4a
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq (%rax), %rsi
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x38(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x10(%rbp), %rax
movzbl 0x6(%rax), %r9d
movl $0x1, %r8d
movl $0x3, %eax
cmpl $0x0, %r9d
cmovnel %eax, %r8d
callq 0x16b7f0
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x6(%rax)
je 0x90a1f
movq -0x38(%rbp), %rdi
addq $0x58, %rdi
leaq 0x11f0cd(%rip), %rsi    # 0x1afae0
movl $0x40, %edx
callq 0x191e0
jmp 0x90a97
movq -0x10(%rbp), %rax
cmpq $0x1, 0x18(%rax)
jne 0x90a42
movq -0x38(%rbp), %rax
movl $0xff000000, 0x58(%rax) # imm = 0xFF000000
movq -0x38(%rbp), %rax
movl $0xfff3f3f3, 0x5c(%rax) # imm = 0xFFF3F3F3
jmp 0x90a95
movl $0x0, -0x54(%rbp)
cmpl $0x10, -0x54(%rbp)
jae 0x90a93
imull $0x41, -0x54(%rbp), %eax
shrl $0x2, %eax
movzbl %al, %edx
shll $0x10, %edx
orl $0xff000000, %edx       # imm = 0xFF000000
imull $0x41, -0x54(%rbp), %eax
shrl $0x2, %eax
movzbl %al, %eax
shll $0x8, %eax
orl %eax, %edx
imull $0x41, -0x54(%rbp), %eax
shrl $0x2, %eax
movzbl %al, %eax
orl %eax, %edx
movq -0x38(%rbp), %rax
movl -0x54(%rbp), %ecx
movl %edx, 0x58(%rax,%rcx,4)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x90a49
jmp 0x90a95
jmp 0x90a97
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x38(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x10(%rbp), %rax
imulq 0x18(%rax), %rcx
movq -0x38(%rbp), %rax
movq 0x18(%rax), %r8
movq -0x38(%rbp), %r9
addq $0x58, %r9
movq -0x40(%rbp), %rax
xorl %r10d, %r10d
movq %rax, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x16c690
movq -0x8(%rbp), %rax
cmpb $0x0, 0xd0(%rax)
je 0x90b08
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
movq -0x48(%rbp), %rax
movq -0x38(%rbp), %rcx
movq 0x48(%rcx), %rdx
movq %rdx, 0x18(%rax)
movq 0x50(%rcx), %rcx
movq %rcx, 0x20(%rax)
movq -0x48(%rbp), %rax
movq -0x38(%rbp), %rcx
movq 0x38(%rcx), %rdx
movq %rdx, 0x28(%rax)
movq 0x40(%rcx), %rcx
movq %rcx, 0x30(%rax)
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl $0x2, %edx
callq 0x16afa0
movq -0x8(%rbp), %rdi
movl -0x24(%rbp), %esi
callq 0x15b5a0
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x15d260
movq -0x40(%rbp), %rdi
callq 0x16af60
cmpq $0x0, -0x38(%rbp)
je 0x90b9a
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rdi
callq 0x171750
movq -0x38(%rbp), %rax
movq 0x30(%rax), %rdi
callq 0x171750
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x15b820
movl -0x28(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)