// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/sgiimage.c
// Function: sgiimage_decompress_rle
static void sgiimage_decompress_rle(deark *c, struct sgiimage_ctx *d,
	dbuf *unc_pixels)
{
	struct sgiimage_offsettab_entry *offsettab = NULL;
	i64 i;
	i64 pos;
	i64 first_alpha_scanline;

	offsettab = de_mallocarray(c, d->num_scanlines,
		sizeof(struct sgiimage_offsettab_entry));

	pos = 512;
	de_dbg(c, "scanline table at %"I64_FMT, pos);
	de_dbg_indent(c, 1);

	de_dbg(c, "offsets at %"I64_FMT, pos);
	for(i=0; i<d->num_scanlines; i++) {
		offsettab[i].pos = (u32)de_getu32be_p(&pos);
	}
	de_dbg(c, "lengths at %"I64_FMT, pos);
	for(i=0; i<d->num_scanlines; i++) {
		offsettab[i].len = (u32)de_getu32be_p(&pos);
	}
	de_dbg(c, "table end: %"I64_FMT, pos);

	if(c->debug_level>=2) {
		for(i=0; i<d->num_scanlines; i++) {
			de_dbg2(c, "scanline[%"I64_FMT"]: offs=%u len=%u", i,
				(UI)offsettab[i].pos, (UI)offsettab[i].len);
		}
	}
	de_dbg_indent(c, -1);

	if(d->has_alpha) {
		first_alpha_scanline = d->height * (d->num_channels-1);
	}
	else {
		first_alpha_scanline = d->num_scanlines;
	}

	for(i=0; i<d->num_scanlines; i++) {
		i64 expected_ulen;
		i64 actual_ulen;

		sgiimage_decompress_rle_scanline(c, d, i,
			(i64)offsettab[i].pos, (i64)offsettab[i].len, unc_pixels);

		expected_ulen = (i+1) * d->rowspan;
		actual_ulen = dbuf_get_length(unc_pixels);
		if(actual_ulen != expected_ulen) {
			if((actual_ulen < expected_ulen) && i>=first_alpha_scanline) {
				// If we didn't decompress enough bytes, don't default to 0 (invisible)
				// if this is the alpha channel.
				dbuf_write_run(unc_pixels, 0xff, expected_ulen - actual_ulen);
			}
			else {
				dbuf_truncate(unc_pixels, expected_ulen);
			}
		}
	}

	dbuf_flush(unc_pixels);
	de_free(c, offsettab);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rsi
movl $0x8, %edx
callq 0x15c450
movq %rax, -0x20(%rbp)
movq $0x200, -0x30(%rbp)     # imm = 0x200
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x7a424(%rip), %rsi     # 0x1d1831
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movl $0x1, %esi
callq 0x15b560
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x7a416(%rip), %rsi     # 0x1d1847
movb $0x0, %al
callq 0x15b0d0
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x48(%rcx), %rax
jge 0x15747a
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1654b0
movl %eax, %edx
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movl %edx, (%rax,%rcx,8)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x157440
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x7a3cd(%rip), %rsi     # 0x1d1856
movb $0x0, %al
callq 0x15b0d0
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x48(%rcx), %rax
jge 0x1574d3
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x30(%rbp), %rsi
callq 0x1654b0
movl %eax, %edx
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movl %edx, 0x4(%rax,%rcx,8)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x157498
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rdx
leaq 0x7a383(%rip), %rsi     # 0x1d1865
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
cmpl $0x2, (%rax)
jl 0x157546
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x48(%rcx), %rax
jge 0x157544
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rdx
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movl (%rax,%rcx,8), %ecx
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rsi
movl 0x4(%rax,%rsi,8), %r8d
leaq 0x7a345(%rip), %rsi     # 0x1d1874
movb $0x0, %al
callq 0x15b350
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x1574fa
jmp 0x157546
movq -0x8(%rbp), %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0x15b560
movq -0x10(%rbp), %rax
cmpb $0x0, 0x39(%rax)
je 0x15757c
movq -0x10(%rbp), %rax
movq 0x28(%rax), %rax
movq -0x10(%rbp), %rcx
movq 0x30(%rcx), %rcx
subq $0x1, %rcx
imulq %rcx, %rax
movq %rax, -0x38(%rbp)
jmp 0x157588
movq -0x10(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x48(%rcx), %rax
jge 0x15764e
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movl (%rax,%rcx,8), %eax
movl %eax, %ecx
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %r8
movl 0x4(%rax,%r8,8), %eax
movl %eax, %r8d
movq -0x18(%rbp), %r9
callq 0x1576c0
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq -0x10(%rbp), %rcx
imulq 0x40(%rcx), %rax
movq %rax, -0x40(%rbp)
movq -0x18(%rbp), %rdi
callq 0x169790
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
je 0x15763b
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0x15762c
movq -0x28(%rbp), %rax
cmpq -0x38(%rbp), %rax
jl 0x15762c
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rdx
subq -0x48(%rbp), %rdx
movl $0xff, %esi
callq 0x168800
jmp 0x157639
movq -0x18(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq 0x168900
jmp 0x15763b
jmp 0x15763d
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
jmp 0x157590
movq -0x18(%rbp), %rdi
callq 0x167c30
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x15b820
addq $0x50, %rsp
popq %rbp
retq
nopw (%rax,%rax)