// File: /workspace/github_c_makefile_star2_O3/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 0xeeb91
movq %rax, %r14
cmpl $0x3, %r14d
ja 0x50444
movq 0x18(%r15), %rdi
movq %rsp, %r12
movq %r12, %rsi
callq 0xeeb91
movq %rax, %rbx
movq 0x18(%r15), %rdi
movq %r12, %rsi
callq 0xeeb91
cmpl $0x20, %eax
ja 0x50444
movl %eax, %eax
movabsq $0x1010181fe, %rcx      # imm = 0x1010181FE
btq %rax, %rcx
jae 0x50444
movq 0x18(%r15), %rdi
movq %rsp, %rsi
callq 0xeeb91
cmpl $0x5, %eax
setae %al
cmpl $0x8, %ebx
setb %cl
orb %al, %cl
jne 0x50444
movl %ebx, %eax
addq %rax, %rax
movq 0x18(%r15), %rdi
cmpq 0x18(%rdi), %rax
jle 0x50457
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x3c, %ebx
jae 0x5048a
movq %rsp, %r13
movq %r13, %rsi
callq 0xeeb91
movq %rax, %r12
movq 0x18(%r15), %rdi
movq %r13, %rsi
callq 0xeeb91
cmpl $0x1, %r12d
je 0x504a5
testl %r12d, %r12d
jne 0x504af
testl %eax, %eax
jne 0x504c7
movb $0x1, %bpl
jmp 0x504c9
cmpl $0x18a, %ebx            # imm = 0x18A
jg 0x5058d
cmpl $0x6b, %ebx
je 0x5045c
cmpl $0xcb, %ebx
je 0x5045c
jmp 0x50444
movb $0x1, %bpl
cmpl $0x1, %eax
jne 0x504c7
jmp 0x504c9
movl $0x515, %ecx            # imm = 0x515
cmpl %ecx, %r12d
setae %dl
cmpl %ecx, %eax
setae %cl
orb %dl, %cl
je 0x505aa
xorl %ebp, %ebp
cmpl $0x9, %ebx
jbe 0x50508
movq 0x18(%r15), %rdi
movl $0x10, %esi
callq 0xeed0d
movq %rax, %r12
leaq 0xcf591(%rip), %rsi     # 0x11fa77
movq %r15, %rdi
callq 0xea491
cmpl $0x58494d47, %r12d      # imm = 0x58494D47
jne 0x5051a
testl %eax, %eax
movl $0x46, %eax
movl $0x5a, %ecx
jmp 0x505e1
leaq 0xcf568(%rip), %rsi     # 0x11fa77
movq %r15, %rdi
callq 0xea491
xorl %r12d, %r12d
testb %bpl, %bpl
je 0x50545
movl $0x28, %ecx
cmpl $0x53545454, %r12d      # imm = 0x53545454
je 0x50446
cmpl $0x54494d47, %r12d      # imm = 0x54494D47
je 0x50446
testl %eax, %eax
setne %cl
jmp 0x5054f
movb $0x1, %cl
testl %eax, %eax
je 0x50444
cmpl $0x1, %r14d
je 0x50570
testl %r14d, %r14d
jne 0x50444
andb %cl, %bpl
movl $0x0, %ecx
cmpb $0x1, %bpl
jne 0x50446
cmpl $0x19, %ebx
ja 0x505da
movl $0x2000300, %ecx        # imm = 0x2000300
btl %ebx, %ecx
jae 0x505da
testl %eax, %eax
movl $0x32, %eax
movl $0x46, %ecx
jmp 0x505e1
cmpl $0x30b, %ebx            # imm = 0x30B
je 0x5045c
cmpl $0x18b, %ebx            # imm = 0x18B
je 0x5045c
jmp 0x50444
xorl %ebp, %ebp
cmpl $0x14, %r12d
jb 0x504c9
cmpl $0x14, %eax
jb 0x504c9
leal (%r12,%r12,2), %ecx
cmpl %eax, %ecx
jb 0x504c9
leal (%rax,%rax,2), %eax
cmpl %r12d, %eax
setae %bpl
jmp 0x504c9
testl %eax, %eax
movl $0xe, %ecx
cmovel %eax, %ecx
jmp 0x50446