// File: /workspace/github_cpp_makefile_star2_O2/emr4378[P]FAT-File-System/FileSys.cpp
// Function: FileSys::copyFileInternally(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::copyFileInternally(string source, string dest) {
	int ret;
	int i;
	int sourceIndex;
	int destIndex;
	int sourceCluster;
	int destCluster;
	int clusterSize = boot->clusterSize;
	void *clusterData = malloc(clusterSize);

	sourceIndex = findIndexForFile(source);
	if (sourceIndex != -1) {
		removeFile(dest); //if dest already exists, delete/overwrite
		destIndex = findIndexForFile(dest);
		if (destIndex == -1) {
			destIndex = createFile(dest);
		}
		if (destIndex >= 0) {
			destCluster = directoryTable[destIndex].index;
			sourceCluster = directoryTable[sourceIndex].index;
			directoryTable[destIndex].size = directoryTable[sourceIndex].size;

			while(sourceCluster != 0xFFFF && destCluster != 0xFFFF) {
				fseek(file, (clusterSize * sourceCluster), SEEK_SET);
				fread(clusterData, clusterSize, 1,  file);
				fseek(file, (clusterSize * destCluster), SEEK_SET);
				fwrite(clusterData, clusterSize, 1,  file);

				sourceCluster = fileAllocationTable[sourceCluster];
			
				fileAllocationTable[destCluster] = 0xFFFF;
				if (sourceCluster != 0xFFFF) {
					fileAllocationTable[destCluster] = findNextFreeCluster();
					destCluster = fileAllocationTable[destCluster];
				}
			}

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

	free(clusterData);
	
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r14
movq 0x58(%rdi), %rax
movslq (%rax), %r15
movq %r15, %rdi
callq 0x22e0
movq %rax, 0x20(%rsp)
leaq 0x88(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x22f0
movq %r14, %rdi
movq %rbp, %rsi
callq 0x2ec8
movl %eax, 0x8(%rsp)
movq %rbp, %rdi
callq 0x2100
leaq 0x48(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x22f0
movq %r14, %rdi
movq %r13, %rsi
callq 0x3a02
leaq 0x48(%rsp), %rdi
callq 0x2100
leaq 0x68(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x22f0
movq %r14, %rdi
movq %r13, %rsi
callq 0x2ec8
movl %eax, %ebx
movq %r13, %rdi
callq 0x2100
cmpl $-0x1, %ebx
jne 0x38a2
leaq 0x28(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x22f0
movq %r14, %rdi
movq %r13, %rsi
callq 0x2f22
movl %eax, %ebx
leaq 0x28(%rsp), %rdi
callq 0x2100
testl %ebx, %ebx
js 0x39ce
leaq 0x40(%r14), %rax
movq %rax, 0x18(%rsp)
movl %ebx, 0x14(%rsp)
movl %ebx, %ecx
movq 0x40(%r14), %rdx
shlq $0x7, %rcx
movl 0x70(%rdx,%rcx), %eax
movslq 0x8(%rsp), %rsi
shlq $0x7, %rsi
movl 0x70(%rdx,%rsi), %ebp
movl 0x74(%rdx,%rsi), %esi
movl %esi, 0x74(%rdx,%rcx)
pushq $0x1
popq %r13
movslq %eax, %r12
movq %r12, %rax
imulq %r15, %rax
movq %rax, 0x8(%rsp)
cmpl $0xffff, %ebp           # imm = 0xFFFF
je 0x3973
cmpl $0xffff, %r12d          # imm = 0xFFFF
je 0x3973
movq 0x20(%r14), %rdi
movl %ebp, %eax
imull %r15d, %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x2260
movq 0x20(%r14), %rcx
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2320
movq 0x20(%r14), %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
callq 0x2260
movq 0x20(%r14), %rcx
movq %rbx, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x2420
movq 0x38(%r14), %rbx
movslq %ebp, %rax
movl (%rbx,%rax,4), %ebp
movl $0xffff, %eax           # imm = 0xFFFF
movl %eax, (%rbx,%r12,4)
cmpl %eax, %ebp
je 0x38ed
movq %r14, %rdi
callq 0x2e9a
movl %eax, (%rbx,%r12,4)
jmp 0x38de
movq 0x38(%r14), %rsi
movq 0x58(%r14), %rax
movl 0xc(%rax), %edx
movq %r14, %rdi
cmpl $0xffff, %ebp           # imm = 0xFFFF
je 0x39b3
callq 0x279e
movq 0x58(%r14), %rax
movl 0x8(%rax), %edx
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x27e2
movq %r14, %rdi
movl 0x14(%rsp), %esi
callq 0x3aae
pushq $-0x2
popq %rbx
jmp 0x39ce
callq 0x2a38
movq 0x58(%r14), %rax
movl 0x8(%rax), %edx
movq %r14, %rdi
movq 0x18(%rsp), %rsi
callq 0x2a7c
xorl %ebx, %ebx
movq 0x20(%rsp), %rdi
callq 0x2290
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x39ee
movq %rax, %rbx
movq %r13, %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x23d0
nop