// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
leaq 0x38(%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, 0x8(%rsp)
movq (%r14), %rdi
leaq 0x2c8f(%rip), %rsi      # 0x6033
callq 0x2280
testq %rax, %rax
je 0x3500
movq %rax, %r14
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x22f0
leaq 0x48(%rsp), %r13
movq %r15, %rdi
movq %r13, %rsi
callq 0x2ec8
movl %eax, %ebx
movq %r13, %rdi
callq 0x2100
cmpl $-0x1, %ebx
je 0x3505
movq %r14, 0x10(%rsp)
movslq %ebx, %rcx
movq 0x38(%r15), %rax
movq 0x40(%r15), %rdx
shlq $0x7, %rcx
addq %rdx, %rcx
addq $0x70, %rcx
movl 0x4(%rcx), %edx
movl %edx, 0x1c(%rsp)
xorl %r13d, %r13d
movq 0x10(%rsp), %rbx
movslq (%rcx), %r14
cmpl $0xffff, (%rax,%r14,4)  # imm = 0xFFFF
je 0x3487
movq %r14, %rbp
imulq %r12, %rbp
movq %rbx, %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0x2260
movq 0x20(%r15), %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x2260
movq 0x20(%r15), %rcx
movq 0x8(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
movq %r13, 0x20(%rsp)
movq %r15, %r13
pushq $0x1
popq %r15
movq %r15, %rdx
callq 0x2320
movq %rbp, %rdi
movq %r12, %rsi
movq %r15, %rdx
movq %r13, %r15
movq 0x20(%rsp), %r13
movq %rbx, %rcx
callq 0x2420
movq 0x38(%r15), %rax
leaq (%rax,%r14,4), %rcx
addq %r12, %r13
jmp 0x340f
xorl %ebx, %ebx
movl 0x1c(%rsp), %eax
xorl %edx, %edx
divl %r12d
testl %edx, %edx
je 0x350a
movslq %edx, %rbx
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x23b0
movq %rax, %rbp
imulq %r12, %r14
movslq %r13d, %rsi
movq 0x10(%rsp), %r12
movq %r12, %rdi
xorl %edx, %edx
callq 0x2260
movq 0x20(%r15), %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x2260
movq 0x20(%r15), %rcx
pushq $0x1
popq %r14
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x2320
movq %rbp, 0x8(%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
xorl %ebx, %ebx
movq %r14, %rdx
movq %r12, %r14
movq %r12, %rcx
callq 0x2420
jmp 0x350f
pushq $-0x1
popq %rbx
jmp 0x3517
pushq $-0x1
popq %rbx
jmp 0x350f
movq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x2220
movq 0x8(%rsp), %rdi
callq 0x2290
leaq 0x28(%rsp), %rdi
callq 0x2100
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x2100
movq %rbx, %rdi
callq 0x23d0
nop