// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
callq 0x2e9a
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x2f4b
pushq $-0x2
jmp 0x3032
movl %eax, 0x4(%rsp)
leaq 0x40(%rbx), %rax
movq %rax, 0x8(%rsp)
movb $0x1, %al
pushq $-0x1
popq %rbp
xorl %r14d, %r14d
xorl %r13d, %r13d
movq 0x40(%rbx), %rdi
movq 0x48(%rbx), %r12
subq %rdi, %r12
movq %r12, %r15
sarq $0x7, %r15
cmpq %r13, %r15
jbe 0x2fb1
testb $0x1, %al
je 0x2fb1
cmpl $-0x1, %ebp
jne 0x2f99
movzbl (%rdi,%r14), %eax
cmpl $0xff, %eax
je 0x2f96
pushq $-0x1
popq %rbp
testl %eax, %eax
jne 0x2f99
movl %r13d, %ebp
addq %r14, %rdi
movq 0x10(%rsp), %rsi
callq 0x43b3
xorb $0x1, %al
incq %r13
subq $-0x80, %r14
jmp 0x2f63
testb $0x1, %al
je 0x3030
movq 0x38(%rbx), %r13
movl 0x4(%rsp), %ecx
movslq %ecx, %rax
movl $0xffff, (%r13,%rax,4)  # imm = 0xFFFF
cmpl $-0x1, %ebp
je 0x3044
movslq %ebp, %r14
shlq $0x7, %r14
addq %r14, %rdi
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movl %ecx, %r15d
callq 0x21f0
movq 0x40(%rbx), %rax
movl %r15d, 0x70(%rax,%r14)
andq $0x0, 0x74(%rax,%r14)
xorl %edi, %edi
callq 0x21e0
movq 0x40(%rbx), %rcx
movl %eax, 0x7c(%rcx,%r14)
movq 0x38(%rbx), %rsi
movq 0x58(%rbx), %rax
movl 0xc(%rax), %edx
movq %rbx, %rdi
callq 0x2a38
movq 0x58(%rbx), %rax
movl 0x8(%rax), %edx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x2a7c
jmp 0x3033
pushq $-0x1
popq %rbp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x58(%rbx), %r14
movl 0x8(%r14), %eax
movslq %eax, %rbp
movl (%r13,%rbp,4), %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x304c
movq %rbx, %rdi
callq 0x2e9a
movl %eax, (%r13,%rbp,4)
cmpl $0xffff, %eax           # imm = 0xFFFF
jne 0x3073
pushq $-0x2
jmp 0x30db
cltq
movl $0xffff, (%r13,%rax,4)  # imm = 0xFFFF
movl %r15d, %ebp
movslq 0x2c(%rbx), %rax
movslq %r15d, %rsi
addq %rax, %rsi
movq 0x8(%rsp), %rdi
callq 0x4332
shrq $0x7, %r12
movslq %r12d, %rax
movq %rax, %rcx
shlq $0x7, %rcx
movq 0x40(%rbx), %rdi
movq 0x48(%rbx), %rdx
subq %rdi, %rdx
sarq $0x7, %rdx
cmpq %rax, %rdx
jbe 0x30c4
movb $0x0, (%rdi,%rcx)
incq %rax
subq $-0x80, %rcx
jmp 0x30a3
cmpl $-0x1, %ebp
movl 0x4(%rsp), %ecx
jne 0x2fce
movq 0x38(%rbx), %r13
movq 0x58(%rbx), %r14
pushq $-0x1
popq %rbp
movl 0xc(%r14), %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x279e
movq 0x58(%rbx), %rax
movl 0x8(%rax), %edx
movq %rbx, %rdi
movq 0x8(%rsp), %rsi
callq 0x27e2
jmp 0x3033