// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
leaq -0x60(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
movl $0x20, %ecx
callq 0x164d10
leaq -0x60(%rbp), %rdi
addq $0xb, %rdi
callq 0x165260
movq %rax, -0x18(%rbp)
movb -0x53(%rbp), %al
movb %al, -0x35(%rbp)
leaq -0x60(%rbp), %rdi
addq $0xe, %rdi
callq 0x165260
movq %rax, -0x28(%rbp)
movb -0x50(%rbp), %al
movb %al, -0x36(%rbp)
leaq -0x60(%rbp), %rdi
addq $0x11, %rdi
callq 0x165260
movq %rax, -0x20(%rbp)
movb -0x4b(%rbp), %al
movb %al, -0x37(%rbp)
cmpq $0x200, -0x18(%rbp)     # imm = 0x200
je 0x82b28
movl $0x0, -0x4(%rbp)
jmp 0x82d37
movzbl -0x35(%rbp), %eax
movl %eax, -0x64(%rbp)
decl %eax
subl $0x2, %eax
jb 0x82b76
jmp 0x82b38
movl -0x64(%rbp), %eax
subl $0x4, %eax
je 0x82b76
jmp 0x82b42
movl -0x64(%rbp), %eax
subl $0x8, %eax
je 0x82b76
jmp 0x82b4c
movl -0x64(%rbp), %eax
subl $0x10, %eax
je 0x82b76
jmp 0x82b56
movl -0x64(%rbp), %eax
subl $0x20, %eax
je 0x82b76
jmp 0x82b60
movl -0x64(%rbp), %eax
subl $0x40, %eax
je 0x82b76
jmp 0x82b6a
movl -0x64(%rbp), %eax
subl $0x80, %eax
jne 0x82b78
jmp 0x82b76
jmp 0x82b84
movl $0x0, -0x4(%rbp)
jmp 0x82d37
movzbl -0x36(%rbp), %eax
cmpl $0x1, %eax
je 0x82ba2
movzbl -0x36(%rbp), %eax
cmpl $0x2, %eax
je 0x82ba2
movl $0x0, -0x4(%rbp)
jmp 0x82d37
movzbl -0x37(%rbp), %eax
cmpl $0xe5, %eax
jge 0x82bc2
movzbl -0x37(%rbp), %eax
cmpl $0x0, %eax
je 0x82bc2
movl $0x0, -0x4(%rbp)
jmp 0x82d37
movl $0x1, -0x2c(%rbp)
movzbl -0x60(%rbp), %eax
cmpl $0xeb, %eax
jne 0x82bea
movzbl -0x5e(%rbp), %eax
cmpl $0x90, %eax
jne 0x82bea
movl -0x2c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x2c(%rbp)
jmp 0x82c16
movzbl -0x60(%rbp), %eax
cmpl $0xe9, %eax
jne 0x82c00
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x82c14
movzbl -0x60(%rbp), %eax
cmpl $0x60, %eax
jne 0x82c12
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x82c14
jmp 0x82c16
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movl $0x1fe, %esi            # imm = 0x1FE
callq 0x1651d0
cmpq $0x55aa, %rax           # imm = 0x55AA
sete %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x30(%rbp)
cmpl $0x0, -0x30(%rbp)
je 0x82c48
movl -0x2c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x2c(%rbp)
movzbl -0x36(%rbp), %eax
cmpl $0x2, %eax
jne 0x82c5a
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
movzbl -0x37(%rbp), %eax
cmpl $0xe5, %eax
jl 0x82c6e
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
cmpq $0x1, -0x28(%rbp)
jne 0x82c7e
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
cmpq $0x70, -0x20(%rbp)
je 0x82c8f
cmpq $0xe0, -0x20(%rbp)
jne 0x82c98
movl -0x2c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x2c(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x12b7d6(%rip), %rsi    # 0x1ae479
callq 0x15d180
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x65(%rbp)
jne 0x82ce9
movq -0x10(%rbp), %rdi
leaq 0x128910(%rip), %rsi    # 0x1ab5cf
callq 0x15d180
movl %eax, %ecx
movb $0x1, %al
cmpl $0x0, %ecx
movb %al, -0x65(%rbp)
jne 0x82ce9
movq -0x10(%rbp), %rdi
leaq 0x14c98a(%rip), %rsi    # 0x1cf665
callq 0x15d180
cmpl $0x0, %eax
setne %al
movb %al, -0x65(%rbp)
movb -0x65(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x34(%rbp)
cmpl $0x6, -0x2c(%rbp)
jl 0x82d12
movl -0x34(%rbp), %edx
movl $0x50, %eax
movl $0x64, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x4(%rbp)
jmp 0x82d37
cmpl $0x4, -0x2c(%rbp)
jl 0x82d30
movl -0x34(%rbp), %edx
movl $0x9, %eax
movl $0x3c, %ecx
cmpl $0x0, %edx
cmovnel %ecx, %eax
movl %eax, -0x4(%rbp)
jmp 0x82d37
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq