// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq %rsi, %r12
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 (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x78(%rsp), %rbp
movq %rbp, %rdi
callq 0x4e76
movq %r14, %rdi
movq %rbp, %rsi
callq 0x315c
movl %eax, 0xc(%rsp)
movq (%rbp), %rdi
cmpq %rbx, %rdi
je 0x3e95
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x68(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %rbp
movq %rbp, %rdi
callq 0x4e76
movq %r14, %rdi
movq %rbp, %rsi
callq 0x4110
movq (%rbp), %rdi
cmpq %rbx, %rdi
je 0x3ed7
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21f0
leaq 0x48(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %r12
movq %r12, %rdi
callq 0x4e76
movq %r14, %rdi
movq %r12, %rsi
callq 0x315c
movl %eax, %ebp
movq (%r12), %rdi
cmpq %rbx, %rdi
je 0x3f1b
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21f0
cmpl $-0x1, %ebp
jne 0x3f67
leaq 0x28(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x4e76
movq %r14, %rdi
movq %r12, %rsi
callq 0x31cc
movl %eax, %ebx
movq 0x18(%rsp), %rdi
cmpq %rbp, %rdi
je 0x3f65
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21f0
movl %ebx, %ebp
testl %ebp, %ebp
js 0x40cf
movl %ebp, 0x8(%rsp)
movl %ebp, %eax
movq 0x40(%r14), %rcx
shlq $0x7, %rax
movl 0x70(%rcx,%rax), %r13d
movslq 0xc(%rsp), %rdx
shlq $0x7, %rdx
movl 0x70(%rcx,%rdx), %r12d
movl 0x74(%rcx,%rdx), %edx
movl %edx, 0x74(%rcx,%rax)
cmpl $0xffff, %r12d          # imm = 0xFFFF
setne %al
je 0x4076
cmpl $0xffff, %r13d          # imm = 0xFFFF
je 0x4076
movq 0x10(%rsp), %rbp
movq 0x20(%r14), %rdi
movl %r12d, %eax
imull %r15d, %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x2220
movq 0x20(%r14), %rcx
movl $0x1, %edx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x22e0
movq 0x20(%r14), %rdi
movslq %r13d, %rbx
movq %rbx, %rsi
imulq %r15, %rsi
xorl %edx, %edx
callq 0x2220
movq 0x20(%r14), %rcx
movl $0x1, %edx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2410
movq 0x38(%r14), %rax
movslq %r12d, %rcx
movl (%rax,%rcx,4), %r12d
movl $0xffff, %ecx           # imm = 0xFFFF
movl %ecx, (%rax,%rbx,4)
cmpl %ecx, %r12d
je 0x405d
movslq 0x30(%r14), %rcx
movl $0xffff, %r13d          # imm = 0xFFFF
cmpq $0x2, %rcx
jl 0x4059
movl $0x1, %edx
cmpl $0x0, (%rax,%rdx,4)
movl %edx, %r13d
je 0x4048
movl $0xffff, %r13d          # imm = 0xFFFF
incq %rdx
cmpq %rcx, %rdx
jae 0x4059
cmpl $0xffff, %r13d          # imm = 0xFFFF
je 0x4039
movl %r13d, (%rax,%rbx,4)
cmpl $0xffff, %r12d          # imm = 0xFFFF
setne %al
je 0x4076
cmpl $0xffff, %r13d          # imm = 0xFFFF
jne 0x3fba
leaq 0x40(%r14), %r15
movq 0x38(%r14), %rsi
movq 0x58(%r14), %rcx
movl 0xc(%rcx), %edx
movq %r14, %rdi
testb %al, %al
je 0x40b6
callq 0x27d2
movq 0x58(%r14), %rax
movl 0x8(%rax), %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2818
movq %r14, %rdi
movl 0x8(%rsp), %esi
callq 0x41c0
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
jmp 0x40cf
callq 0x2b20
movq 0x58(%r14), %rax
movl 0x8(%rax), %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2b66
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 %rbp, %rdi
je 0x4107
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %rbx, %rdi
callq 0x23b0
nop