// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/bmp.c
// Function: de_identify_bmp
static int de_identify_bmp(deark *c)
{
	i64 fsize;
	i64 bits_offset;
	i64 infohdrsize;
	u32 compression_field = 0;
	int bmp_ext;
	u8 buf[6];

	de_read(buf, 0, sizeof(buf));
	if(de_memcmp(buf, "BM", 2)) {
		return 0;
	}

	bmp_ext = de_input_file_has_ext(c, "bmp");
	fsize = de_getu32le_direct(&buf[2]);
	bits_offset = de_getu32le(10);
	infohdrsize = de_getu32le(14);
	if(infohdrsize>=20) {
		compression_field = (u32)de_getu32le(14+16);
		// Don't detect KQP format as BMP
		if(infohdrsize==68 && compression_field==0x4745504aU) return 0;
	}

	if(infohdrsize<12) return 0;
	if(infohdrsize>256) return 0;
	if(bits_offset>=c->infile->len) return 0;
	if(bits_offset<14+infohdrsize) return 0;
	if(fsize==c->infile->len && bmp_ext) return 100;
	if(buf[2]==0xcb) {
		// Possible VBM file.
		// Windows BMP files are highly unlikely to start with 'B' 'M' \xcb,
		// because that would imply the file is an odd number of bytes in size,
		// which is legal but silly.
		if(bmp_ext) return 90;
		return 5;
	}

	if(bmp_ext) return 100;
	if(infohdrsize==12 || infohdrsize==40 || infohdrsize==108 ||
		infohdrsize==124)
	{
		return 100;
	}
	return 90;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x36(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x6, %ecx
callq 0x164d10
leaq -0x36(%rbp), %rax
movw (%rax), %ax
subw $0x4d42, %ax            # imm = 0x4D42
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0x42e3f
movl $0x0, -0x4(%rbp)
jmp 0x42fa9
movq -0x10(%rbp), %rdi
leaq 0x169ebd(%rip), %rsi    # 0x1acd07
callq 0x15d180
movl %eax, -0x30(%rbp)
leaq -0x36(%rbp), %rdi
addq $0x2, %rdi
callq 0x165500
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xa, %esi
callq 0x165540
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0xe, %esi
callq 0x165540
movq %rax, -0x28(%rbp)
cmpq $0x14, -0x28(%rbp)
jl 0x42ec9
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x1e, %esi
callq 0x165540
movl %eax, -0x2c(%rbp)
cmpq $0x44, -0x28(%rbp)
jne 0x42ec7
cmpl $0x4745504a, -0x2c(%rbp) # imm = 0x4745504A
jne 0x42ec7
movl $0x0, -0x4(%rbp)
jmp 0x42fa9
jmp 0x42ec9
cmpq $0xc, -0x28(%rbp)
jge 0x42edc
movl $0x0, -0x4(%rbp)
jmp 0x42fa9
cmpq $0x100, -0x28(%rbp)     # imm = 0x100
jle 0x42ef2
movl $0x0, -0x4(%rbp)
jmp 0x42fa9
movq -0x20(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jl 0x42f10
movl $0x0, -0x4(%rbp)
jmp 0x42fa9
movq -0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
addq $0xe, %rcx
cmpq %rcx, %rax
jge 0x42f2a
movl $0x0, -0x4(%rbp)
jmp 0x42fa9
movq -0x18(%rbp), %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jne 0x42f4b
cmpl $0x0, -0x30(%rbp)
je 0x42f4b
movl $0x64, -0x4(%rbp)
jmp 0x42fa9
movzbl -0x34(%rbp), %eax
cmpl $0xcb, %eax
jne 0x42f6e
cmpl $0x0, -0x30(%rbp)
je 0x42f65
movl $0x5a, -0x4(%rbp)
jmp 0x42fa9
movl $0x5, -0x4(%rbp)
jmp 0x42fa9
cmpl $0x0, -0x30(%rbp)
je 0x42f7d
movl $0x64, -0x4(%rbp)
jmp 0x42fa9
cmpq $0xc, -0x28(%rbp)
je 0x42f99
cmpq $0x28, -0x28(%rbp)
je 0x42f99
cmpq $0x6c, -0x28(%rbp)
je 0x42f99
cmpq $0x7c, -0x28(%rbp)
jne 0x42fa2
movl $0x64, -0x4(%rbp)
jmp 0x42fa9
movl $0x5a, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)