// File: /workspace/github_c_makefile_star2_O1/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 0xe969b
movq %rax, %r14
movq %rbx, %rdi
callq 0xea9a9
movq %rax, 0x18(%r14)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef14d
movl %eax, %ebp
xorl %edx, %edx
cmpb $0x42, %al
je 0x6aafb
movl $0x1, %edx
cmpb $0x52, %bpl
jne 0x6ac00
leaq 0xb8e77(%rip), %rsi     # 0x123979
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %rsp, %r12
movq %r12, %rsi
callq 0xef1ed
movq %rax, %r15
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0xef1ed
movq %rax, %r12
leaq 0xbaa46(%rip), %rsi     # 0x12557a
movq %rbx, %rdi
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0xe9226
cmpq %r12, %r15
setne %al
leaq -0x32(%r15), %rcx
cmpq $0x47f, %rcx            # imm = 0x47F
setae %cl
orb %al, %cl
jne 0x6ab7c
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 0xef1ed
movq %rax, 0x8(%r14)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x10(%r14)
leaq (,%rax,8), %rsi
movq %rsi, (%r14)
movq 0x8(%r14), %rdx
movq %rbx, %rdi
callq 0xe9b05
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %rbx, %rdi
callq 0xf241d
testl %eax, %eax
je 0x6ad33
cmpb $0x42, %bpl
jne 0x6ac16
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 0xf30d9
addq $0x10, %rsp
jmp 0x6ad33
leaq 0xba957(%rip), %rsi     # 0x12555e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x6ad33
movq 0x8(%r14), %r12
imulq (%r14), %r12
movq %rbx, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0xf0faf
movq %rax, %r15
movq %rax, %rdi
callq 0xf0e3f
leaq 0x8(%rsp), %rax
movq $0x9, (%rax)
xorl %r13d, %r13d
cmpq %r12, %r13
jge 0x6ac90
movq 0x8(%rsp), %rax
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rax
jge 0x6ac90
leaq 0x8(%rsp), %rsi
callq 0xef14d
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 0xf14bd
addq %rbp, %r13
movb $0x1, %al
jmp 0x6ac92
xorl %eax, %eax
testb %al, %al
jne 0x6ac48
movq %r15, %rdi
callq 0xf0ea4
cmpq %r12, %r13
je 0x6acba
leaq 0xba8dc(%rip), %rsi     # 0x125586
movq %rbx, %rdi
movq %r12, %rdx
movq %r13, %rcx
xorl %eax, %eax
callq 0xe9b27
movq (%r14), %rsi
movq 0x8(%r14), %rdx
movq %rbx, %rdi
movl $0x1, %ecx
callq 0xf2cab
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 0xf35b8
addq $0x10, %rsp
movq 0x18(%r14), %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0xf2917
movq %r15, %rdi
callq 0xf0b13
movq %r12, %rdi
callq 0xf28d4
testq %r14, %r14
je 0x6ad4f
movq 0x18(%r14), %rsi
movq %rbx, %rdi
callq 0xea9b3
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq