// File: /workspace/github_c_makefile_star2_O3/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
movq $0x0, (%r15)
movl $0x20, %esi
callq 0xe8b2b
movq %rax, %r14
movq %rbx, %rdi
callq 0xea504
movq %rax, 0x18(%r14)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeeb4b
movl %eax, %ebp
xorl %edx, %edx
cmpb $0x42, %al
je 0x6a8ba
movl $0x1, %edx
cmpb $0x52, %bpl
jne 0x6a9bf
leaq 0xb9298(%rip), %rsi     # 0x123b59
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %rsp, %r12
movq %r12, %rsi
callq 0xeebeb
movq %rax, %r15
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xeebeb
movq %rax, %r12
leaq 0xbaeb7(%rip), %rsi     # 0x1257aa
movq %rbx, %rdi
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe8617
cmpq %r12, %r15
setne %al
leaq -0x32(%r15), %rcx
cmpq $0x47f, %rcx            # imm = 0x47F
setae %cl
orb %al, %cl
jne 0x6a93b
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 0xeebeb
movq %rax, 0x8(%r14)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x10(%r14)
leaq (,%rax,8), %rsi
movq %rsi, (%r14)
movq 0x8(%r14), %rdx
movq %rbx, %rdi
callq 0xe8f88
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %rbx, %rdi
callq 0xf1db1
testl %eax, %eax
je 0x6aaea
cmpb $0x42, %bpl
jne 0x6a9d5
movq 0x18(%rbx), %rdi
movq (%rsp), %rsi
movq (%r14), %rdx
movq 0x8(%r14), %rcx
movq 0x10(%r14), %r8
movl $0x1, %r9d
pushq $0x0
pushq 0x18(%r14)
callq 0xf2a98
addq $0x10, %rsp
jmp 0x6aaea
leaq 0xbadc8(%rip), %rsi     # 0x12578e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x6aaea
movq 0x8(%r14), %r12
imulq (%r14), %r12
movq %rbx, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0xf09c5
movq %rax, %r15
movq %rax, %rdi
callq 0xf0855
movq $0x9, 0x8(%rsp)
xorl %r13d, %r13d
testq %r12, %r12
jle 0x6aa4d
movq 0x8(%rsp), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0x6aa4d
leaq 0x8(%rsp), %rsi
callq 0xeeb4b
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 0xf0eba
addq %rbp, %r13
cmpq %r12, %r13
jl 0x6aa09
movq %r15, %rdi
callq 0xf08ba
cmpq %r12, %r13
je 0x6aa71
leaq 0xbad55(%rip), %rsi     # 0x1257b6
movq %rbx, %rdi
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0xe8faa
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %rbx, %rdi
movl $0x1, %ecx
callq 0xf266a
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
movl $0x8, %edx
movq %r15, %rdi
xorl %esi, %esi
movq %r13, %r8
movq %r12, %r9
pushq $0x0
callq 0xf2f79
addq $0x10, %rsp
movq 0x18(%r14), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0xf22d0
movq %r15, %rdi
callq 0xf052b
movq %r12, %rdi
callq 0xf228d
movq 0x18(%r14), %rsi
movq %rbx, %rdi
callq 0xea50e
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq