// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movl $0xa8, %esi
callq 0xe8b2b
movq %rax, %r14
movl $0x40, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r15
leaq 0x20(%r14), %rax
movq %rax, 0x30(%r15)
movl $0x1, 0x28(%r15)
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xeead3
movb %al, (%r14)
movzbl %al, %edx
leaq 0xf1045(%rip), %rsi     # 0x113650
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movzbl (%r14), %edx
cmpl $0x6, %edx
movq %r14, 0x8(%rsp)
jae 0x22ac1
leal (,%rdx,8), %eax
leaq 0xf0d2b(%rip), %rcx     # 0x113360
movq (%rax,%rcx), %rcx
leaq 0xf0d50(%rip), %rsi     # 0x113390
movq (%rax,%rsi), %rsi
movl %edx, %eax
leaq 0xf0d73(%rip), %rdx     # 0x1133c0
movq (%rdx,%rax,8), %rdx
movq %rcx, (%r15)
movq %rsi, 0x10(%r15)
movq %rdx, 0x18(%r15)
movq %rbx, %rdi
callq 0xe8f88
movq (%r15), %rdi
callq 0xe9c9b
movq %rax, 0x8(%r15)
leaq 0xf1028(%rip), %rsi     # 0x11369f
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movl $0x1, %r12d
cmpb $0x3, (%r14)
jb 0x226a6
leaq 0xf12c5(%rip), %rsi     # 0x11395b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movl $0x5, %r12d
movq 0x18(%rbx), %rsi
movq 0x8(%r15), %r9
movq 0x30(%r15), %rcx
movl $0x0, (%rsp)
movl $0x10, %r8d
movq %rbx, %rdi
movq %r12, %rdx
callq 0xf876e
movq %rbx, %rdi
movq %r15, %rsi
callq 0x23ac6
leaq 0x20(%r12), %rsi
movq 0x18(%rbx), %rdi
callq 0xeeb67
movq 0x8(%rsp), %r14
movq %rax, 0x8(%r14)
movq %r12, %r13
orq $0x22, %r13
leaq 0xf12a4(%rip), %rsi     # 0x11399e
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xeeb67
movq %rax, 0x10(%r14)
addq $0x24, %r12
leaq (%rax,%rax), %rcx
movq %rcx, 0x18(%r14)
leaq 0xf1295(%rip), %rsi     # 0x1139be
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x8(%r14), %r13
addq %r12, %r13
addq 0x18(%r14), %r13
movl $0x80, %esi
movq %r13, %rdi
callq 0xe9cb4
movq %rax, %rbp
leaq 0xf128f(%rip), %rsi     # 0x1139e6
movq %rbx, %rdi
movq %r13, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rcx
cmpq %r13, %rcx
setge %al
cmpq %rbp, %rcx
setle %dl
testb %dl, %al
jne 0x22793
leaq 0xf127f(%rip), %rsi     # 0x113a05
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe8faa
movl $0x7d00, %esi           # imm = 0x7D00
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf09c5
movq %rax, 0x20(%r15)
xorl %eax, %eax
cmpq $0x1, (%r15)
sete %al
movl $0xa, %ecx
movl $0x14, %edx
cmoveq %rcx, %rdx
movl $0x50, %ecx
movl $0xa0, %esi
cmoveq %rcx, %rsi
incq %rax
movq 0x8(%rsp), %r13
movq %rax, 0x68(%r13)
movq %rdx, 0x60(%r13)
movq %rsi, 0x70(%r13)
leaq 0xf1249(%rip), %rsi     # 0x113a32
movq %rbx, %r14
xorl %ebx, %ebx
movq %r14, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x8(%r13), %rsi
addq $0x2, %rsi
movq %r14, %rdi
callq 0xe8b2b
movq 0x18(%r14), %rdi
movq 0x8(%r13), %rcx
movq %rax, 0x10(%rsp)
movq %rax, %rsi
movq %r12, %rdx
callq 0xee8d4
addq 0x8(%r13), %r12
leaq 0xf121d(%rip), %rsi     # 0x113a4b
movq %r14, 0x18(%rsp)
movq %r14, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
movl $0x0, 0x78(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x80(%r13)
movups %xmm0, 0x90(%r13)
movq $0x0, 0xa0(%r13)
leaq 0x30(%rsp), %rbp
cmpq 0x8(%r13), %rbx
jge 0x229e8
leaq 0x1(%rbx), %rdi
movq 0x10(%rsp), %rax
movzbl (%rax,%rbx), %r14d
testb %r14b, %r14b
js 0x228f2
cmpq $0x1, %r14
je 0x22938
testl %r14d, %r14d
jne 0x22993
addq 0x10(%rsp), %rdi
callq 0xeeb5c
movq %rax, %r13
addq $0x3, %rbx
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
movl $0x2, %ecx
movq %rbp, %rsi
movq %r12, %rdx
callq 0xee8d4
addq $0x2, %r12
testq %r13, %r13
jle 0x229d0
movq %rbx, %r14
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x23b15
decq %r13
jne 0x228da
jmp 0x229d8
movq %rdi, 0x20(%rsp)
orq $-0x100, %r14
movq 0x18(%rsp), %rbx
movq 0x8(%rsp), %r13
movq 0x18(%rbx), %rdi
movl $0x2, %ecx
movq %rbp, %rsi
movq %r12, %rdx
callq 0xee8d4
addq $0x2, %r12
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x23b15
incq %r14
jne 0x22908
movq %r13, %rbx
jmp 0x2298c
addq 0x10(%rsp), %rdi
callq 0xeeb5c
addq $0x3, %rbx
testq %rax, %rax
jle 0x229d0
movq %rax, %r13
movq %rbx, 0x20(%rsp)
movq 0x18(%rsp), %r14
movq 0x8(%rsp), %rbx
movq 0x18(%r14), %rdi
movl $0x2, %ecx
movq %rbp, %rsi
movq %r12, %rdx
callq 0xee8d4
addq $0x2, %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x23b15
decq %r13
jne 0x22961
movq 0x20(%rsp), %r14
jmp 0x229d8
movq %rdi, %r13
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
movl $0x2, %ecx
movq %rbp, %rsi
movq %r12, %rdx
callq 0xee8d4
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x23b15
decq %r14
jne 0x229b4
addq $0x2, %r12
movq %r13, %r14
jmp 0x229d8
movq %rbx, %r14
movq 0x8(%rsp), %rbx
cmpl $0x0, 0x78(%rbx)
movq %rbx, %r13
movq %r14, %rbx
je 0x2286b
movq 0xa0(%r13), %rdx
leaq 0xf106b(%rip), %rsi     # 0x113a61
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0xa0(%r13), %rdx
cmpq $0x3e7f, %rdx           # imm = 0x3E7F
jg 0x22a26
leaq 0xf105d(%rip), %rsi     # 0x113a79
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0xe8be2
movq 0x8(%r15), %rsi
movq 0x30(%r15), %rdi
callq 0xf1ed8
movq 0x10(%r15), %rsi
movq 0x18(%r15), %rdx
xorl %ecx, %ecx
testl %eax, %eax
sete %cl
leal 0x1(,%rcx,2), %ecx
movq %rbx, %rdi
callq 0xf266a
movq %rax, 0x38(%r15)
movq %rbx, %rdi
callq 0xea504
movq %rax, %r14
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xf8e1c
movq (%r15), %rbx
cmpq $0x4, %rbx
je 0x22ada
cmpq $0x2, %rbx
je 0x22ada
cmpq $0x1, %rbx
jne 0x22ba1
movq 0x8(%rsp), %rax
movq 0x70(%rax), %rcx
movq 0x20(%r15), %rdi
movq 0x30(%r15), %r8
movq 0x38(%r15), %r9
movl $0x0, (%rsp)
movl $0x1, %edx
xorl %esi, %esi
callq 0xf2f79
jmp 0x22ba1
leaq 0xf0e56(%rip), %rsi     # 0x11391e
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0x22bb4
movq %r14, 0x20(%rsp)
movq $0x0, 0x28(%rsp)
cmpl $0x2, %ebx
je 0x22afd
cmpl $0x4, %ebx
jne 0x22b9c
movl $0x140, %eax            # imm = 0x140
jmp 0x22b02
movl $0x280, %eax            # imm = 0x280
movq %rax, 0x10(%rsp)
xorl %r13d, %r13d
leaq 0x28(%rsp), %rbp
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x20(%r15), %rdi
movq %rbp, %rsi
callq 0xeeb91
movl %eax, 0x30(%rsp,%r12,4)
incq %r12
cmpq %r12, %rbx
jne 0x22b15
xorl %r12d, %r12d
movb $0xf, %cl
subb %r12b, %cl
movl $0x1, %eax
shll %cl, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movl 0x30(%rsp,%rcx,4), %esi
movl $0x1, %edi
shll %cl, %edi
andl %eax, %esi
testl %esi, %esi
cmovel %esi, %edi
addl %edi, %edx
incq %rcx
cmpq %rcx, %rbx
jne 0x22b41
movl %edx, %eax
movq 0x30(%r15), %rcx
movq 0x38(%r15), %rdi
movq %r12, %rsi
orq %r14, %rsi
movl (%rcx,%rax,4), %ecx
movq %r13, %rdx
callq 0xf24ba
incq %r12
cmpq $0x10, %r12
jne 0x22b31
addq $0x10, %r14
cmpq 0x10(%rsp), %r14
jb 0x22b12
incq %r13
cmpq $0xc8, %r13
jne 0x22b0f
movq 0x20(%rsp), %r14
movq 0x38(%r15), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xf22d0
movq 0x18(%rsp), %rbx
movq 0x38(%r15), %rdi
callq 0xf228d
movq 0x20(%r15), %rdi
callq 0xf052b
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe8be2
movq %rbx, %rdi
movq %r14, %rsi
callq 0xea50e
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0xe8be2
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq