// File: /workspace/github_c_makefile_star2_O1/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
movl $0x10, %esi
callq 0xe969b
movq %rax, %r12
movq $0x4, (%rax)
movq 0x18(%r14), %rdi
movl $0x4, %esi
callq 0xef1ca
movq %rax, %rbx
movq 0x18(%r14), %rdi
movq (%r12), %rsi
addq $0x2, %rsi
callq 0xef1ca
movq %rax, %r15
movq %r14, %rdi
movq %rbx, %rsi
movq %rax, %rdx
callq 0xe9b05
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0xf241d
testl %eax, %eax
je 0x682d1
movq (%r12), %rdx
addq $0x4, %rdx
movq %rdx, (%r12)
movq 0x18(%r14), %rsi
subq $0x8, %rsp
leaq 0x38(%rsp), %r9
movl $0x10, %ecx
movl $0x3, %r8d
movq %r14, %rdi
pushq $0x0
pushq $0x2
pushq $0x10
callq 0xf32f2
addq $0x20, %rsp
addq $0x30, (%r12)
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
movl $0x3, %ecx
callq 0xf2cab
movq %rax, %r13
movq (%r12), %rax
movq 0x18(%r14), %rcx
cmpq 0x18(%rcx), %rax
setge %al
movq %r15, 0x28(%rsp)
testq %r15, %r15
setle %cl
orb %al, %cl
jne 0x682c3
movq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
movq %r14, 0x10(%rsp)
movq %r12, 0x20(%rsp)
movq %rbx, 0x18(%rsp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x6cff6
cmpb 0x8(%rsp), %al
jne 0x681fc
movq %r12, %rbx
movq %r14, %rdi
movq %r12, %rsi
callq 0x6cff6
testb %al, %al
je 0x68226
movzbl %al, %r12d
jmp 0x68258
movl %eax, %r12d
movzbl %al, %eax
movl 0x30(%rsp,%rax,4), %ecx
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0xf2afd
incq %r15
movl %r12d, %eax
movq %rax, 0x8(%rsp)
movq 0x18(%rsp), %rbx
jmp 0x68288
movq %rbx, %r12
movq %r14, %rdi
movq %rbx, %rsi
callq 0x6cff6
movzbl %al, %ebx
movq 0x10(%rsp), %rdi
movq %r12, %rsi
callq 0x6cff6
movzbl %al, %r12d
shll $0x4, %r12d
orq %rbx, %r12
movq 0x10(%rsp), %r14
addq $0x10, %r12
movq 0x18(%rsp), %rbx
testq %r12, %r12
jle 0x68288
movzbl 0x8(%rsp), %r14d
movl 0x30(%rsp,%r14,4), %ecx
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0xf2afd
incq %r15
decq %r12
jne 0x68268
movq 0x10(%rsp), %r14
cmpq %rbx, %r15
movq 0x20(%rsp), %r12
jl 0x682aa
incq %rbp
movl $0x0, 0x8(%r12)
xorl %r15d, %r15d
movq $0x0, 0x8(%rsp)
movq (%r12), %rax
movq 0x18(%r14), %rcx
cmpq 0x18(%rcx), %rax
jge 0x682c3
cmpq 0x28(%rsp), %rbp
jl 0x681d3
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf2923
jmp 0x682d4
xorl %r13d, %r13d
movq %r13, %rdi
callq 0xf28d4
movq %r14, %rdi
movq %r12, %rsi
callq 0xe9752
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq