// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/tga.c
// Function: de_identify_tga
static int de_identify_tga(deark *c)
{
	u8 b[18];
	u8 x;
	int has_tga_ext;
	i64 v2_footer_pos = 0;

	// TGA v1 format has no signature, but there are only a few common types of
	// it. We'll at least try to identify anything that we support.
	de_read(b, 0, 18);

	if(b[1]>1) return 0; // Color map type should be 0 or 1.

	if(b[12]==0 && b[13]==0) return 0; // Width can't be 0.
	if(b[14]==0 && b[15]==0) return 0; // Height can't be 0.

	if(look_for_v2_signature(c, 1, &v2_footer_pos)) {
		return 100;
	}

	// 0 is valid here, but it means "no image", and we need all the
	// can't-be-0 bytes we can find...
	if(b[2]==0) {
		// ...except that we have to allow 0 in .vst format.
		// TODO: .vst should maybe be a separate module.
		if((UI)de_getu32be(20)!=CODE_IGCH) {
			return 0;
		}
	}
	else if(b[2]==1 || b[2]==2 || b[2]==3 ||
		b[2]==9 || b[2]==10 || b[2]==11 || b[2]==32 || b[2]==33)
	{
		;
	}
	else {
		return 0; // Unknown image type
	}

	// bits/pixel:
	if(b[16]!=1 && b[16]!=8 && b[16]!=15 && b[16]!=16 && b[16]!=24 && b[16]!=32) return 0;

	// Bits per palette entry. Supposed to be 0 if there is no palette, but
	// in practice it may be 24 instead.
	if((b[1]==0 && b[7]==0) || b[7]==15 || b[7]==16 || b[7]==24 || b[7]==32) {
		;
	}
	else {
		return 0;
	}

	has_tga_ext = de_input_file_has_ext(c, "tga");

	x = b[17]&0x0f; // Number of attribute bits
	if(x!=0 && x!=1 && x!=8 && !has_tga_ext) return 0;

	if(has_tga_ext) {
		return 100;
	}
	if(de_input_file_has_ext(c, "vst")) {
		return 40;
	}
	return 8;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x20, %rsp
movq %rdi, %r14
movq $0x0, 0x18(%rsp)
movq 0x18(%rdi), %rdi
xorl %ebx, %ebx
movq %rsp, %r15
movl $0x12, %ecx
movq %r15, %rsi
xorl %edx, %edx
callq 0xeeee8
cmpb $0x1, 0x1(%r15)
ja 0xcb416
movb 0xd(%rsp), %al
orb 0xc(%rsp), %al
je 0xcb416
movb 0xf(%rsp), %al
orb 0xe(%rsp), %al
je 0xcb416
leaq 0x18(%rsp), %rdx
movq %r14, %rdi
movl $0x1, %esi
callq 0xcbb4a
testl %eax, %eax
je 0xcb422
movl $0x64, %ebx
movl %ebx, %eax
addq $0x20, %rsp
popq %rbx
popq %r14
popq %r15
retq
movzbl 0x2(%rsp), %eax
cmpq $0x21, %rax
ja 0xcb416
movabsq $0x300000e0e, %rcx      # imm = 0x300000E0E
btq %rax, %rcx
jae 0xcb4e6
movzbl 0x10(%rsp), %eax
cmpq $0x20, %rax
ja 0xcb416
movabsq $0x101018102, %rcx      # imm = 0x101018102
btq %rax, %rcx
jae 0xcb416
movb 0x7(%rsp), %al
movb 0x1(%rsp), %cl
orb %al, %cl
je 0xcb482
movzbl %al, %eax
cmpl $0x20, %eax
ja 0xcb416
movl %eax, %eax
movabsq $0x101018000, %rcx      # imm = 0x101018000
btq %rax, %rcx
jae 0xcb416
leaq 0x706d3(%rip), %rsi     # 0x13bb5c
movq %r14, %rdi
callq 0xea936
testl %eax, %eax
jne 0xcb411
movzbl 0x11(%rsp), %ecx
andl $0xf, %ecx
cmpl $0x8, %ecx
ja 0xcb416
movl $0x103, %edx            # imm = 0x103
btl %ecx, %edx
jae 0xcb416
movl $0x64, %ebx
testl %eax, %eax
jne 0xcb416
leaq 0x70dc6(%rip), %rsi     # 0x13c292
movq %r14, %rdi
callq 0xea936
xorl %ebx, %ebx
testl %eax, %eax
setne %bl
shll $0x5, %ebx
orl $0x8, %ebx
jmp 0xcb416
testq %rax, %rax
jne 0xcb416
movq 0x18(%r14), %rdi
movl $0x14, %esi
callq 0xef32a
cmpl $0x49474348, %eax       # imm = 0x49474348
jne 0xcb416
jmp 0xcb441