// File: /workspace/github_c_makefile_star2_O3/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 0xe8b2b
movq %rax, %r14
leaq 0x7aaf6(%rip), %rsi     # 0x139926
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0xbee45
movl $0x1, 0x8(%r14)
movq %rbx, %rdi
movl $0x20, %esi
callq 0xeb499
movq %rax, 0x18(%r14)
movq 0x18(%rbx), %rdi
movl $0x6, %esi
callq 0xeebc8
movq %rax, (%r14)
leaq 0x7aac4(%rip), %rsi     # 0x139932
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq (%r14), %rsi
movl $0x30, %edx
movq %rbx, %rdi
callq 0xe96f7
movq %rax, 0x10(%r14)
movq (%r14), %rax
testq %rax, %rax
jle 0xbf03c
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 0xeebeb
movl %eax, -0x28(%r15,%r12,4)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xeebeb
movq %rax, -0x50(%r15,%r12,4)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xeebeb
movq %rax, -0x48(%r15,%r12,4)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xeebeb
movq %rax, -0x38(%r15,%r12,4)
movq 0x18(%rbx), %rdi
movq %r14, %rsi
movq 0x8(%rsp), %r14
callq 0xeed83
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 0x7a9f8(%rip), %rsi     # 0x13994f
movq %rbp, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq -0x40(%r15,%r12,4), %rdx
movq %rbx, %rdi
leaq 0x7a9eb(%rip), %rsi     # 0x139968
xorl %eax, %eax
callq 0xe8617
movq -0x38(%r15,%r12,4), %rdx
movq %rbx, %rdi
leaq 0x7a9e4(%rip), %rsi     # 0x139977
xorl %eax, %eax
callq 0xe8617
movq -0x50(%r15,%r12,4), %rsi
movq -0x48(%r15,%r12,4), %rdx
movq %rbx, %rdi
callq 0xe8f88
movl -0x28(%r15,%r12,4), %edx
movq %rbx, %rdi
leaq 0x85e71(%rip), %rsi     # 0x144e2c
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r14), %rdi
callq 0xeb7fe
movq 0x18(%r14), %rdi
movq 0x18(%rbx), %rsi
movq -0x40(%r15,%r12,4), %rdx
movq -0x38(%r15,%r12,4), %rcx
callq 0xeb944
movq 0x18(%r14), %rdi
callq 0xeb865
movl %eax, %ebp
movq %rbx, %rdi
leaq 0x562ec(%rip), %rsi     # 0x1152e3
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movl -0x28(%r15,%r12,4), %edx
cmpl %edx, %ebp
je 0xbf01c
movq %rbx, %rdi
leaq 0x7a973(%rip), %rsi     # 0x139986
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe8faa
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
incq %r13
movq (%r14), %rax
addq $0xc, %r12
cmpq %rax, %r13
jl 0xbeea8
cmpl $0x0, 0x8(%r14)
je 0xbf091
testq %rax, %rax
jle 0xbf062
xorl %r15d, %r15d
movq 0x10(%r14), %rsi
movq %rbx, %rdi
movq %r15, %rdx
callq 0xbf23d
incq %r15
cmpq (%r14), %r15
jl 0xbf04b
movq 0x18(%r14), %rdi
callq 0xeb858
movq 0x10(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0x3, %rax
jl 0xbf10e
movq 0x10(%r14), %r15
cmpq $0x18, (%r15)
jne 0xbf146
movb $0x1, %cl
cmpq $0x18, 0x8(%r15)
jne 0xbf129
cmpq $0x30, 0x30(%r15)
jne 0xbf048
cmpq $0x30, 0x38(%r15)
jne 0xbf048
movq %r14, %rdi
movl $0x1, %esi
callq 0xbf28e
testl %eax, %eax
je 0xbf048
movq %rbx, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0xbf23d
cmpq $0x3, (%r14)
jl 0xbf062
movl $0x2, %r15d
leaq -0x1(%r15), %rdx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0xbf1b3
addq $0x2, %r15
cmpq (%r14), %r15
jl 0xbf0ee
jmp 0xbf062
testq %rax, %rax
jle 0xbf062
movq 0x10(%r14), %rcx
cmpq $0x18, (%rcx)
jne 0xbf13c
cmpq $0x18, 0x8(%rcx)
setne %cl
cmpq $0x2, %rax
jb 0xbf048
testb %cl, %cl
jne 0xbf146
jmp 0xbf048
cmpq $0x1, %rax
je 0xbf048
movq %r14, %rdi
xorl %esi, %esi
callq 0xbf28e
testl %eax, %eax
je 0xbf048
xorl %r15d, %r15d
leaq 0x1(%r15), %rcx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0xbf1b3
leaq 0x3(%r15), %rax
addq $0x2, %r15
cmpq (%r14), %rax
jl 0xbf15b
jmp 0xbf062