// File: /workspace/github_c_makefile_star2_O1/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
movl $0x20, %esi
callq 0xe969b
movq %rax, %r14
leaq 0x79aae(%rip), %rsi     # 0x139596
movq %rbx, %rdi
callq 0xea6cb
testq %rax, %rax
je 0xbfafd
movl $0x1, 0x8(%r14)
movq %rbx, %rdi
movl $0x20, %esi
callq 0xeba56
movq %rax, 0x18(%r14)
movq 0x18(%rbx), %rdi
movl $0x6, %esi
callq 0xef1ca
movq %rax, (%r14)
leaq 0x79a7c(%rip), %rsi     # 0x1395a2
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq (%r14), %rsi
movl $0x30, %edx
movq %rbx, %rdi
callq 0xea259
movq %rax, 0x10(%r14)
movq (%r14), %r15
testq %r15, %r15
jle 0xbfcf4
movl $0x14, %r12d
xorl %r13d, %r13d
movq %r14, 0x8(%rsp)
movq 0x10(%r14), %r15
leaq -0xc(%r12), %rbp
movq %rbp, 0x10(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, -0x30(%r15,%r12,4)
movups %xmm0, -0x40(%r15,%r12,4)
movups %xmm0, -0x50(%r15,%r12,4)
movq 0x18(%rbx), %rdi
leaq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0xef1ed
movl %eax, -0x28(%r15,%r12,4)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef1ed
movq %rax, -0x50(%r15,%r12,4)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef1ed
movq %rax, -0x48(%r15,%r12,4)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef1ed
movq %rax, -0x38(%r15,%r12,4)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
movq 0x8(%rsp), %r14
callq 0xef3a0
addq %r12, %rax
movq %rax, -0x40(%r15,%r12,4)
movq -0x50(%r15,%r12,4), %rax
leaq 0xf(%rax), %rcx
addq $0x1e, %rax
testq %rcx, %rcx
cmovnsq %rcx, %rax
sarq $0x4, %rax
addq %rax, %rax
movq %rax, -0x30(%r15,%r12,4)
movq %rbx, %rdi
leaq 0x799b0(%rip), %rsi     # 0x1395bf
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq -0x40(%r15,%r12,4), %rdx
movq %rbx, %rdi
leaq 0x799a3(%rip), %rsi     # 0x1395d8
xorl %eax, %eax
callq 0xe9226
movq -0x38(%r15,%r12,4), %rdx
movq %rbx, %rdi
leaq 0x7999c(%rip), %rsi     # 0x1395e7
xorl %eax, %eax
callq 0xe9226
movq -0x50(%r15,%r12,4), %rsi
movq -0x48(%r15,%r12,4), %rdx
movq %rbx, %rdi
callq 0xe9b05
movl -0x28(%r15,%r12,4), %edx
movq %rbx, %rdi
leaq 0x84cd9(%rip), %rsi     # 0x14494c
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r14), %rdi
callq 0xebdc3
movq 0x18(%r14), %rdi
movq 0x18(%rbx), %rsi
movq -0x40(%r15,%r12,4), %rdx
movq -0x38(%r15,%r12,4), %rcx
callq 0xebf09
movq 0x18(%r14), %rdi
callq 0xebe2a
movl %eax, %ebp
movq %rbx, %rdi
leaq 0x55584(%rip), %rsi     # 0x115233
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movl -0x28(%r15,%r12,4), %edx
cmpl %edx, %ebp
je 0xbfcd4
movq %rbx, %rdi
leaq 0x7992b(%rip), %rsi     # 0x1395f6
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe9b27
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
incq %r13
movq (%r14), %r15
addq $0xc, %r12
cmpq %r15, %r13
jl 0xbfb60
cmpl $0x0, 0x8(%r14)
je 0xbfd43
movl $0x1, %ecx
cmpl $0x2, %ecx
je 0xbfdbe
cmpl $0x3, %ecx
jne 0xbfdf4
cmpq $0x2, (%r14)
jl 0xbfe13
xorl %r15d, %r15d
leaq 0x1(%r15), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xbfe76
leaq 0x3(%r15), %rax
addq $0x2, %r15
cmpq (%r14), %rax
jl 0xbfd1f
jmp 0xbfe13
cmpq $0x3, %r15
jl 0xbfd7e
movq 0x10(%r14), %rax
cmpq $0x18, (%rax)
jne 0xbfd7e
cmpq $0x18, 0x8(%rax)
jne 0xbfd7e
cmpq $0x30, 0x30(%rax)
jne 0xbfd7e
cmpq $0x30, 0x38(%rax)
jne 0xbfd7e
movq %r14, %rdi
movl $0x1, %esi
callq 0xbff52
movl $0x2, %ecx
testl %eax, %eax
jne 0xbfd00
testq %r15, %r15
jle 0xbfd98
movq 0x10(%r14), %rax
cmpq $0x18, (%rax)
jne 0xbfd98
cmpq $0x18, 0x8(%rax)
je 0xbfcfb
cmpq $0x2, %r15
jl 0xbfcfb
movq %r14, %rdi
xorl %esi, %esi
callq 0xbff52
movl $0x3, %ecx
testl %eax, %eax
jne 0xbfd00
jmp 0xbfcfb
movq %rbx, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xbff00
cmpq $0x3, (%r14)
jl 0xbfe13
movl $0x2, %r15d
leaq -0x1(%r15), %rdx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0xbfe76
addq $0x2, %r15
cmpq (%r14), %r15
jl 0xbfdd7
jmp 0xbfe13
cmpq $0x0, (%r14)
jle 0xbfe13
xorl %r15d, %r15d
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xbff00
incq %r15
cmpq (%r14), %r15
jl 0xbfdfd
movq 0x18(%r14), %rdi
callq 0xebe1d
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0xe9752
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq