// File: 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
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rsi, -0xd0(%rbp)
movq %rdx, -0xc0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
leaq -0x40(%rbp), %rdi
callq 0x3430
movq -0xc8(%rbp), %rax
movl $0x0, -0x70(%rbp)
movq 0x58(%rax), %rax
movl (%rax), %eax
movl %eax, -0x74(%rbp)
movslq -0x74(%rbp), %rdi
callq 0x3400
movq -0xc0(%rbp), %rdi
movq %rax, -0x80(%rbp)
callq 0x3110
movq %rax, %rdi
leaq 0x7049(%rip), %rsi      # 0xc033
callq 0x3360
movq %rax, -0xb8(%rbp)
jmp 0x4ff8
movq -0xb8(%rbp), %rax
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
je 0x5228
movq -0xd0(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x3120
jmp 0x5023
movq -0xc8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x46f0
movl %eax, -0xd4(%rbp)
jmp 0x503e
movl -0xd4(%rbp), %eax
movl %eax, -0x4c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x3180
cmpl $-0x1, -0x4c(%rbp)
je 0x521b
movq -0xc8(%rbp), %rdi
addq $0x40, %rdi
movslq -0x4c(%rbp), %rsi
callq 0x6a10
movq -0xc8(%rbp), %rdi
movl 0x70(%rax), %eax
movl %eax, -0x58(%rbp)
addq $0x40, %rdi
movslq -0x4c(%rbp), %rsi
callq 0x6a10
movl 0x74(%rax), %eax
xorl %edx, %edx
divl -0x74(%rbp)
movl %edx, -0x6c(%rbp)
movq -0xc8(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x58(%rbp), %rcx
cmpl $0xffff, (%rax,%rcx,4)  # imm = 0xFFFF
je 0x5183
movl -0x74(%rbp), %eax
movl -0x58(%rbp), %ecx
imull %ecx, %eax
movl %eax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
movl -0x74(%rbp), %eax
movl -0x70(%rbp), %ecx
imull %ecx, %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x3330
jmp 0x50d7
movq -0xc8(%rbp), %rax
movq 0x20(%rax), %rdi
movslq -0x50(%rbp), %rsi
xorl %edx, %edx
callq 0x3330
jmp 0x50ef
movq -0xc8(%rbp), %rax
movq -0x80(%rbp), %rdi
movslq -0x74(%rbp), %rsi
movq 0x20(%rax), %rcx
movl $0x1, %edx
callq 0x3460
jmp 0x510e
movq -0x80(%rbp), %rdi
movslq -0x74(%rbp), %rsi
movq -0x48(%rbp), %rcx
movl $0x1, %edx
callq 0x3590
jmp 0x5126
movq -0xc8(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x58(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x58(%rbp)
movl -0x70(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x70(%rbp)
jmp 0x5096
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x5252
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x3180
jmp 0x5252
cmpl $0x0, -0x6c(%rbp)
je 0x5214
movq -0x80(%rbp), %rdi
movslq -0x6c(%rbp), %rsi
callq 0x3500
movq %rax, -0x80(%rbp)
movl -0x74(%rbp), %eax
movl -0x58(%rbp), %ecx
imull %ecx, %eax
movl %eax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
movl -0x74(%rbp), %eax
movl -0x70(%rbp), %ecx
imull %ecx, %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x3330
jmp 0x51c3
movq -0xc8(%rbp), %rax
movq 0x20(%rax), %rdi
movslq -0x50(%rbp), %rsi
xorl %edx, %edx
callq 0x3330
jmp 0x51db
movq -0xc8(%rbp), %rax
movq -0x80(%rbp), %rdi
movslq -0x6c(%rbp), %rsi
movq 0x20(%rax), %rcx
movl $0x1, %edx
callq 0x3460
jmp 0x51fa
movq -0x80(%rbp), %rdi
movslq -0x6c(%rbp), %rsi
movq -0x48(%rbp), %rcx
movl $0x1, %edx
callq 0x3590
jmp 0x5212
jmp 0x5214
movl $0x0, -0x1c(%rbp)
movq -0x48(%rbp), %rdi
callq 0x32a0
jmp 0x5226
jmp 0x5228
movq -0x80(%rbp), %rdi
callq 0x3370
movl -0x1c(%rbp), %eax
movl %eax, -0xd8(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x3180
movl -0xd8(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
leaq -0x40(%rbp), %rdi
callq 0x3180
movq -0x88(%rbp), %rdi
callq 0x3530
nopw (%rax,%rax)