// File: /workspace/github_c_makefile_star2_O3/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
movl $0x14, %esi
callq 0xeed0d
movq %rax, %r15
movq 0x18(%rbx), %rdi
movl $0x18, %esi
callq 0xeed0d
movq %rax, %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xe8f88
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xf1db1
testl %eax, %eax
je 0x69039
movq 0x18(%rbx), %rdi
movl $0x20, %esi
callq 0xeead3
cmpb $0x1, %al
jne 0x6903e
movq 0x18(%rbx), %rdi
movl $0x26, %esi
callq 0xeed0d
movq %rax, %r14
leaq 0xbc623(%rip), %rsi     # 0x125555
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe8617
leaq 0x2a(%r14), %rax
movq 0x18(%rbx), %rcx
movq 0x18(%rcx), %rcx
leaq -0x2a(%rcx), %r13
cmpq %rcx, %rax
cmovleq %r14, %r13
leaq 0x7(%r15), %rax
movq %r15, 0x10(%rsp)
addq $0xe, %r15
testq %rax, %rax
cmovnsq %rax, %r15
sarq $0x3, %r15
movq %r12, 0x8(%rsp)
movq %r15, (%rsp)
imulq %r15, %r12
movq %rbx, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0xf09c5
movq %rax, %r12
testq %r13, %r13
jle 0x68ffb
addq $0x2a, %r13
movl $0x2a, %ebp
movq 0x18(%rbx), %rdi
leaq 0x1(%rbp), %r15
movq %rbp, %rsi
callq 0xeead3
movl %eax, %r14d
testb %al, %al
js 0x68fd3
movzbl %r14b, %r14d
leaq 0x1(%r14), %rdx
movq 0x18(%rbx), %rdi
movq %r15, %rsi
movq %r12, %rcx
callq 0xef481
leaq (%r15,%r14), %rbp
incq %rbp
jmp 0x68ff6
movq 0x18(%rbx), %rdi
addq $0x2, %rbp
movq %r15, %rsi
callq 0xeead3
addb $-0x7f, %r14b
movzbl %r14b, %edx
movzbl %al, %esi
movq %r12, %rdi
callq 0xf0eba
cmpq %r13, %rbp
jl 0x68f9c
movl 0x18(%r12), %edx
leaq 0xa8836(%rip), %rsi     # 0x11183d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %r12, %rdi
xorl %esi, %esi
movq 0x10(%rsp), %rdx
movq 0x8(%rsp), %rcx
movq (%rsp), %r8
movl $0x1, %r9d
pushq $0x0
pushq $0x0
callq 0xf2b5b
addq $0x10, %rsp
jmp 0x69052
xorl %r12d, %r12d
jmp 0x69052
leaq 0xbc4f9(%rip), %rsi     # 0x12553e
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %r12, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf052b