// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/atari-img.c
// Function: tiny_uncompress
static int tiny_uncompress(deark *c, tinyctx *d, struct atari_img_decode_data *adata, i64 pos)
{
	u8 *control_bytes = NULL;
	i64 k;
	i64 count;
	i64 cpos;
	u8 ctrl;
	u8 wordbuf[2];

	if(adata->bpp==1) {
		d->numscans = 2;
		d->numstripes = 10;
		d->dst_rowspan = 80;
	}
	else {
		d->numscans = 1;
		d->numstripes = 20;
		d->dst_rowspan = 160;
	}

	de_dbg(c, "RLE control bytes at %"I64_FMT, pos);
	control_bytes = de_malloc(c, d->num_control_bytes +2);
	de_read(control_bytes, pos, d->num_control_bytes);
	pos += d->num_control_bytes;

	de_dbg(c, "RLE data words at %"I64_FMT, pos);

	cpos = 0;

	d->stopflag = 0;
	d->scan = 0;
	d->ypos = 0;
	d->column = 0;
	d->stripe = 0;
	d->dcmpr_word_count = 0;

	while(1) {
		if(d->stopflag) break;
		if(cpos >= d->num_control_bytes) break;
		ctrl = control_bytes[cpos++];

		if(ctrl >= 128) { // Uncompressed run, count encoded in control byte
			count = 256 - (i64)ctrl;
			for(k=0; k<count; k++) {
				dbuf_read(c->infile, wordbuf, pos, 2);
				pos += 2;
				tiny_setword(c, d, adata, wordbuf);
			}
		}
		else if(ctrl == 0) { // RLE, 16-bit count in next 2 control bytes
			count = de_getu16be_direct(&control_bytes[cpos]);
			cpos += 2;
			dbuf_read(c->infile, wordbuf, pos, 2);
			pos += 2;
			for(k=0; k<count; k++) {
				tiny_setword(c, d, adata, wordbuf);
			}
		}
		else if(ctrl == 1) { // Uncompressed run, 16-bit count in next 2 control bytes
			count = de_getu16be_direct(&control_bytes[cpos]);
			cpos += 2;

			for(k=0; k<count; k++) {
				dbuf_read(c->infile, wordbuf, pos, 2);
				pos += 2;
				tiny_setword(c, d, adata, wordbuf);
			}
		}
		else { // RLE, count encoded in control byte
			count = (i64)ctrl;
			dbuf_read(c->infile, wordbuf, pos, 2);
			pos += 2;
			for(k=0; k<count; k++) {
				tiny_setword(c, d, adata, wordbuf);
			}
		}
	}

	de_dbg(c, "decompressed words: %"I64_FMT, d->dcmpr_word_count);
	if(d->dcmpr_word_count<16000) {
		de_warn(c, "Expected 16000 decompressed words, got %"I64_FMT, d->dcmpr_word_count);
	}

	de_free(c, control_bytes);
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq -0x18(%rbp), %rax
cmpq $0x1, (%rax)
jne 0x2b210
movq -0x10(%rbp), %rax
movq $0x2, 0x68(%rax)
movq -0x10(%rbp), %rax
movq $0xa, 0x60(%rax)
movq -0x10(%rbp), %rax
movq $0x50, 0x70(%rax)
jmp 0x2b234
movq -0x10(%rbp), %rax
movq $0x1, 0x68(%rax)
movq -0x10(%rbp), %rax
movq $0x14, 0x60(%rax)
movq -0x10(%rbp), %rax
movq $0xa0, 0x70(%rax)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x17471f(%rip), %rsi    # 0x19f962
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
addq $0x2, %rsi
callq 0x15b780
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x28(%rbp), %rsi
movq -0x20(%rbp), %rdx
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rcx
callq 0x164d10
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
addq -0x20(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rdx
leaq 0x1746dc(%rip), %rsi    # 0x19f97b
movb $0x0, %al
callq 0x15b0d0
movq $0x0, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl $0x0, 0x78(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x90(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x80(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x98(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0x88(%rax)
movq -0x10(%rbp), %rax
movq $0x0, 0xa0(%rax)
movq -0x10(%rbp), %rax
cmpl $0x0, 0x78(%rax)
je 0x2b313
jmp 0x2b533
movq -0x40(%rbp), %rax
movq -0x10(%rbp), %rcx
cmpq 0x8(%rcx), %rax
jl 0x2b326
jmp 0x2b533
movq -0x28(%rbp), %rax
movq -0x40(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x40(%rbp)
movb (%rax,%rcx), %al
movb %al, -0x41(%rbp)
movzbl -0x41(%rbp), %eax
cmpl $0x80, %eax
jl 0x2b3bc
movzbl -0x41(%rbp), %eax
movl %eax, %ecx
movl $0x100, %eax            # imm = 0x100
subq %rcx, %rax
movq %rax, -0x38(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x2b3b7
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x43(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl $0x2, %ecx
callq 0x164d10
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x43(%rbp), %rcx
callq 0x2b610
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x2b364
jmp 0x2b52e
movzbl -0x41(%rbp), %eax
cmpl $0x0, %eax
jne 0x2b442
movq -0x28(%rbp), %rdi
addq -0x40(%rbp), %rdi
callq 0x1651b0
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x43(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl $0x2, %ecx
callq 0x164d10
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x2b43d
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x43(%rbp), %rcx
callq 0x2b610
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x2b410
jmp 0x2b52c
movzbl -0x41(%rbp), %eax
cmpl $0x1, %eax
jne 0x2b4c5
movq -0x28(%rbp), %rdi
addq -0x40(%rbp), %rdi
callq 0x1651b0
movq %rax, -0x38(%rbp)
movq -0x40(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x40(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x2b4c3
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x43(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl $0x2, %ecx
callq 0x164d10
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x43(%rbp), %rcx
callq 0x2b610
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x2b470
jmp 0x2b52a
movzbl -0x41(%rbp), %eax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x43(%rbp), %rsi
movq -0x20(%rbp), %rdx
movl $0x2, %ecx
callq 0x164d10
movq -0x20(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x20(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x38(%rbp), %rax
jge 0x2b528
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
leaq -0x43(%rbp), %rcx
callq 0x2b610
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x2b4fb
jmp 0x2b52a
jmp 0x2b52c
jmp 0x2b52e
jmp 0x2b304
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdx
leaq 0x174448(%rip), %rsi    # 0x19f991
movb $0x0, %al
callq 0x15b0d0
movq -0x10(%rbp), %rax
cmpq $0x3e80, 0xa0(%rax) # imm = 0x3E80
jge 0x2b57e
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0xa0(%rax), %rdx
leaq 0x174432(%rip), %rsi    # 0x19f9a9
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x15b820
movl $0x1, %eax
addq $0x50, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)