// File: /workspace/github_c_makefile_star2_O1/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 $0x58, %rsp
movq %rdi, %rbx
movl $0xa8, %esi
callq 0xe969b
movq %rax, %r14
movl $0x40, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r15
leaq 0x20(%r14), %rax
movq %rax, 0x30(%r15)
movl $0x1, 0x28(%r15)
movq 0x18(%rbx), %rdi
xorl %esi, %esi
callq 0xef0d5
movb %al, (%r14)
movzbl %al, %edx
leaq 0xf0fd1(%rip), %rsi     # 0x1135a0
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movzbl (%r14), %edx
cmpl $0x6, %edx
movq %r14, 0x10(%rsp)
jae 0x22a85
leal (,%rdx,8), %eax
leaq 0xf0cb7(%rip), %rcx     # 0x1132b0
movq (%rax,%rcx), %rcx
leaq 0xf0cdc(%rip), %rsi     # 0x1132e0
movq (%rax,%rsi), %rsi
movl %edx, %eax
leaq 0xf0cff(%rip), %rdx     # 0x113310
movq (%rdx,%rax,8), %rdx
movq %rcx, (%r15)
movq %rsi, 0x10(%r15)
movq %rdx, 0x18(%r15)
movq %rbx, %rdi
callq 0xe9b05
movq (%r15), %rdi
callq 0xea7f2
movq %rax, 0x8(%r15)
leaq 0xf0fb4(%rip), %rsi     # 0x1135ef
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movl $0x1, %r12d
cmpb $0x3, (%r14)
jb 0x2266a
leaq 0xf1251(%rip), %rsi     # 0x1138ab
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
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 0xf8c62
movq %rbx, %rdi
movq %r15, %rsi
callq 0x23ae2
leaq 0x20(%r12), %rsi
movq 0x18(%rbx), %rdi
callq 0xef169
movq 0x10(%rsp), %r14
movq %rax, 0x8(%r14)
movq %r12, %r13
orq $0x22, %r13
leaq 0xf1230(%rip), %rsi     # 0x1138ee
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r13, %rsi
callq 0xef169
movq %rax, 0x10(%r14)
addq $0x24, %r12
leaq (%rax,%rax), %rcx
movq %rcx, 0x18(%r14)
leaq 0xf1221(%rip), %rsi     # 0x11390e
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x8(%r14), %r13
addq %r12, %r13
addq 0x18(%r14), %r13
movl $0x80, %esi
movq %r13, %rdi
callq 0xea80b
movq %rax, %rbp
leaq 0xf121b(%rip), %rsi     # 0x113936
movq %rbx, %rdi
movq %r13, %rdx
movq %rax, %rcx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rax
movq 0x18(%rax), %rcx
cmpq %r13, %rcx
setge %al
cmpq %rbp, %rcx
setle %dl
testb %dl, %al
jne 0x22757
leaq 0xf120b(%rip), %rsi     # 0x113955
movq %rbx, %rdi
movq %r13, %rdx
xorl %eax, %eax
callq 0xe9b27
movl $0x7d00, %esi           # imm = 0x7D00
movq %rbx, %rdi
movl $0x1, %edx
callq 0xf0faf
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 0x10(%rsp), %r13
movq %rax, 0x68(%r13)
movq %rdx, 0x60(%r13)
movq %rsi, 0x70(%r13)
leaq 0xf11d5(%rip), %rsi     # 0x113982
movq %rbx, %r14
xorl %ebx, %ebx
movq %r14, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x8(%r13), %rsi
addq $0x2, %rsi
movq %r14, %rdi
callq 0xe969b
movq 0x18(%r14), %rdi
movq 0x8(%r13), %rcx
movq %rax, 0x18(%rsp)
movq %rax, %rsi
movq %r12, %rdx
callq 0xeeee8
addq 0x8(%r13), %r12
leaq 0xf11a9(%rip), %rsi     # 0x11399b
movq %r14, 0x20(%rsp)
movq %r14, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movl $0x0, 0x78(%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x80(%r13)
movups %xmm0, 0x90(%r13)
movq $0x0, 0xa0(%r13)
leaq 0x40(%rsp), %rbp
cmpq 0x8(%r13), %rbx
jge 0x229ac
leaq 0x1(%rbx), %rdi
movq 0x18(%rsp), %rax
movzbl (%rax,%rbx), %r14d
testb %r14b, %r14b
js 0x228b6
cmpq $0x1, %r14
je 0x228fc
testl %r14d, %r14d
jne 0x22957
addq 0x18(%rsp), %rdi
callq 0xef15e
movq %rax, %r13
addq $0x3, %rbx
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rdi
movl $0x2, %ecx
movq %rbp, %rsi
movq %r12, %rdx
callq 0xeeee8
addq $0x2, %r12
testq %r13, %r13
jle 0x22994
movq %rbx, %r14
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x23b31
decq %r13
jne 0x2289e
jmp 0x2299c
movq %rdi, 0x28(%rsp)
orq $-0x100, %r14
movq 0x20(%rsp), %rbx
movq 0x10(%rsp), %r13
movq 0x18(%rbx), %rdi
movl $0x2, %ecx
movq %rbp, %rsi
movq %r12, %rdx
callq 0xeeee8
addq $0x2, %r12
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x23b31
incq %r14
jne 0x228cc
movq %r13, %rbx
jmp 0x22950
addq 0x18(%rsp), %rdi
callq 0xef15e
addq $0x3, %rbx
testq %rax, %rax
jle 0x22994
movq %rax, %r13
movq %rbx, 0x28(%rsp)
movq 0x20(%rsp), %r14
movq 0x10(%rsp), %rbx
movq 0x18(%r14), %rdi
movl $0x2, %ecx
movq %rbp, %rsi
movq %r12, %rdx
callq 0xeeee8
addq $0x2, %r12
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x23b31
decq %r13
jne 0x22925
movq 0x28(%rsp), %r14
jmp 0x2299c
movq %rdi, %r13
movq 0x20(%rsp), %rax
movq 0x18(%rax), %rdi
movl $0x2, %ecx
movq %rbp, %rsi
movq %r12, %rdx
callq 0xeeee8
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x23b31
decq %r14
jne 0x22978
addq $0x2, %r12
movq %r13, %r14
jmp 0x2299c
movq %rbx, %r14
movq 0x10(%rsp), %rbx
cmpl $0x0, 0x78(%rbx)
movq %rbx, %r13
movq %r14, %rbx
je 0x2282f
movq 0xa0(%r13), %rdx
leaq 0xf0ff7(%rip), %rsi     # 0x1139b1
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0xa0(%r13), %rdx
cmpq $0x3e7f, %rdx           # imm = 0x3E7F
jg 0x229ea
leaq 0xf0fe9(%rip), %rsi     # 0x1139c9
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0xe9752
movq 0x8(%r15), %rsi
movq 0x30(%r15), %rdi
callq 0xf2544
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 0xf2cab
movq %rax, 0x38(%r15)
movq %rbx, %rdi
callq 0xea9a9
movq %rax, %r14
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0xf9323
movq (%r15), %rax
cmpq $0x4, %rax
je 0x22a9e
cmpq $0x2, %rax
je 0x22a9e
cmpq $0x1, %rax
jne 0x22b95
movq 0x10(%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 0xf35b8
jmp 0x22b95
leaq 0xf0de2(%rip), %rsi     # 0x11386e
xorl %r14d, %r14d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0x22ba8
movq %r15, %rbx
movq $0x0, 0x30(%rsp)
cmpl $0x2, %eax
movq %rax, %r15
movq %r14, 0x38(%rsp)
je 0x22ac8
cmpl $0x4, %r15d
jne 0x22b8d
movl $0x140, %eax            # imm = 0x140
jmp 0x22acd
movl $0x280, %eax            # imm = 0x280
movq %rax, 0x18(%rsp)
movl %r15d, %r14d
xorl %r13d, %r13d
leaq 0x30(%rsp), %rsi
movq %r15, 0x28(%rsp)
xorl %r12d, %r12d
testl %r15d, %r15d
je 0x22b09
movq %rbx, %r15
xorl %ebp, %ebp
movq 0x20(%r15), %rdi
callq 0xef193
leaq 0x30(%rsp), %rsi
movl %eax, 0x40(%rsp,%rbp,4)
incq %rbp
cmpq %rbp, %r14
jne 0x22aef
xorl %ebp, %ebp
movq 0x28(%rsp), %r15
testl %r15d, %r15d
je 0x22b45
movb $0xf, %cl
subb %bpl, %cl
movl $0x1, %eax
shll %cl, %eax
xorl %ecx, %ecx
xorl %edx, %edx
movl 0x40(%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, %r14
jne 0x22b25
movl %edx, %eax
jmp 0x22b47
xorl %eax, %eax
movq 0x30(%rbx), %rcx
movq 0x38(%rbx), %rdi
movq %rbp, %rsi
orq %r12, %rsi
movl (%rcx,%rax,4), %ecx
movq %r13, %rdx
callq 0xf2afd
incq %rbp
cmpq $0x10, %rbp
leaq 0x30(%rsp), %rsi
jne 0x22b10
addq $0x10, %r12
cmpq 0x18(%rsp), %r12
jb 0x22ae5
incq %r13
cmpq $0xc8, %r13
jne 0x22ae2
movq 0x38(%rsp), %r14
movq %rbx, %r15
movq 0x38(%r15), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0xf2917
movq 0x20(%rsp), %rbx
testq %r15, %r15
je 0x22bca
movq 0x38(%r15), %rdi
callq 0xf28d4
movq 0x20(%r15), %rdi
callq 0xf0b13
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe9752
movq %rbx, %rdi
movq %r14, %rsi
callq 0xea9b3
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
callq 0xe9752
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq