// File: /workspace/github_c_makefile_star2_O2/jsummers[P]deark/modules/fat.c
// Function: de_run_fat
static void de_run_fat(deark *c, de_module_params *mparams)
{
	lctx *d = NULL;
	const char *s;
	int got_root_dir = 0;
	de_encoding default_encoding =  DE_ENCODING_CP437_G;

	if(mparams) {
		// out_params.flags:
		//  0x1 = No valid FAT directory structure found
		mparams->out_params.flags = 0;
	}

	d = de_malloc(c, sizeof(lctx));

	d->prescan_root_dir = (u8)de_get_ext_option_bool(c, "fat:scanroot", 1);
	d->opt_check_root_dir = (u8)de_get_ext_option_bool(c, "fat:checkroot", 1);
	s = de_get_ext_option(c, "fat:subfmt");
	if(s) {
		if(!de_strcmp(s, "pc")) {
			d->subfmt_req = FAT_SUBFMT_PC;
		}
		else if(!de_strcmp(s, "atarist")) {
			d->subfmt_req = FAT_SUBFMT_ATARIST;
		}
	}
	d->subfmt = d->subfmt_req;
	if(d->subfmt==FAT_SUBFMT_ATARIST) {
		default_encoding = DE_ENCODING_ATARIST;
	}

	d->input_encoding = de_get_input_encoding(c, mparams, default_encoding);

	// TODO: Detect MBR?
	if(!do_boot_sector(c, d, 0)) goto done;
	if(d->num_fat_bits==0) goto done;

	switch(d->platform) {
	case FAT_PLATFORM_PC:
		de_declare_fmtf(c, "FAT%d - PC", d->num_fat_bits);
		break;
	case FAT_PLATFORM_ATARIST:
		de_declare_fmtf(c, "FAT%d - Atari ST", d->num_fat_bits);
		break;
	default:
		de_declare_fmtf(c, "FAT%d - Unknown platform", d->num_fat_bits);
		break;
	}

	if(!do_read_fat(c, d)) goto done;

	if(d->opt_check_root_dir) {
		if(!root_dir_seems_valid(c, d)) {
			de_warn(c, "This file does not appear to contain a valid FAT "
				"directory structure. (\"-opt fat:checkroot=0\" to try anyway)");
			goto done;
		}
	}

	d->curpath = de_strarray_create(c, MAX_NESTING_LEVEL+10);
	got_root_dir = 1;
	do_root_dir(c, d);

done:
	if(!got_root_dir) {
		// Inform the parent module that we failed to do anything.
		if(mparams) {
			mparams->out_params.flags |= 0x1;
		}
	}

	if(d) {
		de_free(c, d->fat_nextcluster);
		de_free(c, d->cluster_used_flags);
		de_free(c, d->cluster_used_flags_saved);
		if(d->curpath) de_strarray_destroy(d->curpath);
		if(d->ea_data) dbuf_close(d->ea_data);
		de_free(c, d);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %r15
movq %rdi, %rbx
testq %rsi, %rsi
je 0x587e1
andl $0x0, 0x40(%r15)
movl $0xb0, %esi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r14
leaq 0xbca4d(%rip), %rsi     # 0x115245
pushq $0x1
popq %rbp
movq %rbx, %rdi
movl %ebp, %edx
callq 0xdfae5
movb %al, 0x5(%r14)
leaq 0xbca42(%rip), %rsi     # 0x115252
movq %rbx, %rdi
movl %ebp, %edx
callq 0xdfae5
movb %al, 0x4(%r14)
leaq 0xbca3b(%rip), %rsi     # 0x115260
movq %rbx, %rdi
callq 0xdfa87
testq %rax, %rax
je 0x5885d
cmpb $0x70, (%rax)
jne 0x58843
cmpb $0x63, 0x1(%rax)
jne 0x58843
cmpb $0x0, 0x2(%rax)
je 0x58859
leaq 0xbca21(%rip), %rsi     # 0x11526b
movq %rax, %rdi
callq 0x181a0
testl %eax, %eax
jne 0x5885d
pushq $0x2
popq %rbp
movl %ebp, 0x8(%r14)
movl 0x8(%r14), %eax
movl %eax, 0xc(%r14)
xorl %ecx, %ecx
cmpl $0x2, %eax
sete %cl
leal (%rcx,%rcx,4), %edx
addl $0xd, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe08d2
movl %eax, (%r14)
leaq 0xbca8d(%rip), %rsi     # 0x115315
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
xorl %edx, %edx
cmpl $0x2, 0xc(%r14)
sete %dl
xorq $0x3, %rdx
movq 0x18(%rbx), %rdi
leaq 0x20(%rsp), %rcx
pushq $0x10
popq %r8
xorl %esi, %esi
callq 0xdec40
leaq 0xbca60(%rip), %rsi     # 0x115328
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
cmpl $0x2, 0xc(%r14)
jne 0x58918
pushq $0x2
popq %rsi
pushq $0x6
popq %rdx
movq %rbx, %rdi
callq 0x598fc
movq 0x18(%rbx), %rdi
pushq $0x8
popq %rsi
pushq $0x3
popq %rdx
leaq 0x20(%rsp), %rcx
pushq $0x10
popq %r8
callq 0xdec40
leaq 0xbca2e(%rip), %rsi     # 0x115337
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
jmp 0x58926
pushq $0x3
popq %rsi
pushq $0x8
popq %rdx
movq %rbx, %rdi
callq 0x598fc
movq %r15, 0x10(%rsp)
leaq 0x40(%rsp), %r15
movq $0xb, (%r15)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
movq %rax, 0x18(%r14)
leaq 0xad6da(%rip), %rsi     # 0x106028
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3f2a
movzbl %al, %edx
movq %rdx, 0x20(%r14)
leaq 0xbc9d2(%rip), %rsi     # 0x115346
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
movq %rax, 0x50(%r14)
leaq 0xbc9c9(%rip), %rsi     # 0x11535e
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
cmpq $0x0, 0x50(%r14)
jne 0x589b0
movq $0x1, 0x50(%r14)
movq 0x18(%rbx), %rdi
leaq 0x40(%rsp), %r15
movq %r15, %rsi
callq 0xe3f2a
movzbl %al, %edx
movq %rdx, 0x58(%r14)
leaq 0xbc9a4(%rip), %rsi     # 0x115373
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
movq %rax, 0x68(%r14)
leaq 0xbc996(%rip), %rsi     # 0x115386
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
leaq 0xbc9a8(%rip), %rsi     # 0x1153b7
movq %rbx, %rdi
movq %rax, 0x18(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3f2a
movzbl %al, %edx
leaq 0xbc9aa(%rip), %rsi     # 0x1153e0
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
movq %rax, %r13
leaq 0xbc9a3(%rip), %rsi     # 0x1153f9
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
leaq 0xad5c7(%rip), %rsi     # 0x10603d
movq %rbx, %rdi
movq %rax, 0x38(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
movq %rax, %rbp
leaq 0xad5b6(%rip), %rsi     # 0x106053
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xde896
movq $0x1fe, (%r15)          # imm = 0x1FE
movq 0x18(%rbx), %rdi
leaq 0xe(%rsp), %r15
pushq $0x2
popq %r12
movl $0x1fe, %edx            # imm = 0x1FE
movq %r15, %rsi
movq %r12, %rcx
callq 0xe3cb7
leaq 0x20(%rsp), %rdx
pushq $0x10
popq %rcx
movq %r15, %rdi
movq %r12, %rsi
callq 0xdebbe
leaq 0xbc934(%rip), %rsi     # 0x11541b
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
movq 0x18(%rbx), %rdi
pushq $0x32
popq %rcx
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0xe0f6a
movzwl %ax, %r15d
leaq 0xbcab4(%rip), %rsi     # 0x1155c6
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xde896
leaq 0xbc90f(%rip), %rsi     # 0x115435
cmpl $0x1234, %r15d          # imm = 0x1234
jne 0x58b36
movb $0x1, 0x15(%r14)
jmp 0x58b41
cmpb $0x0, 0x15(%r14)
je 0x58d56
pushq $0x2
popq %rax
movq 0x18(%rsp), %r15
movl %eax, 0x10(%r14)
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
testq %r15, %r15
jne 0x58b7f
movq 0x18(%rbx), %rdi
pushq $0x20
popq %rsi
callq 0xe409f
movq %rax, %r15
leaq 0xbc914(%rip), %rsi     # 0x115486
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
testq %r13, %r13
jne 0x58ba7
movq 0x18(%rbx), %rdi
pushq $0x24
popq %rsi
callq 0xe409f
movq %rax, %r13
leaq 0xbc910(%rip), %rsi     # 0x1154aa
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xde896
movq %r13, 0x60(%r14)
movq %r15, 0x30(%r14)
imulq 0x38(%rsp), %rbp
testq %rbp, %rbp
jle 0x58bcf
movq %r15, %rdi
movq %rbp, %rsi
callq 0xdfbb6
cqto
idivq %rbp
movq %rax, %rdx
jmp 0x58bd1
xorl %edx, %edx
movq 0x10(%rsp), %r13
leaq 0xbc8ec(%rip), %rsi     # 0x1154c9
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x20(%r14), %rax
testq %rax, %rax
jle 0x58cc7
movq 0x18(%r14), %rdx
cmpq $0x20, %rdx
jl 0x58cc7
imulq %rdx, %rax
movq %rax, 0x28(%r14)
movq 0x58(%r14), %rcx
imulq 0x60(%r14), %rcx
addq 0x50(%r14), %rcx
movq %rcx, 0x70(%r14)
imulq %rcx, %rdx
leaq 0xbc8c9(%rip), %rsi     # 0x1154ef
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x68(%r14), %rax
shlq $0x5, %rax
movq 0x18(%r14), %rsi
movq 0x30(%r14), %rcx
addq %rsi, %rax
decq %rax
cqto
idivq %rsi
movq %rax, %r15
movq 0x70(%r14), %rax
addq %r15, %rax
subq %rax, %rcx
js 0x58cc7
movq %rcx, %rax
cqto
idivq 0x20(%r14)
movq %rax, 0x48(%r14)
leaq 0xbc8ad(%rip), %rsi     # 0x11551b
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xde896
addq 0x70(%r14), %r15
movq %r15, 0x38(%r14)
movq 0x18(%r14), %rdx
imulq %r15, %rdx
movq %rdx, 0x40(%r14)
leaq 0xbc8aa(%rip), %rsi     # 0x115540
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0xde896
movq 0x48(%r14), %rax
leaq 0x2(%rax), %rcx
movq %rcx, 0x78(%r14)
cmpq $0xff4, %rax            # imm = 0xFF4
jg 0x58d80
movb $0xc, 0x14(%r14)
pushq $0xc
jmp 0x58d98
leaq 0xbc8c6(%rip), %rsi     # 0x115594
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf2a5
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
testq %r13, %r13
je 0x58ced
orb $0x1, 0x40(%r13)
movq 0x90(%r14), %rsi
movq %rbx, %rdi
callq 0xded32
movq 0x98(%r14), %rsi
movq %rbx, %rdi
callq 0xded32
movq 0xa0(%r14), %rsi
movq %rbx, %rdi
callq 0xded32
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0x58d2b
callq 0xeb97e
movq 0xa8(%r14), %rdi
testq %rdi, %rdi
je 0x58d3c
callq 0xe5591
movq %rbx, %rdi
movq %r14, %rsi
callq 0xded32
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x55, 0xe(%rsp)
movq 0x18(%rsp), %r15
jne 0x58b57
cmpb $-0x56, 0xf(%rsp)
jne 0x58b57
leaq 0xbc6ea(%rip), %rsi     # 0x115462
pushq $0x1
popq %rax
jmp 0x58b49
cmpq $0xfff4, %rax           # imm = 0xFFF4
ja 0x58d91
movb $0x10, 0x14(%r14)
pushq $0x10
jmp 0x58d98
movb $0x20, 0x14(%r14)
pushq $0x20
popq %rdx
leaq 0xbc7cf(%rip), %rsi     # 0x11556f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movzbl 0x14(%r14), %edx
testl %edx, %edx
je 0x58ce3
movl 0x10(%r14), %eax
cmpl $0x2, %eax
leaq 0xbc4ae(%rip), %rcx     # 0x11527e
leaq 0xbc4b8(%rip), %rdi     # 0x11528f
cmoveq %rcx, %rdi
cmpl $0x1, %eax
leaq 0xbc48e(%rip), %rsi     # 0x115273
cmovneq %rdi, %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe0824
movq 0x18(%r14), %r15
imulq 0x50(%r14), %r15
leaq 0xbc7dd(%rip), %rsi     # 0x1155e0
movq %rbx, %rdi
xorl %edx, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x78(%r14), %rsi
cmpq $0x17d7840, %rsi        # imm = 0x17D7840
jg 0x58cd8
movq %rsi, 0x88(%r14)
pushq $0x4
popq %rdx
movq %rbx, %rdi
callq 0xdf66f
movq %rax, 0x90(%r14)
movq 0x88(%r14), %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, 0x98(%r14)
movq %r15, 0x20(%rsp)
movzbl 0x14(%r14), %eax
cmpl $0xc, %eax
je 0x58ea9
cmpl $0x10, %eax
jne 0x590d5
xorl %r12d, %r12d
leaq 0x20(%rsp), %r15
movq 0x88(%r14), %rcx
cmpq %rcx, %r12
jge 0x58f20
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3fc4
movq 0x90(%r14), %rcx
movl %eax, (%rcx,%r12,4)
incq %r12
jmp 0x58e7d
movq 0x88(%r14), %rcx
xorl %r13d, %r13d
pushq $0x3
popq %rbp
pushq $0x1
popq %r12
cmpq %rcx, %r13
jg 0x58f16
movq 0x18(%rbx), %rdi
movq %r15, %rsi
movl %ebp, %edx
movl %r12d, %ecx
xorl %r8d, %r8d
callq 0xe424b
movq 0x88(%r14), %rcx
cmpq %rcx, %r13
jge 0x58ef2
movl %eax, %edx
andl $0xfff, %edx            # imm = 0xFFF
movq 0x90(%r14), %rsi
movl %edx, (%rsi,%r13,4)
leaq 0x1(%r13), %rdx
cmpq %rcx, %rdx
jge 0x58f0a
shrl $0xc, %eax
movq 0x90(%r14), %rsi
movl %eax, 0x4(%rsi,%r13,4)
incq %rdx
addq $0x3, %r15
movq %rdx, %r13
jmp 0x58eba
movq %r15, 0x20(%rsp)
movq 0x10(%rsp), %r13
cmpl $0x3, (%rbx)
jl 0x58f5b
leaq 0xbc6e4(%rip), %r15     # 0x115610
xorl %r12d, %r12d
cmpq %rcx, %r12
jge 0x58f5b
movq 0x90(%r14), %rax
movl (%rax,%r12,4), %ecx
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0xdea83
incq %r12
movq 0x88(%r14), %rcx
jmp 0x58f2f
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
cmpb $0x0, 0x4(%r14)
je 0x58f78
cmpb $0x20, 0x14(%r14)
jne 0x590e1
pushq $0x1a
popq %rsi
movq %rbx, %rdi
callq 0xeb95d
movq %rax, 0x80(%r14)
movl $0x220, %esi            # imm = 0x220
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %r15
movq 0x18(%r14), %r12
imulq 0x70(%r14), %r12
leaq 0xde58d(%rip), %rsi     # 0x137537
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xde896
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
cmpq 0x18(%r14), %r12
jl 0x590ba
cmpb $0x0, 0x5(%r14)
je 0x5909a
leaq 0xbc640(%rip), %rsi     # 0x11561e
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x98(%r14), %rsi
testq %rsi, %rsi
je 0x5902c
movq 0xa0(%r14), %rdi
testq %rdi, %rdi
jne 0x59020
movq 0x88(%r14), %rsi
movq %rbx, %rdi
callq 0xdecaf
movq %rax, %rdi
movq %rax, 0xa0(%r14)
movq 0x98(%r14), %rsi
movq 0x88(%r14), %rdx
callq 0x181e0
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0x68(%r14), %r8
shlq $0x5, %r8
movl $0x1, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
xorl %r9d, %r9d
callq 0x59975
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
movq 0xa0(%r14), %rsi
testq %rsi, %rsi
je 0x59089
movq 0x98(%r14), %rdi
testq %rdi, %rdi
je 0x59089
movq 0x88(%r14), %rdx
callq 0x181e0
leaq 0xbc5a2(%rip), %rsi     # 0x115632
movq %rbx, %rdi
xorl %eax, %eax
callq 0xde896
movq 0x68(%r14), %r8
shlq $0x5, %r8
andl $0x0, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
xorl %r9d, %r9d
callq 0x59975
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5a3bd
pushq $-0x1
popq %rsi
movq %rbx, %rdi
callq 0xdeba7
jmp 0x58ced
leaq 0xbc512(%rip), %rsi     # 0x1155ee
jmp 0x58cce
movq 0x68(%r14), %rdi
testq %rdi, %rdi
jle 0x59188
movq 0x18(%r14), %r15
imulq 0x70(%r14), %r15
movq %rdi, %rax
shlq $0x5, %rax
addq %r15, %rax
movq 0x18(%rbx), %rcx
cmpq 0x18(%rcx), %rax
jg 0x59188
pushq $0xa
popq %rsi
callq 0xdfb50
movq %rax, %r12
xorl %r13d, %r13d
testq %rax, %rax
cmovleq %r13, %r12
xorl %ebp, %ebp
subq $0x1, %r12
jb 0x59168
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xe3eb4
cmpb $-0x1b, %al
je 0x59162
movzbl %al, %eax
testl %eax, %eax
je 0x59168
incq %rbp
movq 0x18(%rbx), %rdi
leaq 0xb(%r15), %rsi
callq 0xe3eb4
cmpb $0x40, %al
jae 0x5915f
notb %al
andb $0x18, %al
cmpb $0x1, %al
adcq $0x0, %r13
jmp 0x59162
incq %r13
addq $0x20, %r15
jmp 0x59122
cmpq $0x1, %r13
movq 0x10(%rsp), %r13
jg 0x59188
sete %al
cmpq $0x2, %rbp
setl %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x58f78
leaq 0xbc119(%rip), %rsi     # 0x1152a8
movq %rbx, %rdi
xorl %eax, %eax
callq 0xdf05a
jmp 0x58ce3