// File: /workspace/github_c_makefile_star2_O3/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, %r12
movq %rdx, 0x18(%rsp)
movq %rsi, %r15
movq %rdi, %rbx
movl $0x30, %esi
callq 0xe8b2b
movq %rax, %r14
movq 0x28(%r15), %rsi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, 0x20(%r14)
movq 0x38(%r15), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xf09c5
movq %rax, 0x18(%r14)
movq %r12, (%r14)
movq $0x0, 0x8(%r14)
cmpq $0x0, 0x10(%r15)
jle 0x50d64
movq $0x0, 0x28(%r14)
movq 0x18(%r14), %rdi
callq 0xf145f
cmpq $0x0, 0x20(%r15)
jle 0x50cb7
xorl %ebp, %ebp
movq (%r14), %rcx
movq 0x18(%rbx), %rax
movq %rcx, 0x8(%rsp)
cmpq 0x18(%rax), %rcx
jge 0x50d64
movq 0x18(%r14), %rax
movq 0x18(%rax), %rax
movq %rbp, 0x10(%r14)
movq 0x30(%r15), %r13
movq %rax, 0x20(%rsp)
addq %rax, %r13
testq %rbp, %rbp
jne 0x50ad8
movq $0x1, 0x28(%r14)
movl 0x8(%r14), %edx
movq %rbx, %rdi
leaq 0xcee99(%rip), %rsi     # 0x11f97f
movl %ebp, %ecx
movq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0xe889f
movq (%r14), %r12
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %r12
jge 0x50c42
movq %rbp, 0x10(%rsp)
movq 0x18(%r14), %rax
cmpq %r13, 0x18(%rax)
jge 0x50c42
movq %r14, %rsi
callq 0xeeb4b
cmpb $-0x80, %al
je 0x50b84
movzbl %al, %ecx
testl %ecx, %ecx
jne 0x50baf
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xeeb4b
movq 0x18(%rbx), %rdi
testb %al, %al
je 0x50bc7
movl %eax, %ebp
movq (%r14), %rdx
movq 0x20(%r14), %rsi
movq 0x28(%r15), %rcx
callq 0xee8d4
movq 0x28(%r15), %rax
addq %rax, (%r14)
movzbl %bpl, %r12d
movq 0x10(%rsp), %rbp
movq 0x18(%r14), %rdi
movq 0x20(%r14), %rsi
movq 0x28(%r15), %rdx
callq 0xef547
decq %r12
jne 0x50b69
jmp 0x50c31
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xeeb4b
movzbl %al, %r12d
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq 0x18(%r14), %rcx
movq %r12, %rdx
callq 0xef481
addq %r12, (%r14)
jmp 0x50c31
movl %eax, %ecx
sarb $0x7, %cl
andb $0x7f, %al
movzbl %al, %edx
movq 0x18(%r14), %rdi
movzbl %cl, %esi
callq 0xf0eba
jmp 0x50c31
movq %r14, %rsi
callq 0xeeb4b
cmpb $-0x1, %al
jne 0x50d02
movq 0x10(%r14), %rcx
cmpq 0x8(%rsp), %r12
jne 0x50d1c
testq %rcx, %rcx
jne 0x50d1c
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xeeb4b
movzbl %al, %r9d
movq %r9, 0x28(%r14)
movl 0x8(%r14), %edx
movl 0x10(%r14), %ecx
movq %rbx, %rdi
leaq 0xcedd4(%rip), %rsi     # 0x11f9e9
movq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0xe889f
cmpq $0x0, 0x28(%r14)
movq 0x10(%rsp), %rbp
je 0x50d36
movq (%r14), %r12
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %r12
jl 0x50b0a
cmpb $0x0, (%r15)
jne 0x50d64
cmpq 0x8(%rsp), %r12
jle 0x50d64
movq %rbp, %r13
incq %r13
movq 0x30(%r15), %r8
movq %r8, %r12
imulq %r13, %r12
movq 0x18(%r14), %rax
movq 0x18(%rax), %r9
cmpq %r12, %r9
je 0x50caa
subq 0x20(%rsp), %r9
leaq 0x4(%r8), %rax
movl 0x8(%r14), %edx
cmpq %rax, %r9
jg 0x50d4d
movq %rbx, %rdi
leaq 0xcecbc(%rip), %rsi     # 0x11f951
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r14), %rdi
movq %r12, %rsi
callq 0xf0f27
movq %r13, %rbp
cmpq 0x20(%r15), %r13
jl 0x50a9d
cmpq $0x0, 0x28(%r14)
movq 0x8(%r14), %rax
jle 0x50cf6
xorl %r12d, %r12d
movq 0x18(%rsp), %r13
cmpq 0x10(%r15), %rax
jge 0x50cf6
movq 0x18(%r14), %rdi
movq 0x38(%r15), %rdx
xorl %esi, %esi
movq %r13, %rcx
callq 0xef481
movq 0x8(%r14), %rax
incq %rax
movq %rax, 0x8(%r14)
incq %r12
cmpq 0x28(%r14), %r12
jl 0x50cca
cmpq 0x10(%r15), %rax
jl 0x50a7f
jmp 0x50d64
movl 0x8(%r14), %edx
leaq 0xcec8b(%rip), %rsi     # 0x11f998
movq %rbx, %rdi
movq %r12, %rcx
xorl %eax, %eax
callq 0xe9295
jmp 0x50d60
movl 0x8(%r14), %edx
leaq 0xcec95(%rip), %rsi     # 0x11f9bc
movq %rbx, %rdi
movq %r12, %r8
xorl %eax, %eax
callq 0xe9295
jmp 0x50d60
movl 0x8(%r14), %edx
leaq 0xcecd6(%rip), %rsi     # 0x11fa17
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x50d60
leaq 0xceb97(%rip), %rsi     # 0x11f8eb
movq %rbx, %rdi
movl %ebp, %ecx
xorl %eax, %eax
callq 0xe9295
movb $0x1, (%r15)
movq 0x18(%r14), %rdi
callq 0xf052b
movq 0x20(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq %rbx, %rdi
movq %r14, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe8be2