// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/tga.c
// Function: do_decode_rle_internal
static void do_decode_rle_internal(deark *c1, struct de_dfilter_in_params *dcmpri,
	struct de_dfilter_out_params *dcmpro, struct de_dfilter_results *dres,
	i64 bytes_per_pixel)
{
	u8 b;
	i64 count;
	i64 k;
	u8 buf[8];
	i64 pos = dcmpri->pos;
	i64 nbytes_written = 0;

	if(bytes_per_pixel<1 || bytes_per_pixel>8) return;

	while(1) {
		if(pos >= dcmpri->pos + dcmpri->len) break;
		if(nbytes_written >= dcmpro->expected_len) break;

		b = dbuf_getbyte(dcmpri->f, pos);
		pos++;

		if(b & 0x80) { // RLE block
			count = (i64)(b - 0x80) + 1;
			dbuf_read(dcmpri->f, buf, pos, bytes_per_pixel);
			pos += bytes_per_pixel;
			for(k=0; k<count; k++) {
				dbuf_write(dcmpro->f, buf, bytes_per_pixel);
			}
			nbytes_written += count * bytes_per_pixel;
		}
		else { // uncompressed block
			count = (i64)(b) + 1;
			dbuf_copy(dcmpri->f, pos, count * bytes_per_pixel, dcmpro->f);
			pos += count * bytes_per_pixel;
			nbytes_written += count * bytes_per_pixel;
		}
	}
	dres->bytes_consumed = pos - dcmpri->pos;
	dres->bytes_consumed_valid = 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
cmpq $0x1, -0x28(%rbp)
jl 0x12d38e
cmpq $0x8, -0x28(%rbp)
jle 0x12d393
jmp 0x12d4e0
jmp 0x12d395
movq -0x50(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq -0x10(%rbp), %rdx
addq 0x10(%rdx), %rcx
cmpq %rcx, %rax
jl 0x12d3b3
jmp 0x12d4c4
movq -0x58(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jl 0x12d3c6
jmp 0x12d4c4
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rsi
callq 0x165060
movb %al, -0x29(%rbp)
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
movzbl -0x29(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x12d471
movzbl -0x29(%rbp), %eax
subl $0x80, %eax
cltq
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
leaq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x28(%rbp), %rcx
callq 0x164d10
movq -0x28(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x12d45e
movq -0x18(%rbp), %rax
movq (%rax), %rdi
leaq -0x48(%rbp), %rsi
movq -0x28(%rbp), %rdx
callq 0x166170
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x12d432
movq -0x38(%rbp), %rax
imulq -0x28(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x12d4bf
movzbl -0x29(%rbp), %eax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rsi
movq -0x38(%rbp), %rdx
imulq -0x28(%rbp), %rdx
movq -0x18(%rbp), %rax
movq (%rax), %rcx
callq 0x166070
movq -0x38(%rbp), %rax
imulq -0x28(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x38(%rbp), %rax
imulq -0x28(%rbp), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x58(%rbp)
jmp 0x12d395
movq -0x50(%rbp), %rcx
movq -0x10(%rbp), %rax
subq 0x8(%rax), %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x20(%rbp), %rax
movb $0x1, 0x4(%rax)
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)