// File: /workspace/github_c_makefile_star2_O1/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 0xef32a
movq %rax, %r15
movq 0x18(%rbx), %rdi
movl $0x18, %esi
callq 0xef32a
movq %rax, %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xe9b05
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xf241d
testl %eax, %eax
je 0x69289
movq 0x18(%rbx), %rdi
movl $0x20, %esi
callq 0xef0d5
cmpb $0x1, %al
jne 0x6928e
movq 0x18(%rbx), %rdi
movl $0x26, %esi
callq 0xef32a
movq %rax, %r14
leaq 0xbc1a3(%rip), %rsi     # 0x125325
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
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 0xf0faf
movq %rax, %r12
testq %r13, %r13
jle 0x6924b
addq $0x2a, %r13
movl $0x2a, %ebp
movq 0x18(%rbx), %rdi
leaq 0x1(%rbp), %r15
movq %rbp, %rsi
callq 0xef0d5
movl %eax, %r14d
testb %al, %al
js 0x69223
movzbl %r14b, %r14d
leaq 0x1(%r14), %rdx
movq 0x18(%rbx), %rdi
movq %r15, %rsi
movq %r12, %rcx
callq 0xefabc
leaq (%r15,%r14), %rbp
incq %rbp
jmp 0x69246
movq 0x18(%rbx), %rdi
addq $0x2, %rbp
movq %r15, %rsi
callq 0xef0d5
addb $-0x7f, %r14b
movzbl %r14b, %edx
movzbl %al, %esi
movq %r12, %rdi
callq 0xf14bd
cmpq %r13, %rbp
jl 0x691ec
movl 0x18(%r12), %edx
leaq 0xa85d7(%rip), %rsi     # 0x11182e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
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 0xf319c
addq $0x10, %rsp
jmp 0x692a2
xorl %r12d, %r12d
jmp 0x692a2
leaq 0xbc079(%rip), %rsi     # 0x12530e
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %r12, %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf0b13