// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, %r13
movq %rsi, %r15
movq %rdi, %r14
leaq 0x28(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movq 0x58(%rdi), %rax
movslq (%rax), %r12
movq %r12, %rdi
callq 0x22b0
movq %rax, 0x10(%rsp)
movq (%r15), %rdi
leaq 0x451e(%rip), %rsi      # 0x8039
callq 0x2250
testq %rax, %rax
je 0x3d1c
movq %rax, %r15
leaq 0x68(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %rdi
callq 0x4e42
leaq 0x58(%rsp), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x40c6
movq (%rbp), %rdi
cmpq %rbx, %rdi
je 0x3b70
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x48(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %rdi
callq 0x4e42
leaq 0x38(%rsp), %rsi
movq %r14, %rdi
callq 0x31a8
movl %eax, %r13d
movq 0x38(%rsp), %rdi
cmpq %rbp, %rdi
je 0x3bb5
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21f0
testl %r13d, %r13d
js 0x3d4d
movl %r13d, 0xc(%rsp)
movl %r13d, %ebx
movq 0x40(%r14), %rax
shlq $0x7, %rbx
movl 0x70(%rax,%rbx), %r13d
movq %r15, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2220
movq %r15, %rdi
callq 0x2030
movq 0x40(%r14), %rcx
movl %eax, 0x74(%rcx,%rbx)
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2220
movq %r15, %rdi
callq 0x2330
testl %eax, %eax
jne 0x3ce2
cmpl $0xffff, %r13d          # imm = 0xFFFF
je 0x3ce2
xorl %ebp, %ebp
movl %r13d, %ebx
imull %r12d, %ebx
movq %rbp, %rsi
imulq %r12, %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x2220
movq 0x20(%r14), %rdi
movslq %ebx, %rsi
xorl %edx, %edx
callq 0x2220
movl $0x1, %edx
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x22e0
movq 0x20(%r14), %rcx
movl $0x1, %edx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2410
movq 0x38(%r14), %rax
movslq %r13d, %rbx
movl $0xffff, (%rax,%rbx,4)  # imm = 0xFFFF
movq %r15, %rdi
callq 0x2330
testl %eax, %eax
jne 0x3cc6
movslq 0x30(%r14), %rcx
movq 0x38(%r14), %rax
movl $0xffff, %r13d          # imm = 0xFFFF
cmpq $0x2, %rcx
jl 0x3cc2
movl $0x1, %edx
cmpl $0x0, (%rax,%rdx,4)
movl %edx, %r13d
je 0x3cb1
movl $0xffff, %r13d          # imm = 0xFFFF
incq %rdx
cmpq %rcx, %rdx
jae 0x3cc2
cmpl $0xffff, %r13d          # imm = 0xFFFF
je 0x3ca2
movl %r13d, (%rax,%rbx,4)
movq %r15, %rdi
callq 0x2330
testl %eax, %eax
jne 0x3ce2
incq %rbp
cmpl $0xffff, %r13d          # imm = 0xFFFF
jne 0x3c1d
leaq 0x40(%r14), %r12
movq %r15, %rdi
callq 0x2330
movq 0x38(%r14), %rsi
movq 0x58(%r14), %rcx
movl 0xc(%rcx), %edx
movq %r14, %rdi
testl %eax, %eax
je 0x3d24
callq 0x2b18
movq 0x58(%r14), %rax
movl 0x8(%rax), %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2b5e
xorl %r13d, %r13d
jmp 0x3d4d
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
jmp 0x3d55
callq 0x27ca
movq 0x58(%r14), %rax
movl 0x8(%rax), %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2810
movq %r14, %rdi
movl 0xc(%rsp), %esi
callq 0x4176
movl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
movq %r15, %rdi
callq 0x21d0
leaq 0x28(%rsp), %rbx
movq 0x10(%rsp), %rdi
callq 0x2260
movq 0x18(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3d7b
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21f0
movl %r13d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %rbp, %rdi
je 0x3dac
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x3dac
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x3dc8
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %rbx, %rdi
callq 0x23b0