// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/gemras.c
// Function: decompress_plane_row
static void decompress_plane_row(deark *c, lctx *d, struct decompr_ctx *dctx)
{
	i64 pos1 = dctx->curpos;
	u8 val;
	i64 count;
	i64 k;
	i64 unc_line_endpos;

	unc_line_endpos = dctx->unc_line->len + d->rowspan_per_plane;
	if(dctx->plane==0) {
		// By default, an encoded scanline is rendered once. This may be overridden by a
		// scanline run opcode.
		dctx->repeat_count = 1;
	}
	de_dbg3(c, "[row %u plane %u at %"I64_FMT"]", (UI)dctx->rownum, (UI)dctx->plane, pos1);

	while(1) {
		u8 b0, b1;
		i64 opcode_pos = dctx->curpos;

		if(dctx->curpos >= c->infile->len) break;
		if(dctx->unc_line->len >= unc_line_endpos) break;

		b0 = de_getbyte_p(&dctx->curpos);

		if(b0==0) { // Pattern run or scanline run
			b1 = de_getbyte_p(&dctx->curpos);
			if(b1>0) { // pattern run
				de_read(dctx->pattern_buf, dctx->curpos, d->patlen);
				dctx->curpos += d->patlen;
				count = (i64)b1;
				for(k=0; k<count; k++) {
					dbuf_write(dctx->unc_line, dctx->pattern_buf, d->patlen);
				}
			}
			else { // (b1==0) scanline run
				u8 flagbyte;

				flagbyte = de_getbyte_p(&dctx->curpos);
				if(flagbyte!=0xff) {
					de_err(c, "row %u: Invalid 00 00 opcode at %"I64_FMT, (UI)dctx->rownum,
						opcode_pos);
					d->errflag = 1;
					goto done;
				}

				// Note that the 'repeat scanline' opcode appears *before* the
				// scanline (row) to be repeated.
				// We're assuming it is only allowed at the very start of a row.
				// The count seems to the total number of times the row is
				// rendered, so the minimum useful count is 2, and we don't allow
				// a count of 0.
				if(dctx->plane!=0 || opcode_pos!=pos1) {
					de_err(c, "row %u plane %u: Invalid scanline run at %"I64_FMT, (UI)dctx->rownum,
						(UI)dctx->plane, opcode_pos);
					d->errflag = 1;
					goto done;
				}

				dctx->repeat_count = (i64)de_getbyte_p(&dctx->curpos);
				de_dbg3(c, "scanline run row=%u plane=%u pos=%"I64_FMT" count=%u", (UI)dctx->rownum,
					(UI)dctx->plane, opcode_pos, (UI)dctx->repeat_count);
				if(dctx->repeat_count == 0) {
					de_err(c, "row %u: Bad scanline run count", (UI)dctx->rownum);
					d->errflag = 1;
					goto done;
				}
			}
		}
		else if(b0==0x80) { // "Uncompressed bit string"
			count = (i64)de_getbyte_p(&dctx->curpos);
			dbuf_copy(c->infile, dctx->curpos, count, dctx->unc_line);
			dctx->curpos += count;
		}
		else { // "solid run"
			val = (b0&0x80) ? 0xff : 0x00;
			count = (i64)(b0 & 0x7f);
			dbuf_write_run(dctx->unc_line, val, count);
		}
	}

done:
	;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq -0x10(%rbp), %rcx
addq 0x30(%rcx), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x71f4e
movq -0x18(%rbp), %rax
movq $0x1, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %ecx
movq -0x20(%rbp), %r8
leaq 0x139566(%rip), %rsi    # 0x1ab4d7
movb $0x0, %al
callq 0x15b400
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0x71f9d
jmp 0x72231
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
cmpq -0x40(%rbp), %rax
jl 0x71fb4
jmp 0x72231
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x165170
movb %al, -0x41(%rbp)
movzbl -0x41(%rbp), %eax
cmpl $0x0, %eax
jne 0x72199
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x165170
movb %al, -0x42(%rbp)
movzbl -0x42(%rbp), %eax
cmpl $0x0, %eax
jle 0x72076
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rdx
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
callq 0x164d10
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rcx
movq -0x18(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movzbl -0x42(%rbp), %eax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
jge 0x72071
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rsi
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rdx
callq 0x166170
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
jmp 0x7203c
jmp 0x72194
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x165170
movb %al, -0x51(%rbp)
movzbl -0x51(%rbp), %eax
cmpl $0xff, %eax
je 0x720c1
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
movq -0x50(%rbp), %rcx
leaq 0x139442(%rip), %rsi    # 0x1ab4f0
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
jmp 0x72233
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
jne 0x720d6
movq -0x50(%rbp), %rax
cmpq -0x20(%rbp), %rax
je 0x7210c
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %ecx
movq -0x50(%rbp), %r8
leaq 0x13941b(%rip), %rsi    # 0x1ab514
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
jmp 0x72233
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq %rcx, 0x28(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %ecx
movq -0x50(%rbp), %r8
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movl %eax, %r9d
leaq 0x1393e9(%rip), %rsi    # 0x1ab541
movb $0x0, %al
callq 0x15b400
movq -0x18(%rbp), %rax
cmpq $0x0, 0x28(%rax)
jne 0x72192
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
leaq 0x1393f0(%rip), %rsi    # 0x1ab56f
movb $0x0, %al
callq 0x15bfb0
movq -0x10(%rbp), %rax
movb $0x1, (%rax)
jmp 0x72233
jmp 0x72194
jmp 0x7222c
movzbl -0x41(%rbp), %eax
cmpl $0x80, %eax
jne 0x721ec
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
callq 0x165170
movzbl %al, %eax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x30(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rcx
callq 0x166070
movq -0x30(%rbp), %rcx
movq -0x18(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
jmp 0x7222a
movzbl -0x41(%rbp), %edx
andl $0x80, %edx
xorl %eax, %eax
movl $0xff, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movb %al, -0x21(%rbp)
movzbl -0x41(%rbp), %eax
andl $0x7f, %eax
cltq
movq %rax, -0x30(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdi
movb -0x21(%rbp), %al
movq -0x30(%rbp), %rdx
movzbl %al, %esi
callq 0x168800
jmp 0x7222c
jmp 0x71f78
jmp 0x72233
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax)