// File: /workspace/github_cpp_makefile_star2_O1/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 $0x78, %rsp
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %r15
leaq 0x48(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq $0x0, -0x8(%rbx)
movb $0x0, (%rbx)
movq 0x58(%rdi), %rax
movslq (%rax), %r12
movq %r12, %rdi
callq 0x22b0
movq %rax, %rbp
movq (%r14), %rdi
leaq 0x4748(%rip), %rsi      # 0x8033
callq 0x2250
testq %rax, %rax
je 0x398e
movq %rax, %r14
movq %rbp, 0x8(%rsp)
leaq 0x68(%rsp), %rbx
movq %rbx, -0x10(%rbx)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x58(%rsp), %rdi
callq 0x4e76
leaq 0x58(%rsp), %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x315c
movl %eax, %ebp
movq (%r13), %rdi
cmpq %rbx, %rdi
je 0x3947
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x21f0
cmpl $-0x1, %ebp
je 0x3999
movq %r14, 0x10(%rsp)
movslq %ebp, %rax
movq 0x38(%r15), %rcx
movq %r15, 0x28(%rsp)
movq 0x40(%r15), %rdx
shlq $0x7, %rax
movl 0x70(%rdx,%rax), %ebx
movl 0x74(%rdx,%rax), %eax
xorl %edx, %edx
movq %r12, 0x20(%rsp)
divl %r12d
movl %edx, 0x1c(%rsp)
movslq %ebx, %rdx
cmpl $0xffff, (%rcx,%rdx,4)  # imm = 0xFFFF
jne 0x39a9
xorl %ebp, %ebp
jmp 0x3a2d
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
jmp 0x3abe
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq 0x8(%rsp), %rbp
jmp 0x3ab1
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x20(%rsp), %r14
movq 0x10(%rsp), %r15
movq 0x28(%rsp), %r12
movq %rdx, 0x30(%rsp)
imull %r14d, %ebx
movq %r15, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x2220
movq 0x20(%r12), %rdi
movslq %ebx, %rsi
xorl %edx, %edx
callq 0x2220
movq 0x20(%r12), %rcx
movl $0x1, %edx
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x22e0
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rcx
callq 0x2410
movq 0x38(%r12), %rax
movq 0x30(%rsp), %rcx
movl (%rax,%rcx,4), %ebx
movslq %ebx, %rdx
addq %r14, %r13
incl %ebp
cmpl $0xffff, (%rax,%rdx,4)  # imm = 0xFFFF
jne 0x39bd
movl 0x1c(%rsp), %eax
testl %eax, %eax
je 0x3aa4
movslq %eax, %r13
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x2370
movq 0x20(%rsp), %rcx
imull %ecx, %ebx
imull %ecx, %ebp
movslq %ebp, %rsi
movq %rax, %rbp
xorl %r12d, %r12d
movq 0x10(%rsp), %r14
movq %r14, %rdi
xorl %edx, %edx
callq 0x2220
movq 0x28(%rsp), %r15
movq 0x20(%r15), %rdi
movslq %ebx, %rsi
xorl %edx, %edx
callq 0x2220
movq 0x20(%r15), %rcx
movl $0x1, %edx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x22e0
movl $0x1, %edx
movq %rbp, %rdi
movq %r13, %rsi
movq %r14, %rcx
callq 0x2410
jmp 0x3ab1
movq 0x8(%rsp), %rbp
movq 0x10(%rsp), %r14
xorl %r12d, %r12d
movq %r14, %rdi
callq 0x21d0
leaq 0x48(%rsp), %rbx
movq %rbp, %rdi
callq 0x2260
movq 0x38(%rsp), %rdi
cmpq %rbx, %rdi
je 0x3add
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21f0
movl %r12d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x3b0e
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x21f0
movq %rbx, %rdi
callq 0x23b0