// File: emr4378[P]FAT-File-System/FileSys.cpp
// Function: FileSys::copyFileExtToIn(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int FileSys::copyFileExtToIn(string source, string dest) {
	int ret = -1;
	string fileName;
	FILE *outerFile;
	int index;
	int offset;
	int j;
	int cluster;
	int sourceIndex;
	int destIndex;
	int sourceCluster;
	int destCluster;
	int i = 0;
	int clusterSize = boot->clusterSize;
	void *clusterData = malloc(clusterSize);
	
	//external (real) to internal (fake/the FileSys)
	outerFile = fopen(source.c_str(), "r");
	if (outerFile != NULL) {
		removeFile(dest); //if dest already exists, delete/overwrite
		index = createFile(dest);
		if (index >= 0) {
			cluster = directoryTable[index].index;

			fseek(outerFile, 0, SEEK_END);
			directoryTable[index].size = ftell(outerFile);
			fseek(outerFile, 0, SEEK_SET);

			while (!feof(outerFile) && cluster != 0xFFFF) {
				offset = clusterSize * cluster;
				fseek(outerFile, (clusterSize * i), SEEK_SET);
				fseek(file, offset, SEEK_SET);
				fread(clusterData, clusterSize, 1, outerFile);
				fwrite(clusterData, clusterSize, 1, file);

				fileAllocationTable[cluster] = 0xFFFF;
				if (!feof(outerFile)) {
					fileAllocationTable[cluster] = findNextFreeCluster();
					cluster = fileAllocationTable[cluster];
				}
				i++;
			}

			if (!feof(outerFile)) {
				//OH SHIT, outta room baby!
				//undo all the changes
				readFAT(fileAllocationTable, boot->FAT);
				readDirectoryTable(&directoryTable, boot->rootDir);
				removeFile(index);
				ret = -2;
			} else {
				writeFAT(fileAllocationTable, boot->FAT);
				writeDirectoryTable(&directoryTable, boot->rootDir);
				ret = 0;
			}
		} else {
			ret = index;
		}
		fclose(outerFile);
	}

	free(clusterData);

	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rsi, -0xd8(%rbp)
movq %rdx, -0xe8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xe0(%rbp)
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
leaq -0x40(%rbp), %rdi
callq 0x3430
movq -0xe0(%rbp), %rax
movl $0x0, -0x6c(%rbp)
movq 0x58(%rax), %rax
movl (%rax), %eax
movl %eax, -0x70(%rbp)
movslq -0x70(%rbp), %rdi
callq 0x3400
movq -0xd8(%rbp), %rdi
movq %rax, -0x78(%rbp)
callq 0x3110
movq %rax, %rdi
leaq 0x6d4f(%rip), %rsi      # 0xc039
callq 0x3360
movq %rax, -0xd0(%rbp)
jmp 0x52f8
movq -0xd0(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x5621
movq -0xe8(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x3120
jmp 0x5323
movq -0xe0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x5a70
jmp 0x5338
leaq -0xa8(%rbp), %rdi
callq 0x3180
movq -0xe8(%rbp), %rsi
leaq -0xc8(%rbp), %rdi
callq 0x3120
jmp 0x5359
movq -0xe0(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x47a0
movl %eax, -0xec(%rbp)
jmp 0x5374
movl -0xec(%rbp), %eax
movl %eax, -0x4c(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x3180
cmpl $0x0, -0x4c(%rbp)
jl 0x560e
movq -0xe0(%rbp), %rdi
addq $0x40, %rdi
movslq -0x4c(%rbp), %rsi
callq 0x6a10
movl 0x70(%rax), %eax
movl %eax, -0x58(%rbp)
movq -0x48(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x3330
jmp 0x53c1
movq -0x48(%rbp), %rdi
callq 0x3030
movq %rax, -0xf8(%rbp)
jmp 0x53d3
movq -0xe0(%rbp), %rdi
movq -0xf8(%rbp), %rax
movl %eax, -0xfc(%rbp)
addq $0x40, %rdi
movslq -0x4c(%rbp), %rsi
callq 0x6a10
movl -0xfc(%rbp), %ecx
movl %ecx, 0x74(%rax)
movq -0x48(%rbp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x3330
jmp 0x540c
jmp 0x540e
movq -0x48(%rbp), %rdi
callq 0x34a0
movl %eax, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0xfd(%rbp)
jne 0x5436
cmpl $0xffff, -0x58(%rbp)    # imm = 0xFFFF
setne %al
movb %al, -0xfd(%rbp)
movb -0xfd(%rbp), %al
testb $0x1, %al
jne 0x5445
jmp 0x5573
movl -0x70(%rbp), %eax
movl -0x58(%rbp), %ecx
imull %ecx, %eax
movl %eax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
movl -0x70(%rbp), %eax
movl -0x6c(%rbp), %ecx
imull %ecx, %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x3330
jmp 0x546a
movq -0xe0(%rbp), %rax
movq 0x20(%rax), %rdi
movslq -0x50(%rbp), %rsi
xorl %edx, %edx
callq 0x3330
jmp 0x5482
movq -0x78(%rbp), %rdi
movslq -0x70(%rbp), %rsi
movq -0x48(%rbp), %rcx
movl $0x1, %edx
callq 0x3460
jmp 0x549a
movq -0xe0(%rbp), %rax
movq -0x78(%rbp), %rdi
movslq -0x70(%rbp), %rsi
movq 0x20(%rax), %rcx
movl $0x1, %edx
callq 0x3590
jmp 0x54b9
movq -0xe0(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x58(%rbp), %rcx
movl $0xffff, (%rax,%rcx,4)  # imm = 0xFFFF
movq -0x48(%rbp), %rdi
callq 0x34a0
cmpl $0x0, %eax
jne 0x5565
movq -0xe0(%rbp), %rdi
callq 0x4680
movl %eax, %esi
movq -0xe0(%rbp), %rax
movq 0x38(%rax), %rcx
movslq -0x58(%rbp), %rdx
movl %esi, (%rcx,%rdx,4)
movq 0x38(%rax), %rax
movslq -0x58(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x58(%rbp)
jmp 0x5565
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
jmp 0x564b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x3180
jmp 0x564b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0xc8(%rbp), %rdi
callq 0x3180
jmp 0x564b
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x540e
movq -0x48(%rbp), %rdi
callq 0x34a0
cmpl $0x0, %eax
jne 0x55d0
movq -0xe0(%rbp), %rdi
movq 0x38(%rdi), %rsi
movq 0x58(%rdi), %rax
movl 0xc(%rax), %edx
callq 0x3ac0
jmp 0x559a
movq -0xe0(%rbp), %rdi
movq %rdi, %rsi
addq $0x40, %rsi
movq 0x58(%rdi), %rax
movl 0x8(%rax), %edx
callq 0x3b30
jmp 0x55b6
movq -0xe0(%rbp), %rdi
movl -0x4c(%rbp), %esi
callq 0x5b90
jmp 0x55c7
movl $0xfffffffe, -0x1c(%rbp) # imm = 0xFFFFFFFE
jmp 0x560c
movq -0xe0(%rbp), %rdi
movq 0x38(%rdi), %rsi
movq 0x58(%rdi), %rax
movl 0xc(%rax), %edx
callq 0x3f00
jmp 0x55e9
movq -0xe0(%rbp), %rdi
movq %rdi, %rsi
addq $0x40, %rsi
movq 0x58(%rdi), %rax
movl 0x8(%rax), %edx
callq 0x3f70
jmp 0x5605
movl $0x0, -0x1c(%rbp)
jmp 0x5614
movl -0x4c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movq -0x48(%rbp), %rdi
callq 0x32a0
jmp 0x561f
jmp 0x5621
movq -0x78(%rbp), %rdi
callq 0x3370
movl -0x1c(%rbp), %eax
movl %eax, -0x104(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x3180
movl -0x104(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x3180
movq -0x80(%rbp), %rdi
callq 0x3530
nopl (%rax)