// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movl $0x30, %esi
callq 0x15b780
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rsi
callq 0x15b780
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rsi
xorl %edx, %edx
callq 0x168020
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x20(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, (%rax)
movq -0x40(%rbp), %rax
movq $0x0, 0x8(%rax)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jl 0x71bce
jmp 0x71dbd
movq -0x40(%rbp), %rax
movq $0x0, 0x28(%rax)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x169310
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x20(%rcx), %rax
jge 0x71d53
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0x71c36
jmp 0x71dbf
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0x71f00
movq -0x10(%rbp), %rax
cmpb $0x0, (%rax)
je 0x71c61
jmp 0x71dbf
movq -0x40(%rbp), %rax
movq (%rax), %rax
cmpq -0x50(%rbp), %rax
jg 0x71c73
jmp 0x71dbf
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq -0x10(%rbp), %rcx
imulq 0x30(%rcx), %rax
movq %rax, -0x48(%rbp)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
cmpq -0x48(%rbp), %rax
je 0x71d40
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
subq -0x58(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
addq $0x4, %rcx
cmpq %rcx, %rax
jle 0x71d01
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
movq -0x38(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %r8
movq -0x60(%rbp), %r9
leaq 0x139755(%rip), %rsi    # 0x1ab443
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
jmp 0x71dbf
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
movq -0x38(%rbp), %rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movq 0x30(%rax), %r8
movq -0x60(%rbp), %r9
leaq 0x139781(%rip), %rsi    # 0x1ab4a9
movb $0x0, %al
callq 0x15b0d0
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rsi
callq 0x168900
jmp 0x71d42
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x71bef
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jge 0x71db8
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x10(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jl 0x71d7d
jmp 0x71db8
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x10(%rbp), %rax
movq 0x38(%rax), %rdx
movq -0x18(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
callq 0x166070
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x71d5b
jmp 0x71bb7
jmp 0x71dbf
cmpq $0x0, -0x40(%rbp)
je 0x71df1
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x15b820
addq $0x60, %rsp
popq %rbp
retq
nopw (%rax,%rax)