// File: 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
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rsi, -0xf0(%rbp)
movq %rdx, -0xf8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq 0x58(%rax), %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movslq -0x34(%rbp), %rdi
callq 0x3400
movq -0xf0(%rbp), %rsi
movq %rax, -0x40(%rbp)
leaq -0x60(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x3120
movq -0xe8(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x46f0
movl %eax, -0xd4(%rbp)
jmp 0x56d8
movl -0xd4(%rbp), %eax
movl %eax, -0x24(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x3180
cmpl $-0x1, -0x24(%rbp)
je 0x5a4e
movq -0xf8(%rbp), %rsi
leaq -0x90(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x3120
movq -0xe8(%rbp), %rdi
movq -0x100(%rbp), %rsi
callq 0x5a70
jmp 0x5723
leaq -0x90(%rbp), %rdi
callq 0x3180
movq -0xf8(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x3120
movq -0xe8(%rbp), %rdi
movq -0x110(%rbp), %rsi
callq 0x46f0
movl %eax, -0x104(%rbp)
jmp 0x5764
movl -0x104(%rbp), %eax
movl %eax, -0x28(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x3180
cmpl $-0x1, -0x28(%rbp)
jne 0x5840
movq -0xf8(%rbp), %rsi
leaq -0xd0(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x3120
movq -0xe8(%rbp), %rdi
movq -0x120(%rbp), %rsi
callq 0x47a0
movl %eax, -0x114(%rbp)
jmp 0x57b8
movl -0x114(%rbp), %eax
movl %eax, -0x28(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x3180
jmp 0x5840
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x3180
jmp 0x5a63
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x3180
jmp 0x5a63
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x3180
jmp 0x5a63
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x3180
jmp 0x5a63
cmpl $0x0, -0x28(%rbp)
jl 0x5a46
movq -0xe8(%rbp), %rdi
addq $0x40, %rdi
movslq -0x28(%rbp), %rsi
callq 0x6a10
movq -0xe8(%rbp), %rdi
movl 0x70(%rax), %eax
movl %eax, -0x30(%rbp)
addq $0x40, %rdi
movslq -0x24(%rbp), %rsi
callq 0x6a10
movq -0xe8(%rbp), %rdi
movl 0x70(%rax), %eax
movl %eax, -0x2c(%rbp)
addq $0x40, %rdi
movslq -0x24(%rbp), %rsi
callq 0x6a10
movq -0xe8(%rbp), %rdi
movl 0x74(%rax), %eax
movl %eax, -0x124(%rbp)
addq $0x40, %rdi
movslq -0x28(%rbp), %rsi
callq 0x6a10
movl -0x124(%rbp), %ecx
movl %ecx, 0x74(%rax)
xorl %eax, %eax
cmpl $0xffff, -0x2c(%rbp)    # imm = 0xFFFF
movb %al, -0x125(%rbp)
je 0x58d9
cmpl $0xffff, -0x30(%rbp)    # imm = 0xFFFF
setne %al
movb %al, -0x125(%rbp)
movb -0x125(%rbp), %al
testb $0x1, %al
jne 0x58e8
jmp 0x59ba
movq -0xe8(%rbp), %rax
movq 0x20(%rax), %rdi
movl -0x34(%rbp), %eax
imull -0x2c(%rbp), %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x3330
movq -0xe8(%rbp), %rax
movq -0x40(%rbp), %rdi
movslq -0x34(%rbp), %rsi
movq 0x20(%rax), %rcx
movl $0x1, %edx
callq 0x3460
movq -0xe8(%rbp), %rax
movq 0x20(%rax), %rdi
movl -0x34(%rbp), %eax
imull -0x30(%rbp), %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x3330
movq -0xe8(%rbp), %rax
movq -0x40(%rbp), %rdi
movslq -0x34(%rbp), %rsi
movq 0x20(%rax), %rcx
movl $0x1, %edx
callq 0x3590
movq -0xe8(%rbp), %rax
movq 0x38(%rax), %rcx
movslq -0x2c(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movl %ecx, -0x2c(%rbp)
movq 0x38(%rax), %rax
movslq -0x30(%rbp), %rcx
movl $0xffff, (%rax,%rcx,4)  # imm = 0xFFFF
cmpl $0xffff, -0x2c(%rbp)    # imm = 0xFFFF
je 0x59b5
movq -0xe8(%rbp), %rdi
callq 0x4680
movl %eax, %esi
movq -0xe8(%rbp), %rax
movq 0x38(%rax), %rcx
movslq -0x30(%rbp), %rdx
movl %esi, (%rcx,%rdx,4)
movq 0x38(%rax), %rax
movslq -0x30(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x30(%rbp)
jmp 0x58b8
cmpl $0xffff, -0x2c(%rbp)    # imm = 0xFFFF
je 0x5a0c
movq -0xe8(%rbp), %rdi
movq 0x38(%rdi), %rsi
movq 0x58(%rdi), %rax
movl 0xc(%rax), %edx
callq 0x3ac0
movq -0xe8(%rbp), %rdi
movq %rdi, %rsi
addq $0x40, %rsi
movq 0x58(%rdi), %rax
movl 0x8(%rax), %edx
callq 0x3b30
movq -0xe8(%rbp), %rdi
movl -0x28(%rbp), %esi
callq 0x5b90
movl $0xfffffffe, -0x1c(%rbp) # imm = 0xFFFFFFFE
jmp 0x5a44
movq -0xe8(%rbp), %rdi
movq 0x38(%rdi), %rsi
movq 0x58(%rdi), %rax
movl 0xc(%rax), %edx
callq 0x3f00
movq -0xe8(%rbp), %rdi
movq %rdi, %rsi
addq $0x40, %rsi
movq 0x58(%rdi), %rax
movl 0x8(%rax), %edx
callq 0x3f70
movl $0x0, -0x1c(%rbp)
jmp 0x5a4c
movl -0x28(%rbp), %eax
movl %eax, -0x1c(%rbp)
jmp 0x5a4e
movq -0x40(%rbp), %rdi
callq 0x3370
movl -0x1c(%rbp), %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x3530
nopl (%rax)