// File: /workspace/github_cpp_makefile_star2_O3/emr4378[P]FAT-File-System/FileSys.cpp
// Function: FileSys::createFileSys(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, int)
int FileSys::createFileSys(string name, int fSize, int cSize) {
	int ret = -1;
	int i;

	file = fopen(name.c_str(), "w+");
	if (file != NULL) {
		boot = new BootRecord();
		boot->clusterSize = cSize * 1024;
		boot->size = fSize * 1024 * 1024;
		boot->FAT = 1;
		boot->rootDir = 2;
		
		sysName = name;
		entriesPerTable = (boot->clusterSize)/DT_ENTRY_SIZE;
		numClusters = (boot->size)/(boot->clusterSize);
		fileAllocationTable = new int[numClusters];

		fileAllocationTable[0] = 0xFFFF;
		fileAllocationTable[1] = 0xFFFF;
		fileAllocationTable[2] = 0xFFFF;
		for (i = 3; i < numClusters; i++) {
			fileAllocationTable[i] = 0x0000;
		}
		directoryTable.resize(entriesPerTable);
		for (i = 0; i < entriesPerTable; i++) {
			directoryTable[i].name[0] = 0x00;
		}

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

		findUsedClusterCount();

		cout << "ls" << endl;

		printDirectoryTable(directoryTable);

		ret = 0;
	}
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
movl %ecx, %r15d
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %rdi
leaq 0x575e(%rip), %rsi      # 0x8033
callq 0x2250
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x2aae
movl $0x10, %edi
callq 0x21e0
movq %rax, 0x58(%rbx)
shll $0xa, %r15d
movl %r15d, (%rax)
shll $0x14, %ebp
movl %ebp, 0x4(%rax)
movabsq $0x100000002, %rcx      # imm = 0x100000002
movq %rcx, 0x8(%rax)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x20f0
movq 0x58(%rbx), %rax
movl (%rax), %ecx
movl %ecx, %r15d
shrl $0x7, %r15d
movl %r15d, 0x2c(%rbx)
movl 0x4(%rax), %eax
xorl %edx, %edx
divl %ecx
movl %eax, %r14d
movl %eax, 0x30(%rbx)
movslq %eax, %r12
leaq (,%r12,4), %rax
testl %r12d, %r12d
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0x2040
movq %rax, 0x38(%rbx)
movabsq $0xffff0000ffff, %rcx   # imm = 0xFFFF0000FFFF
movq %rcx, (%rax)
movl $0xffff, 0x8(%rax)      # imm = 0xFFFF
cmpl $0x4, %r12d
jl 0x298e
addq $0xc, %rax
addl $-0x3, %r14d
shlq $0x2, %r14
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x2100
leaq 0x40(%rbx), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x4d8a
cmpl $0x0, 0x2c(%rbx)
jle 0x29bf
xorl %eax, %eax
xorl %ecx, %ecx
movq 0x40(%rbx), %rdx
movb $0x0, (%rdx,%rax)
incq %rcx
movslq 0x2c(%rbx), %rdx
subq $-0x80, %rax
cmpq %rdx, %rcx
jl 0x29a7
movq 0x20(%rbx), %rdi
movq 0x58(%rbx), %r15
xorl %r12d, %r12d
xorl %esi, %esi
xorl %edx, %edx
callq 0x2220
movq 0x20(%rbx), %rcx
movl $0x10, %esi
movl $0x1, %edx
movq %r15, %rdi
callq 0x2410
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 %r14, %rsi
callq 0x2b5e
movl $0x0, 0x28(%rbx)
movslq 0x30(%rbx), %rax
testq %rax, %rax
jle 0x2a39
movq 0x38(%rbx), %rcx
xorl %edx, %edx
cmpl $0x0, (%rcx,%rdx,4)
je 0x2a31
incl %r12d
movl %r12d, 0x28(%rbx)
incq %rdx
cmpq %rdx, %rax
jne 0x2a24
movq 0x8590(%rip), %rbx      # 0xafd0
leaq 0x55ef(%rip), %rsi      # 0x8036
movl $0x2, %edx
movq %rbx, %rdi
callq 0x2240
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21b0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2050
movq %rax, %rdi
callq 0x2120
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4dbe
movq %rbx, %rsi
callq 0x2bfc
movq 0x8(%rsp), %rdi
xorl %ebx, %ebx
testq %rdi, %rdi
je 0x2ab3
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
jmp 0x2ab3
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movl %ebx, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x2adc
movq 0x18(%rsp), %rsi
subq %rdi, %rsi
callq 0x21f0
movq %rbx, %rdi
callq 0x23b0