// File: /workspace/github_c_makefile_star2_O3/jsummers[P]deark/modules/misc2.c
// Function: de_run_pm_xv
static void de_run_pm_xv(deark *c, de_module_params *mparams)
{
	de_bitmap *img = NULL;
	int is_le;
	i64 width, height;
	i64 nplanes;
	i64 nbands;
	i64 pixelformat;
	i64 commentsize;
	i64 i, j;
	i64 plane;
	i64 rowspan;
	i64 planespan;
	i64 pos;
	u8 b;

	if(!dbuf_memcmp(c->infile, 0, "WEIV", 4))
		is_le = 1;
	else
		is_le = 0;

	nplanes = dbuf_geti32x(c->infile, 4, is_le);
	de_dbg(c, "planes: %d", (int)nplanes);

	height = dbuf_geti32x(c->infile, 8, is_le);
	width = dbuf_geti32x(c->infile, 12, is_le);
	de_dbg_dimensions(c, width, height);
	if(!de_good_image_dimensions(c, width, height)) goto done;

	nbands = dbuf_geti32x(c->infile, 16, is_le);
	de_dbg(c, "bands: %d", (int)nbands);

	pixelformat = dbuf_geti32x(c->infile, 20, is_le);
	de_dbg(c, "pixel format: 0x%04x", (unsigned int)pixelformat);

	commentsize = dbuf_geti32x(c->infile, 24, is_le);
	de_dbg(c, "comment size: %d", (int)commentsize);

	pos = 28;

	if((pixelformat==0x8001 && nplanes==3 && nbands==1) ||
		(pixelformat==0x8001 && nplanes==1 && nbands==1))
	{
		;
	}
	else {
		de_err(c, "Unsupported image type (pixel format=0x%04x, "
			"planes=%d, bands=%d)", (unsigned int)pixelformat,
			(int)nplanes, (int)nbands);
		goto done;
	}

	rowspan = width;
	planespan = rowspan*height;

	img = de_bitmap_create(c, width, height, (int)nplanes);

	for(plane=0; plane<nplanes; plane++) {
		for(j=0; j<height; j++) {
			for(i=0; i<width; i++) {
				b = de_getbyte(pos + plane*planespan + j*rowspan + i);
				if(nplanes==3) {
					de_bitmap_setsample(img, i, j, plane, b);
				}
				else {
					de_bitmap_setpixel_gray(img, i, j, b);
				}
			}
		}
	}
	de_bitmap_write_to_file(img, NULL, 0);

done:
	de_bitmap_destroy(img);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
leaq 0xbc8d8(%rip), %rdx     # 0x1254c2
xorl %r13d, %r13d
movl $0x4, %ecx
xorl %esi, %esi
callq 0xefc85
xorl %ebp, %ebp
testl %eax, %eax
sete %bpl
movq 0x18(%rbx), %rdi
movl $0x4, %esi
movl %ebp, %edx
callq 0xef033
leaq 0xd2897(%rip), %rsi     # 0x13b4af
movq %rbx, %rdi
movq %rax, (%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0x8, %esi
movl %ebp, %edx
callq 0xef033
movq %rax, %r14
movq 0x18(%rbx), %rdi
movl $0xc, %esi
movl %ebp, %edx
callq 0xef033
movq %rax, %r12
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0xe8f88
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, 0x10(%rsp)
movq %r14, %rdx
callq 0xf1db1
testl %eax, %eax
je 0x68e15
movq 0x18(%rbx), %rdi
movl $0x10, %esi
movl %ebp, %edx
callq 0xef033
movq %rax, %r14
leaq 0xbc836(%rip), %rsi     # 0x1254c7
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0x14, %esi
movl %ebp, %edx
callq 0xef033
movq %rax, %r15
leaq 0xbc819(%rip), %rsi     # 0x1254d1
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0x18, %esi
movl %ebp, %edx
callq 0xef033
leaq 0xbc80a(%rip), %rsi     # 0x1254e6
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
cmpq $0x1, %r14
jne 0x68cfe
cmpq $0x3, (%rsp)
jne 0x68cfe
cmpq $0x8001, %r15           # imm = 0x8001
je 0x68d20
cmpq $0x1, %r14
jne 0x68df7
cmpq $0x1, (%rsp)
jne 0x68df7
cmpq $0x8001, %r15           # imm = 0x8001
jne 0x68df7
movq %rbx, %rbp
movq %rbx, %rdi
movq %r12, %rsi
movq 0x10(%rsp), %rbx
movq %rbx, %rdx
movq (%rsp), %rcx
callq 0xf266a
movq %rax, %r13
imulq %r12, %rbx
movq %rbx, 0x20(%rsp)
movl $0x1c, %eax
movq %rax, 0x8(%rsp)
movq $0x0, 0x18(%rsp)
cmpq $0x0, 0x10(%rsp)
jle 0x68dc3
movq 0x8(%rsp), %r14
xorl %r15d, %r15d
testq %r12, %r12
jle 0x68db6
xorl %ebx, %ebx
movq 0x18(%rbp), %rdi
leaq (%r14,%rbx), %rsi
callq 0xeead3
movzbl %al, %r8d
cmpq $0x3, (%rsp)
jne 0x68d9d
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movq 0x18(%rsp), %rcx
callq 0xf2355
jmp 0x68dae
movq %r13, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl %r8d, %ecx
callq 0xf2405
incq %rbx
cmpq %rbx, %r12
jne 0x68d70
incq %r15
addq %r12, %r14
cmpq 0x10(%rsp), %r15
jne 0x68d69
movq 0x18(%rsp), %rcx
incq %rcx
movq 0x8(%rsp), %rax
addq 0x20(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rcx, 0x18(%rsp)
cmpq (%rsp), %rcx
jne 0x68d59
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf22dc
jmp 0x68e15
leaq 0xbc6f9(%rip), %rsi     # 0x1254f7
xorl %r13d, %r13d
movq %rbx, %rdi
movl %r15d, %edx
movq (%rsp), %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0xe9295
movq %r13, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf228d