// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/misc2.c
// Function: de_run_lss16
static void de_run_lss16(deark *c, de_module_params *mparams)
{
	struct lss16ctx *d = NULL;
	de_bitmap *img = NULL;
	i64 width, height;
	i64 i;
	i64 xpos, ypos;
	u8 n;
	u8 prev;
	i64 run_len;
	u32 pal[16];

	d = de_malloc(c, sizeof(struct lss16ctx));

	d->pos = 4;
	width = de_getu16le(d->pos);
	height = de_getu16le(d->pos+2);
	de_dbg_dimensions(c, width, height);
	if(!de_good_image_dimensions(c, width, height)) goto done;

	d->pos += 4;
	de_read_simple_palette(c, c->infile, d->pos, 16, 3, pal, 16, DE_RDPALTYPE_VGA18BIT, 0);
	d->pos += 16*3;

	img = de_bitmap_create(c, width, height, 3);

	xpos=0; ypos=0;
	prev=0;
	while(d->pos<c->infile->len && ypos<height) {
		n = lss16_get_nibble(c, d);

		if(n == prev) {
			// A run of pixels
			run_len = (i64)lss16_get_nibble(c, d);
			if(run_len==0) {
				run_len = lss16_get_nibble(c, d);
				run_len |= ((i64)lss16_get_nibble(c, d)<<4);
				run_len += 16;
			}
			for(i=0; i<run_len; i++) {
				de_bitmap_setpixel_rgb(img, xpos, ypos, pal[prev]);
				xpos++;
			}
		}
		else {
			// An uncompressed pixel
			de_bitmap_setpixel_rgb(img, xpos, ypos, pal[n]);
			xpos++;
			prev = n;
		}

		// End of row reached?
		if(xpos>=width) {
			xpos=0;
			ypos++;
			d->nextnibble_valid = 0;
			prev = 0;
		}
	}

	de_bitmap_write_to_file(img, NULL, 0);
done:
	de_bitmap_destroy(img);
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdi, %r14
pushq $0x10
popq %rsi
callq 0xdecaf
movq %rax, %rbx
movq $0x4, (%rax)
movq 0x18(%r14), %rdi
pushq $0x4
popq %rsi
callq 0xe3fa3
movq %rax, %r12
movq 0x18(%r14), %rdi
movq (%rbx), %rsi
addq $0x2, %rsi
callq 0xe3fa3
movq %rax, %r15
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0xdf038
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xe6be1
testl %eax, %eax
je 0x64751
movq (%rbx), %rdx
addq $0x4, %rdx
movq %rdx, (%rbx)
movq 0x18(%r14), %rsi
subq $0x8, %rsp
pushq $0x10
popq %rcx
pushq $0x3
popq %r8
leaq 0x38(%rsp), %r9
movq %r14, %rdi
pushq $0x0
pushq $0x2
pushq $0x10
callq 0xe79ca
addq $0x20, %rsp
addq $0x30, (%rbx)
pushq $0x3
popq %rcx
movq %r14, %rdi
movq %r12, %rsi
movq %r15, 0x28(%rsp)
movq %r15, %rdx
callq 0xe7437
movq %rax, %r13
xorl %ebp, %ebp
movq %r14, 0x10(%rsp)
movq %rbx, 0x20(%rsp)
movq %r12, 0x18(%rsp)
xorl %r15d, %r15d
movq $0x0, 0x8(%rsp)
movq (%rbx), %rax
movq 0x18(%r14), %rcx
cmpq 0x18(%rcx), %rax
jge 0x64743
cmpq 0x28(%rsp), %rbp
jge 0x64743
movq %r14, %rdi
movq %rbx, %rsi
callq 0x690a7
cmpb 0x8(%rsp), %al
jne 0x646a4
movq %r14, %rdi
movq %rbx, %rsi
callq 0x690a7
testb %al, %al
je 0x646cc
movzbl %al, %r12d
jmp 0x646f4
movl %eax, %r12d
movzbl %al, %eax
movl 0x30(%rsp,%rax,4), %ecx
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0xe7287
incq %r15
movq %r15, %rbx
movl %r12d, %eax
movq %rax, 0x8(%rsp)
jmp 0x64724
movq %r14, %rdi
movq %rbx, %rsi
callq 0x690a7
movq %rbx, %rsi
movzbl %al, %ebx
movq %r14, %rdi
callq 0x690a7
movzbl %al, %r12d
shll $0x4, %r12d
orq %rbx, %r12
addq $0x10, %r12
movzbl 0x8(%rsp), %r14d
leaq (%r12,%r15), %rbx
subq $0x1, %r12
jb 0x6471c
movl 0x30(%rsp,%r14,4), %ecx
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0xe7287
incq %r15
jmp 0x646fe
movq %rbx, %r15
movq 0x10(%rsp), %r14
movq 0x18(%rsp), %r12
cmpq %r12, %rbx
movq 0x20(%rsp), %rbx
jl 0x64662
incq %rbp
andl $0x0, 0x8(%rbx)
jmp 0x64656
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe70b7
jmp 0x64754
xorl %r13d, %r13d
movq %r13, %rdi
callq 0xe7084
movq %r14, %rdi
movq %rbx, %rsi
callq 0xded32
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq