// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/printshop.c
// Function: do_printshop_etc_image
static void do_printshop_etc_image(deark *c, lctx *d, i64 imgnum,
	i64 pos, i64 *bytes_consumed)
{
	i64 width, height;
	i64 rowspan;
	i64 imgspan;
	i64 imgoffset = 0;
	de_finfo *fi = NULL;
	u8 x;

	*bytes_consumed = 0;

	if(d->fmt==PRINTSHOP_FMT_SHP) {
		x = de_getbyte(pos);
		if(x!=0x0b) goto done; // No more images?
		height = (i64)de_getbyte(pos+1);
		width = (i64)de_getbyte(pos+2);
		if(width==0 || height==0) goto done;
		rowspan = (width+7)/8; // This is just a guess.
		imgoffset = 4;
		imgspan = 4 + rowspan * height + 1;
		if(pos+imgspan > c->infile->len) goto done;
	}
	else { // DAT or POG format
		width = 88;
		height = 52;
		rowspan = (width+7)/8;
		imgspan = rowspan * height;
		if(pos+imgspan > c->infile->len) goto done; // Reached end of file
	}

	de_dbg(c, "image[%d] at %d, %d"DE_CHAR_TIMES"%d", (int)imgnum, (int)pos, (int)width, (int)height);
	de_dbg_indent(c, 1);

	fi = de_finfo_create(c);

	if(d->namefile && (d->namefile->len >= (imgnum+1)*16)) {
		de_ucstring *name = NULL;
		name = ucstring_create(c);
		dbuf_read_to_ucstring(d->namefile, imgnum*16, 16, name, DE_CONVFLAG_STOP_AT_NUL, DE_ENCODING_ASCII);
		de_dbg(c, "name: \"%s\"", ucstring_getpsz(name));
		de_finfo_set_name_from_ucstring(c, fi, name, 0);
		ucstring_destroy(name);
	}

	de_convert_and_write_image_bilevel(c->infile, pos+ imgoffset,
		width, height, rowspan, DE_CVTF_WHITEISZERO, fi, 0);

	de_dbg_indent(c, -1);

	*bytes_consumed = imgspan;
done:
	de_finfo_destroy(c, fi);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x80, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x28(%rbp), %rax
movq $0x0, (%rax)
movq -0x10(%rbp), %rax
cmpl $0x3, (%rax)
jne 0x1191c4
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
callq 0x165060
movb %al, -0x59(%rbp)
movzbl -0x59(%rbp), %eax
cmpl $0xb, %eax
je 0x119129
jmp 0x119358
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x1, %rsi
callq 0x165060
movzbl %al, %eax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq $0x2, %rsi
callq 0x165060
movzbl %al, %eax
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x11916f
cmpq $0x0, -0x38(%rbp)
jne 0x119174
jmp 0x119358
movq -0x30(%rbp), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0x40(%rbp)
movq $0x4, -0x50(%rbp)
movq -0x40(%rbp), %rax
imulq -0x38(%rbp), %rax
addq $0x4, %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
addq -0x48(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x1191c2
jmp 0x119358
jmp 0x119214
movq $0x58, -0x30(%rbp)
movq $0x34, -0x38(%rbp)
movq -0x30(%rbp), %rax
addq $0x7, %rax
movl $0x8, %ecx
cqto
idivq %rcx
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
imulq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x20(%rbp), %rax
addq -0x48(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x119212
jmp 0x119358
jmp 0x119214
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl %eax, %edx
movq -0x20(%rbp), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %eax, %r8d
movq -0x38(%rbp), %rax
movl %eax, %r9d
leaq 0xabbfe(%rip), %rsi     # 0x1c4e37
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x119305
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
addq $0x1, %rcx
shlq $0x4, %rcx
cmpq %rcx, %rax
jl 0x119305
movq $0x0, -0x68(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1711d0
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rsi
shlq $0x4, %rsi
movq -0x68(%rbp), %rcx
movl $0x10, %edx
movl $0x1, %r9d
movl %r9d, %r8d
callq 0x1669f0
movq -0x8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
callq 0x172500
movq -0x70(%rbp), %rdi
movq %rax, %rdx
leaq 0x94eee(%rip), %rsi     # 0x1ae1d0
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x68(%rbp), %rdx
xorl %ecx, %ecx
callq 0x15d2c0
movq -0x68(%rbp), %rdi
callq 0x171750
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x20(%rbp), %rsi
addq -0x50(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0x58(%rbp), %rax
movl $0x1, %r9d
xorl %r10d, %r10d
movq %rax, (%rsp)
movl $0x0, 0x8(%rsp)
callq 0x16bf50
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x48(%rbp), %rcx
movq -0x28(%rbp), %rax
movq %rcx, (%rax)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x15d260
addq $0x80, %rsp
popq %rbp
retq
nop