// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/wpg.c
// Function: do_uncompress_rle
static int do_uncompress_rle(deark *c, lctx *d, dbuf *f, i64 pos1, i64 len,
	i64 rowspan, dbuf *unc_pixels)
{
	i64 pos;
	u8 b, b2;
	i64 count;
	i64 endpos;
	i64 k;

	pos = pos1;
	endpos = pos1+len;

	while(1) {
		if(pos>=endpos) {
			break; // Reached the end of source data
		}
		b = dbuf_getbyte(f, pos++);

		if(b==0x00) { // repeat scanline
			i64 src_line_pos;

			count = dbuf_getbyte(f, pos++);

			// Make 'count' more copies of the previous scanline
			src_line_pos = unc_pixels->len - rowspan;
			for(k=0; k<count; k++) {
				// (It is allowed to copy from a membuf to itself.)
				dbuf_copy(unc_pixels, src_line_pos, rowspan, unc_pixels);
			}
		}
		else if(b<=0x7f) { // uncompressed run
			count = (i64)b;
			dbuf_copy(f, pos, count, unc_pixels);
			pos += count;
		}
		else if(b==0x80) { // Special 0xff compression
			count = (i64)dbuf_getbyte(f, pos++);
			dbuf_write_run(unc_pixels, 0xff, count);
		}
		else { // byte RLE compression
			count = (i64)(b&0x7f);
			b2 = dbuf_getbyte(f, pos++);
			dbuf_write_run(unc_pixels, b2, count);
		}
	}

	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq 0x10(%rbp), %rax
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 -0x20(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x20(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x50(%rbp), %rax
jl 0x13b9f7
jmp 0x13bb3a
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
callq 0x165060
movb %al, -0x39(%rbp)
movzbl -0x39(%rbp), %eax
cmpl $0x0, %eax
jne 0x13ba84
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
callq 0x165060
movzbl %al, %eax
movq %rax, -0x48(%rbp)
movq 0x10(%rbp), %rax
movq 0x18(%rax), %rax
subq -0x30(%rbp), %rax
movq %rax, -0x60(%rbp)
movq $0x0, -0x58(%rbp)
movq -0x58(%rbp), %rax
cmpq -0x48(%rbp), %rax
jge 0x13ba7f
movq 0x10(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq 0x10(%rbp), %rcx
callq 0x166070
movq -0x58(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
jmp 0x13ba52
jmp 0x13bb35
movzbl -0x39(%rbp), %eax
cmpl $0x7f, %eax
jg 0x13bab8
movzbl -0x39(%rbp), %eax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq 0x10(%rbp), %rcx
callq 0x166070
movq -0x48(%rbp), %rax
addq -0x38(%rbp), %rax
movq %rax, -0x38(%rbp)
jmp 0x13bb33
movzbl -0x39(%rbp), %eax
cmpl $0x80, %eax
jne 0x13baf6
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
callq 0x165060
movzbl %al, %eax
movq %rax, -0x48(%rbp)
movq 0x10(%rbp), %rdi
movq -0x48(%rbp), %rdx
movl $0xff, %esi
callq 0x168800
jmp 0x13bb31
movzbl -0x39(%rbp), %eax
andl $0x7f, %eax
cltq
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq %rsi, %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
callq 0x165060
movb %al, -0x3a(%rbp)
movq 0x10(%rbp), %rdi
movb -0x3a(%rbp), %al
movq -0x48(%rbp), %rdx
movzbl %al, %esi
callq 0x168800
jmp 0x13bb33
jmp 0x13bb35
jmp 0x13b9e8
movl $0x1, %eax
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop