// File: /workspace/github_c_makefile_star2_O1/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 $0x38, %rsp
movq %rsi, %r12
movq %rdi, %rbx
testq %rsi, %rsi
je 0x5ba59
movl $0x0, 0x40(%r12)
movl $0xb0, %esi
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r14
leaq 0xc678d(%rip), %rsi     # 0x1221fd
movl $0x1, %ebp
movq %rbx, %rdi
movl $0x1, %edx
callq 0xea727
movb %al, 0x5(%r14)
leaq 0xc677d(%rip), %rsi     # 0x12220a
movq %rbx, %rdi
movl $0x1, %edx
callq 0xea727
movb %al, 0x4(%r14)
leaq 0xc6773(%rip), %rsi     # 0x122218
movq %rbx, %rdi
callq 0xea6cb
testq %rax, %rax
je 0x5bae4
movq %rax, %r15
leaq 0xdf8f9(%rip), %rsi     # 0x13b3b5
movq %rax, %rdi
callq 0x181a0
testl %eax, %eax
je 0x5bae0
leaq 0xc6754(%rip), %rsi     # 0x122223
movq %r15, %rdi
callq 0x181a0
testl %eax, %eax
jne 0x5bae4
movl $0x2, %ebp
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 %r12, %rsi
callq 0xeb7d1
movl %eax, (%r14)
leaq 0xc67be(%rip), %rsi     # 0x1222cd
movq %rbx, %rdi
xorl %edx, %edx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
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 0xe962c
leaq 0xc678d(%rip), %rsi     # 0x1222e0
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
cmpl $0x2, 0xc(%r14)
jne 0x5bbad
movl $0x2, %esi
movl $0x6, %edx
movq %rbx, %rdi
callq 0x5cc28
movq 0x18(%rbx), %rdi
leaq 0x20(%rsp), %rcx
movl $0x8, %esi
movl $0x3, %edx
movl $0x10, %r8d
callq 0xe962c
leaq 0xc6751(%rip), %rsi     # 0x1222ef
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
jmp 0x5bbbf
movl $0x3, %esi
movl $0x8, %edx
movq %rbx, %rdi
callq 0x5cc28
movq %r12, 0x18(%rsp)
leaq 0x30(%rsp), %r15
movq $0xb, (%r15)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x18(%r14)
leaq 0xb7451(%rip), %rsi     # 0x113038
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef14d
movzbl %al, %edx
movq %rdx, 0x20(%r14)
leaq 0xc66f1(%rip), %rsi     # 0x1222fe
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq %rax, 0x50(%r14)
leaq 0xc66e8(%rip), %rsi     # 0x122316
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
cmpq $0x0, 0x50(%r14)
jne 0x5bc49
movq $0x1, 0x50(%r14)
movq 0x18(%rbx), %rdi
leaq 0x30(%rsp), %rbp
movq %rbp, %rsi
callq 0xef14d
movzbl %al, %edx
movq %rdx, 0x58(%r14)
leaq 0xc66c3(%rip), %rsi     # 0x12232b
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xef1ed
movq %rax, 0x68(%r14)
leaq 0xc66b5(%rip), %rsi     # 0x12233e
movq %rbx, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xef1ed
movq %rax, %r12
leaq 0xc66c4(%rip), %rsi     # 0x12236f
movq %rbx, %rdi
movl %r12d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xef14d
movzbl %al, %edx
leaq 0xc66ca(%rip), %rsi     # 0x122398
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xef1ed
movq %rax, %r13
leaq 0xc66c3(%rip), %rsi     # 0x1223b1
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xef1ed
leaq 0xb733f(%rip), %rsi     # 0x11304d
movq %rbx, %rdi
movq %rax, 0x10(%rsp)
movl %eax, %edx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movq %rbp, %rsi
callq 0xef1ed
movq %rax, %r15
leaq 0xb732e(%rip), %rsi     # 0x113063
movq %rbx, %rdi
movl %r15d, %edx
xorl %eax, %eax
callq 0xe9226
movq $0x1fe, (%rbp)          # imm = 0x1FE
movq 0x18(%rbx), %rdi
leaq 0xe(%rsp), %rbp
movl $0x1fe, %edx            # imm = 0x1FE
movl $0x2, %ecx
movq %rbp, %rsi
callq 0xeeee8
leaq 0x20(%rsp), %rdx
movl $0x2, %esi
movl $0x10, %ecx
movq %rbp, %rdi
callq 0xe959c
leaq 0xc6650(%rip), %rsi     # 0x1223d3
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
movq 0x18(%rbx), %rdi
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
movl $0x32, %ecx
callq 0xebf42
movzwl %ax, %ebp
leaq 0xc67cf(%rip), %rsi     # 0x12257e
movq %rbx, %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0xe9226
cmpl $0x1234, %ebp           # imm = 0x1234
jne 0x5bdc8
movb $0x1, 0x15(%r14)
cmpb $0x0, 0x15(%r14)
je 0x5bddd
leaq 0xc6617(%rip), %rsi     # 0x1223ed
movl $0x2, %eax
jmp 0x5bdf7
cmpb $0x55, 0xe(%rsp)
jne 0x5be05
cmpb $-0x56, 0xf(%rsp)
jne 0x5be05
leaq 0xc6628(%rip), %rsi     # 0x12241a
movl $0x1, %eax
movl %eax, 0x10(%r14)
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
testq %r12, %r12
jne 0x5be2f
movq 0x18(%rbx), %rdi
movl $0x20, %esi
callq 0xef37e
movq %rax, %r12
leaq 0xc661c(%rip), %rsi     # 0x12243e
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
testq %r13, %r13
jne 0x5be59
movq 0x18(%rbx), %rdi
movl $0x24, %esi
callq 0xef37e
movq %rax, %r13
leaq 0xc6616(%rip), %rsi     # 0x122462
movq %rbx, %rdi
movl %r13d, %edx
xorl %eax, %eax
callq 0xe9226
movq %r13, 0x60(%r14)
movq %r12, 0x30(%r14)
imulq 0x10(%rsp), %r15
testq %r15, %r15
jle 0x5be82
movq 0x30(%r14), %rdi
movq %r15, %rsi
callq 0xea80b
cqto
idivq %r15
movq %rax, %rdx
jmp 0x5be84
xorl %edx, %edx
movq 0x18(%rsp), %r13
leaq 0xc65f1(%rip), %rsi     # 0x122481
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq 0x20(%r14), %rax
testq %rax, %rax
jle 0x5bf78
movq 0x18(%r14), %rdx
cmpq $0x20, %rdx
jl 0x5bf78
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 0xc65ce(%rip), %rsi     # 0x1224a7
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
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 0x5bf78
movq %rcx, %rax
cqto
idivq 0x20(%r14)
movq %rax, 0x48(%r14)
leaq 0xc65b2(%rip), %rsi     # 0x1224d3
movq %rbx, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0xe9226
addq 0x70(%r14), %r15
movq %r15, 0x38(%r14)
movq 0x18(%r14), %rdx
imulq %r15, %rdx
movq %rdx, 0x40(%r14)
leaq 0xc65af(%rip), %rsi     # 0x1224f8
movq %rbx, %rdi
movq %r15, %rcx
xorl %eax, %eax
callq 0xe9226
movq 0x48(%r14), %rax
leaq 0x2(%rax), %rcx
movq %rcx, 0x78(%r14)
cmpq $0xff4, %rax            # imm = 0xFF4
jg 0x5c016
movb $0xc, 0x14(%r14)
jmp 0x5c02a
leaq 0xc65cd(%rip), %rsi     # 0x12254c
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movb $0x1, %bpl
testq %r13, %r13
sete %al
xorb $0x1, %bpl
orb %al, %bpl
jne 0x5bfad
orb $0x1, 0x40(%r13)
movq 0x90(%r14), %rsi
movq %rbx, %rdi
callq 0xe9752
movq 0x98(%r14), %rsi
movq %rbx, %rdi
callq 0xe9752
movq 0xa0(%r14), %rsi
movq %rbx, %rdi
callq 0xe9752
movq 0x80(%r14), %rdi
testq %rdi, %rdi
je 0x5bfeb
callq 0xf7689
movq 0xa8(%r14), %rdi
testq %rdi, %rdi
je 0x5bffc
callq 0xf0b13
movq %rbx, %rdi
movq %r14, %rsi
callq 0xe9752
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpq $0xfff4, %rax           # imm = 0xFFF4
jg 0x5c025
movb $0x10, 0x14(%r14)
jmp 0x5c02a
movb $0x20, 0x14(%r14)
movzbl 0x14(%r14), %edx
leaq 0xc64f1(%rip), %rsi     # 0x122527
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movzbl 0x14(%r14), %edx
movb $0x1, %bpl
testl %edx, %edx
je 0x5bf99
movl 0x10(%r14), %eax
cmpl $0x2, %eax
leaq 0xc61cb(%rip), %rcx     # 0x122236
leaq 0xc61d5(%rip), %rdi     # 0x122247
cmoveq %rcx, %rdi
cmpl $0x1, %eax
leaq 0xc61ab(%rip), %rsi     # 0x12222b
cmovneq %rdi, %rsi
movq %rbx, %rdi
xorl %eax, %eax
callq 0xeb703
movq 0x18(%r14), %r15
imulq 0x50(%r14), %r15
leaq 0xc64fa(%rip), %rsi     # 0x122598
movq %rbx, %rdi
xorl %edx, %edx
movq %r15, %rcx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
movq 0x78(%r14), %rsi
cmpq $0x17d7840, %rsi        # imm = 0x17D7840
jg 0x5c3a0
movq %rsi, 0x88(%r14)
movl $0x4, %edx
movq %rbx, %rdi
callq 0xea259
movq %rax, 0x90(%r14)
movq 0x88(%r14), %rsi
movq %rbx, %rdi
callq 0xe969b
movq %rax, 0x98(%r14)
movq %r15, 0x20(%rsp)
movzbl 0x14(%r14), %eax
cmpl $0xc, %eax
je 0x5c14f
cmpl $0x10, %eax
jne 0x5c38f
cmpq $0x0, 0x88(%r14)
jle 0x5c1bf
xorl %r12d, %r12d
leaq 0x20(%rsp), %r15
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef1ed
movq 0x90(%r14), %rcx
movl %eax, (%rcx,%r12,4)
incq %r12
cmpq 0x88(%r14), %r12
jl 0x5c12a
jmp 0x5c1bf
cmpq $0x0, 0x88(%r14)
js 0x5c1bf
xorl %r15d, %r15d
movq 0x18(%rbx), %rdi
movq 0x20(%rsp), %rsi
movl $0x3, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0xef6f2
addq $0x3, 0x20(%rsp)
movq 0x88(%r14), %rcx
cmpq %rcx, %r15
jge 0x5c19c
movl %eax, %edx
andl $0xfff, %edx            # imm = 0xFFF
movq 0x90(%r14), %rsi
movl %edx, (%rsi,%r15,4)
leaq 0x1(%r15), %rdx
cmpq %rcx, %rdx
jge 0x5c1b4
shrl $0xc, %eax
movq 0x90(%r14), %rsi
movl %eax, 0x4(%rsi,%r15,4)
incq %rdx
movq %rdx, %r15
cmpq %rcx, %rdx
jle 0x5c15c
cmpl $0x3, (%rbx)
jl 0x5c1ff
cmpq $0x0, 0x88(%r14)
jle 0x5c1ff
leaq 0xc63f3(%rip), %r15     # 0x1225c8
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 0xe9461
incq %r12
cmpq 0x88(%r14), %r12
jl 0x5c1d8
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
cmpb $0x0, 0x4(%r14)
je 0x5c21e
cmpb $0x20, 0x14(%r14)
jne 0x5c3b2
movl $0x1a, %esi
movq %rbx, %rdi
callq 0xf7666
movq %rax, 0x80(%r14)
movl $0x220, %esi            # imm = 0x220
movq %rbx, %rdi
callq 0xe969b
movq %rax, %r15
movq 0x18(%r14), %r12
imulq 0x70(%r14), %r12
leaq 0xe8255(%rip), %rsi     # 0x1444a7
movq %rbx, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0xe9226
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
cmpq 0x18(%r14), %r12
jl 0x5c36b
cmpb $0x0, 0x5(%r14)
je 0x5c348
leaq 0xc634e(%rip), %rsi     # 0x1225d6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
cmpq $0x0, 0x98(%r14)
je 0x5c2d6
cmpq $0x0, 0xa0(%r14)
jne 0x5c2bc
movq 0x88(%r14), %rsi
movq %rbx, %rdi
callq 0xe969b
movq %rax, 0xa0(%r14)
movq 0xa0(%r14), %rdi
movq 0x88(%r14), %rdx
movq 0x98(%r14), %rsi
callq 0x181e0
movq %rbx, %rdi
movl $0x1, %esi
callq 0xe9585
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 0x5cc96
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
movq 0xa0(%r14), %rsi
testq %rsi, %rsi
je 0x5c337
movq 0x98(%r14), %rdi
testq %rdi, %rdi
je 0x5c337
movq 0x88(%r14), %rdx
callq 0x181e0
leaq 0xc62ac(%rip), %rsi     # 0x1225ea
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9226
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 0x5cc96
testq %r15, %r15
je 0x5c37b
movq %rbx, %rdi
movq %r15, %rsi
callq 0xe9752
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
xorl %ebp, %ebp
jmp 0x5bf99
leaq 0xc6210(%rip), %rsi     # 0x1225a6
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9df7
movq %rbx, %rdi
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
callq 0xe9585
jmp 0x5bf99
movq 0x68(%r14), %rdi
testq %rdi, %rdi
jle 0x5c3d8
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
jle 0x5c3ee
leaq 0xc5e81(%rip), %rsi     # 0x122260
movq %rbx, %rdi
xorl %eax, %eax
callq 0xe9b27
jmp 0x5bf99
movl $0xa, %esi
callq 0xea79d
testq %rax, %rax
jle 0x5c47c
movq %rax, %r12
decq %r12
xorl %r13d, %r13d
movq $0x0, 0x10(%rsp)
movq 0x18(%rbx), %rdi
movq %r15, %rsi
callq 0xef0d5
testb %al, %al
je 0x5c44b
movzbl %al, %eax
cmpl $0xe5, %eax
je 0x5c454
incq %r13
movq 0x18(%rbx), %rdi
leaq 0xb(%r15), %rsi
callq 0xef0d5
cmpb $0x40, %al
jae 0x5c44f
notb %al
andb $0x18, %al
cmpb $0x1, %al
adcq $0x0, 0x10(%rsp)
jmp 0x5c454
movb $0x1, %al
jmp 0x5c456
incq 0x10(%rsp)
xorl %eax, %eax
subq $0x1, %r12
setb %cl
testb %al, %al
jne 0x5c469
addq $0x20, %r15
testb %cl, %cl
je 0x5c40f
cmpq $0x2, %r13
setge %al
movq 0x18(%rsp), %r13
movq 0x10(%rsp), %rcx
jmp 0x5c480
xorl %ecx, %ecx
xorl %eax, %eax
cmpq $0x1, %rcx
setne %cl
jg 0x5c3d8
orb %al, %cl
testb $0x1, %cl
jne 0x5c21e
jmp 0x5c3d8