// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movq %rsi, -0x60(%rbp)
movq %rdi, %rax
movq -0x60(%rbp), %rdi
movq %rdi, -0x58(%rbp)
movq %rax, -0x8(%rbp)
movq %rdi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x50(%rbp)
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
callq 0x3110
movq %rax, %rdi
leaq 0x83bd(%rip), %rsi      # 0xc033
callq 0x3360
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x20(%rax)
cmpq $0x0, 0x20(%rax)
je 0x3e94
movl $0x10, %edi
callq 0x32b0
movq %rax, %rdi
movq %rdi, -0x68(%rbp)
xorl %esi, %esi
movl $0x10, %edx
callq 0x3170
movq -0x68(%rbp), %rax
movq -0x58(%rbp), %rsi
movq -0x50(%rbp), %rdi
movq %rax, 0x58(%rdi)
movl -0x18(%rbp), %ecx
shll $0xa, %ecx
movq 0x58(%rdi), %rax
movl %ecx, (%rax)
movl -0x14(%rbp), %ecx
shll $0xa, %ecx
shll $0xa, %ecx
movq 0x58(%rdi), %rax
movl %ecx, 0x4(%rax)
movq 0x58(%rdi), %rax
movl $0x1, 0xc(%rax)
movq 0x58(%rdi), %rax
movl $0x2, 0x8(%rax)
callq 0x3050
movq -0x50(%rbp), %rcx
movq 0x58(%rcx), %rax
movl (%rax), %eax
shrl $0x7, %eax
movl %eax, 0x2c(%rcx)
movq 0x58(%rcx), %rax
movl 0x4(%rax), %eax
movq 0x58(%rcx), %rcx
xorl %edx, %edx
divl (%rcx)
movl %eax, %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x30(%rax)
movslq 0x30(%rax), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x3040
movq %rax, %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x38(%rax)
movq 0x38(%rax), %rcx
movl $0xffff, (%rcx)         # imm = 0xFFFF
movq 0x38(%rax), %rcx
movl $0xffff, 0x4(%rcx)      # imm = 0xFFFF
movq 0x38(%rax), %rax
movl $0xffff, 0x8(%rax)      # imm = 0xFFFF
movl $0x3, -0x20(%rbp)
movq -0x50(%rbp), %rcx
movl -0x20(%rbp), %eax
cmpl 0x30(%rcx), %eax
jge 0x3d9e
movq -0x50(%rbp), %rax
movq 0x38(%rax), %rax
movslq -0x20(%rbp), %rcx
movl $0x0, (%rax,%rcx,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x3d74
movq -0x50(%rbp), %rax
movq %rax, %rdi
addq $0x40, %rdi
movslq 0x2c(%rax), %rsi
callq 0x6980
movl $0x0, -0x20(%rbp)
movq -0x50(%rbp), %rcx
movl -0x20(%rbp), %eax
cmpl 0x2c(%rcx), %eax
jge 0x3de4
movq -0x50(%rbp), %rdi
addq $0x40, %rdi
movslq -0x20(%rbp), %rsi
callq 0x6a10
movb $0x0, (%rax)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x3db9
movq -0x50(%rbp), %rdi
movq 0x58(%rdi), %rsi
callq 0x3eb0
movq -0x50(%rbp), %rdi
movq 0x38(%rdi), %rsi
movq 0x58(%rdi), %rax
movl 0xc(%rax), %edx
callq 0x3f00
movq -0x50(%rbp), %rdi
movq %rdi, %rsi
addq $0x40, %rsi
movq %rsi, -0x78(%rbp)
movq 0x58(%rdi), %rax
movl 0x8(%rax), %edx
callq 0x3f70
movq -0x50(%rbp), %rdi
callq 0x4020
movq 0xc1a0(%rip), %rdi      # 0xffd0
leaq 0x81ff(%rip), %rsi      # 0xc036
callq 0x3290
movq %rax, %rdi
movq 0xc17a(%rip), %rsi      # 0xffc0
callq 0x32e0
movq -0x78(%rbp), %rsi
leaq -0x38(%rbp), %rdi
movq %rdi, -0x70(%rbp)
callq 0x6a30
movq -0x50(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x4080
jmp 0x3e6b
leaq -0x38(%rbp), %rdi
callq 0x6b20
movl $0x0, -0x1c(%rbp)
jmp 0x3e94
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x6b20
jmp 0x3ea0
movl -0x1c(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x3530
nopl (%rax)