// File: /workspace/github_c_makefile_star2_O1/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 %r15d, %r15d
leaq 0x10(%rsp), %rsi
movl $0x20, %ecx
xorl %edx, %edx
callq 0xeeee8
leaq 0x1b(%rsp), %r14
movq %r14, %rdi
callq 0xef1c6
movq %rax, %r13
movzbl 0x2(%r14), %r12d
leaq 0x1e(%rsp), %r14
movq %r14, %rdi
callq 0xef1c6
movq %rax, 0x8(%rsp)
movb 0x2(%r14), %bpl
leaq 0x21(%rsp), %rdi
callq 0xef1c6
cmpq $0x200, %r13            # imm = 0x200
jne 0x5c64c
movq %rax, %r14
movb 0x25(%rsp), %r13b
leal -0x1(%r12), %eax
cmpl $0x3f, %eax
ja 0x5c623
movabsq $-0x7fffffff7fff7f75, %rcx # imm = 0x800000008000808B
btq %rax, %rcx
jae 0x5c623
leal -0x3(%rbp), %eax
cmpb $-0x2, %al
setb %al
leal -0x1(%r13), %ecx
cmpb $-0x1c, %cl
setb %cl
orb %al, %cl
jne 0x5c64c
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
movl $0x3, %r15d
cmovnel %eax, %r15d
movq 0x18(%rbx), %rdi
movl $0x1fe, %esi            # imm = 0x1FE
callq 0xef169
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 0xc6249(%rip), %rsi     # 0x12281c
movq %rbx, %rdi
callq 0xea936
movb $0x1, %r14b
testl %eax, %eax
jne 0x5c60a
leaq 0xc3366(%rip), %rsi     # 0x11f94f
movq %rbx, %rdi
callq 0xea936
testl %eax, %eax
jne 0x5c60a
leaq 0xe7056(%rip), %rsi     # 0x143652
movq %rbx, %rdi
callq 0xea936
testl %eax, %eax
setne %r14b
cmpl $0x6, %ebp
jl 0x5c632
testb %r14b, %r14b
movl $0x64, %eax
movl $0x50, %r15d
cmovnel %eax, %r15d
jmp 0x5c64c
cmpl $0x80, %r12d
je 0x5c532
jmp 0x5c64c
testb %r14b, %r14b
movl $0x3c, %eax
movl $0x9, %ecx
cmovnel %eax, %ecx
xorl %r15d, %r15d
cmpl $0x4, %ebp
cmovgel %ecx, %r15d
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq