// File: /workspace/github_c_makefile_star2_O1/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 0x8(%rsp), %rsi
callq 0xe958c
movl $0x420, %esi            # imm = 0x420
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r14
leaq 0x6e738(%rip), %rsi     # 0x13f95e
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%rbx), %rdi
movl $0x4, %r15d
movl $0x4, %esi
callq 0xef37e
movl %eax, %ecx
andl $0x7, %ecx
movl %ecx, (%r14)
movl %eax, %ecx
shrl $0x3, %ecx
andl $0x1, %ecx
movl %ecx, 0x4(%r14)
leaq 0x6e709(%rip), %rsi     # 0x13f977
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movl (%r14), %edx
cmpq $0x4, %rdx
ja 0xd15e1
leaq 0x6e691(%rip), %rax     # 0x13f92c
movslq (%rax,%rdx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0x8, %r15d
jmp 0xd12d0
leaq 0x6e6d5(%rip), %rsi     # 0x13f988
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0xd161c
movl $0x10, %r15d
jmp 0xd12d0
movl $0x18, %r15d
movq %r15, 0x8(%r14)
movl 0x4(%r14), %ecx
leaq 0x6e6e1(%rip), %rsi     # 0x13f9c0
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x8(%r14), %rdx
cmpq $0x8, %rdx
jne 0xd15d8
leaq 0xc(%rsp), %rsi
movq %rbx, %rdi
callq 0xe958c
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
cmpl $0x0, 0x4(%r14)
je 0xd15f4
movq 0x18(%rbx), %rdi
movl $0x8, %esi
callq 0xef37e
movq %rax, %r12
movq 0x18(%rbx), %rdi
movl $0x10, %esi
callq 0xef1ca
movq %rax, %r15
movq 0x18(%rbx), %rdi
movl $0x12, %esi
callq 0xef1ca
movq %rax, %r13
leaq 0x6e6b9(%rip), %rsi     # 0x13fa2c
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
leaq 0x6e6b5(%rip), %rsi     # 0x13fa3c
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
leaq 0x6e6b5(%rip), %rsi     # 0x13fa50
movq %rbx, %rdi
movq %r13, 0x20(%rsp)
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
leaq 0x6e703(%rip), %rsi     # 0x13fac4
movq %rbx, %rdi
movl $0x14, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq %r15, 0x10(%rsp)
testq %r15, %r15
movq %rbx, %r13
movq %r14, 0x18(%rsp)
jle 0xd1483
movq 0x10(%rsp), %rax
decq %rax
movl $0xff, %ecx
cmpq %rcx, %rax
cmovbq %rax, %rcx
movl %ecx, %eax
leaq 0x2(,%rax,2), %rax
movq %rax, 0x28(%rsp)
leaq 0x30(%rsp), %rbp
xorl %ebx, %ebx
xorl %r12d, %r12d
movq 0x18(%r13), %rdi
leaq 0x14(%rbx), %rsi
callq 0xef1ca
movq %rax, %r15
movl %r15d, %edi
callq 0xecf12
movl %eax, %r14d
movl $0x20, %esi
movq %rbp, %rdi
leaq 0x6e68b(%rip), %rdx     # 0x13fad5
movl %r15d, %ecx
xorl %eax, %eax
callq 0xe8d67
movq %r13, %rdi
movq %r12, %rsi
movl %r14d, %edx
movq %rbp, %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe9c1e
movq 0x18(%rsp), %rax
movl %r14d, 0x20(%rax,%rbx,2)
incq %r12
addq $0x2, %rbx
cmpq %rbx, 0x28(%rsp)
jne 0xd1420
movq %r13, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x10(%rsp), %r15
imulq 0x20(%rsp), %r15
leaq 0x14(,%r15,2), %r14
leaq 0x6e5b4(%rip), %rsi     # 0x13fa5e
xorl %r12d, %r12d
movq %r13, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
movq %r13, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x18(%r13), %rdi
movq %r14, %rsi
callq 0xef37e
leaq 0x6e59e(%rip), %rsi     # 0x13fa78
movq %r13, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%r13), %rdi
leaq 0x1c(,%r15,2), %rsi
callq 0xef1ca
movq %rax, %r14
addq %rax, %rax
movq 0x18(%rsp), %rbp
movq %rax, 0x10(%rbp)
movq 0x18(%r13), %rdi
leaq 0x1e(,%r15,2), %rsi
callq 0xef1ca
movq %rax, 0x18(%rbp)
movl 0x10(%rbp), %ecx
leaq 0x6e56d(%rip), %rsi     # 0x13fa92
movq %r13, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe9226
movl 0x18(%rbp), %edx
leaq 0x45bf5(%rip), %rsi     # 0x117131
movq %r13, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x10(%rbp), %rsi
movq 0x18(%rbp), %rdx
movq %r13, %rdi
callq 0xf241d
testl %eax, %eax
movq %r13, %rbx
movq %rbp, %r14
je 0xd1608
addq %r15, %r15
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0x10(%r14), %rsi
movq 0x18(%r14), %rdx
movq %rbx, %rdi
movl $0x3, %ecx
callq 0xf2cab
movq %rax, %r12
addq $0x20, %r15
leaq 0x6e516(%rip), %rsi     # 0x13faad
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x10(%r14), %rcx
movq 0x18(%rbx), %rdi
movq %r14, %r8
addq $0x20, %r8
movl $0x0, (%rsp)
movl $0x8, %edx
movq %r15, %rsi
movq %r12, %r9
callq 0xf35b8
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf2923
jmp 0xd1608
leaq 0x6e401(%rip), %rsi     # 0x13f9e0
jmp 0xd15e8
leaq 0x6e3bb(%rip), %rsi     # 0x13f9a3
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
jmp 0xd161c
leaq 0x6e401(%rip), %rsi     # 0x13f9fc
xorl %r12d, %r12d
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movl 0xc(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
movq %r12, %rdi
callq 0xf28d4
movl 0x8(%rsp), %esi
movq %rbx, %rdi
callq 0xe9595
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq