// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/insetpix.c
// Function: do_decompress_tile
static void do_decompress_tile(deark *c, lctx *d, struct insetpix_item_data *itd,
	dbuf *unc_pixels, i64 num_rows)
{
	u8 *rowbuf1 = NULL;
	u8 *compression_bytes = NULL;
	i64 pos;
	i64 i, j;
	i64 plane;
	i64 endpos = itd->loc + itd->len;

	// There are d->gfore planes (1-bpp images). The first row of each plane is
	// uncompressed. The rest are compressed with a delta compression algorithm.
	// There are d->page_rows rows in each plane.

	rowbuf1 = de_malloc(c, d->rowspan);
	compression_bytes = de_malloc(c, d->compression_bytes_per_row);

	pos = itd->loc;

	for(plane=0; plane<d->gfore; plane++) {
		for(j=0; j<num_rows; j++) {
			if(pos >= endpos) {
				de_warn(c, "Not enough data in tile %u", itd->tile_num);
				goto done;
			}

			if(j==0) {
				// First row is stored uncompressed
				dbuf_copy(c->infile, pos, d->rowspan, unc_pixels);
				pos += d->rowspan;
			}
			else {
				de_read(compression_bytes, pos, d->compression_bytes_per_row);
				pos += d->compression_bytes_per_row;

				// Read back a copy of the previous row
				dbuf_read(unc_pixels, rowbuf1, unc_pixels->len - d->rowspan, d->rowspan);

				// For every 1 bit in the compression_bytes array, read a byte from the file.
				// For every 0 bit, copy the byte from the previous row.
				for(i=0; i<d->rowspan; i++) {
					u8 b;

					if(getbit(compression_bytes, i)) {
						b =  de_getbyte_p(&pos);
					}
					else {
						b = rowbuf1[i];
					}
					dbuf_writebyte(unc_pixels, b);
				}
			}
		}
	}

	de_dbg(c, "decompressed %"I64_FMT" bytes to %"I64_FMT, pos-itd->loc, unc_pixels->len);

done:
	de_free(c, compression_bytes);
	de_free(c, rowbuf1);
}

// 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 $0x0, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x18(%rbp), %rcx
addq 0x10(%rcx), %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rsi
callq 0x15b780
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xd0(%rax), %rsi
callq 0x15b780
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x80(%rcx), %rax
jge 0xb8f15
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0xb8f02
movq -0x40(%rbp), %rax
cmpq -0x60(%rbp), %rax
jl 0xb8de7
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x4(%rax), %edx
leaq 0xfc812(%rip), %rsi     # 0x1b55ed
movb $0x0, %al
callq 0x15bc10
jmp 0xb8f3b
cmpq $0x0, -0x50(%rbp)
jne 0xb8e26
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x40(%rbp), %rsi
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rdx
movq -0x20(%rbp), %rcx
callq 0x166070
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
jmp 0xb8eef
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0xd0(%rax), %rcx
callq 0x164d10
movq -0x10(%rbp), %rax
movq 0xd0(%rax), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rdx
movq -0x10(%rbp), %rax
subq 0xc8(%rax), %rdx
movq -0x10(%rbp), %rax
movq 0xc8(%rax), %rcx
callq 0x164d10
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0xc8(%rcx), %rax
jge 0xb8eed
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0xb8f60
cmpb $0x0, %al
je 0xb8ec4
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x40(%rbp), %rsi
callq 0x165170
movb %al, -0x61(%rbp)
jmp 0xb8ed2
movq -0x30(%rbp), %rax
movq -0x48(%rbp), %rcx
movb (%rax,%rcx), %al
movb %al, -0x61(%rbp)
movq -0x20(%rbp), %rdi
movzbl -0x61(%rbp), %esi
callq 0x168380
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0xb8e8c
jmp 0xb8eef
jmp 0xb8ef1
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0xb8db1
jmp 0xb8f04
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0xb8d94
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rdx
movq -0x18(%rbp), %rax
subq 0x18(%rax), %rdx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rcx
leaq 0xe5e0f(%rip), %rsi     # 0x19ed43
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0x15b820
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)