// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x488, %rsp            # imm = 0x488
movq %rdi, %rbx
callq 0x548e
movq 0x10(%rbx), %rdi
callq 0x4310
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x4358
leaq -0xa8(%rbp), %r14
movq %r14, %rdi
callq 0x5bc4
orb $-0x40, 0x1(%r14)
movl $0x1, 0x4(%r14)
movq 0x10(%rbx), %rdi
pushq $0x40
popq %rdx
pushq $-0x1
popq %rcx
movq %r14, %rsi
xorl %r8d, %r8d
callq 0x4358
leaq -0x68(%rbp), %rdi
callq 0x5bc4
pushq $0x1
popq %r13
leaq -0x68(%rbp), %r14
pushq $0x40
popq %r15
pushq $-0x1
popq %r12
cmpl $0x1ff0, %r13d          # imm = 0x1FF0
je 0x4fbb
movq 0x8(%rbx), %rax
movslq 0x19c(%rax), %rcx
cmpq $0x63, %rcx
jg 0x4fa1
leal 0x1(%rcx), %edx
movl %edx, 0x19c(%rax)
movl %r13d, 0x1a0(%rax,%rcx,4)
movq 0x10(%rbx), %rdi
movq %r14, %rsi
movl %r15d, %edx
movl %r12d, %ecx
xorl %r8d, %r8d
callq 0x4358
incl %r13d
jmp 0x4f76
leaq -0x2b0(%rbp), %rdi
xorl %r13d, %r13d
movl $0x200, %edx            # imm = 0x200
xorl %esi, %esi
callq 0x3130
leaq -0x4b0(%rbp), %r15
movl $0x200, %edx            # imm = 0x200
movq %r15, %rdi
xorl %esi, %esi
callq 0x3130
movl $0x3c00, %r14d          # imm = 0x3C00
pushq $-0x1
popq %r12
movq 0x8(%rbx), %rsi
subl $0x1, %r14d
jb 0x5070
cmpl $0x64, 0x8(%rsi)
jl 0x5035
addq $0x8, %rsi
movl $0x194, %edx            # imm = 0x194
movq %r15, %rdi
callq 0x3190
movq 0x10(%rbx), %rdi
movq %r15, %rsi
movl $0x200, %edx            # imm = 0x200
movl %r12d, %ecx
xorl %r8d, %r8d
callq 0x4358
movq 0x8(%rbx), %rax
andl $0x0, 0x8(%rax)
xorl %ecx, %ecx
jmp 0x5057
movq 0x10(%rbx), %rdi
leaq -0x2b0(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
movl %r12d, %ecx
xorl %r8d, %r8d
callq 0x4358
movq 0x8(%rbx), %rax
movl 0x8(%rax), %ecx
leal 0x400(%r13), %edx
leal 0x1(%rcx), %esi
movl %esi, 0x8(%rax)
movslq %ecx, %rcx
movl %edx, 0xc(%rax,%rcx,4)
incl %r13d
jmp 0x4ff1
addq $0x340, %rsi            # imm = 0x340
movq %rsi, %rdi
callq 0x31d0
movq 0x8(%rbx), %rsi
movq 0x10(%rbx), %rdi
movl $0x400, %edx            # imm = 0x400
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x4358
leaq -0x68(%rbp), %rdi
callq 0x5bf6
leaq -0xa8(%rbp), %rdi
callq 0x5bf6
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x50c8
jmp 0x50c1
movq %rax, %rbx
jmp 0x50d4
jmp 0x50c8
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x5bf6
leaq -0xa8(%rbp), %rdi
callq 0x5bf6
movq %rbx, %rdi
callq 0x33c0