// File: /workspace/github_c_makefile_star2_O3/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, %rbx
movl $0x10, %esi
callq 0xe8b2b
movq %rax, %r14
movq $0x4, (%rax)
movq 0x18(%rbx), %rdi
movl $0x4, %esi
callq 0xeebc8
movq %rax, %r12
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
addq $0x2, %rsi
callq 0xeebc8
movq %rax, %r15
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0xe8f88
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0xf1db1
testl %eax, %eax
je 0x6808d
movq (%r14), %rdx
addq $0x4, %rdx
movq %rdx, (%r14)
movq 0x18(%rbx), %rsi
subq $0x8, %rsp
leaq 0x38(%rsp), %r9
movl $0x10, %ecx
movl $0x3, %r8d
movq %rbx, %rdi
pushq $0x0
pushq $0x2
pushq $0x10
callq 0xf2cb1
addq $0x20, %rsp
addq $0x30, (%r14)
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
movl $0x3, %ecx
callq 0xf266a
movq %rax, %r13
movq (%r14), %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
setge %al
movq %r15, 0x28(%rsp)
testq %r15, %r15
setle %cl
orb %al, %cl
jne 0x6807f
movq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %rbx, 0x18(%rsp)
movq %r14, 0x10(%rsp)
movq %r12, 0x20(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6cdb9
cmpb 0x8(%rsp), %al
jne 0x67fbf
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6cdb9
testb %al, %al
je 0x67fe7
movzbl %al, %r12d
jmp 0x6800f
movl %eax, %r12d
movzbl %al, %eax
movl 0x30(%rsp,%rax,4), %ecx
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0xf24ba
incq %r15
movq %r15, %r14
movl %r12d, %eax
movq %rax, 0x8(%rsp)
jmp 0x68037
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6cdb9
movq %rbx, %rdi
movzbl %al, %ebx
movq %r14, %rsi
callq 0x6cdb9
movzbl %al, %r12d
shll $0x4, %r12d
orq %rbx, %r12
addq $0x10, %r12
movzbl 0x8(%rsp), %ebx
leaq (%r12,%r15), %r14
movl 0x30(%rsp,%rbx,4), %ecx
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0xf24ba
incq %r15
decq %r12
jne 0x68018
movq 0x18(%rsp), %rbx
movq 0x20(%rsp), %r12
cmpq %r12, %r14
jge 0x6804b
movq %r14, %r15
movq 0x10(%rsp), %r14
jmp 0x68067
incq %rbp
movq 0x10(%rsp), %r14
movl $0x0, 0x8(%r14)
xorl %r15d, %r15d
movq $0x0, 0x8(%rsp)
movq (%r14), %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jge 0x6807f
cmpq 0x28(%rsp), %rbp
jl 0x67f99
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf22dc
jmp 0x68090
xorl %r13d, %r13d
movq %r13, %rdi
callq 0xf228d
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq