// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movq %rdx, %rbx
movq %rsi, %r14
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rdi, %r15
movb $0x0, (%rax)
movq 0x58(%rdi), %rax
movslq (%rax), %r12
movq %r12, %rdi
callq 0x22e0
movq %rax, 0x18(%rsp)
movq (%r14), %rdi
leaq 0x2a9e(%rip), %rsi      # 0x6039
callq 0x2280
movq %rax, (%rsp)
testq %rax, %rax
je 0x3748
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x22f0
leaq 0x48(%rsp), %rsi
movq %r15, %rdi
callq 0x3a02
leaq 0x48(%rsp), %rdi
callq 0x2100
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x22f0
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0x2f22
movl %eax, %ebx
leaq 0x28(%rsp), %rdi
callq 0x2100
testl %ebx, %ebx
js 0x3775
leaq 0x40(%r15), %rax
movq %rax, 0x10(%rsp)
movl %ebx, 0xc(%rsp)
movl %ebx, %ebx
movq 0x40(%r15), %rax
shlq $0x7, %rbx
movl 0x70(%rax,%rbx), %r13d
pushq $0x2
popq %rdx
movq (%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x2260
movq %r14, %rdi
callq 0x2030
movq 0x40(%r15), %rcx
movl %eax, 0x74(%rcx,%rbx)
xorl %ebp, %ebp
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2260
movq (%rsp), %rdi
callq 0x2350
testl %eax, %eax
jne 0x3710
cmpl $0xffff, %r13d          # imm = 0xFFFF
je 0x3710
movq %rbp, %rsi
movl %r13d, %ebx
imull %r12d, %ebx
movq (%rsp), %r14
movq %r14, %rdi
movq %rbp, 0x20(%rsp)
xorl %edx, %edx
callq 0x2260
movq 0x20(%r15), %rdi
movslq %ebx, %rsi
xorl %edx, %edx
callq 0x2260
movq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r12, %rbp
movq %r14, %r12
movq %r15, %r14
pushq $0x1
popq %r15
movq %r15, %rdx
movq %r12, %rcx
callq 0x2320
movq 0x20(%r14), %rcx
movq %rbx, %rdi
movq %rbp, %rsi
movq %r15, %rdx
movq %r14, %r15
callq 0x2420
movq 0x38(%r14), %rax
movl %r13d, %r14d
movslq %r13d, %rbx
movl $0xffff, (%rax,%rbx,4)  # imm = 0xFFFF
movq %r12, %rdi
callq 0x2350
testl %eax, %eax
jne 0x36fa
movq %r15, %rdi
callq 0x2e9a
movq 0x38(%r15), %rcx
movl %eax, %r14d
movl %eax, (%rcx,%rbx,4)
movq 0x20(%rsp), %rax
addq %rbp, %rax
movq %rbp, %r12
movq %rax, %rbp
movl %r14d, %r13d
jmp 0x364a
movq (%rsp), %rdi
callq 0x2350
movq 0x38(%r15), %rsi
movq 0x58(%r15), %rcx
movl 0xc(%rcx), %edx
movq %r15, %rdi
testl %eax, %eax
je 0x374d
callq 0x2a38
movq 0x58(%r15), %rax
movl 0x8(%rax), %edx
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x2a7c
xorl %ebx, %ebx
jmp 0x3775
pushq $-0x1
popq %rbx
jmp 0x377e
callq 0x279e
movq 0x58(%r15), %rax
movl 0x8(%rax), %edx
movq %r15, %rdi
movq 0x10(%rsp), %rsi
callq 0x27e2
movq %r15, %rdi
movl 0xc(%rsp), %esi
callq 0x3aae
pushq $-0x2
popq %rbx
movq (%rsp), %rdi
callq 0x2220
movq 0x18(%rsp), %rdi
callq 0x2290
leaq 0x68(%rsp), %rdi
callq 0x2100
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
jmp 0x37b8
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x2100
jmp 0x37c2
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x23d0