// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movq $0x0, -0x38(%rbp)
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x19140
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165210
movl %eax, -0x30(%rbp)
cmpl $0x3, -0x30(%rbp)
jbe 0x7109a
movl $0x0, -0x4(%rbp)
jmp 0x71267
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165210
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165210
movl %eax, -0x28(%rbp)
cmpl $0x1, -0x28(%rbp)
jb 0x710ce
cmpl $0x8, -0x28(%rbp)
jbe 0x710e6
cmpl $0xf, -0x28(%rbp)
je 0x710e6
cmpl $0x10, -0x28(%rbp)
je 0x710e6
cmpl $0x18, -0x28(%rbp)
je 0x710e6
cmpl $0x20, -0x28(%rbp)
jne 0x710e8
jmp 0x710f4
movl $0x0, -0x4(%rbp)
jmp 0x71267
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165210
movl %eax, -0x24(%rbp)
cmpl $0x4, -0x24(%rbp)
jbe 0x7111a
movl $0x0, -0x4(%rbp)
jmp 0x71267
movq -0x10(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x72320
cmpl $0x0, %eax
jne 0x71138
movl $0x0, -0x4(%rbp)
jmp 0x71267
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165210
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x38(%rbp), %rsi
callq 0x165210
movl %eax, -0x1c(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x723c0
movl %eax, -0x3c(%rbp)
cmpl $0xa, -0x2c(%rbp)
jb 0x71187
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x10, %esi
callq 0x165470
movl %eax, -0x18(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x13a43d(%rip), %rsi    # 0x1ab5cf
callq 0x15d180
movl %eax, -0x14(%rbp)
cmpl $0x58494d47, -0x18(%rbp) # imm = 0x58494D47
jne 0x711be
movl -0x14(%rbp), %edx
movl $0x46, %eax
movl $0x5a, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x4(%rbp)
jmp 0x71267
cmpl $0x0, -0x3c(%rbp)
je 0x711e2
cmpl $0x53545454, -0x18(%rbp) # imm = 0x53545454
je 0x711d6
cmpl $0x54494d47, -0x18(%rbp) # imm = 0x54494D47
jne 0x711e2
movl $0x28, -0x4(%rbp)
jmp 0x71267
cmpl $0x0, -0x3c(%rbp)
jne 0x711f7
cmpl $0x0, -0x14(%rbp)
jne 0x711f7
movl $0x0, -0x4(%rbp)
jmp 0x71267
cmpl $0x0, -0x30(%rbp)
jne 0x71214
cmpl $0x0, -0x14(%rbp)
je 0x71209
cmpl $0x0, -0x3c(%rbp)
jne 0x71212
movl $0x0, -0x4(%rbp)
jmp 0x71267
jmp 0x71227
cmpl $0x1, -0x30(%rbp)
jne 0x7121c
jmp 0x71225
movl $0x0, -0x4(%rbp)
jmp 0x71267
jmp 0x71227
cmpl $0x8, -0x2c(%rbp)
je 0x71239
cmpl $0x9, -0x2c(%rbp)
je 0x71239
cmpl $0x19, -0x2c(%rbp)
jne 0x71251
movl -0x14(%rbp), %edx
movl $0x32, %eax
movl $0x46, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x4(%rbp)
jmp 0x71267
cmpl $0x0, -0x14(%rbp)
je 0x71260
movl $0xe, -0x4(%rbp)
jmp 0x71267
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq