// File: /workspace/github_c_makefile_star2_O0/jsummers[P]deark/modules/fat.c
// Function: root_dir_seems_valid
static int root_dir_seems_valid(deark *c, lctx *d)
{
	i64 pos1;
	i64 max_entries_to_check;
	i64 i;
	i64 entrycount = 0;
	i64 errcount = 0;

	if(d->num_fat_bits==32) return 1;

	if(d->max_root_dir_entries16<=0) return 0;
	pos1 = sectornum_to_offset(c, d, d->root_dir_sector);
	if(pos1 + d->max_root_dir_entries16 * 32 > c->infile->len) {
		return 0;
	}

	max_entries_to_check = de_max_int(d->max_root_dir_entries16, 10);
	for(i=0; i<max_entries_to_check; i++) {
		i64 entrypos;
		u8 firstbyte;
		u8 attribs;

		entrypos = pos1 + 32*i;
		firstbyte = de_getbyte(entrypos);
		if(firstbyte==0x00) break;
		if(firstbyte==0xe5) continue; // Don't validate deleted entries
		entrycount++;
		attribs = de_getbyte(entrypos+11);
		if(attribs & 0xc0) {
			errcount++;
		}
		else if((attribs & 0x3f) == 0x0f) {
			; // LFN; OK
		}
		else if((attribs & 0x18)==0x18) {
			errcount++; // dir + vol.label not valid
		}

		// TODO: It's really lame to only validate the attribs field, when there's
		// so much more we could be doing. But it's a hard problem. We don't want
		// to be too sensitive to minor errors.
	}

	if(errcount>1 || (errcount==1 && entrycount<=1)) {
		return 0;
	}
	return 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x18(%rbp), %rax
movzbl 0x14(%rax), %eax
cmpl $0x20, %eax
jne 0x83a69
movl $0x1, -0x4(%rbp)
jmp 0x83bd6
movq -0x18(%rbp), %rax
cmpq $0x0, 0x68(%rax)
jg 0x83a80
movl $0x0, -0x4(%rbp)
jmp 0x83bd6
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq -0x18(%rbp), %rax
movq 0x70(%rax), %rdx
callq 0x83e80
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x68(%rcx), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq -0x10(%rbp), %rcx
movq 0x18(%rcx), %rcx
cmpq 0x18(%rcx), %rax
jle 0x83ac6
movl $0x0, -0x4(%rbp)
jmp 0x83bd6
movq -0x18(%rbp), %rax
movq 0x68(%rax), %rdi
movl $0xa, %esi
callq 0x15cd20
movq %rax, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x28(%rbp), %rax
jge 0x83bb1
movq -0x20(%rbp), %rax
movq -0x30(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rsi
callq 0x165060
movb %al, -0x49(%rbp)
movzbl -0x49(%rbp), %eax
cmpl $0x0, %eax
jne 0x83b27
jmp 0x83bb1
movzbl -0x49(%rbp), %eax
cmpl $0xe5, %eax
jne 0x83b34
jmp 0x83ba0
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
movq -0x48(%rbp), %rsi
addq $0xb, %rsi
callq 0x165060
movb %al, -0x4a(%rbp)
movzbl -0x4a(%rbp), %eax
andl $0xc0, %eax
cmpl $0x0, %eax
je 0x83b74
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x83b9e
movzbl -0x4a(%rbp), %eax
andl $0x3f, %eax
cmpl $0xf, %eax
jne 0x83b82
jmp 0x83b9c
movzbl -0x4a(%rbp), %eax
andl $0x18, %eax
cmpl $0x18, %eax
jne 0x83b9a
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0x83b9c
jmp 0x83b9e
jmp 0x83ba0
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x83ae4
cmpq $0x1, -0x40(%rbp)
jg 0x83bc6
cmpq $0x1, -0x40(%rbp)
jne 0x83bcf
cmpq $0x1, -0x38(%rbp)
jg 0x83bcf
movl $0x0, -0x4(%rbp)
jmp 0x83bd6
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nop