// File: /workspace/github_c_makefile_star2_O1/jsummers[P]deark/modules/gemras.c
// Function: de_identify_gemraster
static int de_identify_gemraster(deark *c)
{
	struct gemras_id id;
	i64 pos = 0;
	int dens_ok;

	de_zeromem(&id, sizeof(struct gemras_id));
	id.ver = (UI)de_getu16be_p(&pos);
	if(id.ver>3) return 0;
	id.hdrlen = (UI)de_getu16be_p(&pos);
	id.nplanes = (UI)de_getu16be_p(&pos);
	if((id.nplanes>=1 && id.nplanes<=8) || id.nplanes==15 ||
		id.nplanes==16 || id.nplanes==24 || id.nplanes==32)
	{
		;
	}
	else {
		return 0;
	}
	id.patlen = (UI)de_getu16be_p(&pos);
	// patlen possibly can be up to 8, but 3 is the most I've seen.
	if(id.patlen>4) return 0;
	if(!hdrlen_seems_ok(c, &id)) {
		return 0;
	}
	id.pixwidth = (UI)de_getu16be_p(&pos);
	id.pixheight = (UI)de_getu16be_p(&pos);
	// TODO: Consolidate with set_density().
	dens_ok = has_sane_density(&id);

	if(id.hdrlen>=10) {
		id.sig16 = (u32)de_getu32be(16);
	}
	id.has_ext = de_input_file_has_ext(c, "img");
	if(id.sig16==CODE_XIMG) {
		return id.has_ext?90:70;
	}

	if(dens_ok && (id.sig16==CODE_STTT || id.sig16==CODE_TIMG)) {
		return 40;
	}

	if(!dens_ok && !id.has_ext) return 0;

	// Unfortunately, some files with version=0 exist.
	// If version is >1, require a known signature.
	// TODO: False negatives apparently exist, but we need more info to
	// support them.
	if(id.ver==0) {
		if(!id.has_ext || !dens_ok) return 0;
	}
	else if(id.ver==1) {
		;
	}
	else {
		return 0;
	}

	if(id.hdrlen==8 || id.hdrlen==9 || id.hdrlen==25) {
		return id.has_ext?70:50;
	}

	if(id.has_ext) return 14;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %r15
movq %rsp, %rsi
movq $0x0, (%rsi)
movq 0x18(%rdi), %rdi
callq 0xef193
movq %rax, %r14
xorl %ebp, %ebp
cmpl $0x3, %r14d
ja 0x506a0
movq 0x18(%r15), %rdi
movq %rsp, %r12
movq %r12, %rsi
callq 0xef193
movq %rax, %rbx
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xef193
cmpl $0x20, %eax
ja 0x506a0
movl %eax, %eax
movabsq $0x1010181fe, %rcx      # imm = 0x1010181FE
btq %rax, %rcx
jae 0x506a0
movq 0x18(%r15), %rdi
movq %rsp, %rsi
callq 0xef193
cmpl $0x5, %eax
setae %al
cmpl $0x8, %ebx
setb %cl
orb %al, %cl
jne 0x506a0
movl %ebx, %eax
addq %rax, %rax
movq 0x18(%r15), %rcx
cmpq 0x18(%rcx), %rax
jg 0x506a0
cmpl $0x3c, %ebx
jae 0x5058b
movq 0x18(%r15), %rdi
movq %rsp, %r13
movq %r13, %rsi
callq 0xef193
movq %rax, %r12
movq 0x18(%r15), %rdi
movq %r13, %rsi
callq 0xef193
cmpl $0x1, %r12d
je 0x505a9
testl %r12d, %r12d
jne 0x505b1
movb $0x1, %r13b
testl %eax, %eax
je 0x505e5
movl %r12d, %ecx
xorl $0x1, %ecx
movl %eax, %edx
xorl $0x1, %edx
orl %ecx, %edx
jne 0x505b1
jmp 0x505e5
cmpl $0x18a, %ebx            # imm = 0x18A
jg 0x50688
cmpl $0x6b, %ebx
je 0x5054a
cmpl $0xcb, %ebx
je 0x5054a
jmp 0x506a0
movb $0x1, %r13b
cmpl $0x1, %eax
je 0x505e5
movl $0x515, %ecx            # imm = 0x515
cmpl %ecx, %r12d
setae %dl
cmpl %ecx, %eax
setae %cl
xorl %r13d, %r13d
orb %dl, %cl
jne 0x505e5
cmpl $0x14, %r12d
jb 0x505e5
cmpl $0x14, %eax
jb 0x505e5
leal (%r12,%r12,2), %ecx
cmpl %eax, %ecx
jb 0x505e5
leal (%rax,%rax,2), %eax
cmpl %r12d, %eax
setae %r13b
xorl %r12d, %r12d
cmpl $0xa, %ebx
jb 0x505fe
movq 0x18(%r15), %rdi
movl $0x10, %esi
callq 0xef32a
movq %rax, %r12
leaq 0xcf34a(%rip), %rsi     # 0x11f94f
movq %r15, %rdi
callq 0xea936
cmpl $0x58494d47, %r12d      # imm = 0x58494D47
jne 0x50627
testl %eax, %eax
movl $0x46, %eax
movl $0x5a, %ebp
cmovel %eax, %ebp
jmp 0x506a0
testb %r13b, %r13b
je 0x50643
movl $0x28, %ebp
cmpl $0x53545454, %r12d      # imm = 0x53545454
je 0x506a0
cmpl $0x54494d47, %r12d      # imm = 0x54494D47
je 0x506a0
testl %eax, %eax
setne %cl
orb %r13b, %cl
xorl %ebp, %ebp
cmpb $0x1, %cl
jne 0x506a0
cmpl $0x1, %r14d
je 0x5066b
testl %r14d, %r14d
jne 0x506a0
testl %eax, %eax
sete %cl
xorb $0x1, %r13b
orb %cl, %r13b
jne 0x506a0
cmpl $0x19, %ebx
ja 0x506b1
movl $0x2000300, %ecx        # imm = 0x2000300
btl %ebx, %ecx
jae 0x506b1
testl %eax, %eax
movl $0x32, %eax
movl $0x46, %ebp
jmp 0x50622
cmpl $0x30b, %ebx            # imm = 0x30B
je 0x5054a
cmpl $0x18b, %ebx            # imm = 0x18B
je 0x5054a
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %eax, %eax
movl $0xe, %ebp
jmp 0x50622