// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/misc2.c
// Function: de_run_lotus_mscr
static void de_run_lotus_mscr(deark *c, de_module_params *mparams)
{
	i64 pos = 0;
	i64 dens1, dens2;
	u8 x;
	u8 is_rle;
	struct lotus_mscr_ctx *d = NULL;

	d = de_malloc(c, sizeof(struct lotus_mscr_ctx));
	d->fi = de_finfo_create(c);
	x = de_getbyte_p(&pos);
	if(x=='B') {
		is_rle = 0;
	}
	else if(x=='R') {
		is_rle = 1;
	}
	else {
		de_err(c, "Not a Lotus Manuscript file");
		goto done;
	}
	de_dbg(c, "compressed: %u", (UI)is_rle);
	dens1 = de_getu16le_p(&pos);
	dens2 = de_getu16le_p(&pos);
	de_dbg(c, "dpi: %d, %d", (int)dens1, (int)dens2);
	// TODO: Figure out which dpi is which.
	// ART2WP thinks x is first, but that's a little surprising when the y
	// *dimension* is first, so I'm not trusting it.
	if(dens1==dens2 && dens1>=50 && dens1<=1200) {
		d->fi->density.code = DE_DENSITY_DPI;
		d->fi->density.xdens = (double)dens1;
		d->fi->density.ydens = d->fi->density.xdens;
	}
	d->h = de_getu16le_p(&pos);
	d->rowspan = de_getu16le_p(&pos);
	d->w = d->rowspan * 8;
	de_dbg_dimensions(c, d->w, d->h);
	if(!de_good_image_dimensions(c, d->w, d->h)) goto done;
	if(is_rle) {
		do_lotus_mscr_rle(c, d);
	}
	else {
		de_convert_and_write_image_bilevel(c->infile, pos, d->w, d->h, d->rowspan,
			DE_CVTF_WHITEISZERO, d->fi, 0);
	}

done:
	if(d) {
		de_finfo_destroy(c, d->fi);
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x418, %rsp            # imm = 0x418
movq %rdi, %rbx
movq %rsp, %r15
andq $0x0, (%r15)
pushq $0x20
popq %rsi
callq 0xdecaf
movq %rax, %r14
movq %rbx, %rdi
callq 0xdfd1a
movq %rax, 0x18(%r14)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3f2a
movl %eax, %ebp
xorl %edx, %edx
cmpb $0x42, %al
je 0x66dc3
cmpb $0x52, %bpl
jne 0x66ec4
pushq $0x1
popq %rdx
leaq 0xafbcf(%rip), %rsi     # 0x116999
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %rsp, %r12
movq %r12, %rsi
callq 0xe3fc4
movq %rax, %r15
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xe3fc4
movq %rax, %r12
leaq 0xb17ae(%rip), %rsi     # 0x1185aa
movq %rbx, %rdi
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xde896
cmpq %r12, %r15
setne %al
leaq -0x32(%r15), %rcx
cmpq $0x47f, %rcx            # imm = 0x47F
setae %cl
orb %al, %cl
jne 0x66e44
movq 0x18(%r14), %rax
movl $0x2, 0x78(%rax)
cvtsi2sd %r15, %xmm0
movsd %xmm0, 0x80(%rax)
movsd %xmm0, 0x88(%rax)
movq 0x18(%rbx), %rdi
movq %rsp, %r15
movq %r15, %rsi
callq 0xe3fc4
movq %rax, 0x8(%r14)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
movq %rax, 0x10(%r14)
leaq (,%rax,8), %rsi
movq %rsi, (%r14)
movq 0x8(%r14), %rdx
movq %rbx, %rdi
callq 0xdf038
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %rbx, %rdi
callq 0xe6be1
testl %eax, %eax
je 0x66fe7
cmpb $0x42, %bpl
jne 0x66eda
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
movq (%r14), %rdx
movq 0x8(%r14), %rcx
movq 0x10(%r14), %r8
pushq $0x1
popq %r9
pushq $0x0
pushq 0x18(%r14)
callq 0xe7804
popq %rax
popq %rcx
jmp 0x66fe7
leaq 0xb16c3(%rip), %rsi     # 0x11858e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x66fe7
movq 0x8(%r14), %r12
imulq (%r14), %r12
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0xe59cd
movq %rax, %r15
movq %rax, %rdi
callq 0xe589c
leaq 0x8(%rsp), %rax
movq $0x9, (%rax)
xorl %r13d, %r13d
cmpq %r12, %r13
jge 0x66f50
movq 0x8(%rsp), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0x66f50
leaq 0x8(%rsp), %rsi
callq 0xe3f2a
movl %eax, %ecx
shrb $0x7, %cl
andb $0x7f, %al
movzbl %al, %eax
movl $0x80, %edx
cmovel %edx, %eax
movzbl %al, %ebp
movzbl %cl, %esi
movq %r15, %rdi
movq %rbp, %rdx
callq 0xe5e40
addq %rbp, %r13
jmp 0x66f0a
movq %r15, %rdi
callq 0xe58e8
cmpq %r12, %r13
je 0x66f74
leaq 0xb1652(%rip), %rsi     # 0x1185b6
movq %rbx, %rdi
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0xdf05a
movq (%r14), %rsi
movq 0x8(%r14), %rdx
pushq $0x1
popq %rcx
movq %rbx, %rdi
callq 0xe7437
movq %rax, %r12
leaq 0x10(%rsp), %r13
movl $0x400, %edx            # imm = 0x400
movq %r13, %rdi
xorl %esi, %esi
callq 0x18140
movabsq $-0xffffff00000001, %rax # imm = 0xFF000000FFFFFFFF
movq %rax, (%r13)
movq (%r14), %rcx
subq $0x8, %rsp
pushq $0x8
popq %rdx
movq %r15, %rdi
xorl %esi, %esi
movq %r13, %r8
movq %r12, %r9
pushq $0x0
callq 0xe7cbc
popq %rax
popq %rcx
movq 0x18(%r14), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0xe70ab
movq %r15, %rdi
callq 0xe5591
movq %r12, %rdi
callq 0xe7084
movq 0x18(%r14), %rsi
movq %rbx, %rdi
callq 0xdfd24
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq