// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/atari-img.c
// Function: de_run_tinystuff
static void de_run_tinystuff(deark *c, de_module_params *mparams)
{
	struct atari_img_decode_data *adata = NULL;
	de_finfo *fi = NULL;
	tinyctx *d = NULL;
	i64 pos = 0;
	i64 expected_min_file_size;
	i64 expected_max_file_size;
	int is_grayscale;

	d = de_malloc(c, sizeof(tinyctx));

	adata = de_malloc(c, sizeof(struct atari_img_decode_data));
	adata->pal = d->pal;
	adata->was_compressed = 1;

	d->res_code = de_getbyte(pos);
	pos++;
	de_dbg(c, "resolution code: %u", (UI)d->res_code);

	switch(d->res_code) {
	case 0: case 3:
		adata->bpp = 4;
		adata->w = 320;
		adata->h = 200;
		break;
	case 1: case 4:
		adata->bpp = 2;
		adata->w = 640;
		adata->h = 200;
		break;
	case 2: case 5:
		adata->bpp = 1;
		adata->w = 640;
		adata->h = 400;
		break;
	default:
		de_err(c, "Invalid resolution code (%u). This is not a Tiny Stuff file.",
			(UI)d->res_code);
		goto done;
	}

	de_dbg_dimensions(c, adata->w, adata->h);
	adata->ncolors = de_pow2(adata->bpp);
	de_dbg(c, "colors: %d", (int)adata->ncolors);

	if(d->res_code>=3) {
		de_warn(c, "This image uses palette cycling animation, which is not supported.");
		pos += 4; // skip animation_info
	}

	fmtutil_read_atari_palette(c, c->infile, pos, adata->pal, 16, adata->ncolors, 0);
	fix_dark_pal(c, adata);
	pos += 16*2;

	d->num_control_bytes = de_getu16be(pos);
	pos += 2;
	de_dbg(c, "number of RLE control bytes: %d", (int)d->num_control_bytes);

	d->num_data_words = de_getu16be(pos);
	pos += 2;
	d->data_words_nbytes = 2*d->num_data_words;
	de_dbg(c, "number of RLE data words: %d (%d bytes)", (int)d->num_data_words,
		(int)d->data_words_nbytes);

	// It seems that files are often padded to the next multiple of 128 bytes,
	// so don't warn about that.
	expected_min_file_size = pos + d->num_control_bytes + d->data_words_nbytes;
	expected_max_file_size = de_pad_to_n(expected_min_file_size, 128);
	de_dbg(c, "expected file size: %"I64_FMT" or %"I64_FMT, expected_min_file_size,
		expected_max_file_size);
	if(c->infile->len<expected_min_file_size || c->infile->len>expected_max_file_size) {
		de_warn(c, "Expected file size to be %"I64_FMT", but it is %"I64_FMT".",
			expected_min_file_size, c->infile->len);
	}

	adata->unc_pixels = dbuf_create_membuf(c, 32000, 1);

	if(!tiny_uncompress(c, d, adata, pos)) {
		goto done;
	}

	is_grayscale = de_is_grayscale_palette(adata->pal, adata->ncolors);

	adata->img = de_bitmap_create(c, adata->w, adata->h, is_grayscale?1:3);

	fi = de_finfo_create(c);
	fmtutil_atari_set_standard_density(c, adata, fi);

	do_tinystuff_image(c, d, adata);
	de_bitmap_write_to_file_finfo(adata->img, fi, 0);

done:
	if(adata) {
		de_bitmap_destroy(adata->img);
		dbuf_close(adata->unc_pixels);
		de_free(c, adata);
	}
	de_finfo_destroy(c, fi);
	de_free(c, d);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movq $0x0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movl $0xa8, %esi
callq 0x15b780
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movl $0x40, %esi
callq 0x15b780
movq %rax, -0x18(%rbp)
movq -0x28(%rbp), %rcx
addq $0x20, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x30(%rax)
movq -0x18(%rbp), %rax
movl $0x1, 0x28(%rax)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x165060
movb %al, %cl
movq -0x28(%rbp), %rax
movb %cl, (%rax)
movq -0x30(%rbp), %rax
incq %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movzbl (%rax), %edx
leaq 0x17671b(%rip), %rsi    # 0x19f580
xorl %eax, %eax
callq 0x15b0d0
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
movl %eax, -0x48(%rbp)
testl %eax, %eax
je 0x28eae
jmp 0x28e7c
movl -0x48(%rbp), %eax
subl $0x1, %eax
je 0x28ed3
jmp 0x28e86
movl -0x48(%rbp), %eax
subl $0x2, %eax
je 0x28ef8
jmp 0x28e90
movl -0x48(%rbp), %eax
subl $0x3, %eax
je 0x28eae
jmp 0x28e9a
movl -0x48(%rbp), %eax
subl $0x4, %eax
je 0x28ed3
jmp 0x28ea4
movl -0x48(%rbp), %eax
subl $0x5, %eax
je 0x28ef8
jmp 0x28f1d
movq -0x18(%rbp), %rax
movq $0x4, (%rax)
movq -0x18(%rbp), %rax
movq $0x140, 0x10(%rax)      # imm = 0x140
movq -0x18(%rbp), %rax
movq $0xc8, 0x18(%rax)
jmp 0x28f3b
movq -0x18(%rbp), %rax
movq $0x2, (%rax)
movq -0x18(%rbp), %rax
movq $0x280, 0x10(%rax)      # imm = 0x280
movq -0x18(%rbp), %rax
movq $0xc8, 0x18(%rax)
jmp 0x28f3b
movq -0x18(%rbp), %rax
movq $0x1, (%rax)
movq -0x18(%rbp), %rax
movq $0x280, 0x10(%rax)      # imm = 0x280
movq -0x18(%rbp), %rax
movq $0x190, 0x18(%rax)      # imm = 0x190
jmp 0x28f3b
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movzbl (%rax), %edx
leaq 0x17691f(%rip), %rsi    # 0x19f84e
movb $0x0, %al
callq 0x15bfb0
jmp 0x291fd
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
callq 0x15bba0
movq -0x18(%rbp), %rax
movq (%rax), %rdi
callq 0x15ce50
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
leaq 0x17664f(%rip), %rsi    # 0x19f5cf
movb $0x0, %al
callq 0x15b0d0
movq -0x28(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x3, %eax
jl 0x28fb1
movq -0x8(%rbp), %rdi
leaq 0x1768ed(%rip), %rsi    # 0x19f88b
movb $0x0, %al
callq 0x15bc10
movq -0x30(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rsi
movq -0x30(%rbp), %rdx
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rcx
movq -0x18(%rbp), %rax
movq 0x8(%rax), %r9
movl $0x10, %r8d
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0x174460
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x2abf0
movq -0x30(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x1651d0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movl %eax, %edx
leaq 0x176893(%rip), %rsi    # 0x19f8ce
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x30(%rbp), %rsi
callq 0x1651d0
movq %rax, %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rcx
shlq %rcx
movq -0x28(%rbp), %rax
movq %rcx, 0x18(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movl %eax, %edx
movq -0x28(%rbp), %rax
movq 0x18(%rax), %rax
movl %eax, %ecx
leaq 0x176852(%rip), %rsi    # 0x19f8ee
movb $0x0, %al
callq 0x15b0d0
movq -0x30(%rbp), %rax
movq -0x28(%rbp), %rcx
addq 0x8(%rcx), %rax
movq -0x28(%rbp), %rcx
addq 0x18(%rcx), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
movl $0x80, %esi
callq 0x15ce90
movq %rax, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
leaq 0x176836(%rip), %rsi    # 0x19f916
movb $0x0, %al
callq 0x15b0d0
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
cmpq -0x38(%rbp), %rax
jl 0x2910b
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rax
cmpq -0x40(%rbp), %rax
jle 0x2912d
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x18(%rax), %rax
movq 0x18(%rax), %rcx
leaq 0x17680f(%rip), %rsi    # 0x19f935
movb $0x0, %al
callq 0x15bc10
movq -0x8(%rbp), %rdi
movl $0x7d00, %esi           # imm = 0x7D00
movl $0x1, %edx
callq 0x168020
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x20(%rax)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x18(%rbp), %rdx
movq -0x30(%rbp), %rcx
callq 0x2b1c0
cmpl $0x0, %eax
jne 0x2916a
jmp 0x291fd
movq -0x18(%rbp), %rax
movq 0x30(%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x16aa20
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rdx
movl -0x44(%rbp), %r8d
movl $0x3, %ecx
movl $0x1, %eax
cmpl $0x0, %r8d
cmovnel %eax, %ecx
callq 0x16b750
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x8(%rbp), %rdi
callq 0x15d230
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x20(%rbp), %rdx
callq 0x174df0
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x2b5a0
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdi
movq -0x20(%rbp), %rsi
xorl %edx, %edx
callq 0x16afa0
cmpq $0x0, -0x18(%rbp)
je 0x2922b
movq -0x18(%rbp), %rax
movq 0x38(%rax), %rdi
callq 0x16af60
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0x1676a0
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x15b820
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0x15d260
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x15b820
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax,%rax)