// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %rax
movslq 0x30(%rdi), %rax
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
cmpq $0x2, %rax
jl 0x33f5
movq %rsi, %r15
movq %rdi, %rbx
movq 0x38(%rdi), %rcx
movl $0x1, %edx
cmpl $0x0, (%rcx,%rdx,4)
movl %edx, %r12d
je 0x31e4
movl $0xffff, %r12d          # imm = 0xFFFF
incq %rdx
cmpq %rax, %rdx
jae 0x31f5
cmpl $0xffff, %r12d          # imm = 0xFFFF
je 0x31d5
cmpl $0xffff, %r12d          # imm = 0xFFFF
je 0x33f5
leaq 0x40(%rbx), %r13
movq 0x40(%rbx), %rsi
movq 0x48(%rbx), %rcx
subq %rsi, %rcx
je 0x328e
movq %r13, (%rsp)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
xorl %r14d, %r14d
cmpl $-0x1, %ebp
jne 0x323f
movzbl (%rsi,%r13), %eax
cmpl $0xff, %eax
je 0x323c
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x323f
movl %r14d, %ebp
addq %r13, %rsi
movq %r15, %rdi
callq 0x2070
incq %r14
movq 0x40(%rbx), %rsi
movq 0x48(%rbx), %rcx
subq %rsi, %rcx
sarq $0x7, %rcx
cmpq %r14, %rcx
jbe 0x3269
subq $-0x80, %r13
testl %eax, %eax
jne 0x3222
testl %eax, %eax
je 0x33f0
movq 0x38(%rbx), %rax
movslq %r12d, %rdx
movl $0xffff, (%rax,%rdx,4)  # imm = 0xFFFF
cmpl $-0x1, %ebp
movq (%rsp), %r13
jne 0x3361
jmp 0x32a0
sarq $0x7, %rcx
movq 0x38(%rbx), %rax
movslq %r12d, %rdx
movl $0xffff, (%rax,%rdx,4)  # imm = 0xFFFF
movq 0x58(%rbx), %rdx
movl 0x8(%rdx), %edi
movslq %edi, %rsi
movl (%rax,%rsi,4), %edi
cmpl $0xffff, %edi           # imm = 0xFFFF
jne 0x32a7
movslq 0x30(%rbx), %rdi
cmpq $0x1, %rdi
jle 0x33c2
movl $0x1, %r8d
cmpl $0x0, (%rax,%r8,4)
movl %r8d, %r9d
je 0x32d9
movl $0xffff, %r9d           # imm = 0xFFFF
incq %r8
cmpq %rdi, %r8
jae 0x32ea
cmpl $0xffff, %r9d           # imm = 0xFFFF
je 0x32c9
movl %r9d, (%rax,%rsi,4)
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
cmpl $0xffff, %r9d           # imm = 0xFFFF
je 0x33ce
movslq %r9d, %rdx
movl $0xffff, (%rax,%rdx,4)  # imm = 0xFFFF
movl %ecx, %ebp
movslq 0x2c(%rbx), %rsi
movslq %ecx, %r14
addq %r14, %rsi
movq %r13, %rdi
callq 0x4d8a
movq 0x40(%rbx), %rsi
movq 0x48(%rbx), %rax
subq %rsi, %rax
sarq $0x7, %rax
cmpq %r14, %rax
jbe 0x3358
movq %r14, %rax
shlq $0x7, %rax
movb $0x0, (%rsi,%rax)
incq %r14
movq 0x40(%rbx), %rsi
movq 0x48(%rbx), %rcx
subq %rsi, %rcx
sarq $0x7, %rcx
subq $-0x80, %rax
cmpq %r14, %rcx
ja 0x3339
cmpl $-0x1, %ebp
je 0x3406
movslq %ebp, %r14
shlq $0x7, %r14
addq %r14, %rsi
movq (%r15), %rax
movq %rsi, %rdi
movq %rax, %rsi
callq 0x21a0
movq 0x40(%rbx), %rax
movl %r12d, 0x70(%rax,%r14)
movq $0x0, 0x74(%rax,%r14)
xorl %edi, %edi
callq 0x2190
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 0x2b18
movq 0x58(%rbx), %rax
movl 0x8(%rax), %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2b5e
jmp 0x33f5
movl $0xffff, (%rax,%rsi,4)  # imm = 0xFFFF
movl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
movl 0xc(%rdx), %edx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x27ca
movq 0x58(%rbx), %rax
movl 0x8(%rax), %edx
movq %rbx, %rdi
movq %r13, %rsi
callq 0x2810
jmp 0x33f5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38(%rbx), %rax
movq 0x58(%rbx), %rdx
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x33ce
nop