// File: /workspace/github_cpp_makefile_star2_O3/emr4378[P]FAT-File-System/FileSys.cpp
// Function: FileSys::copyFileInToExt(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::copyFileInToExt(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 leftOver;
	int i = 0;
	int clusterSize = boot->clusterSize;
	void *clusterData = malloc(clusterSize);

	//internal (fake/the FileSys) to external (real)
	outerFile = fopen(dest.c_str(), "w+");
	if (outerFile != NULL) {
		index = findIndexForFile(source);	
		if (index != -1) {
			cluster = directoryTable[index].index;
			leftOver = directoryTable[index].size % clusterSize;

			while (fileAllocationTable[cluster] != 0xFFFF) {
				offset = clusterSize * cluster;
				fseek(outerFile, (clusterSize * i), SEEK_SET);
				fseek(file, offset, SEEK_SET);
				fread(clusterData, clusterSize, 1, file);
				fwrite(clusterData, clusterSize, 1, outerFile);
				
				cluster = fileAllocationTable[cluster];
				i++;
			}

			if (leftOver != 0) {
				clusterData = realloc(clusterData, leftOver);
				offset = clusterSize * cluster;
				fseek(outerFile, (clusterSize * i), SEEK_SET);
				fseek(file, offset, SEEK_SET);
				fread(clusterData, leftOver, 1, file);
				fwrite(clusterData, leftOver, 1, outerFile);
			}

			ret = 0;
		}
		fclose(outerFile);
	}

	free(clusterData);

	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r12
leaq 0x38(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq $0x0, -0x8(%rbx)
movb $0x0, (%rbx)
movq 0x58(%rdi), %rax
movslq (%rax), %rdi
movq %rdi, 0x8(%rsp)
callq 0x22b0
movq %rax, %rbp
movq (%r14), %rdi
leaq 0x4788(%rip), %rsi      # 0x8033
callq 0x2250
testq %rax, %rax
je 0x394f
movq %rax, %r15
movq %rbp, (%rsp)
leaq 0x58(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %rdi
callq 0x4e42
leaq 0x48(%rsp), %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x3138
movl %eax, %ebp
movq (%r13), %rdi
cmpq %rbx, %rdi
je 0x3906
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x21f0
cmpl $-0x1, %ebp
je 0x395a
movq %r15, 0x10(%rsp)
movslq %ebp, %rax
movq 0x38(%r12), %rcx
movq %r12, 0x20(%rsp)
movq 0x40(%r12), %rdx
shlq $0x7, %rax
movl 0x70(%rdx,%rax), %ebp
movl 0x74(%rdx,%rax), %eax
xorl %edx, %edx
movq 0x8(%rsp), %rsi
divl %esi
movl %edx, 0x1c(%rsp)
movslq %ebp, %r14
cmpl $0xffff, (%rcx,%r14,4)  # imm = 0xFFFF
jne 0x3969
xorl %ebx, %ebx
jmp 0x39e8
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x3a7a
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq (%rsp), %rbp
jmp 0x3a6d
xorl %ebx, %ebx
xorl %r13d, %r13d
movq 0x10(%rsp), %r12
movq 0x20(%rsp), %r15
movslq %ebp, %rbp
imulq %rsi, %rbp
movq %r12, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x2220
movq 0x20(%r15), %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x2220
movq 0x20(%r15), %rcx
movl $0x1, %edx
movq (%rsp), %rbp
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
callq 0x22e0
movl $0x1, %edx
movq %rbp, %rdi
movq 0x8(%rsp), %rsi
movq %r12, %rcx
callq 0x2410
movq 0x8(%rsp), %rsi
movq 0x38(%r15), %rax
movl (%rax,%r14,4), %ebp
movslq %ebp, %r14
addq %rsi, %r13
incl %ebx
cmpl $0xffff, (%rax,%r14,4)  # imm = 0xFFFF
jne 0x3978
movl 0x1c(%rsp), %eax
testl %eax, %eax
je 0x3a61
movslq %eax, %r13
movq (%rsp), %rdi
movq %rsi, %r15
movq %r13, %rsi
callq 0x2370
movq %rax, %r14
imull %r15d, %ebp
imull %r15d, %ebx
movslq %ebx, %rsi
xorl %r12d, %r12d
movq 0x10(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x2220
movq 0x20(%rsp), %rbx
movq 0x20(%rbx), %rdi
movslq %ebp, %rsi
movq %r14, %rbp
xorl %edx, %edx
callq 0x2220
movq 0x20(%rbx), %rcx
movl $0x1, %edx
movq %r14, %rdi
movq %r13, %rsi
callq 0x22e0
movl $0x1, %edx
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rcx
callq 0x2410
jmp 0x3a6d
movq (%rsp), %rbp
movq 0x10(%rsp), %r15
xorl %r12d, %r12d
movq %r15, %rdi
callq 0x21d0
leaq 0x38(%rsp), %rbx
movq %rbp, %rdi
callq 0x2260
movq 0x28(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3a99
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21f0
movl %r12d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x3aca
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %rbx, %rdi
callq 0x23b0