// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, (%rsi)
movq 0x18(%rdi), %rdi
callq 0xe3f6e
movq %rax, %r14
xorl %ebp, %ebp
cmpl $0x3, %r14d
ja 0x4ddcc
movq 0x18(%r15), %rdi
movq %rsp, %r12
movq %r12, %rsi
callq 0xe3f6e
movq %rax, %rbx
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xe3f6e
cmpl $0x20, %eax
ja 0x4ddcc
movl %eax, %eax
movabsq $0x1010181fe, %rcx      # imm = 0x1010181FE
btq %rax, %rcx
jae 0x4ddcc
movq 0x18(%r15), %rdi
movq %rsp, %rsi
callq 0xe3f6e
cmpl $0x5, %eax
setae %al
cmpl $0x8, %ebx
setb %cl
orb %al, %cl
jne 0x4ddcc
movl %ebx, %eax
addq %rax, %rax
movq 0x18(%r15), %rdi
cmpq 0x18(%rdi), %rax
jg 0x4ddcc
cmpl $0x3c, %ebx
jae 0x4dd28
movq %rsp, %r13
movq %r13, %rsi
callq 0xe3f6e
movq %rax, %r12
movq 0x18(%r15), %rdi
movq %r13, %rsi
callq 0xe3f6e
cmpl $0x1, %r12d
je 0x4dd4a
testl %r12d, %r12d
jne 0x4dd5e
testl %eax, %eax
jne 0x4dd52
movb $0x1, %r13b
jmp 0x4dd92
cmpl $0x6b, %ebx
je 0x4dcfa
cmpl $0xcb, %ebx
je 0x4dcfa
cmpl $0x30b, %ebx            # imm = 0x30B
je 0x4dcfa
cmpl $0x18b, %ebx            # imm = 0x18B
je 0x4dcfa
jmp 0x4ddcc
movb $0x1, %r13b
cmpl $0x1, %eax
je 0x4dd92
cmpl $0x514, %eax            # imm = 0x514
jbe 0x4dd72
xorl %r13d, %r13d
jmp 0x4dd92
movl $0x515, %ecx            # imm = 0x515
cmpl %ecx, %r12d
setae %dl
cmpl %ecx, %eax
setae %cl
orb %dl, %cl
jne 0x4dd59
xorl %r13d, %r13d
cmpl $0x14, %r12d
jb 0x4dd92
cmpl $0x14, %eax
jb 0x4dd92
leal (%r12,%r12,2), %ecx
cmpl %eax, %ecx
jb 0x4dd92
leal (%rax,%rax,2), %eax
cmpl %r12d, %eax
setae %r13b
xorl %r12d, %r12d
cmpl $0xa, %ebx
jb 0x4dda9
movq 0x18(%r15), %rdi
pushq $0x10
popq %rsi
callq 0xe404f
movq %rax, %r12
leaq 0xc4bf7(%rip), %rsi     # 0x1129a7
movq %r15, %rdi
callq 0xdfcd5
cmpl $0x58494d47, %r12d      # imm = 0x58494D47
jne 0x4dddd
testl %eax, %eax
pushq $0x46
popq %rax
pushq $0x5a
popq %rbp
cmovel %eax, %ebp
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testb %r13b, %r13b
je 0x4ddfe
pushq $0x28
popq %rbp
cmpl $0x53545454, %r12d      # imm = 0x53545454
je 0x4ddcc
cmpl $0x54494d47, %r12d      # imm = 0x54494D47
je 0x4ddcc
testl %eax, %eax
setne %cl
jmp 0x4de04
movb $0x1, %cl
testl %eax, %eax
je 0x4de32
cmpl $0x1, %r14d
je 0x4de1a
xorl %ebp, %ebp
testl %r14d, %r14d
jne 0x4ddcc
andb %cl, %r13b
cmpb $0x1, %r13b
jne 0x4ddcc
cmpl $0x19, %ebx
ja 0x4de36
movl $0x2000300, %ecx        # imm = 0x2000300
btl %ebx, %ecx
jae 0x4de36
testl %eax, %eax
pushq $0x32
popq %rax
pushq $0x46
jmp 0x4ddc8
xorl %ebp, %ebp
jmp 0x4ddcc
testl %eax, %eax
pushq $0xe
jmp 0x4ddc8