// File: /workspace/github_cpp_makefile_star2_O1/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 0x44da(%rip), %rsi      # 0x8039
callq 0x2250
movq %rax, (%rsp)
testq %rax, %rax
je 0x3d64
leaq 0x68(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %rdi
callq 0x4e76
leaq 0x58(%rsp), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x4110
movq (%rbp), %rdi
cmpq %rbx, %rdi
je 0x3bb5
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x48(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %rdi
callq 0x4e76
leaq 0x38(%rsp), %rsi
movq %r14, %rdi
callq 0x31cc
movl %eax, %ebp
movq 0x38(%rsp), %rdi
cmpq %r15, %rdi
movq (%rsp), %r13
je 0x3bfd
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21f0
testl %ebp, %ebp
js 0x3d97
movl %ebp, 0xc(%rsp)
movl %ebp, %ebx
movq 0x40(%r14), %rax
shlq $0x7, %rbx
movl 0x70(%rax,%rbx), %ebp
movq %r13, %rdi
xorl %esi, %esi
movl $0x2, %edx
callq 0x2220
movq %r13, %rdi
callq 0x2030
movq 0x40(%r14), %rcx
movl %eax, 0x74(%rcx,%rbx)
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2220
movq %r13, %rdi
callq 0x2330
testl %eax, %eax
jne 0x3d2b
cmpl $0xffff, %ebp           # imm = 0xFFFF
je 0x3d2b
xorl %ebx, %ebx
movslq %ebp, %r15
movq %r15, %r13
imulq %r12, %r13
movq %rbx, %rsi
imulq %r12, %rsi
movq (%rsp), %rdi
xorl %edx, %edx
callq 0x2220
movq 0x20(%r14), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x2220
movl $0x1, %edx
movq 0x10(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
movq (%rsp), %rcx
callq 0x22e0
movq 0x20(%r14), %rcx
movl $0x1, %edx
movq %r13, %rdi
movq (%rsp), %r13
movq %r12, %rsi
callq 0x2410
movq 0x38(%r14), %rax
movl $0xffff, (%rax,%r15,4)  # imm = 0xFFFF
movq %r13, %rdi
callq 0x2330
testl %eax, %eax
jne 0x3d10
movslq 0x30(%r14), %rax
movl $0xffff, %ebp           # imm = 0xFFFF
cmpq $0x2, %rax
jl 0x3d08
movq 0x38(%r14), %rcx
movl $0x1, %edx
cmpl $0x0, (%rcx,%rdx,4)
movl %edx, %ebp
je 0x3cf8
movl $0xffff, %ebp           # imm = 0xFFFF
incq %rdx
cmpq %rax, %rdx
jae 0x3d08
cmpl $0xffff, %ebp           # imm = 0xFFFF
je 0x3ceb
movq 0x38(%r14), %rax
movl %ebp, (%rax,%r15,4)
movq %r13, %rdi
callq 0x2330
testl %eax, %eax
jne 0x3d2b
incq %rbx
cmpl $0xffff, %ebp           # imm = 0xFFFF
jne 0x3c60
leaq 0x40(%r14), %r12
movq %r13, %rdi
callq 0x2330
movq 0x38(%r14), %rsi
movq 0x58(%r14), %rcx
movl 0xc(%rcx), %edx
movq %r14, %rdi
testl %eax, %eax
je 0x3d6b
callq 0x2b20
movq 0x58(%r14), %rax
movl 0x8(%rax), %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2b66
xorl %ebp, %ebp
jmp 0x3d97
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x3d9f
callq 0x27d2
movq 0x58(%r14), %rax
movl 0x8(%rax), %edx
movq %r14, %rdi
movq %r12, %rsi
callq 0x2818
movq %r14, %rdi
movl 0xc(%rsp), %esi
callq 0x41c0
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
movq (%rsp), %r13
movq %r13, %rdi
callq 0x21d0
leaq 0x28(%rsp), %rbx
movq 0x10(%rsp), %rdi
callq 0x2260
movq 0x18(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3dc5
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21f0
movl %ebp, %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 %r15, %rdi
je 0x3df5
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21f0
jmp 0x3df5
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x3e11
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %rbx, %rdi
callq 0x23b0
nop