// File: /workspace/github_cpp_makefile_star2_O2/talyerushalmy[P]SimpleFileSystem/src/Commands.cpp
// Function: LsCommand::execute(FileSystem&)
void LsCommand::execute(FileSystem& fs)
{
	std::vector<std::string> words = StringTools::split(this->getArgs(), " ");
	
	if (words.size() != 1 && (words.size() == 2 && words[0] != "-s"))
	{
		std::cout << "Usage: ls [-s] <path>" << std::endl;
		return;
	}

	Directory* dir = fs.getDirectoryFromPath(words.back());
	if (words.back() == "" || words.back() == "-s")
		dir = &fs.getWorkingDirectory();

	if (!dir)
	{
		std::cout << "The system cannot find the path specified" << std::endl;
		return;
	}

	if (words[0] == "-s")
	{
		dir->sortByName();
		dir->sortBySize();
	}
	else
	{
		dir->sortBySize();
		dir->sortByName();
	}

	for (BaseFile* bf : dir->getChildren())
	{
		if (Directory* d = dynamic_cast<Directory*>(bf))
			std::cout << "DIR" << '\t' << d->getName() << '\t' << d->getSize() << std::endl;
		else
			std::cout << "FILE" << '\t' << bf->getName() << '\t' << bf->getSize() << std::endl;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %rbx
leaq 0x28(%rdi), %rsi
leaq -0xd8(%rbp), %rdi
callq 0x3210
leaq 0x5e91(%rip), %rsi      # 0xb14b
leaq -0xb8(%rbp), %rdi
leaq -0x78(%rbp), %rdx
callq 0x30a0
leaq -0x40(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
leaq -0xb8(%rbp), %rdx
callq 0x348c
leaq -0xb8(%rbp), %rdi
callq 0x30c0
leaq -0xd8(%rbp), %rdi
callq 0x30c0
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq %rsi, %rax
subq %rdi, %rax
cmpq $0x40, %rax
jne 0x5341
leaq 0x5e74(%rip), %rsi      # 0xb188
callq 0x8396
testb %al, %al
je 0x533d
movq 0xbca4(%rip), %rdi      # 0x10fc8
leaq 0x5e60(%rip), %rsi      # 0xb18b
callq 0x3180
movq %rax, %rdi
callq 0x30d0
jmp 0x550c
movq -0x38(%rbp), %rsi
addq $-0x20, %rsi
leaq -0x98(%rbp), %rdi
callq 0x3210
leaq -0x98(%rbp), %rsi
movq %rbx, %rdi
callq 0x8590
movq %rax, %r14
leaq -0x98(%rbp), %rdi
callq 0x30c0
movq -0x38(%rbp), %rdi
addq $-0x20, %rdi
leaq 0x5dce(%rip), %rsi      # 0xb14c
callq 0x8386
testb %al, %al
jne 0x53a3
movq -0x38(%rbp), %rdi
addq $-0x20, %rdi
leaq 0x5df2(%rip), %rsi      # 0xb188
callq 0x8386
testb %al, %al
je 0x5527
movq %rbx, %rdi
callq 0x857c
movq %rax, %r14
movq -0x40(%rbp), %rdi
leaq 0x5dcf(%rip), %rsi      # 0xb188
callq 0x8386
testb %al, %al
je 0x53d4
movq %r14, %rdi
callq 0x923a
movq %r14, %rdi
callq 0x9250
jmp 0x53e4
movq %r14, %rdi
callq 0x9250
movq %r14, %rdi
callq 0x923a
leaq -0x58(%rbp), %rdi
movq %r14, %rsi
callq 0x9266
movq -0x58(%rbp), %rbx
movq -0x50(%rbp), %r15
leaq -0x78(%rbp), %r12
cmpq %r15, %rbx
je 0x5503
movq (%rbx), %r13
testq %r13, %r13
je 0x5492
movq %r13, %rdi
leaq 0xb94d(%rip), %rsi      # 0x10d68
leaq 0xb956(%rip), %rdx      # 0x10d78
xorl %ecx, %ecx
callq 0x31c0
testq %rax, %rax
je 0x5492
movq %rax, %r14
movq 0xbb90(%rip), %rdi      # 0x10fc8
leaq 0x5d62(%rip), %rsi      # 0xb1a1
callq 0x3180
movq %rax, %rdi
pushq $0x9
popq %rsi
callq 0x31d0
movq %rax, %r13
movq %r12, %rdi
movq %r14, %rsi
callq 0x8a0a
movq %r13, %rdi
movq %r12, %rsi
callq 0x3150
movq %rax, %rdi
pushq $0x9
popq %rsi
callq 0x31d0
movq %rax, %r13
movq (%r14), %rax
movq %r14, %rdi
callq *(%rax)
movq %r13, %rdi
movl %eax, %esi
callq 0x3280
movq %rax, %rdi
callq 0x30d0
jmp 0x54f2
movq 0xbb2f(%rip), %rdi      # 0x10fc8
leaq 0x5d05(%rip), %rsi      # 0xb1a5
callq 0x3180
movq %rax, %rdi
pushq $0x9
popq %rsi
callq 0x31d0
movq %rax, %r14
movq %r12, %rdi
movq %r13, %rsi
callq 0x8a0a
movq %r14, %rdi
movq %r12, %rsi
callq 0x3150
movq %rax, %rdi
pushq $0x9
popq %rsi
callq 0x31d0
movq %rax, %r14
movq (%r13), %rax
movq %r13, %rdi
callq *(%rax)
movq %r14, %rdi
movl %eax, %esi
callq 0x3280
movq %rax, %rdi
callq 0x30d0
movq %r12, %rdi
callq 0x30c0
addq $0x8, %rbx
jmp 0x53fc
leaq -0x58(%rbp), %rdi
callq 0x84e6
leaq -0x40(%rbp), %rdi
callq 0x3614
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r14, %r14
jne 0x53ae
movq 0xba91(%rip), %rdi      # 0x10fc8
leaq 0x5c20(%rip), %rsi      # 0xb15e
callq 0x3180
movq %rax, %rdi
callq 0x30d0
jmp 0x550c
jmp 0x5584
jmp 0x5584
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x30c0
jmp 0x55a9
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0x30c0
jmp 0x5576
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
callq 0x30c0
jmp 0x55b2
movq %rax, %rbx
jmp 0x55a9
jmp 0x558f
jmp 0x558f
jmp 0x5594
movq %rax, %rbx
jmp 0x55a0
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x30c0
leaq -0x58(%rbp), %rdi
callq 0x84e6
leaq -0x40(%rbp), %rdi
callq 0x3614
movq %rbx, %rdi
callq 0x3290