// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %r14
movq 0x58(%rdi), %rax
movslq (%rax), %r15
movq %r15, %rdi
callq 0x22b0
movq %rax, 0x10(%rsp)
leaq 0x88(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x78(%rsp), %rbp
movq %rbp, %rdi
callq 0x4e42
movq %r14, %rdi
movq %rbp, %rsi
callq 0x3138
movl %eax, 0x4(%rsp)
movq (%rbp), %rdi
cmpq %rbx, %rdi
je 0x3e4a
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x68(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %rbp
movq %rbp, %rdi
callq 0x4e42
movq %r14, %rdi
movq %rbp, %rsi
callq 0x40c6
movq (%rbp), %rdi
cmpq %rbx, %rdi
je 0x3e8d
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x48(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %r13
movq %r13, %rdi
callq 0x4e42
movq %r14, %rdi
movq %r13, %rsi
callq 0x3138
movl %eax, %ebp
movq (%r13), %rdi
cmpq %rbx, %rdi
je 0x3ed2
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21f0
cmpl $-0x1, %ebp
jne 0x3f1d
leaq 0x28(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x4e42
movq %r14, %rdi
movq %r12, %rsi
callq 0x31a8
movl %eax, %ebp
movq 0x18(%rsp), %rdi
cmpq %r13, %rdi
je 0x3f1d
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21f0
testl %ebp, %ebp
js 0x4085
leaq 0x40(%r14), %rsi
movl %ebp, (%rsp)
movl %ebp, %eax
movq 0x40(%r14), %rcx
shlq $0x7, %rax
movl 0x70(%rcx,%rax), %ebx
movslq 0x4(%rsp), %rdx
shlq $0x7, %rdx
movl 0x70(%rcx,%rdx), %r13d
movl 0x74(%rcx,%rdx), %edx
movl %edx, 0x74(%rcx,%rax)
cmpl $0xffff, %r13d          # imm = 0xFFFF
movq %rsi, 0x8(%rsp)
je 0x401a
cmpl $0xffff, %ebx           # imm = 0xFFFF
je 0x401a
movq 0x10(%rsp), %r12
movq 0x20(%r14), %rdi
movl %r13d, %eax
imull %r15d, %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x2220
movq 0x20(%r14), %rcx
movl $0x1, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x22e0
movq 0x20(%r14), %rdi
movslq %ebx, %rbp
movq %rbp, %rsi
imulq %r15, %rsi
xorl %edx, %edx
callq 0x2220
movq 0x20(%r14), %rcx
movl $0x1, %edx
movq %r12, %rdi
movq %r15, %rsi
callq 0x2410
movq 0x38(%r14), %rax
movslq %r13d, %rcx
movl (%rax,%rcx,4), %r13d
movl $0xffff, %ecx           # imm = 0xFFFF
movl %ecx, (%rax,%rbp,4)
cmpl %ecx, %r13d
je 0x405c
movslq 0x30(%r14), %rcx
cmpq $0x2, %rcx
jl 0x4023
movl $0x1, %edx
cmpl $0x0, (%rax,%rdx,4)
movl %edx, %ebx
je 0x3ff9
movl $0xffff, %ebx           # imm = 0xFFFF
incq %rdx
cmpq %rcx, %rdx
jae 0x4009
cmpl $0xffff, %ebx           # imm = 0xFFFF
je 0x3fec
movl %ebx, (%rax,%rbp,4)
cmpl $0xffff, %ebx           # imm = 0xFFFF
jne 0x3f73
jmp 0x4023
cmpl $0xffff, %r13d          # imm = 0xFFFF
je 0x405c
movq 0x38(%r14), %rsi
movq 0x58(%r14), %rax
movl 0xc(%rax), %edx
movq %r14, %rdi
callq 0x27ca
movq 0x58(%r14), %rax
movl 0x8(%rax), %edx
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x2810
movq %r14, %rdi
movl (%rsp), %esi
callq 0x4176
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0x4085
movq 0x38(%r14), %rsi
movq 0x58(%r14), %rax
movl 0xc(%rax), %edx
movq %r14, %rdi
callq 0x2b18
movq 0x58(%r14), %rax
movl 0x8(%rax), %edx
movq %r14, %rdi
movq 0x8(%rsp), %rsi
callq 0x2b5e
xorl %ebp, %ebp
movq 0x10(%rsp), %rdi
callq 0x2260
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x18(%rsp), %rdi
cmpq %r13, %rdi
je 0x40bd
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %rbx, %rdi
callq 0x23b0
nop