// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/gemras.c
// Function: decompress_pixels
static void decompress_pixels(deark *c, lctx *d, dbuf *unc_pixels,
	i64 pos1, i64 len)
{
	i64 k;
	i64 plane;
	struct decompr_ctx *dctx = NULL;

	dctx = de_malloc(c, sizeof(struct decompr_ctx));
	dctx->pattern_buf = de_malloc(c, d->patlen);
	dctx->unc_line = dbuf_create_membuf(c, d->rowspan_total, 0);
	dctx->curpos = pos1;

	dctx->rownum = 0;
	while(1) {
		i64 expected_len;

		if(dctx->rownum >= d->h) break;

		dctx->repeat_count = 0;
		dbuf_empty(dctx->unc_line);
		for(plane=0; plane<d->nplanes; plane++) {
			i64 prev_ipos = dctx->curpos;
			i64 prev_opos = dctx->unc_line->len;

			if(dctx->curpos >= c->infile->len) goto done;
			dctx->plane = plane;
			decompress_plane_row(c, d, dctx);
			if(d->errflag) goto done;
			if(dctx->curpos<=prev_ipos) goto done;

			expected_len = (plane+1)*d->rowspan_per_plane;
			if(dctx->unc_line->len != expected_len) {
				i64 obytes_this_time;

				obytes_this_time = dctx->unc_line->len - prev_opos;

				// Tolerate a few extra bytes, but if there are a lot, treat it
				// as a fatal error.
				if(obytes_this_time > d->rowspan_per_plane+4) {
					de_err(c, "[row %u plane %u] Expected %"I64_FMT" bytes, "
						"got %"I64_FMT". This file might use an unsupported "
						"GEM Raster variant.",
						(UI)dctx->rownum, (UI)plane,
						d->rowspan_per_plane, obytes_this_time);
					d->errflag = 1;
					goto done;
				}

				de_dbg(c, "[row %u plane %u] expected %"I64_FMT" bytes, "
					"got %"I64_FMT, (UI)dctx->rownum, (UI)plane,
					d->rowspan_per_plane, obytes_this_time);

				dbuf_truncate(dctx->unc_line, expected_len);
			}
		}

		for(k=0; k<dctx->repeat_count; k++) {
			if(dctx->rownum >= d->h) break;
			dbuf_copy(dctx->unc_line, 0, d->rowspan_total, unc_pixels);
			dctx->rownum++;
		}
	}

done:
	if(dctx) {
		dbuf_close(dctx->unc_line);
		de_free(c, dctx->pattern_buf);
		de_free(c, dctx);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rcx, %r15
movq %rdx, 0x18(%rsp)
movq %rsi, %r12
movq %rdi, %rbx
movl $0x30, %esi
callq 0xe969b
movq %rax, %r14
movq 0x28(%r12), %rsi
movq %rbx, %rdi
callq 0xe969b
movq %rax, 0x20(%r14)
movq 0x38(%r12), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf0faf
movq %rax, 0x18(%r14)
movq %r15, (%r14)
movq $0x0, 0x8(%r14)
cmpq $0x0, 0x10(%r12)
jle 0x50e58
movq $0x0, 0x28(%r14)
movq 0x18(%r14), %rdi
callq 0xf1a5f
cmpq $0x0, 0x20(%r12)
jle 0x50e0f
xorl %ebp, %ebp
movq (%r14), %r8
movq 0x18(%rbx), %rax
movl $0x7, %ecx
cmpq 0x18(%rax), %r8
jge 0x50dfd
movq 0x18(%r14), %rax
movq 0x18(%rax), %rax
movq %rbp, 0x10(%r14)
movq 0x30(%r12), %rcx
movq %rax, 0x20(%rsp)
addq %rax, %rcx
movq %rcx, 0x8(%rsp)
testq %rbp, %rbp
jne 0x50bb1
movq $0x1, 0x28(%r14)
movl 0x8(%r14), %edx
movq %rbx, %rdi
leaq 0xcec98(%rip), %rsi     # 0x11f857
movl %ebp, %ecx
movq %r8, 0x10(%rsp)
xorl %eax, %eax
callq 0xe9461
movq (%r14), %r15
movq 0x18(%rbx), %rdi
movl $0x3, %r13d
cmpq 0x18(%rdi), %r15
jge 0x50c9c
movq 0x18(%r14), %rax
movq 0x8(%rsp), %rcx
cmpq %rcx, 0x18(%rax)
jge 0x50c9c
movq %r14, %rsi
callq 0xef14d
cmpb $-0x80, %al
je 0x50c5b
movzbl %al, %ecx
testl %ecx, %ecx
jne 0x50c83
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef14d
movq 0x18(%rbx), %rdi
testb %al, %al
je 0x50caa
movl %eax, %r13d
movq (%r14), %rdx
movq 0x20(%r14), %rsi
movq 0x28(%r12), %rcx
callq 0xeeee8
movq 0x28(%r12), %rax
addq %rax, (%r14)
movzbl %r13b, %r15d
movq 0x18(%r14), %rdi
movq 0x20(%r14), %rsi
movq 0x28(%r12), %rdx
callq 0xefb82
decq %r15
jne 0x50c42
jmp 0x50c99
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef14d
movzbl %al, %r15d
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq 0x18(%r14), %rcx
movq %r15, %rdx
callq 0xefabc
addq %r15, (%r14)
jmp 0x50c99
movl %eax, %ecx
sarb $0x7, %cl
andb $0x7f, %al
movzbl %al, %edx
movq 0x18(%r14), %rdi
movzbl %cl, %esi
callq 0xf14bd
xorl %r13d, %r13d
testl %r13d, %r13d
je 0x50bcd
jmp 0x50d66
movq %r14, %rsi
callq 0xef14d
cmpb $-0x1, %al
je 0x50cd0
movl 0x8(%r14), %edx
movq %rbx, %rdi
leaq 0xcebac(%rip), %rsi     # 0x11f870
movq %r15, %rcx
xorl %eax, %eax
callq 0xe9df7
jmp 0x50d4c
movq 0x10(%r14), %rcx
movq %r15, %rax
xorq 0x10(%rsp), %rax
orq %rcx, %rax
jne 0x50d34
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xef14d
movzbl %al, %r9d
movq %r9, 0x28(%r14)
movl 0x8(%r14), %edx
movl 0x10(%r14), %ecx
xorl %r13d, %r13d
movq %rbx, %rdi
leaq 0xcebb7(%rip), %rsi     # 0x11f8c1
movq %r15, %r8
xorl %eax, %eax
callq 0xe9461
movb $0x1, %al
cmpq $0x0, 0x28(%r14)
jne 0x50d59
movl 0x8(%r14), %edx
movq %rbx, %rdi
leaq 0xcebc4(%rip), %rsi     # 0x11f8ef
xorl %eax, %eax
callq 0xe9df7
jmp 0x50d4c
movl 0x8(%r14), %edx
movq %rbx, %rdi
leaq 0xceb52(%rip), %rsi     # 0x11f894
movq %r15, %r8
xorl %eax, %eax
callq 0xe9df7
movb $0x1, (%r12)
movl $0x7, %r13d
xorl %eax, %eax
testb %al, %al
jne 0x50c99
jmp 0x50c9c
cmpb $0x0, (%r12)
movl $0x7, %ecx
jne 0x50dfd
movq 0x10(%rsp), %rax
cmpq %rax, (%r14)
jle 0x50dfd
leaq 0x1(%rbp), %r13
movq 0x30(%r12), %r8
imulq %r8, %r13
movq 0x18(%r14), %rax
movq 0x18(%rax), %r15
cmpq %r13, %r15
je 0x50dfb
subq 0x20(%rsp), %r15
leaq 0x4(%r8), %rax
movl 0x8(%r14), %edx
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
cmpq %rax, %r15
jle 0x50dd3
leaq 0xcea08(%rip), %rsi     # 0x11f7c3
movl %ebp, %ecx
movq %r15, %r9
xorl %eax, %eax
callq 0xe9df7
movb $0x1, (%r12)
movl $0x7, %ecx
jmp 0x50df4
leaq 0xcea4f(%rip), %rsi     # 0x11f829
movl %ebp, %ecx
movq %r15, %r9
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r14), %rdi
movq %r13, %rsi
callq 0xf152a
xorl %ecx, %ecx
cmpq 0x8(%rsp), %r15
jg 0x50dfd
xorl %ecx, %ecx
testl %ecx, %ecx
jne 0x50e58
incq %rbp
cmpq 0x20(%r12), %rbp
jl 0x50b70
cmpq $0x0, 0x28(%r14)
movq 0x18(%rsp), %r13
jle 0x50e49
xorl %r15d, %r15d
movq 0x8(%r14), %rax
cmpq 0x10(%r12), %rax
jge 0x50e49
movq 0x18(%r14), %rdi
movq 0x38(%r12), %rdx
xorl %esi, %esi
movq %r13, %rcx
callq 0xefabc
incq 0x8(%r14)
incq %r15
cmpq 0x28(%r14), %r15
jl 0x50e1e
movq 0x8(%r14), %rax
cmpq 0x10(%r12), %rax
jl 0x50b51
movq 0x18(%r14), %rdi
callq 0xf0b13
movq 0x20(%r14), %rsi
movq %rbx, %rdi
callq 0xe9752
movq %rbx, %rdi
movq %r14, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe9752