// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
leaq 0xb3088(%rip), %rdx     # 0x1182c2
xorl %r13d, %r13d
pushq $0x4
popq %r14
xorl %esi, %esi
movq %r14, %rcx
callq 0xe4d48
xorl %ebp, %ebp
testl %eax, %eax
sete %bpl
movq 0x18(%rbx), %rdi
movq %r14, %rsi
movl %ebp, %edx
callq 0xe41d8
leaq 0xc8f57(%rip), %rsi     # 0x12e1bf
movq %rbx, %rdi
movq %rax, 0x8(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rsi
movl %ebp, %edx
callq 0xe41d8
movq %rax, %r14
movq 0x18(%rbx), %rdi
pushq $0xc
popq %rsi
movl %ebp, %edx
callq 0xe41d8
movq %rax, %r15
movq %rbx, %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0xdf038
movq %rbx, %rdi
movq %r15, 0x10(%rsp)
movq %r15, %rsi
movq %r14, %r12
movq %r14, %rdx
callq 0xe6be1
testl %eax, %eax
je 0x65472
movq 0x18(%rbx), %rdi
pushq $0x10
popq %rsi
movl %ebp, %edx
callq 0xe41d8
movq %rax, %r14
leaq 0xb2fe8(%rip), %rsi     # 0x1182c7
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
pushq $0x14
popq %rsi
movl %ebp, %edx
callq 0xe41d8
movq %rax, %r15
leaq 0xb2fcd(%rip), %rsi     # 0x1182d1
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
pushq $0x18
popq %rsi
movl %ebp, %edx
callq 0xe41d8
leaq 0xb2fc0(%rip), %rsi     # 0x1182e6
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
cmpq $0x1, %r14
jne 0x65349
cmpq $0x3, 0x8(%rsp)
jne 0x65349
cmpq $0x8001, %r15           # imm = 0x8001
je 0x6536c
cmpq $0x1, %r14
jne 0x65453
cmpq $0x1, 0x8(%rsp)
jne 0x65453
cmpq $0x8001, %r15           # imm = 0x8001
jne 0x65453
movq %rbx, %r13
movq %rbx, %rdi
movq 0x10(%rsp), %r14
movq %r14, %rsi
movq %r12, %rdx
movq 0x8(%rsp), %rcx
callq 0xe7437
movq %rax, %rbp
xorl %eax, %eax
testq %r14, %r14
movl $0x0, %ebx
cmovgq %r14, %rbx
testq %r12, %r12
movl $0x0, %ecx
cmovgq %r12, %rcx
movq %rcx, 0x30(%rsp)
imulq %r14, %r12
movq %r12, 0x20(%rsp)
pushq $0x1c
popq %r14
cmpq 0x8(%rsp), %rax
je 0x65442
movq %rax, 0x18(%rsp)
movq %r14, 0x28(%rsp)
xorl %r15d, %r15d
cmpq 0x30(%rsp), %r15
je 0x6542b
xorl %r12d, %r12d
cmpq %r12, %rbx
je 0x65421
movq 0x18(%r13), %rdi
leaq (%r14,%r12), %rsi
callq 0xe3eb4
movzbl %al, %r8d
cmpq $0x3, 0x8(%rsp)
jne 0x6540b
movq %rbp, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq 0x18(%rsp), %rcx
callq 0xe712a
jmp 0x6541c
movq %rbp, %rdi
movq %r12, %rsi
movq %r15, %rdx
movl %r8d, %ecx
callq 0xe71da
incq %r12
jmp 0x653d8
incq %r15
addq 0x10(%rsp), %r14
jmp 0x653ce
movq 0x18(%rsp), %rax
incq %rax
movq 0x28(%rsp), %r14
addq 0x20(%rsp), %r14
jmp 0x653b6
movq %rbp, %r13
movq %rbp, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe70b7
jmp 0x65472
leaq 0xb2e9d(%rip), %rsi     # 0x1182f7
xorl %r13d, %r13d
movq %rbx, %rdi
movl %r15d, %edx
movq 0x8(%rsp), %rcx
movl %r14d, %r8d
xorl %eax, %eax
callq 0xdf2a5
movq %r13, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe7084