// File: /workspace/github_cpp_makefile_star2_O2/talyerushalmy[P]SimpleFileSystem/src/Commands.cpp
// Function: MkdirCommand::execute(FileSystem&)
void MkdirCommand::execute(FileSystem& fs)
{
	std::vector<std::string> arguments = StringTools::split(this->getArgs(), " ");

	if (arguments.size() != 1 || arguments[0] == "")
	{
		std::cout << "Usage: mkdir <path>" << std::endl;
		return;
	}

	if (fs.getDirectoryFromPath(arguments[0]))
	{
		std::cout << "The directory already exists" << std::endl;
		return;
	}

	Directory* cwd = &fs.getWorkingDirectory(); // push working directory

	if (arguments[0][0] == '/')
		fs.setWorkingDirectory(&fs.getRootDirectory());

	std::vector<std::string> names = StringTools::split(arguments[0], "/");
	uint16_t processed_names = 0;

	for (std::string name : names)
	{
		if (fs.getWorkingDirectory().childDirectoryExists(name))
		{
			CdCommand cd_command(name);
			cd_command.execute(fs);
			processed_names++;
		}
		else if (fs.getWorkingDirectory().childExists(name))
		{
			std::cout << "Invalid path" << std::endl;
			return;
		}
		else break;
	}

	for (size_t i = processed_names; i < names.size(); i++)
	{
		if (names[i] == ".")
			continue;
		if (names[i] == "..")
		{
			fs.setWorkingDirectory(fs.getDirectoryFromPath(".."));
		}
		else if (names[i] != "")
		{
			Directory* dir = new Directory(names[i], &fs.getWorkingDirectory());
			fs.getWorkingDirectory().addFile(dir);
			fs.setWorkingDirectory(dir);
		}
	}

	fs.setWorkingDirectory(cwd); // pop working directory
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %rsi, %rbx
leaq 0x28(%rdi), %rsi
leaq -0x138(%rbp), %rdi
callq 0x3210
leaq 0x5a11(%rip), %rsi      # 0xb14b
leaq -0x118(%rbp), %rdi
leaq -0x1c0(%rbp), %rdx
callq 0x30a0
leaq -0x68(%rbp), %rdi
leaq -0x138(%rbp), %rsi
leaq -0x118(%rbp), %rdx
callq 0x348c
leaq -0x118(%rbp), %rdi
callq 0x30c0
leaq -0x138(%rbp), %rdi
callq 0x30c0
movq -0x68(%rbp), %rdi
movq -0x60(%rbp), %rax
subq %rdi, %rax
leaq 0x5a1c(%rip), %r14      # 0xb1aa
cmpq $0x20, %rax
jne 0x57e2
leaq 0x59b1(%rip), %rsi      # 0xb14c
callq 0x8386
testb %al, %al
jne 0x57e2
movq -0x68(%rbp), %rsi
leaq -0xf8(%rbp), %rdi
callq 0x3210
leaq -0xf8(%rbp), %rsi
movq %rbx, -0x30(%rbp)
movq %rbx, %rdi
callq 0x8590
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x30c0
testq %rbx, %rbx
je 0x5814
leaq 0x59dc(%rip), %r14      # 0xb1be
movq 0xb7df(%rip), %rdi      # 0x10fc8
movq %r14, %rsi
callq 0x3180
movq %rax, %rdi
callq 0x30d0
leaq -0x68(%rbp), %rdi
callq 0x3614
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
callq 0x857c
movq %rax, %r14
movq -0x68(%rbp), %rsi
movq (%rsi), %rax
cmpb $0x2f, (%rax)
jne 0x5846
movq %rbx, %rdi
callq 0x8572
movq %rbx, %rdi
movq %rax, %rsi
callq 0x8586
movq -0x68(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
callq 0x3210
leaq 0x5982(%rip), %rsi      # 0xb1db
leaq -0xb8(%rbp), %rdi
leaq -0x1c0(%rbp), %rdx
callq 0x30a0
movq %r14, -0x70(%rbp)
leaq -0x50(%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 -0x50(%rbp), %r15
movq -0x48(%rbp), %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x34(%rbp)
cmpq -0x78(%rbp), %r15
je 0x59db
leaq -0x98(%rbp), %rdi
movq %r15, %rsi
callq 0x3210
movq %rbx, %rdi
callq 0x857c
leaq -0x220(%rbp), %rbx
leaq -0x200(%rbp), %r13
movq %rax, %r14
movq %rbx, %rdi
leaq -0x98(%rbp), %rsi
callq 0x3210
movq %r14, %rdi
movq %rbx, %rsi
callq 0x9408
movl %eax, %r14d
movq %rbx, %rdi
callq 0x30c0
testb %r14b, %r14b
movq -0x30(%rbp), %rbx
je 0x5956
movq %r13, %rdi
leaq -0x98(%rbp), %rsi
callq 0x3210
leaq -0x1c0(%rbp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x4f9e
movq %r13, %rdi
callq 0x30c0
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4ff4
incl -0x34(%rbp)
movq %r12, %rdi
callq 0x49c6
xorl %r13d, %r13d
jmp 0x59b9
movq %rbx, %rdi
callq 0x857c
movq %rax, %rbx
leaq -0x178(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x3210
movq %rbx, %rdi
leaq -0x178(%rbp), %r12
movq %r12, %rsi
callq 0x90c2
movl %eax, %ebx
movq %r12, %rdi
callq 0x30c0
movb $0x1, %r13b
testb %bl, %bl
je 0x59b5
movq 0xb62a(%rip), %rdi      # 0x10fc8
leaq 0x5838(%rip), %rsi      # 0xb1dd
callq 0x3180
movq %rax, %rdi
callq 0x30d0
xorl %r13d, %r13d
movq -0x30(%rbp), %rbx
leaq -0x98(%rbp), %rdi
callq 0x30c0
addq $0x20, %r15
testb %r14b, %r14b
jne 0x58b2
testb %r13b, %r13b
je 0x5b03
movzwl -0x34(%rbp), %r15d
movl %r15d, %ebx
shll $0x5, %ebx
leaq -0x1e0(%rbp), %r14
movq -0x50(%rbp), %rdi
movq -0x48(%rbp), %rax
subq %rdi, %rax
sarq $0x5, %rax
cmpq %rax, %r15
jae 0x5af6
addq %rbx, %rdi
leaq 0x57dc(%rip), %rsi      # 0xb1eb
callq 0x8386
testb %al, %al
jne 0x5aea
movq -0x50(%rbp), %rdi
addq %rbx, %rdi
leaq 0x57c0(%rip), %r13      # 0xb1ea
movq %r13, %rsi
callq 0x8386
testb %al, %al
je 0x5a77
leaq -0x158(%rbp), %r12
movq %r12, %rdi
movq %r13, %rsi
leaq -0x1c0(%rbp), %rdx
callq 0x30a0
movq -0x30(%rbp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x8590
movq %r13, %rdi
movq %rax, %rsi
callq 0x8586
leaq -0x158(%rbp), %rdi
callq 0x30c0
jmp 0x5aea
movq -0x50(%rbp), %rdi
addq %rbx, %rdi
leaq 0x56c7(%rip), %rsi      # 0xb14c
callq 0x8396
testb %al, %al
je 0x5aea
pushq $0x48
popq %rdi
callq 0x3190
movq %rax, %r13
movq -0x50(%rbp), %rsi
addq %rbx, %rsi
movq %r14, %rdi
callq 0x3210
movq -0x30(%rbp), %rdi
callq 0x857c
movq %r14, %r12
movq %r13, %rdi
movq %r14, %rsi
movq %rax, %rdx
callq 0x8a9e
movq %r12, %rdi
callq 0x30c0
movq -0x30(%rbp), %rdi
callq 0x857c
movq %rax, %rdi
movq %r13, %rsi
callq 0x9030
movq -0x30(%rbp), %rdi
movq %r13, %rsi
callq 0x8586
incq %r15
addq $0x20, %rbx
jmp 0x59ed
movq -0x30(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0x8586
leaq -0x50(%rbp), %rdi
callq 0x3614
jmp 0x57f9
jmp 0x5bcf
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0x30c0
jmp 0x5b2a
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
jmp 0x5b47
jmp 0x5be5
jmp 0x5be5
movq %rax, %rbx
leaq -0xf8(%rbp), %rdi
callq 0x30c0
jmp 0x5c02
movq %rax, %rbx
jmp 0x5b65
movq %rax, %rbx
leaq -0x1e0(%rbp), %rdi
callq 0x30c0
movq %r13, %rdi
callq 0x3170
jmp 0x5bf9
jmp 0x5bcf
movq %rax, %rbx
leaq -0x1c0(%rbp), %rdi
callq 0x49c6
jmp 0x5bed
movq %rax, %rbx
leaq -0x200(%rbp), %rdi
jmp 0x5bde
movq %rax, %rbx
leaq -0x178(%rbp), %rdi
jmp 0x5bde
jmp 0x5bea
movq %rax, %rbx
leaq -0x118(%rbp), %rdi
callq 0x30c0
jmp 0x5bb3
movq %rax, %rbx
leaq -0x138(%rbp), %rdi
callq 0x30c0
jmp 0x5c0b
jmp 0x5bcf
movq %rax, %rbx
leaq -0x158(%rbp), %rdi
jmp 0x5bf4
movq %rax, %rbx
jmp 0x5bf9
movq %rax, %rbx
leaq -0x220(%rbp), %rdi
callq 0x30c0
jmp 0x5bed
movq %rax, %rbx
jmp 0x5c02
movq %rax, %rbx
leaq -0x98(%rbp), %rdi
callq 0x30c0
leaq -0x50(%rbp), %rdi
callq 0x3614
leaq -0x68(%rbp), %rdi
callq 0x3614
movq %rbx, %rdi
callq 0x3290
nop