// File: /workspace/github_c_makefile_star2_O2/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, %r13
movq %rsi, %r15
movq %rdi, %rbx
pushq $0x30
popq %rsi
callq 0xdecaf
movq %rax, %r14
movq 0x28(%r15), %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, 0x20(%r14)
movq 0x38(%r15), %rsi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xe59cd
movq %rax, 0x18(%r14)
movq %r12, (%r14)
andq $0x0, 0x8(%r14)
xorl %eax, %eax
movq %r13, 0x10(%rsp)
cmpq 0x10(%r15), %rax
jge 0x4e579
andq $0x0, 0x28(%r14)
movq 0x18(%r14), %rdi
callq 0xe62cf
xorl %r13d, %r13d
cmpq 0x20(%r15), %r13
jge 0x4e4d3
movq (%r14), %rcx
movq 0x18(%rbx), %rax
movq %rcx, 0x8(%rsp)
cmpq 0x18(%rax), %rcx
jge 0x4e579
movq 0x18(%r14), %rax
movq 0x18(%rax), %rax
movq %r13, 0x10(%r14)
movq 0x30(%r15), %r12
movq %rax, 0x18(%rsp)
addq %rax, %r12
testq %r13, %r13
jne 0x4e30a
movq $0x1, 0x28(%r14)
movl 0x8(%r14), %edx
movq %r13, 0x20(%rsp)
movl %r13d, %ecx
movq %rbx, %rdi
leaq 0xc458f(%rip), %rsi     # 0x1128af
movl %r13d, 0x4(%rsp)
movq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0xdea83
movq (%r14), %rbp
movq 0x18(%rbx), %rdi
cmpq 0x18(%rdi), %rbp
jge 0x4e45e
movq 0x18(%r14), %rax
cmpq %r12, 0x18(%rax)
jge 0x4e45e
movq %r14, %rsi
callq 0xe3f2a
cmpb $-0x80, %al
je 0x4e3ae
movzbl %al, %ecx
testl %ecx, %ecx
jne 0x4e3d9
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xe3f2a
movq 0x18(%rbx), %rdi
testb %al, %al
je 0x4e3f4
movl %eax, %r13d
movq (%r14), %rdx
movq 0x20(%r14), %rsi
movq 0x28(%r15), %rcx
callq 0xe3cb7
movq 0x28(%r15), %rax
addq %rax, (%r14)
movzbl %r13b, %r13d
subq $0x1, %r13
jb 0x4e331
movq 0x18(%r14), %rdi
movq 0x20(%r14), %rsi
movq 0x28(%r15), %rdx
callq 0xe46ba
jmp 0x4e395
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xe3f2a
movzbl %al, %r13d
movq 0x18(%rbx), %rdi
movq (%r14), %rsi
movq 0x18(%r14), %rcx
movq %r13, %rdx
callq 0xe45f4
addq %r13, (%r14)
jmp 0x4e331
movl %eax, %ecx
sarb $0x7, %cl
andb $0x7f, %al
movzbl %al, %edx
movq 0x18(%r14), %rdi
movzbl %cl, %esi
callq 0xe5e40
jmp 0x4e331
movq %r14, %rsi
callq 0xe3f2a
cmpb $-0x1, %al
jne 0x4e515
movq 0x10(%r14), %rcx
cmpq 0x8(%rsp), %rbp
jne 0x4e52f
testq %rcx, %rcx
jne 0x4e52f
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xe3f2a
movzbl %al, %r9d
movq %r9, 0x28(%r14)
movl 0x8(%r14), %edx
movl 0x10(%r14), %ecx
movq %rbx, %rdi
leaq 0xc44d7(%rip), %rsi     # 0x112919
movq 0x8(%rsp), %r8
xorl %eax, %eax
callq 0xdea83
cmpq $0x0, 0x28(%r14)
jne 0x4e331
jmp 0x4e549
cmpb $0x0, (%r15)
movq 0x20(%rsp), %r13
jne 0x4e579
cmpq 0x8(%rsp), %rbp
jle 0x4e579
incq %r13
movq 0x30(%r15), %r8
movq %r8, %r12
imulq %r13, %r12
movq 0x18(%r14), %rax
movq 0x18(%rax), %r9
cmpq %r12, %r9
je 0x4e2c5
subq 0x18(%rsp), %r9
leaq 0x4(%r8), %rax
movl 0x8(%r14), %edx
cmpq %rax, %r9
jg 0x4e560
movq %rbx, %rdi
leaq 0xc43ca(%rip), %rsi     # 0x112881
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0xde896
movq 0x18(%r14), %rdi
movq %r12, %rsi
callq 0xe5eb2
jmp 0x4e2c5
movq 0x8(%r14), %rax
xorl %r12d, %r12d
movq 0x10(%rsp), %r13
cmpq 0x28(%r14), %r12
jge 0x4e2aa
cmpq 0x10(%r15), %rax
jge 0x4e2aa
movq 0x18(%r14), %rdi
movq 0x38(%r15), %rdx
xorl %esi, %esi
movq %r13, %rcx
callq 0xe45f4
movq 0x8(%r14), %rax
incq %rax
movq %rax, 0x8(%r14)
incq %r12
jmp 0x4e4df
movl 0x8(%r14), %edx
leaq 0xc43a8(%rip), %rsi     # 0x1128c8
movq %rbx, %rdi
movq %rbp, %rcx
xorl %eax, %eax
callq 0xdf2a5
jmp 0x4e575
movl 0x8(%r14), %edx
leaq 0xc43b2(%rip), %rsi     # 0x1128ec
movq %rbx, %rdi
movq %rbp, %r8
xorl %eax, %eax
callq 0xdf2a5
jmp 0x4e575
movl 0x8(%r14), %edx
leaq 0xc43f3(%rip), %rsi     # 0x112947
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0x4e575
leaq 0xc42b4(%rip), %rsi     # 0x11281b
movq %rbx, %rdi
movl 0x4(%rsp), %ecx
xorl %eax, %eax
callq 0xdf2a5
movb $0x1, (%r15)
movq 0x18(%r14), %rdi
callq 0xe5591
movq 0x20(%r14), %rsi
movq %rbx, %rdi
callq 0xded32
movq %rbx, %rdi
movq %r14, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xded32