// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/sgiimage.c
// Function: sgiimage_decompress_rle_scanline
static void sgiimage_decompress_rle_scanline(deark *c,
	struct sgiimage_ctx *d, i64 scanline_num, i64 pos1, i64 len,
	dbuf *unc_pixels)
{
	i64 curpos;
	i64 endpos;
	i64 num_dcmpr_bytes = 0;

	curpos = pos1;
	endpos = pos1 + len;

	// Some files seem to set the offset to 0 for a nonexistent alpha channel.
	if(pos1 < 20) {
		if(!d->warned_bad_offset) {
			de_warn(c, "Bad offset at scanline %"I64_FMT": %"I64_FMT,
				scanline_num, pos1);
			d->warned_bad_offset = 1;
		}
		goto done;
	}

	while(1) {
		UI n;
		i64 count;

		if(curpos >= endpos) break; // end of input
		if(num_dcmpr_bytes >= d->rowspan) break; // sufficient output

		n = sgiimage_getsample_p(d, c->infile, &curpos);
		count = (i64)(n & 0x7f);
		if(count==0) break;
		if(n & 0x80) { // noncompressed run
			dbuf_copy(c->infile, curpos, count*d->bytes_per_sample, unc_pixels);
			curpos += count*d->bytes_per_sample;
		}
		else { // RLE run
			UI n2;

			n2 = sgiimage_getsample_p(d, c->infile, &curpos);
			if(d->bytes_per_sample==2) {
				i64 k2;

				for(k2=0; k2<count; k2++) {
					dbuf_writeu16be(unc_pixels, (i64)n2);
				}
			}
			else {
				dbuf_write_run(unc_pixels, (u8)n2, count);
			}
		}
		num_dcmpr_bytes += count*d->bytes_per_sample;
	}

done:
	;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
cmpq $0x14, -0x20(%rbp)
jge 0x157734
movq -0x10(%rbp), %rax
cmpb $0x0, 0x3a(%rax)
jne 0x15772f
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq 0x7a172(%rip), %rsi     # 0x1d1892
movb $0x0, %al
callq 0x15bc10
movq -0x10(%rbp), %rax
movb $0x1, 0x3a(%rax)
jmp 0x157857
jmp 0x157736
movq -0x38(%rbp), %rax
cmpq -0x40(%rbp), %rax
jl 0x157745
jmp 0x157855
movq -0x48(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jl 0x157758
jmp 0x157855
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
leaq -0x38(%rbp), %rdx
callq 0x157670
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
andl $0x7f, %eax
movl %eax, %eax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x157788
jmp 0x157855
movl -0x4c(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x1577d2
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x58(%rbp), %rdx
movq -0x10(%rbp), %rax
movl 0x8(%rax), %eax
imulq %rax, %rdx
movq -0x30(%rbp), %rcx
callq 0x166070
movq -0x58(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x8(%rcx), %ecx
imulq %rcx, %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x157839
movq -0x10(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
leaq -0x38(%rbp), %rdx
callq 0x157670
movl %eax, -0x5c(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x2, 0x8(%rax)
jne 0x157824
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x58(%rbp), %rax
jge 0x157822
movq -0x30(%rbp), %rdi
movl -0x5c(%rbp), %eax
movl %eax, %esi
callq 0x168a50
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x1577fc
jmp 0x157837
movq -0x30(%rbp), %rdi
movl -0x5c(%rbp), %eax
movq -0x58(%rbp), %rdx
movzbl %al, %esi
callq 0x168800
jmp 0x157839
movq -0x58(%rbp), %rax
movq -0x10(%rbp), %rcx
movl 0x8(%rcx), %ecx
imulq %rcx, %rax
addq -0x48(%rbp), %rax
movq %rax, -0x48(%rbp)
jmp 0x157736
jmp 0x157857
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)