// File: /workspace/github_cpp_makefile_star2_O1/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, %r8
movslq 0x30(%rdi), %rax
movl $0xffff, %r12d          # imm = 0xFFFF
cmpq $0x2, %rax
jl 0x321b
movq 0x38(%r8), %rcx
movl $0x1, %edx
cmpl $0x0, (%rcx,%rdx,4)
movl %edx, %r12d
je 0x320a
movl $0xffff, %r12d          # imm = 0xFFFF
incq %rdx
cmpq %rax, %rdx
jae 0x321b
cmpl $0xffff, %r12d          # imm = 0xFFFF
je 0x31fb
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
cmpl $0xffff, %r12d          # imm = 0xFFFF
je 0x345a
movq 0x40(%r8), %rsi
movq 0x48(%r8), %r13
subq %rsi, %r13
movq %r8, 0x8(%rsp)
je 0x32a7
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movb $0x1, %r15b
xorl %r14d, %r14d
xorl %ebx, %ebx
cmpl $-0x1, %ebp
jne 0x3268
movzbl (%rsi,%r14), %eax
cmpl $0xff, %eax
je 0x3266
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testl %eax, %eax
jne 0x3268
movl %ebx, %ebp
addq %r14, %rsi
movq 0x10(%rsp), %rdi
callq 0x2070
testl %eax, %eax
movzbl %r15b, %r15d
cmovel %eax, %r15d
incq %rbx
movq 0x8(%rsp), %r8
movq 0x40(%r8), %rsi
movq 0x48(%r8), %r13
subq %rsi, %r13
sarq $0x7, %r13
cmpq %rbx, %r13
jbe 0x32b3
subq $-0x80, %r14
testb $0x1, %r15b
jne 0x324c
jmp 0x32b3
sarq $0x7, %r13
movb $0x1, %r15b
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testb $0x1, %r15b
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
je 0x345a
leaq 0x40(%r8), %r14
movq 0x38(%r8), %rax
movslq %r12d, %rcx
movl $0xffff, (%rax,%rcx,4)  # imm = 0xFFFF
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movb $0x1, %sil
cmpl $-0x1, %ebp
jne 0x33b5
movq 0x58(%r8), %rcx
movl 0x8(%rcx), %edx
movslq %edx, %rcx
movl (%rax,%rcx,4), %edx
cmpl $0xffff, %edx           # imm = 0xFFFF
jne 0x32ed
movslq 0x30(%r8), %rdx
movl $0xffff, %esi           # imm = 0xFFFF
cmpq $0x2, %rdx
jl 0x332c
movl $0x1, %edi
cmpl $0x0, (%rax,%rdi,4)
movl %edi, %esi
je 0x331c
movl $0xffff, %esi           # imm = 0xFFFF
incq %rdi
cmpq %rdx, %rdi
jae 0x332c
cmpl $0xffff, %esi           # imm = 0xFFFF
je 0x330f
movl %esi, (%rax,%rcx,4)
cmpl $0xffff, %esi           # imm = 0xFFFF
jne 0x3346
movl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
xorl %esi, %esi
jmp 0x33b5
movslq %esi, %rcx
movl $0xffff, (%rax,%rcx,4)  # imm = 0xFFFF
movslq 0x2c(%r8), %rsi
movslq %r13d, %rbx
addq %rbx, %rsi
movq %r14, %rdi
callq 0x4dbe
movq 0x8(%rsp), %r8
movq 0x40(%r8), %rax
movq 0x48(%r8), %rcx
subq %rax, %rcx
sarq $0x7, %rcx
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
cmpq %rbx, %rcx
jbe 0x33af
movq %rbx, %rcx
shlq $0x7, %rcx
movb $0x1, %sil
movb $0x0, (%rax,%rcx)
incq %rbx
movq 0x40(%r8), %rax
movq 0x48(%r8), %rdx
subq %rax, %rdx
sarq $0x7, %rdx
subq $-0x80, %rcx
cmpq %rbx, %rdx
ja 0x338b
movl %r13d, %ebp
jmp 0x33b5
movl %r13d, %ebp
movb $0x1, %sil
cmpl $-0x1, %ebp
sete %al
xorb $0x1, %sil
orb %al, %sil
jne 0x342f
movslq %ebp, %rbx
shlq $0x7, %rbx
movq 0x40(%r8), %rdi
addq %rbx, %rdi
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq %r14, %r15
movq %r8, %r14
callq 0x21a0
movq 0x40(%r14), %rax
movl %r12d, 0x70(%rax,%rbx)
movq $0x0, 0x74(%rax,%rbx)
xorl %edi, %edi
callq 0x2190
movq 0x40(%r14), %rcx
movl %eax, 0x7c(%rcx,%rbx)
movq 0x38(%r14), %rsi
movq 0x58(%r14), %rax
movl 0xc(%rax), %edx
movq %r14, %rdi
callq 0x2b20
movq 0x58(%r14), %rax
movl 0x8(%rax), %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x2b66
movl %ebp, %eax
jmp 0x345a
movq 0x38(%r8), %rsi
movq 0x58(%r8), %rax
movl 0xc(%rax), %edx
movq %r8, %rdi
movq %r8, %rbx
callq 0x27d2
movq 0x58(%rbx), %rax
movl 0x8(%rax), %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2818
movl %r15d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop