// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x480, %rsp            # imm = 0x480
movq %rdi, %rbx
movq 0x8(%rdi), %rdi
movaps 0x415e(%rip), %xmm0     # 0x9050
movups %xmm0, (%rdi)
movl $0x0, 0x19c(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x330(%rdi)
addq $0x340, %rdi            # imm = 0x340
callq 0x31e0
movq 0x10(%rbx), %rdi
callq 0x4290
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x42da
leaq -0xa0(%rbp), %r14
movq %r14, %rdi
callq 0x5c66
orb $-0x40, 0x1(%r14)
movl $0x1, 0x4(%r14)
movq 0x10(%rbx), %rdi
movq %r14, %rsi
movl $0x40, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x42da
leaq -0x60(%rbp), %rdi
callq 0x5c66
movl $0x1, %r15d
leaq -0x60(%rbp), %r14
movq 0x8(%rbx), %rax
movslq 0x19c(%rax), %rcx
cmpq $0x63, %rcx
jg 0x4f9f
leal 0x1(%rcx), %edx
movl %edx, 0x19c(%rax)
movl %r15d, 0x1a0(%rax,%rcx,4)
movq 0x10(%rbx), %rdi
movq %r14, %rsi
movl $0x40, %edx
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x42da
incl %r15d
cmpl $0x1ff0, %r15d          # imm = 0x1FF0
jne 0x4f7d
leaq -0x4a0(%rbp), %r14
movl $0x200, %edx            # imm = 0x200
movq %r14, %rdi
xorl %esi, %esi
callq 0x3140
leaq -0x2a0(%rbp), %r15
movl $0x200, %edx            # imm = 0x200
movq %r15, %rdi
xorl %esi, %esi
callq 0x3140
movq 0x8(%rbx), %rsi
movl $0x400, %r12d           # imm = 0x400
cmpl $0x64, 0x8(%rsi)
jl 0x5039
addq $0x8, %rsi
movl $0x194, %edx            # imm = 0x194
movq %r15, %rdi
callq 0x31b0
movq 0x10(%rbx), %rdi
movq %r15, %rsi
movl $0x200, %edx            # imm = 0x200
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x42da
movq 0x8(%rbx), %rsi
movl $0x0, 0x8(%rsi)
xorl %eax, %eax
jmp 0x5059
movq 0x10(%rbx), %rdi
movq %r14, %rsi
movl $0x200, %edx            # imm = 0x200
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x42da
movq 0x8(%rbx), %rsi
movl 0x8(%rsi), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x8(%rsi)
cltq
movl %r12d, 0xc(%rsi,%rax,4)
incl %r12d
cmpl $0x4000, %r12d          # imm = 0x4000
jne 0x4ffa
addq $0x340, %rsi            # imm = 0x340
movq %rsi, %rdi
callq 0x31e0
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x42da
leaq -0x60(%rbp), %rdi
callq 0x5c90
leaq -0xa0(%rbp), %rdi
callq 0x5c90
addq $0x480, %rsp            # imm = 0x480
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x50c8
jmp 0x50c1
movq %rax, %rbx
jmp 0x50d4
jmp 0x50c8
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x5c90
leaq -0xa0(%rbp), %rdi
callq 0x5c90
movq %rbx, %rdi
callq 0x33e0