// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/misc2.c
// Function: de_run_cserve_rle
static void de_run_cserve_rle(deark *c, de_module_params *mparams)
{
	int fmt;
	i64 w, expected_h, actual_h;
	i64 max_npixels;
	i64 npixels_expected;
	i64 npixels_found;
	i64 pos;
	u8 next_color;
	dbuf *unc_pixels = NULL;
	de_bitmap *img = NULL;
	de_color pal[256];

	fmt = get_cserve_rle_fmt(c);
	if(fmt==1) {
		w = 128;
		expected_h = 96;
	}
	else if(fmt==2) {
		w = 256;
		expected_h = 192;
	}
	else {
		de_err(c, "Not a CompuServe RLE file");
		goto done;
	}

	de_dbg(c, "width: %"I64_FMT, w);
	npixels_expected = w*expected_h;
#define CSRLE_MAX_H 1920 // arbitrary
	max_npixels = w*CSRLE_MAX_H;
	unc_pixels = dbuf_create_membuf(c, max_npixels, 0x1);
	dbuf_enable_wbuffer(unc_pixels);
	pos = 3;
	npixels_found = 0;
	next_color = 1; // 1=black, 2=white, 0=unset
	while(1) {
		u8 x;

		if(npixels_found >= max_npixels) {
			break;
		}
		if(pos >= c->infile->len) break;

		x = de_getbyte_p(&pos);
		if(x>=0x20) {
			i64 count;

			count = (i64)x - 0x20;
			dbuf_write_run(unc_pixels, next_color, count);
			npixels_found += count;
			next_color = (next_color==1)?2:1;
		}
		else if(x==0x1b) {
			break;
		}
		// TODO: Could do more error checking
	}

	dbuf_flush(unc_pixels);
	actual_h = expected_h;
	if(npixels_found != npixels_expected) {
		de_warn(c, "Expected %"I64_FMT" pixels, found %"I64_FMT"%s", npixels_expected,
			npixels_found,
			((npixels_found>npixels_expected && !c->padpix)?" (try -padpix)":""));
		if(npixels_found>npixels_expected && c->padpix) {
			actual_h = de_pad_to_n(npixels_found, w)/w;
		}
	}

	img = de_bitmap_create(c, w, actual_h, 3);
	de_zeromem(pal, sizeof(pal));
	// Images that don't end cleanly are common enough that we go to
	// the trouble of making missing pixels a special color.
	pal[0] = DE_MAKE_RGB(255,0,255);
	pal[1] = DE_STOCKCOLOR_BLACK;
	pal[2] = DE_STOCKCOLOR_WHITE;
	de_convert_image_paletted(unc_pixels, 0, 8, w, pal, img, 0);
	de_bitmap_write_to_file(img, NULL, DE_CREATEFLAG_OPT_IMAGE);

done:
	dbuf_close(unc_pixels);
	de_bitmap_destroy(img);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4b0, %rsp            # imm = 0x4B0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x8(%rbp), %rdi
callq 0x9b840
movl %eax, -0x14(%rbp)
cmpl $0x1, -0x14(%rbp)
jne 0x98f47
movq $0x80, -0x20(%rbp)
movq $0x60, -0x28(%rbp)
jmp 0x98f78
cmpl $0x2, -0x14(%rbp)
jne 0x98f5f
movq $0x100, -0x20(%rbp)     # imm = 0x100
movq $0xc0, -0x28(%rbp)
jmp 0x98f76
movq -0x8(%rbp), %rdi
leaq 0x117c3f(%rip), %rsi    # 0x1b0ba9
movb $0x0, %al
callq 0x15bfb0
jmp 0x991ea
jmp 0x98f78
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x117c3c(%rip), %rsi    # 0x1b0bc3
movb $0x0, %al
callq 0x15b0d0
movq -0x20(%rbp), %rax
imulq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
imulq $0x780, -0x20(%rbp), %rax # imm = 0x780
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl $0x1, %edx
callq 0x168020
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
callq 0x167b90
movq $0x3, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movb $0x1, -0x51(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x38(%rbp), %rax
jl 0x98fe9
jmp 0x99088
movq -0x50(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0x99000
jmp 0x99088
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x50(%rbp), %rsi
callq 0x165170
movb %al, -0x471(%rbp)
movzbl -0x471(%rbp), %eax
cmpl $0x20, %eax
jl 0x99073
movzbl -0x471(%rbp), %eax
subq $0x20, %rax
movq %rax, -0x480(%rbp)
movq -0x60(%rbp), %rdi
movb -0x51(%rbp), %al
movq -0x480(%rbp), %rdx
movzbl %al, %esi
callq 0x168800
movq -0x480(%rbp), %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
movzbl -0x51(%rbp), %edx
movl $0x1, %eax
movl $0x2, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
movb %al, -0x51(%rbp)
jmp 0x99083
movzbl -0x471(%rbp), %eax
cmpl $0x1b, %eax
jne 0x99081
jmp 0x99088
jmp 0x99083
jmp 0x98fda
movq -0x60(%rbp), %rdi
callq 0x167c30
movq -0x28(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
je 0x99163
movq -0x8(%rbp), %rax
movq %rax, -0x4a0(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x498(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x490(%rbp)
movq -0x48(%rbp), %rcx
xorl %eax, %eax
cmpq -0x40(%rbp), %rcx
movb %al, -0x481(%rbp)
jle 0x990f0
movq -0x8(%rbp), %rax
cmpb $0x0, 0xd5(%rax)
setne %al
xorb $-0x1, %al
movb %al, -0x481(%rbp)
movq -0x490(%rbp), %rcx
movq -0x498(%rbp), %rdx
movq -0x4a0(%rbp), %rdi
movb -0x481(%rbp), %sil
leaq 0x137abd(%rip), %r8     # 0x1d0bd0
leaq 0x117ad5(%rip), %rax    # 0x1b0bef
testb $0x1, %sil
cmovneq %rax, %r8
leaq 0x117aa5(%rip), %rsi    # 0x1b0bce
movb $0x0, %al
callq 0x15bc10
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
jle 0x99161
movq -0x8(%rbp), %rax
movzbl 0xd5(%rax), %eax
cmpl $0x0, %eax
je 0x99161
movq -0x48(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x15ce90
cqto
idivq -0x20(%rbp)
movq %rax, -0x30(%rbp)
jmp 0x99163
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x30(%rbp), %rdx
movl $0x3, %ecx
callq 0x16b750
movq %rax, -0x68(%rbp)
leaq -0x470(%rbp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x19140
movl $0xffff00ff, -0x470(%rbp) # imm = 0xFFFF00FF
movl $0xff000000, -0x46c(%rbp) # imm = 0xFF000000
movl $0xffffffff, -0x468(%rbp) # imm = 0xFFFFFFFF
movq -0x60(%rbp), %rdi
movq -0x20(%rbp), %rcx
leaq -0x470(%rbp), %r8
movq -0x68(%rbp), %r9
xorl %eax, %eax
movl %eax, %esi
movl $0x8, %edx
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x16c520
movq -0x68(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x16afd0
movq -0x60(%rbp), %rdi
callq 0x1676a0
movq -0x68(%rbp), %rdi
callq 0x16af60
addq $0x4b0, %rsp            # imm = 0x4B0
popq %rbp
retq
nopw %cs:(%rax,%rax)