// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/pcx.c
// Function: do_uncompress
static int do_uncompress(deark *c, lctx *d)
{
	i64 pos;
	u8 b, b2;
	i64 count;
	i64 expected_bytes;
	i64 endpos;

	pos = PCX_HDRSIZE;
	de_dbg(c, "compressed bitmap at %"I64_FMT, pos);

	expected_bytes = d->rowspan * d->height;
	d->unc_pixels = dbuf_create_membuf(c, expected_bytes, 0);
	dbuf_enable_wbuffer(d->unc_pixels);

	endpos = c->infile->len;
	if(d->has_vga_pal) {
		// The last 769 bytes of this file are reserved for the palette.
		// Don't try to decode them as pixels.
		endpos -= 769;
	}

	while(1) {
		if(pos>=endpos) {
			break; // Reached the end of source data
		}
		if(d->unc_pixels->len >= expected_bytes) {
			break; // Reached the end of the image
		}
		b = de_getbyte(pos++);

		if(b>=0xc0) {
			count = (i64)(b&0x3f);
			b2 = de_getbyte(pos++);
			dbuf_write_run(d->unc_pixels, b2, count);
		}
		else {
			dbuf_writebyte(d->unc_pixels, b);
		}
	}

	dbuf_flush(d->unc_pixels);
	if(d->unc_pixels->len < expected_bytes) {
		de_warn(c, "Expected %d bytes of image data, only found %d",
			(int)expected_bytes, (int)d->unc_pixels->len);
	}

	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x80, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0xbcd55(%rip), %rsi     # 0x1c1efc
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x10(%rbp), %rcx
imulq 0x88(%rcx), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
xorl %edx, %edx
callq 0x168020
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, 0xa8(%rax)
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rdi
callq 0x167b90
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x94(%rax)
je 0x10521e
movq -0x38(%rbp), %rax
subq $0x301, %rax            # imm = 0x301
movq %rax, -0x38(%rbp)
jmp 0x105220
movq -0x18(%rbp), %rax
cmpq -0x38(%rbp), %rax
jl 0x10522f
jmp 0x1052d4
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x18(%rax), %rax
cmpq -0x30(%rbp), %rax
jl 0x105249
jmp 0x1052d4
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
callq 0x165060
movb %al, -0x19(%rbp)
movzbl -0x19(%rbp), %eax
cmpl $0xc0, %eax
jl 0x1052bb
movzbl -0x19(%rbp), %eax
andl $0x3f, %eax
cltq
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x18(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x18(%rbp)
callq 0x165060
movb %al, -0x1a(%rbp)
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rdi
movb -0x1a(%rbp), %al
movq -0x28(%rbp), %rdx
movzbl %al, %esi
callq 0x168800
jmp 0x1052cf
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rdi
movzbl -0x19(%rbp), %esi
callq 0x168380
jmp 0x105220
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rdi
callq 0x167c30
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x18(%rax), %rax
cmpq -0x30(%rbp), %rax
jge 0x105322
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movl %eax, %edx
movq -0x10(%rbp), %rax
movq 0xa8(%rax), %rax
movq 0x18(%rax), %rax
movl %eax, %ecx
leaq 0xbcbfa(%rip), %rsi     # 0x1c1f15
movb $0x0, %al
callq 0x15bc10
movl $0x1, %eax
addq $0x40, %rsp
popq %rbp
retq
nopl (%rax)