// File: ljl0222[P]SecondFileSystem/FileSystem.cpp
// Function: FileSystem::FormatFileSystem()
void FileSystem::FormatFileSystem()
{
	FormatSuperBlock();
	m_DeviceManager->Construct();

	m_DeviceManager->write(m_SuperBlock, sizeof(SuperBlock), 0);

	DiskInode root;
	root.d_mode |= Inode::IALLOC | Inode::IFDIR;
	root.d_nlink = 1;

	m_DeviceManager->write(&root, sizeof(root));

	DiskInode empty;
	for (int cnt = 1; cnt < 8176; cnt++) {
		if (m_SuperBlock->s_ninode < 100) {
			m_SuperBlock->s_inode[m_SuperBlock->s_ninode++] = cnt;
		}
		m_DeviceManager->write(&empty, sizeof(empty));
	}

	char freeBlock[512], tmpFreeBlock[512];
	memset(freeBlock, 0, sizeof(freeBlock));
	memset(tmpFreeBlock, 0, sizeof(tmpFreeBlock));

	for (int cnt = 0; cnt < FileSystem::DATA_ZONE_SIZE; cnt++) {
		if (m_SuperBlock->s_nfree >= 100) {
			memcpy(tmpFreeBlock, &m_SuperBlock->s_nfree, sizeof(m_SuperBlock->s_free) + sizeof(int));
			m_DeviceManager->write(tmpFreeBlock, 512);
			m_SuperBlock->s_nfree = 0;
		}
		else {
			m_DeviceManager->write(freeBlock, 512);
		}
		m_SuperBlock->s_free[m_SuperBlock->s_nfree++] = cnt + DATA_ZONE_START_SECTOR;
	}

	time((time_t*)&m_SuperBlock->s_time);

	m_DeviceManager->write(m_SuperBlock, sizeof(SuperBlock), 0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x4c0, %rsp            # imm = 0x4C0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x4b8(%rbp)
callq 0x7cf0
movq -0x4b8(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x60a0
movq -0x4b8(%rbp), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %r8d, %r8d
movl %r8d, -0x4a8(%rbp)
movl %r8d, %ecx
callq 0x6100
leaq -0x48(%rbp), %rdi
movq %rdi, -0x4b0(%rbp)
callq 0x8a10
movq -0x4b8(%rbp), %rax
movq -0x4b0(%rbp), %rsi
movl -0x4a8(%rbp), %r8d
movb -0x47(%rbp), %cl
orb $-0x40, %cl
movb %cl, -0x47(%rbp)
movl $0x1, -0x44(%rbp)
movq 0x10(%rax), %rdi
movl $0x40, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
callq 0x6100
jmp 0x74cf
leaq -0x94(%rbp), %rdi
callq 0x8a10
jmp 0x74dd
movl $0x1, -0x98(%rbp)
cmpl $0x1ff0, -0x98(%rbp)    # imm = 0x1FF0
jge 0x75a7
movq -0x4b8(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x64, 0x19c(%rax)
jge 0x754e
movq -0x4b8(%rbp), %rcx
movl -0x98(%rbp), %edx
movq 0x8(%rcx), %rax
movq 0x8(%rcx), %rsi
movl 0x19c(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x19c(%rsi)
movslq %ecx, %rcx
movl %edx, 0x1a0(%rax,%rcx,4)
jmp 0x754e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x7718
movq -0x4b8(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x94(%rbp), %rsi
movl $0x40, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x6100
jmp 0x7574
jmp 0x7576
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x74e7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x94(%rbp), %rdi
callq 0x8a80
jmp 0x7718
leaq -0x2a0(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x3190
leaq -0x4a0(%rbp), %rdi
xorl %esi, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x3190
movl $0x0, -0x4a4(%rbp)
cmpl $0x3c00, -0x4a4(%rbp)   # imm = 0x3C00
jge 0x76c2
movq -0x4b8(%rbp), %rax
movq 0x8(%rax), %rax
cmpl $0x64, 0x8(%rax)
jl 0x7659
movq -0x4b8(%rbp), %rax
movq 0x8(%rax), %rsi
addq $0x8, %rsi
leaq -0x4a0(%rbp), %rdi
movq %rdi, -0x4c0(%rbp)
movl $0x194, %edx            # imm = 0x194
callq 0x3260
movq -0x4c0(%rbp), %rsi
movq -0x4b8(%rbp), %rax
movq 0x10(%rax), %rdi
movl $0x200, %edx            # imm = 0x200
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x6100
jmp 0x7645
movq -0x4b8(%rbp), %rax
movq 0x8(%rax), %rax
movl $0x0, 0x8(%rax)
jmp 0x7681
movq -0x4b8(%rbp), %rax
movq 0x10(%rax), %rdi
leaq -0x2a0(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x6100
jmp 0x767f
jmp 0x7681
movq -0x4b8(%rbp), %rcx
movl -0x4a4(%rbp), %edx
addl $0x400, %edx            # imm = 0x400
movq 0x8(%rcx), %rax
movq 0x8(%rcx), %rsi
movl 0x8(%rsi), %ecx
movl %ecx, %edi
addl $0x1, %edi
movl %edi, 0x8(%rsi)
movslq %ecx, %rcx
movl %edx, 0xc(%rax,%rcx,4)
movl -0x4a4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4a4(%rbp)
jmp 0x75d7
movq -0x4b8(%rbp), %rax
movq 0x8(%rax), %rdi
addq $0x340, %rdi            # imm = 0x340
callq 0x32c0
movq -0x4b8(%rbp), %rax
movq 0x8(%rax), %rsi
movq 0x10(%rax), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %r8d, %r8d
movl %r8d, %ecx
callq 0x6100
jmp 0x76fa
leaq -0x94(%rbp), %rdi
callq 0x8a80
leaq -0x48(%rbp), %rdi
callq 0x8a80
addq $0x4c0, %rsp            # imm = 0x4C0
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x8a80
movq -0x50(%rbp), %rdi
callq 0x3580
nopw (%rax,%rax)