// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/fat.c
// Function: de_identify_fat
static int de_identify_fat(deark *c)
{
	i64 bytes_per_sector;
	i64 max_root_dir_entries;
	i64 num_rsvd_sectors;
	int confidence = 0;
	int has_pc_sig;
	int has_ext;
	u8 sectors_per_cluster;
	u8 num_fats;
	u8 media_descr;
	u8 b[32];

	// TODO: This needs a lot of work.
	// It's good enough for most FAT12 floppy disk images.

	de_read(b, 0, sizeof(b));
	bytes_per_sector = de_getu16le_direct(&b[11]);
	sectors_per_cluster = b[13];
	num_rsvd_sectors = de_getu16le_direct(&b[14]);
	num_fats = b[16];
	max_root_dir_entries = de_getu16le_direct(&b[17]);
	media_descr = b[21];

	if(bytes_per_sector!=512) return 0;
	switch(sectors_per_cluster) {
	case 1: case 2: case 4: case 8:
	case 16: case 32: case 64: case 128:
		break;
	default:
		return 0;
	}
	if(num_fats!=1 && num_fats!=2) return 0;
	if(media_descr<0xe5 && media_descr!=0) return 0; // Media descriptor

	confidence = 1;
	if(b[0]==0xeb && b[2]==0x90) confidence += 2;
	else if(b[0]==0xe9) confidence += 1;
	else if(b[0]==0x60) confidence += 1;
	has_pc_sig = (de_getu16be(510)==0x55aa);
	if(has_pc_sig) confidence += 2;
	if(num_fats==2) confidence += 1;
	if(media_descr>=0xe5) confidence += 1;
	if(num_rsvd_sectors==1) confidence += 1;
	if(max_root_dir_entries==112 || max_root_dir_entries==224) confidence += 2;

	has_ext = de_input_file_has_ext(c, "ima") ||
		de_input_file_has_ext(c, "img") ||
		de_input_file_has_ext(c, "st");

	if(confidence>=6) return (has_ext?100:80);
	else if(confidence>=4) return (has_ext?60:9);
	else return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq 0x18(%rdi), %rdi
xorl %r12d, %r12d
leaq 0x10(%rsp), %rsi
pushq $0x20
popq %rcx
xorl %edx, %edx
callq 0xe3cb7
leaq 0x1b(%rsp), %r14
movq %r14, %rdi
callq 0xe3f9f
movq %rax, %r13
movzbl 0x2(%r14), %r15d
leaq 0x1e(%rsp), %r14
movq %r14, %rdi
callq 0xe3f9f
movq %rax, 0x8(%rsp)
movb 0x2(%r14), %bpl
leaq 0x21(%rsp), %rdi
callq 0xe3f9f
cmpq $0x200, %r13            # imm = 0x200
jne 0x59340
movq %rax, %r14
movb 0x25(%rsp), %r13b
leal -0x1(%r15), %eax
cmpl $0x3f, %eax
ja 0x5931b
movabsq $-0x7fffffff7fff7f75, %rcx # imm = 0x800000008000808B
btq %rax, %rcx
jae 0x5931b
leal -0x3(%rbp), %eax
cmpb $-0x2, %al
setb %al
leal -0x1(%r13), %ecx
cmpb $-0x1c, %cl
setb %cl
orb %al, %cl
jne 0x59340
movb 0x10(%rsp), %al
movb 0x12(%rsp), %cl
movl %eax, %edx
xorb $-0x15, %dl
xorb $-0x70, %cl
cmpb $-0x17, %al
sete %sil
cmpb $0x60, %al
sete %al
orb %sil, %al
movzbl %al, %eax
incl %eax
orb %dl, %cl
pushq $0x3
popq %r15
cmovnel %eax, %r15d
movq 0x18(%rbx), %rdi
movl $0x1fe, %esi            # imm = 0x1FE
callq 0xe3f46
leal 0x2(%r15), %ecx
cmpq $0x55aa, %rax           # imm = 0x55AA
cmovnel %r15d, %ecx
xorl %eax, %eax
cmpb $0x2, %bpl
sete %al
xorl %edx, %edx
cmpq $0x1, 0x8(%rsp)
sete %dl
addl %eax, %edx
cmpb $-0x1b, %r13b
sbbl $-0x1, %edx
leal (%rdx,%rcx), %ebp
cmpq $0xe0, %r14
leal 0x2(%rdx,%rcx), %eax
cmovel %eax, %ebp
cmpq $0x70, %r14
cmovel %eax, %ebp
leaq 0xbc595(%rip), %rsi     # 0x115864
movq %rbx, %rdi
callq 0xdfcd5
movb $0x1, %r14b
testl %eax, %eax
jne 0x59306
leaq 0xb96c2(%rip), %rsi     # 0x1129a7
movq %rbx, %rdi
callq 0xdfcd5
testl %eax, %eax
jne 0x59306
leaq 0xdd3ea(%rip), %rsi     # 0x1366e2
movq %rbx, %rdi
callq 0xdfcd5
testl %eax, %eax
setne %r14b
cmpl $0x6, %ebp
jb 0x5932a
testb %r14b, %r14b
pushq $0x64
popq %rax
pushq $0x50
popq %r12
cmovnel %eax, %r12d
jmp 0x59340
cmpl $0x80, %r15d
je 0x59230
jmp 0x59340
testb %r14b, %r14b
pushq $0x3c
popq %rax
pushq $0x9
popq %rcx
cmovnel %eax, %ecx
xorl %r12d, %r12d
cmpl $0x4, %ebp
cmovael %ecx, %r12d
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq