// File: /workspace/github_c_makefile_star2_O3/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, %r15
leaq 0x10(%rsp), %rsi
callq 0xe8a1c
movl $0x420, %esi            # imm = 0x420
movq %r15, %rdi
callq 0xe8b2b
movq %rax, %rbp
leaq 0x6f740(%rip), %rsi     # 0x13fd66
movq %r15, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe8617
movq %r15, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%r15), %rdi
movl $0x4, %r14d
movl $0x4, %esi
callq 0xeed61
movl %eax, %ecx
andl $0x7, %ecx
movl %ecx, (%rbp)
movl %eax, %ecx
shrl $0x3, %ecx
andl $0x1, %ecx
movl %ecx, 0x4(%rbp)
leaq 0x6f712(%rip), %rsi     # 0x13fd7f
movq %r15, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq %r15, %rdi
movl $0x1, %esi
callq 0xe8a15
movl (%rbp), %edx
cmpq $0x4, %rdx
ja 0xd09dc
leaq 0x6f69a(%rip), %rax     # 0x13fd34
movslq (%rax,%rdx,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0x4, %edx
jmp 0xd06e5
leaq 0x6f6df(%rip), %rsi     # 0x13fd90
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0xd0a17
movl $0x10, %edx
movl $0x10, %r14d
jmp 0xd06e5
movl $0x18, %edx
movl $0x18, %r14d
jmp 0xd06e5
movl $0x8, %edx
movl $0x8, %r14d
movq %r14, 0x8(%rbp)
movl 0x4(%rbp), %ecx
leaq 0x6f6d5(%rip), %rsi     # 0x13fdc8
movq %r15, %rdi
xorl %eax, %eax
callq 0xe8617
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x8(%rbp), %rdx
cmpq $0x8, %rdx
jne 0xd09d3
leaq 0x14(%rsp), %rsi
movq %r15, %rdi
callq 0xe8a1c
movq %r15, %rdi
movl $0x1, %esi
callq 0xe8a15
cmpl $0x0, 0x4(%rbp)
je 0xd09ef
movq 0x18(%r15), %rdi
movl $0x8, %esi
callq 0xeed61
movq %rax, %r12
movq 0x18(%r15), %rdi
movl $0x10, %esi
callq 0xeebc8
movq %rax, %rbx
movq 0x18(%r15), %rdi
movl $0x12, %esi
callq 0xeebc8
movq %rax, %r14
leaq 0x6f6b1(%rip), %rsi     # 0x13fe34
movq %r15, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
leaq 0x6f6ad(%rip), %rsi     # 0x13fe44
movq %r15, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0xe8617
leaq 0x6f6ae(%rip), %rsi     # 0x13fe58
movq %r15, %rdi
movq %r14, 0x20(%rsp)
movl %r14d, %edx
xorl %eax, %eax
callq 0xe8617
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
leaq 0x6f6fc(%rip), %rsi     # 0x13fecc
movq %r15, %rdi
movl $0x14, %edx
xorl %eax, %eax
callq 0xe8617
movq %r15, %rdi
movl $0x1, %esi
callq 0xe8a15
movq %rbx, 0x18(%rsp)
testq %rbx, %rbx
jle 0xd088a
movq 0x18(%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)
xorl %ebx, %ebx
xorl %r12d, %r12d
movq 0x18(%r15), %rdi
leaq 0x14(%rbx), %rsi
callq 0xeebc8
movq %r15, %r13
movq %rax, %r15
movl %r15d, %edi
callq 0xec8b9
movl %eax, %r14d
movl $0x20, %esi
leaq 0x30(%rsp), %rdi
leaq 0x6f68c(%rip), %rdx     # 0x13fedd
movl %r15d, %ecx
movq %r13, %r15
xorl %eax, %eax
callq 0xe8193
movq %r13, %rdi
movq %r12, %rsi
movl %r14d, %edx
leaq 0x30(%rsp), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0xe90a3
movl %r14d, 0x20(%rbp,%rbx,2)
incq %r12
addq $0x2, %rbx
cmpq %rbx, 0x28(%rsp)
jne 0xd0822
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x18(%rsp), %rbx
imulq 0x20(%rsp), %rbx
leaq 0x14(,%rbx,2), %r14
leaq 0x6f5b5(%rip), %rsi     # 0x13fe66
xorl %r12d, %r12d
movq %r15, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe8617
movq %r15, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x18(%r15), %rdi
movq %r14, %rsi
callq 0xeed61
leaq 0x6f59f(%rip), %rsi     # 0x13fe80
movq %r15, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%r15), %rdi
leaq 0x1c(,%rbx,2), %rsi
callq 0xeebc8
movq %rax, %r14
addq %rax, %rax
movq %rax, 0x10(%rbp)
movq 0x18(%r15), %rdi
leaq 0x1e(,%rbx,2), %rsi
callq 0xeebc8
movq %rax, 0x18(%rbp)
movl 0x10(%rbp), %ecx
leaq 0x6f573(%rip), %rsi     # 0x13fe9a
movq %r15, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0xe8617
movl 0x18(%rbp), %edx
leaq 0x46893(%rip), %rsi     # 0x1171d1
movq %r15, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x10(%rbp), %rsi
movq 0x18(%rbp), %rdx
movq %r15, %rdi
callq 0xf1db1
testl %eax, %eax
je 0xd0a03
addq %rbx, %rbx
movq %r15, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0x10(%rbp), %rsi
movq 0x18(%rbp), %rdx
movq %r15, %rdi
movl $0x3, %ecx
callq 0xf266a
movq %rax, %r12
addq $0x20, %rbx
leaq 0x6f522(%rip), %rsi     # 0x13feb5
movq %r15, %rdi
movl %ebx, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x10(%rbp), %rcx
movq 0x18(%r15), %rdi
movq %rbp, %r8
addq $0x20, %r8
movl $0x0, (%rsp)
movl $0x8, %edx
movq %rbx, %rsi
movq %r12, %r9
callq 0xf2f79
movq %r12, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xf22dc
jmp 0xd0a03
leaq 0x6f40e(%rip), %rsi     # 0x13fde8
jmp 0xd09e3
leaq 0x6f3c8(%rip), %rsi     # 0x13fdab
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9295
jmp 0xd0a17
leaq 0x6f40e(%rip), %rsi     # 0x13fe04
xorl %r12d, %r12d
movq %r15, %rdi
xorl %eax, %eax
callq 0xe9295
movl 0x14(%rsp), %esi
movq %r15, %rdi
callq 0xe8a25
movq %r12, %rdi
callq 0xf228d
movl 0x10(%rsp), %esi
movq %r15, %rdi
callq 0xe8a25
movq %r15, %rdi
movq %rbp, %rsi
callq 0xe8be2
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq