// File: /workspace/github_c_makefile_star2_O3/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 0x5b8bc
movl $0x0, 0x40(%r15)
movl $0xb0, %esi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r14
leaq 0xc6b32(%rip), %rsi     # 0x122405
movq %rbx, %rdi
movl $0x1, %edx
callq 0xe9bd0
movb %al, 0x5(%r14)
leaq 0xc6b27(%rip), %rsi     # 0x122412
movq %rbx, %rdi
movl $0x1, %edx
callq 0xe9bd0
movb %al, 0x4(%r14)
leaq 0xc6b1d(%rip), %rsi     # 0x122420
movq %rbx, %rdi
callq 0xe9b74
testq %rax, %rax
je 0x5b944
cmpb $0x70, (%rax)
jne 0x5b921
cmpb $0x63, 0x1(%rax)
jne 0x5b921
cmpb $0x0, 0x2(%rax)
je 0x5b93b
leaq 0xc6b03(%rip), %rsi     # 0x12242b
movq %rax, %rdi
callq 0x181a0
testl %eax, %eax
jne 0x5b944
movl $0x2, %eax
jmp 0x5b940
movl $0x1, %eax
movl %eax, 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 0xeb220
movl %eax, (%r14)
leaq 0xc6b66(%rip), %rsi     # 0x1224d5
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
xorl %edx, %edx
cmpl $0x2, 0xc(%r14)
sete %dl
xorq $0x3, %rdx
movq 0x18(%rbx), %rdi
leaq 0x20(%rsp), %rcx
movl $0x10, %r8d
xorl %esi, %esi
callq 0xe8abc
leaq 0xc6b35(%rip), %rsi     # 0x1224e8
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
cmpl $0x2, 0xc(%r14)
jne 0x5ba0d
movl $0x2, %esi
movl $0x6, %edx
movq %rbx, %rdi
callq 0x5ca47
movq 0x18(%rbx), %rdi
leaq 0x20(%rsp), %rcx
movl $0x8, %esi
movl $0x3, %edx
movl $0x10, %r8d
callq 0xe8abc
leaq 0xc6af9(%rip), %rsi     # 0x1224f7
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
jmp 0x5ba1f
movl $0x3, %esi
movl $0x8, %edx
movq %rbx, %rdi
callq 0x5ca47
movq %r15, 0x18(%rsp)
leaq 0x40(%rsp), %r15
movq $0xb, (%r15)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x18(%r14)
leaq 0xb7601(%rip), %rsi     # 0x113048
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeeb4b
movzbl %al, %edx
movq %rdx, 0x20(%r14)
leaq 0xc6a99(%rip), %rsi     # 0x122506
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq %rax, 0x50(%r14)
leaq 0xc6a90(%rip), %rsi     # 0x12251e
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
cmpq $0x0, 0x50(%r14)
jne 0x5baa9
movq $0x1, 0x50(%r14)
movq 0x18(%rbx), %rdi
leaq 0x40(%rsp), %rbp
movq %rbp, %rsi
callq 0xeeb4b
movzbl %al, %edx
movq %rdx, 0x58(%r14)
leaq 0xc6a6b(%rip), %rsi     # 0x122533
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xeebeb
movq %rax, 0x68(%r14)
leaq 0xc6a5d(%rip), %rsi     # 0x122546
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xeebeb
movq %rax, %r12
leaq 0xc6a6c(%rip), %rsi     # 0x122577
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xeeb4b
movzbl %al, %edx
leaq 0xc6a72(%rip), %rsi     # 0x1225a0
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xeebeb
movq %rax, %r13
leaq 0xc6a6b(%rip), %rsi     # 0x1225b9
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xeebeb
leaq 0xb74ef(%rip), %rsi     # 0x11305d
movq %rbx, %rdi
movq %rax, 0x38(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xeebeb
movq %rax, %r15
leaq 0xb74de(%rip), %rsi     # 0x113073
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe8617
movq $0x1fe, (%rbp)          # imm = 0x1FE
movq 0x18(%rbx), %rdi
leaq 0x16(%rsp), %rbp
movl $0x1fe, %edx            # imm = 0x1FE
movl $0x2, %ecx
movq %rbp, %rsi
callq 0xee8d4
leaq 0x20(%rsp), %rdx
movl $0x2, %esi
movl $0x10, %ecx
movq %rbp, %rdi
callq 0xe8a2c
leaq 0xc69f8(%rip), %rsi     # 0x1225db
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
movq 0x18(%rbx), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
movl $0x32, %ecx
callq 0xeb97d
movzwl %ax, %ebp
leaq 0xc6b77(%rip), %rsi     # 0x122786
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe8617
leaq 0xc69d3(%rip), %rsi     # 0x1225f5
cmpl $0x1234, %ebp           # imm = 0x1234
jne 0x5bc36
movb $0x1, 0x15(%r14)
movl $0x2, %eax
jmp 0x5bc5c
movl $0x2, %eax
cmpb $0x0, 0x15(%r14)
jne 0x5bc5c
cmpb $0x55, 0x16(%rsp)
jne 0x5bc6a
cmpb $-0x56, 0x17(%rsp)
jne 0x5bc6a
leaq 0xc69cb(%rip), %rsi     # 0x122622
movl $0x1, %eax
movl %eax, 0x10(%r14)
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
testq %r12, %r12
jne 0x5bc94
movq 0x18(%rbx), %rdi
movl $0x20, %esi
callq 0xeed61
movq %rax, %r12
leaq 0xc69bf(%rip), %rsi     # 0x122646
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
testq %r13, %r13
jne 0x5bcbe
movq 0x18(%rbx), %rdi
movl $0x24, %esi
callq 0xeed61
movq %rax, %r13
leaq 0xc69b9(%rip), %rsi     # 0x12266a
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe8617
movq %r13, 0x60(%r14)
movq %r12, 0x30(%r14)
imulq 0x38(%rsp), %r15
testq %r15, %r15
jle 0x5bce6
movq %r12, %rdi
movq %r15, %rsi
callq 0xe9cb4
cqto
idivq %r15
movq %rax, %rdx
jmp 0x5bce8
xorl %edx, %edx
movq 0x18(%rsp), %r12
leaq 0xc6995(%rip), %rsi     # 0x122689
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x20(%r14), %rax
testq %rax, %rax
jle 0x5bde1
movq 0x18(%r14), %rdx
cmpq $0x20, %rdx
jl 0x5bde1
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 0xc6972(%rip), %rsi     # 0x1226af
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
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 0x5bde1
movq %rcx, %rax
cqto
idivq 0x20(%r14)
movq %rax, 0x48(%r14)
leaq 0xc6956(%rip), %rsi     # 0x1226db
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe8617
addq 0x70(%r14), %r15
movq %r15, 0x38(%r14)
movq 0x18(%r14), %rdx
imulq %r15, %rdx
movq %rdx, 0x40(%r14)
leaq 0xc6953(%rip), %rsi     # 0x122700
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0xe8617
movq 0x48(%r14), %rax
leaq 0x2(%rax), %rcx
movq %rcx, 0x78(%r14)
cmpq $0xff4, %rax            # imm = 0xFF4
jg 0x5be73
movb $0xc, 0x14(%r14)
movl $0xc, %edx
jmp 0x5be91
leaq 0xc696c(%rip), %rsi     # 0x122754
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9295
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
testq %r12, %r12
je 0x5be0a
orb $0x1, 0x40(%r12)
movq 0x90(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq 0x98(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq 0xa0(%r14), %rsi
movq %rbx, %rdi
callq 0xe8be2
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0x5be48
callq 0xf7228
movq 0xa8(%r14), %rdi
testq %rdi, %rdi
je 0x5be59
callq 0xf052b
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe8be2
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0xfff4, %rax           # imm = 0xFFF4
ja 0x5be87
movb $0x10, 0x14(%r14)
movl $0x10, %edx
jmp 0x5be91
movb $0x20, 0x14(%r14)
movl $0x20, %edx
leaq 0xc6897(%rip), %rsi     # 0x12272f
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movzbl 0x14(%r14), %edx
testl %edx, %edx
je 0x5bdff
movl 0x10(%r14), %eax
cmpl $0x2, %eax
leaq 0xc6574(%rip), %rcx     # 0x12243e
leaq 0xc657e(%rip), %rdi     # 0x12244f
cmoveq %rcx, %rdi
cmpl $0x1, %eax
leaq 0xc6554(%rip), %rsi     # 0x122433
cmovneq %rdi, %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0xeb152
movq 0x18(%r14), %r15
imulq 0x50(%r14), %r15
leaq 0xc68a3(%rip), %rsi     # 0x1227a0
movq %rbx, %rdi
xorl %edx, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
movq 0x78(%r14), %rsi
cmpq $0x17d7840, %rsi        # imm = 0x17D7840
jg 0x5bdf2
movq %rsi, 0x88(%r14)
movl $0x4, %edx
movq %rbx, %rdi
callq 0xe96f7
movq %rax, 0x90(%r14)
movq 0x88(%r14), %rsi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, 0x98(%r14)
movq %r15, 0x20(%rsp)
movzbl 0x14(%r14), %eax
cmpl $0xc, %eax
je 0x5bfb1
cmpl $0x10, %eax
jne 0x5c1ee
cmpq $0x0, 0x88(%r14)
jle 0x5c060
xorl %r12d, %r12d
leaq 0x20(%rsp), %r15
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeebeb
movq 0x90(%r14), %rcx
movl %eax, (%rcx,%r12,4)
incq %r12
movq 0x88(%r14), %rcx
cmpq %rcx, %r12
jl 0x5bf89
jmp 0x5c025
movq 0x88(%r14), %rcx
testq %rcx, %rcx
js 0x5c020
xorl %r12d, %r12d
movq 0x18(%rbx), %rdi
movq %r15, %rsi
movl $0x3, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0xef0f6
movq 0x88(%r14), %rcx
cmpq %rcx, %r12
jge 0x5bff8
movl %eax, %edx
andl $0xfff, %edx            # imm = 0xFFF
movq 0x90(%r14), %rsi
movl %edx, (%rsi,%r12,4)
leaq 0x1(%r12), %rdx
cmpq %rcx, %rdx
jge 0x5c011
shrl $0xc, %eax
movq 0x90(%r14), %rsi
movl %eax, 0x4(%rsi,%r12,4)
incq %rdx
addq $0x3, %r15
movq %rdx, %r12
cmpq %rcx, %rdx
jle 0x5bfc0
movq %r15, 0x20(%rsp)
testq %rcx, %rcx
jle 0x5c060
cmpl $0x3, (%rbx)
jl 0x5c060
leaq 0xc679a(%rip), %r15     # 0x1227d0
xorl %r12d, %r12d
movq 0x90(%r14), %rax
movl (%rax,%r12,4), %ecx
movq %rbx, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe889f
incq %r12
cmpq 0x88(%r14), %r12
jl 0x5c039
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
cmpb $0x0, 0x4(%r14)
je 0x5c07f
cmpb $0x20, 0x14(%r14)
jne 0x5c1fa
movl $0x1a, %esi
movq %rbx, %rdi
callq 0xf7205
movq %rax, 0x80(%r14)
movl $0x220, %esi            # imm = 0x220
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %r15
movq 0x18(%r14), %r12
imulq 0x70(%r14), %r12
leaq 0xe88d4(%rip), %rsi     # 0x144987
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe8617
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
cmpq 0x18(%r14), %r12
jl 0x5c1cc
cmpb $0x0, 0x5(%r14)
je 0x5c1a9
leaq 0xc66f5(%rip), %rsi     # 0x1227de
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x98(%r14), %rsi
testq %rsi, %rsi
je 0x5c137
movq 0xa0(%r14), %rdi
testq %rdi, %rdi
jne 0x5c12b
movq 0x88(%r14), %rsi
movq %rbx, %rdi
callq 0xe8b2b
movq %rax, %rdi
movq %rax, 0xa0(%r14)
movq 0x98(%r14), %rsi
movq 0x88(%r14), %rdx
callq 0x181e0
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe8a15
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 0x5cab5
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
movq 0xa0(%r14), %rsi
testq %rsi, %rsi
je 0x5c198
movq 0x98(%r14), %rdi
testq %rdi, %rdi
je 0x5c198
movq 0x88(%r14), %rdx
callq 0x181e0
leaq 0xc6653(%rip), %rsi     # 0x1227f2
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8617
movq 0x68(%r14), %r8
shlq $0x5, %r8
movl $0x0, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r12, %rcx
xorl %r9d, %r9d
callq 0x5cab5
testq %r15, %r15
je 0x5c1dc
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe8be2
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe8a15
jmp 0x5be0a
leaq 0xc65b9(%rip), %rsi     # 0x1227ae
jmp 0x5bde8
movq 0x68(%r14), %rdi
testq %rdi, %rdi
jle 0x5c2a1
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 0x5c2a1
movl $0xa, %esi
callq 0xe9c46
testq %rax, %rax
jle 0x5c07f
movq %rax, %r12
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xeead3
cmpb $-0x1b, %al
je 0x5c279
movzbl %al, %eax
testl %eax, %eax
je 0x5c282
incq %r13
movq 0x18(%rbx), %rdi
leaq 0xb(%r15), %rsi
callq 0xeead3
cmpb $0x40, %al
jae 0x5c276
notb %al
andb $0x18, %al
cmpb $0x1, %al
adcq $0x0, %rbp
jmp 0x5c279
incq %rbp
addq $0x20, %r15
decq %r12
jne 0x5c23f
cmpq $0x1, %rbp
jg 0x5c2a1
cmpq $0x2, %r13
setl %al
cmpq $0x1, %rbp
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x5c07f
leaq 0xc61c0(%rip), %rsi     # 0x122468
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe8faa
movq 0x18(%rsp), %r12
jmp 0x5bdff