// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/misc2.c
// Function: de_run_crg
static void de_run_crg(deark *c, de_module_params *mparams)
{
	i64 width, height;
	i64 rowspan;
	i64 pos;
	u8 b1, b2;
	i64 count;
	i64 cmpr_img_start;
	i64 num_cmpr_bytes;
	dbuf *unc_pixels = NULL;

	width = de_getu32be(20);
	height = de_getu32be(24);
	de_dbg_dimensions(c, width, height);
	if(!de_good_image_dimensions(c, width, height)) goto done;

	b1 = de_getbyte(32);
	if(b1!=0x01) {
		de_err(c, "Unsupported CRG format");
		goto done;
	}

	num_cmpr_bytes = de_getu32be(38);
	de_dbg(c, "compressed data size: %d", (int)num_cmpr_bytes);
	cmpr_img_start = 42;

	if(cmpr_img_start + num_cmpr_bytes > c->infile->len) {
		num_cmpr_bytes = c->infile->len - cmpr_img_start;
	}

	// Uncompress the image
	rowspan = (width+7)/8;
	unc_pixels = dbuf_create_membuf(c, height*rowspan, 1);

	pos = cmpr_img_start;
	while(pos < cmpr_img_start + num_cmpr_bytes) {
		b1 = de_getbyte(pos++);
		if(b1<=0x7f) { // Uncompressed bytes
			count = 1+(i64)b1;
			dbuf_copy(c->infile, pos, count, unc_pixels);
			pos += count;
		}
		else { // A compressed run
			b2 = de_getbyte(pos++);
			count = (i64)(b1-127);
			dbuf_write_run(unc_pixels, b2, count);
		}
	}
	de_dbg(c, "decompressed to %d bytes", (int)unc_pixels->len);

	de_convert_and_write_image_bilevel2(unc_pixels, 0, width, height, rowspan,
		DE_CVTF_WHITEISZERO, NULL, 0);

done:
	dbuf_close(unc_pixels);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
pushq $0x14
popq %rsi
callq 0xe404f
movq %rax, %r12
movq 0x18(%rbx), %rdi
pushq $0x18
popq %rsi
callq 0xe404f
movq %rax, %r13
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0xdf038
movq %rbx, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0xe6be1
testl %eax, %eax
je 0x6563d
movq 0x18(%rbx), %rdi
pushq $0x20
popq %rsi
callq 0xe3eb4
cmpb $0x1, %al
jne 0x6567c
movq 0x18(%rbx), %rdi
pushq $0x26
popq %rsi
callq 0xe404f
movq %rax, %r14
leaq 0xb2dd6(%rip), %rsi     # 0x118355
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xde896
leaq 0x2a(%r14), %rax
movq 0x18(%rbx), %rcx
movq 0x18(%rcx), %rcx
leaq -0x2a(%rcx), %r15
cmpq %rcx, %rax
cmovleq %r14, %r15
movq %r12, 0x10(%rsp)
addq $0x7, %r12
pushq $0x8
popq %rcx
movq %r12, %rax
cqto
idivq %rcx
movq %r13, 0x8(%rsp)
movq %rax, (%rsp)
imulq %rax, %r13
pushq $0x1
popq %rdx
movq %rbx, %rdi
movq %r13, %rsi
callq 0xe59cd
movq %rax, %r12
addq $0x2a, %r15
pushq $0x2a
popq %rbp
cmpq %r15, %rbp
jge 0x65642
movq 0x18(%rbx), %rdi
leaq 0x1(%rbp), %r13
movq %rbp, %rsi
callq 0xe3eb4
movl %eax, %r14d
testb %al, %al
js 0x65618
movzbl %r14b, %r14d
leaq 0x1(%r14), %rdx
movq 0x18(%rbx), %rdi
movq %r13, %rsi
movq %r12, %rcx
callq 0xe45f4
leaq (%r14,%r13), %rbp
incq %rbp
jmp 0x655dc
movq 0x18(%rbx), %rdi
addq $0x2, %rbp
movq %r13, %rsi
callq 0xe3eb4
addb $-0x7f, %r14b
movzbl %r14b, %edx
movzbl %al, %esi
movq %r12, %rdi
callq 0xe5e40
jmp 0x655dc
xorl %r12d, %r12d
jmp 0x65690
movl 0x18(%r12), %edx
leaq 0x9f1cc(%rip), %rsi     # 0x10481a
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %r9
movq %r12, %rdi
xorl %esi, %esi
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
pushq $0x0
pushq $0x0
callq 0xe78af
popq %rax
popq %rcx
jmp 0x65690
leaq 0xb2cbb(%rip), %rsi     # 0x11833e
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movq %r12, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5591