// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/tim.c
// Function: de_run_tim
static void de_run_tim(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	unsigned int tim_type;
	int saved_indent_level;

	de_dbg_indent_save(c, &saved_indent_level);
	d = de_malloc(c, sizeof(lctx));

	de_dbg(c, "first header block at %d", 0);
	de_dbg_indent(c, 1);

	tim_type = (unsigned int)de_getu32le(4);
	d->bpp_code = tim_type & 0x07;
	d->palette_flag = (tim_type>>3)&0x01;

	de_dbg(c, "TIM type: 0x%08x", tim_type);
	de_dbg_indent(c, 1);

	switch(d->bpp_code) {
	case 0: d->bpp = 4; break;
	case 1: d->bpp = 8; break;
	case 2: d->bpp = 16; break;
	case 3: d->bpp = 24; break;
	case 4:
		de_err(c, "Mixed Format not supported");
		goto done;
	default:
		de_err(c, "Unknown bits/pixel code (%u)", d->bpp_code);
		goto done;
	}

	de_dbg(c, "bits/pixel: %d, has-palette: %u", (int)d->bpp, d->palette_flag);

	de_dbg_indent(c, -1); // end of TIM type field

	// Hack: Unindent as if the first header block were complete.
	// But it probably isn't. We'll re-indent if needed.
	de_dbg_indent(c, -1);

	switch(d->bpp) {
	case 8:
		do_pal8(c, d);
		break;
	default:
		de_err(c, "Unsupported bits/pixel (%d)", (int)d->bpp);
		goto done;
	}

done:
	de_dbg_indent_restore(c, saved_indent_level);
	de_free(c, d);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
leaq 0x10(%rsp), %rsi
callq 0xdebae
movl $0x420, %esi            # imm = 0x420
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r12
leaq 0x6af60(%rip), %rsi     # 0x132a36
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0xdeba7
movq 0x18(%rbx), %rdi
pushq $0x4
popq %r15
movq %r15, %rsi
callq 0xe409f
movl %eax, %ecx
andl $0x7, %ecx
movl %ecx, (%r12)
movl %eax, %ecx
shrl $0x3, %ecx
andl $0x1, %ecx
movl %ecx, 0x4(%r12)
leaq 0x6af33(%rip), %rsi     # 0x132a4f
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq %rbx, %rdi
movl %ebp, %esi
callq 0xdeba7
movl (%r12), %edx
cmpq $0x4, %rdx
ja 0xc7e61
leaq 0x6aebd(%rip), %rax     # 0x132a04
movslq (%rax,%rdx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
pushq $0x4
popq %rdx
jmp 0xc7b80
leaq 0x6af04(%rip), %rsi     # 0x132a60
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0xc7e9c
pushq $0x10
popq %rdx
pushq $0x10
jmp 0xc7b7e
pushq $0x18
popq %rdx
pushq $0x18
jmp 0xc7b7e
pushq $0x8
popq %rdx
pushq $0x8
popq %r15
movq %r15, 0x8(%r12)
movl 0x4(%r12), %ecx
leaq 0x6af07(%rip), %rsi     # 0x132a98
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %esi
callq 0xdeba7
movq %rbx, %rdi
movl %ebp, %esi
callq 0xdeba7
movq 0x8(%r12), %rdx
cmpq $0x8, %rdx
jne 0xc7d11
leaq 0x14(%rsp), %rsi
movq %rbx, %rdi
callq 0xdebae
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
cmpl $0x0, 0x4(%r12)
je 0xc7e74
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rsi
callq 0xe409f
movq %rax, %r14
movq 0x18(%rbx), %rdi
pushq $0x10
popq %rsi
callq 0xe3fa3
movq %rax, %r13
movq 0x18(%rbx), %rdi
pushq $0x12
popq %rsi
callq 0xe3fa3
movq %rax, %r15
leaq 0x6aeeb(%rip), %rsi     # 0x132b04
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xde896
leaq 0x6aee7(%rip), %rsi     # 0x132b14
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xde896
leaq 0x6aee7(%rip), %rsi     # 0x132b28
movq %rbx, %rdi
movq %r15, 0x20(%rsp)
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
leaq 0x6af37(%rip), %rsi     # 0x132b9c
pushq $0x14
popq %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq %r12, %rax
addq $0x20, %rax
movq %rax, 0x18(%rsp)
pushq $0x14
popq %rbp
xorl %r15d, %r15d
movq %r13, 0x28(%rsp)
cmpq %r13, %r15
jge 0xc7d1d
cmpq $0xff, %r15
ja 0xc7d1d
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xe3fa3
movq %rbx, %r13
movq %r12, %rbx
movq %rax, %r12
movl %r12d, %edi
callq 0xe1e15
movl %eax, %r14d
leaq 0x30(%rsp), %rdi
pushq $0x20
popq %rsi
leaq 0x6aed8(%rip), %rdx     # 0x132bad
movl %r12d, %ecx
movq %rbx, %r12
movq %r13, %rbx
movq 0x28(%rsp), %r13
xorl %eax, %eax
callq 0xde40c
movq %rbx, %rdi
movq %r15, %rsi
movl %r14d, %edx
leaq 0x30(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xdf153
movl %r14d, -0x8(%r12,%rbp,2)
incq %r15
addq $0x2, %rbp
jmp 0xc7c94
leaq 0x6ada0(%rip), %rsi     # 0x132ab8
jmp 0xc7e68
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
imulq 0x20(%rsp), %r13
leaq 0x14(,%r13,2), %r14
leaq 0x6adf9(%rip), %rsi     # 0x132b36
movq %r13, %r15
xorl %r13d, %r13d
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x18(%rbx), %rdi
movq %r14, %rsi
callq 0xe409f
leaq 0x6ade2(%rip), %rsi     # 0x132b50
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
leaq 0x1c(,%r15,2), %rsi
callq 0xe3fa3
movq %rax, %r14
addq %rax, %rax
movq %rax, 0x10(%r12)
movq 0x18(%rbx), %rdi
leaq 0x1e(,%r15,2), %rsi
callq 0xe3fa3
movq %rax, 0x18(%r12)
movl 0x10(%r12), %ecx
leaq 0x6adb2(%rip), %rsi     # 0x132b6a
movq %rbx, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xde896
movl 0x18(%r12), %edx
leaq 0x42370(%rip), %rsi     # 0x10a141
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x10(%r12), %rsi
movq 0x18(%r12), %rdx
movq %rbx, %rdi
callq 0xe6be1
testl %eax, %eax
je 0xc7e88
addq %r15, %r15
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x10(%r12), %rsi
movq 0x18(%r12), %rdx
pushq $0x3
popq %rcx
movq %rbx, %rdi
callq 0xe7437
movq %rax, %r13
addq $0x20, %r15
leaq 0x6ad5f(%rip), %rsi     # 0x132b85
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x10(%r12), %rcx
movq 0x18(%rbx), %rdi
andl $0x0, (%rsp)
pushq $0x8
popq %rdx
movq %r15, %rsi
movq 0x18(%rsp), %r8
movq %r13, %r9
callq 0xe7cbc
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xe70b7
jmp 0xc7e88
leaq 0x6ac13(%rip), %rsi     # 0x132a7b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
jmp 0xc7e9c
leaq 0x6ac59(%rip), %rsi     # 0x132ad4
xorl %r13d, %r13d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
movl 0x14(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
movq %r13, %rdi
callq 0xe7084
movl 0x10(%rsp), %esi
movq %rbx, %rdi
callq 0xdebb7
movq %rbx, %rdi
movq %r12, %rsi
callq 0xded32
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq