// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/psionpic.c
// Function: de_run_psionpic
static void de_run_psionpic(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	i64 i;
	int format;
	const char *s;

	d = de_malloc(c, sizeof(lctx));

	s = de_get_ext_option(c, "psionpic:bw");
	if(s) {
		d->bw = 1;
	}

	d->crco = de_crcobj_create(c, DE_CRCOBJ_CRC16_XMODEM);

	d->num_planes = de_getu16le(6);
	de_dbg(c, "number of planes/bitmaps: %d", (int)d->num_planes);

	// After the 8-byte header are [num_images] 12-byte bitmap descriptors.
	d->plane_info = de_mallocarray(c, d->num_planes, sizeof(struct plane_info_struct));
	for(i=0; i<d->num_planes; i++) {
		do_read_plane_info(c, d, &d->plane_info[i], 8+12*i);
	}

	// The PIC format seems like it was intended to store an arbitrary
	// number of bilevel images, but some of them are clearly planes that
	// are intended to be combined to form an image. I don't know for sure
	// how I'm supposed to do that.

	format = detect_format(c, d);

	switch(format) {
	case PPIC_FMT_2_2:
		for(i=0; i+1<d->num_planes; i+=2) {
			do_bitmap_2planes(c, d, i, i+1);
		}
		break;
	case PPIC_FMT_1_2:
		do_bitmap_1plane(c, d, 0);
		for(i=1; i+1<d->num_planes; i+=2) {
			do_bitmap_2planes(c, d, i, i+1);
		}
		break;
	default:
		for(i=0; i<d->num_planes; i++) {
			do_bitmap_1plane(c, d, i);
		}
	}

	de_crcobj_destroy(d->crco);
	de_free(c, d->plane_info);
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
pushq $0x20
popq %rsi
callq 0xdecaf
movq %rax, %r14
leaq 0x74ecc(%rip), %rsi     # 0x12c626
movq %rbx, %rdi
callq 0xdfa87
testq %rax, %rax
je 0xb776f
movl $0x1, 0x8(%r14)
pushq $0x20
popq %rsi
movq %rbx, %rdi
callq 0xe0ae9
movq %rax, 0x18(%r14)
movq 0x18(%rbx), %rdi
pushq $0x6
popq %rsi
callq 0xe3fa3
movq %rax, (%r14)
leaq 0x74e9e(%rip), %rsi     # 0x12c632
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq (%r14), %rsi
pushq $0x30
popq %rdx
movq %rbx, %rdi
callq 0xdf66f
movq %rax, 0x10(%r14)
pushq $0x14
popq %r13
xorl %eax, %eax
movq (%r14), %rbp
cmpq %rbp, %rax
jge 0xb794a
movq %rax, 0x8(%rsp)
movq 0x10(%r14), %r15
leaq -0xc(%r13), %rbp
movq %rbp, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, -0x30(%r15,%r13,4)
movups %xmm0, -0x40(%r15,%r13,4)
movups %xmm0, -0x50(%r15,%r13,4)
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %r12
movq %r12, %rsi
callq 0xe3fc4
movl %eax, -0x28(%r15,%r13,4)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3fc4
movq %rax, -0x50(%r15,%r13,4)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3fc4
movq %rax, -0x48(%r15,%r13,4)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3fc4
movq %rax, -0x38(%r15,%r13,4)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe40bf
addq %r13, %rax
movq %rax, -0x40(%r15,%r13,4)
movq -0x50(%r15,%r13,4), %rax
addq $0xf, %rax
cqto
pushq $0x10
popq %rcx
idivq %rcx
addq %rax, %rax
movq %rax, -0x30(%r15,%r13,4)
movq %rbx, %rdi
leaq 0x74de4(%rip), %rsi     # 0x12c64f
movq %rbp, %rdx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
pushq $0x1
popq %rsi
callq 0xdeba7
movq -0x40(%r15,%r13,4), %rdx
movq %rbx, %rdi
leaq 0x74dd9(%rip), %rsi     # 0x12c668
xorl %eax, %eax
callq 0xde896
movq -0x38(%r15,%r13,4), %rdx
movq %rbx, %rdi
leaq 0x74dd2(%rip), %rsi     # 0x12c677
xorl %eax, %eax
callq 0xde896
movq -0x50(%r15,%r13,4), %rsi
movq -0x48(%r15,%r13,4), %rdx
movq %rbx, %rdi
callq 0xdf038
movl -0x28(%r15,%r13,4), %edx
movq %rbx, %rdi
leaq 0x8010f(%rip), %rsi     # 0x1379dc
xorl %eax, %eax
callq 0xde896
movq 0x18(%r14), %rdi
callq 0xe0e44
movq 0x18(%r14), %rdi
movq 0x18(%rbx), %rsi
movq -0x40(%r15,%r13,4), %rdx
movq -0x38(%r15,%r13,4), %rcx
callq 0xe0f33
movq 0x18(%r14), %rdi
callq 0xe0e95
movl %eax, %ebp
movq %rbx, %rdi
leaq 0x5094e(%rip), %rsi     # 0x108257
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movl -0x28(%r15,%r13,4), %edx
cmpl %edx, %ebp
je 0xb792e
movq %rbx, %rdi
leaq 0x74d61(%rip), %rsi     # 0x12c686
movl %ebp, %ecx
xorl %eax, %eax
callq 0xdf05a
movq %rbx, %rdi
pushq $-0x1
popq %rsi
callq 0xdeba7
movq 0x8(%rsp), %rax
incq %rax
addq $0xc, %r13
jmp 0xb77b8
cmpl $0x0, 0x8(%r14)
je 0xb7973
xorl %r15d, %r15d
cmpq %rbp, %r15
jge 0xb7a4d
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xb7b2c
incq %r15
movq (%r14), %rbp
jmp 0xb7954
cmpq $0x3, %rbp
jl 0xb79df
movq 0x10(%r14), %rax
cmpq $0x18, (%rax)
jne 0xb7a17
movb $0x1, %cl
cmpq $0x18, 0x8(%rax)
jne 0xb79fa
cmpq $0x30, 0x30(%rax)
jne 0xb7951
cmpq $0x30, 0x38(%rax)
jne 0xb7951
pushq $0x1
popq %rsi
movq %r14, %rdi
callq 0xb7b76
testl %eax, %eax
je 0xb7951
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xb7b2c
pushq $0x1
popq %r15
leaq 0x1(%r15), %rcx
cmpq (%r14), %rcx
jge 0xb7a4d
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xb7aac
addq $0x2, %r15
jmp 0xb79be
testq %rbp, %rbp
jle 0xb7951
movq 0x10(%r14), %rax
cmpq $0x18, (%rax)
jne 0xb7a0d
cmpq $0x18, 0x8(%rax)
setne %cl
cmpq $0x2, %rbp
jb 0xb7951
testb %cl, %cl
jne 0xb7a17
jmp 0xb7951
cmpq $0x1, %rbp
je 0xb7951
movq %r14, %rdi
xorl %esi, %esi
callq 0xb7b76
testl %eax, %eax
je 0xb7951
pushq $0x1
popq %r15
cmpq %rbp, %r15
jge 0xb7a4d
leaq -0x1(%r15), %rdx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0xb7aac
movq (%r14), %rbp
addq $0x2, %r15
jmp 0xb7a2d
movq 0x18(%r14), %rdi
callq 0xe0e8d
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0xded32
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq