// File: emr4378[P]FAT-File-System/FileSys.cpp
// Function: FileSys::createFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
int FileSys::createFile(string name) {
	int ret = -2;
	int i;
	int cluster = findNextFreeCluster();
	int index = -1;
	bool unique = true;

	if (cluster != 0xFFFF) {
		ret = -1;
		for (i = 0; i < directoryTable.size() && unique; i++) {
			if (index == -1 &&
				(directoryTable[i].name[0] == (char)0x00 || 
				directoryTable[i].name[0] == (char)0xFF)) {
				index = i;
			}
			if (directoryTable[i].name == name) {
				unique = false;
			}
		}
		if (unique) {
			//new file at cluster; filled here in case directory table must grow
			fileAllocationTable[cluster] = 0xFFFF;
			if (index == -1) {
				//if directory table is filled
				int dirCluster = boot->rootDir;
				while (fileAllocationTable[dirCluster] != 0xFFFF) {
					dirCluster = fileAllocationTable[dirCluster];
				}
			
				fileAllocationTable[dirCluster] = findNextFreeCluster();
				if (fileAllocationTable[dirCluster] != 0xFFFF) {
					dirCluster = fileAllocationTable[dirCluster];
					fileAllocationTable[dirCluster] = 0xFFFF;
					int dirTableSize = directoryTable.size();
					directoryTable.resize(dirTableSize+entriesPerTable);
					for (i = dirTableSize; i < directoryTable.size(); i++) {
						directoryTable[i].name[0] = 0x00;
					}
					index = dirTableSize;
				} else {
					ret = -2;
				}
			}

			if (index != -1 && ret != -2) {
				strcpy(directoryTable[index].name, name.c_str());
				directoryTable[index].index = cluster;
				directoryTable[index].size = 0;
				directoryTable[index].type = 0x00;
				directoryTable[index].creation = time(NULL);

				writeFAT(fileAllocationTable, boot->FAT);
				writeDirectoryTable(&directoryTable, boot->rootDir);

				ret = index;
			} else {
				readFAT(fileAllocationTable, boot->FAT);
				readDirectoryTable(&directoryTable, boot->rootDir);
			}
		}
	}

	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rsi, -0x40(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x38(%rbp)
movl $0xfffffffe, -0x14(%rbp) # imm = 0xFFFFFFFE
callq 0x4680
movl %eax, -0x1c(%rbp)
movl $0xffffffff, -0x20(%rbp) # imm = 0xFFFFFFFF
movb $0x1, -0x21(%rbp)
cmpl $0xffff, -0x1c(%rbp)    # imm = 0xFFFF
je 0x4ad5
movl $0xffffffff, -0x14(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x18(%rbp)
movq -0x38(%rbp), %rdi
movslq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
addq $0x40, %rdi
callq 0x6b80
movq -0x50(%rbp), %rcx
movq %rax, %rdx
xorl %eax, %eax
cmpq %rdx, %rcx
movb %al, -0x41(%rbp)
jae 0x481d
movb -0x21(%rbp), %al
movb %al, -0x41(%rbp)
movb -0x41(%rbp), %al
testb $0x1, %al
jne 0x4826
jmp 0x489b
cmpl $-0x1, -0x20(%rbp)
jne 0x4864
movq -0x38(%rbp), %rdi
addq $0x40, %rdi
movslq -0x18(%rbp), %rsi
callq 0x6a10
movsbl (%rax), %eax
cmpl $0x0, %eax
je 0x485e
movq -0x38(%rbp), %rdi
addq $0x40, %rdi
movslq -0x18(%rbp), %rsi
callq 0x6a10
movsbl (%rax), %eax
cmpl $-0x1, %eax
jne 0x4864
movl -0x18(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x38(%rbp), %rdi
addq $0x40, %rdi
movslq -0x18(%rbp), %rsi
callq 0x6a10
movq -0x40(%rbp), %rsi
movq %rax, %rdi
callq 0x6ba0
testb $0x1, %al
jne 0x4887
jmp 0x488b
movb $0x0, -0x21(%rbp)
jmp 0x488d
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x47f1
testb $0x1, -0x21(%rbp)
je 0x4ad3
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x1c(%rbp), %rcx
movl $0xffff, (%rax,%rcx,4)  # imm = 0xFFFF
cmpl $-0x1, -0x20(%rbp)
jne 0x49c5
movq -0x38(%rbp), %rax
movq 0x58(%rax), %rax
movl 0x8(%rax), %eax
movl %eax, -0x28(%rbp)
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x28(%rbp), %rcx
cmpl $0xffff, (%rax,%rcx,4)  # imm = 0xFFFF
je 0x48f9
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x28(%rbp)
jmp 0x48d0
movq -0x38(%rbp), %rdi
callq 0x4680
movl %eax, %esi
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rcx
movslq -0x28(%rbp), %rdx
movl %esi, (%rcx,%rdx,4)
movq 0x38(%rax), %rax
movslq -0x28(%rbp), %rcx
cmpl $0xffff, (%rax,%rcx,4)  # imm = 0xFFFF
je 0x49bc
movq -0x38(%rbp), %rdi
movq 0x38(%rdi), %rax
movslq -0x28(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl %eax, -0x28(%rbp)
movq 0x38(%rdi), %rax
movslq -0x28(%rbp), %rcx
movl $0xffff, (%rax,%rcx,4)  # imm = 0xFFFF
addq $0x40, %rdi
callq 0x6b80
movq -0x38(%rbp), %rcx
movl %eax, -0x2c(%rbp)
movq %rcx, %rdi
addq $0x40, %rdi
movl -0x2c(%rbp), %eax
addl 0x2c(%rcx), %eax
movslq %eax, %rsi
callq 0x6980
movl -0x2c(%rbp), %eax
movl %eax, -0x18(%rbp)
movq -0x38(%rbp), %rdi
movslq -0x18(%rbp), %rax
movq %rax, -0x58(%rbp)
addq $0x40, %rdi
callq 0x6b80
movq %rax, %rcx
movq -0x58(%rbp), %rax
cmpq %rcx, %rax
jae 0x49b4
movq -0x38(%rbp), %rdi
addq $0x40, %rdi
movslq -0x18(%rbp), %rsi
callq 0x6a10
movb $0x0, (%rax)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x4974
movl -0x2c(%rbp), %eax
movl %eax, -0x20(%rbp)
jmp 0x49c3
movl $0xfffffffe, -0x14(%rbp) # imm = 0xFFFFFFFE
jmp 0x49c5
cmpl $-0x1, -0x20(%rbp)
je 0x4aa6
cmpl $-0x2, -0x14(%rbp)
je 0x4aa6
movq -0x38(%rbp), %rdi
addq $0x40, %rdi
movslq -0x20(%rbp), %rsi
callq 0x6a10
movq -0x40(%rbp), %rdi
movq %rax, -0x68(%rbp)
callq 0x3110
movq -0x68(%rbp), %rdi
movq %rax, %rsi
callq 0x3260
movq -0x38(%rbp), %rdi
movl -0x1c(%rbp), %eax
movl %eax, -0x60(%rbp)
addq $0x40, %rdi
movslq -0x20(%rbp), %rsi
callq 0x6a10
movl -0x60(%rbp), %ecx
movq -0x38(%rbp), %rdi
movl %ecx, 0x70(%rax)
addq $0x40, %rdi
movslq -0x20(%rbp), %rsi
callq 0x6a10
movq -0x38(%rbp), %rdi
movl $0x0, 0x74(%rax)
addq $0x40, %rdi
movslq -0x20(%rbp), %rsi
callq 0x6a10
movl $0x0, 0x78(%rax)
xorl %eax, %eax
movl %eax, %edi
callq 0x3250
movq -0x38(%rbp), %rdi
movl %eax, -0x5c(%rbp)
addq $0x40, %rdi
movslq -0x20(%rbp), %rsi
callq 0x6a10
movl -0x5c(%rbp), %ecx
movq -0x38(%rbp), %rdi
movl %ecx, 0x7c(%rax)
movq 0x38(%rdi), %rsi
movq 0x58(%rdi), %rax
movl 0xc(%rax), %edx
callq 0x3f00
movq -0x38(%rbp), %rdi
movq %rdi, %rsi
addq $0x40, %rsi
movq 0x58(%rdi), %rax
movl 0x8(%rax), %edx
callq 0x3f70
movl -0x20(%rbp), %eax
movl %eax, -0x14(%rbp)
jmp 0x4ad1
movq -0x38(%rbp), %rdi
movq 0x38(%rdi), %rsi
movq 0x58(%rdi), %rax
movl 0xc(%rax), %edx
callq 0x3ac0
movq -0x38(%rbp), %rdi
movq %rdi, %rsi
addq $0x40, %rsi
movq 0x58(%rdi), %rax
movl 0x8(%rax), %edx
callq 0x3b30
jmp 0x4ad3
jmp 0x4ad5
movl -0x14(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nop