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

	if (arguments.size() != 2)
	{
		std::cout << "Usage: mv <source-path/file-name> <destination-path>" << std::endl;
		return;
	}
	if (StringTools::trim(arguments[0]) == "" || StringTools::trim(arguments[1]) == "")
	{
		std::cout << "Usage: mv <source-path/file-name> <destination-path>" << std::endl;
		return;
	}

	std::string src_name = StringTools::split(StringTools::trim(arguments[0]), "/").back();
	std::string src_dir = StringTools::trim(arguments[0]).substr(0, arguments[0].length() - src_name.length());

	Directory* src = fs.getDirectoryFromPath(src_dir);
	if (StringTools::trim(src_dir) == "")
		src = &fs.getWorkingDirectory();
	Directory* dest = fs.getDirectoryFromPath(arguments[1]);
	if (StringTools::trim(arguments[1]) == "")
		dest = &fs.getWorkingDirectory();

	if (!src->childExists(src_name) || !dest)
	{
		std::cout << "No such file or directory" << std::endl;
		return;
	}

	BaseFile* bf = src->getChildByName(src_name);
	if (Directory* d = dynamic_cast<Directory*>(bf))
	{
		Directory* check_cwd_or_parents = &fs.getWorkingDirectory();
		while (check_cwd_or_parents->getParent() != NULL)
		{
			if (d == check_cwd_or_parents)
			{
				std::cout << "Can't move directory" << std::endl;
				return;
			}
			check_cwd_or_parents = check_cwd_or_parents->getParent();
		}
		Directory* new_d = new Directory(*d);
		dest->addFile(new_d);
	}
	else if (File* f = dynamic_cast<File*>(bf))
	{
		File* new_f = new File(*f);
		dest->addFile(new_f);
	}
	src->removeFile(src_name);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rsi, %rbx
leaq 0x28(%rdi), %rsi
leaq -0x1c0(%rbp), %rdi
callq 0x3210
leaq 0x4807(%rip), %rsi      # 0xb14b
leaq -0x1a0(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0x30a0
leaq -0x40(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
leaq -0x1a0(%rbp), %rdx
callq 0x348c
leaq -0x1a0(%rbp), %rdi
callq 0x30c0
leaq -0x1c0(%rbp), %rdi
callq 0x30c0
movq -0x40(%rbp), %rsi
movq -0x38(%rbp), %rax
subq %rsi, %rax
cmpq $0x40, %rax
jne 0x69f9
leaq -0x60(%rbp), %rdi
callq 0x3574
leaq 0x47a8(%rip), %rsi      # 0xb14c
leaq -0x60(%rbp), %rdi
callq 0x8386
testb %al, %al
je 0x69bc
leaq -0x60(%rbp), %rdi
callq 0x30c0
jmp 0x69f9
movq -0x40(%rbp), %rsi
addq $0x20, %rsi
leaq -0x80(%rbp), %rdi
callq 0x3574
leaq 0x4778(%rip), %rsi      # 0xb14c
leaq -0x80(%rbp), %r14
movq %r14, %rdi
callq 0x8386
movl %eax, %r15d
movq %r14, %rdi
callq 0x30c0
leaq -0x60(%rbp), %rdi
callq 0x30c0
testb %r15b, %r15b
je 0x6a2f
movq 0xa5c8(%rip), %rdi      # 0x10fc8
leaq 0x487c(%rip), %rsi      # 0xb283
callq 0x3180
movq %rax, %rdi
callq 0x30d0
leaq -0x40(%rbp), %rdi
callq 0x3614
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq -0x40(%rbp), %rsi
leaq -0x180(%rbp), %rdi
callq 0x3574
leaq 0x4795(%rip), %rsi      # 0xb1db
leaq -0x160(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x30a0
leaq -0x80(%rbp), %rdi
leaq -0x180(%rbp), %rsi
leaq -0x160(%rbp), %rdx
callq 0x348c
movq -0x78(%rbp), %rsi
addq $-0x20, %rsi
leaq -0x60(%rbp), %rdi
callq 0x3210
leaq -0x80(%rbp), %rdi
callq 0x3614
leaq -0x160(%rbp), %rdi
callq 0x30c0
leaq -0x180(%rbp), %rdi
callq 0x30c0
movq -0x40(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x3574
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rcx
subq -0x58(%rbp), %rcx
leaq -0x80(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
xorl %edx, %edx
callq 0x31a0
leaq -0xa0(%rbp), %rdi
callq 0x30c0
leaq -0x140(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x3210
leaq -0x140(%rbp), %rsi
movq %rbx, %rdi
callq 0x8590
movq %rax, %r14
leaq -0x140(%rbp), %rdi
callq 0x30c0
leaq -0xa0(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0x3574
leaq 0x462b(%rip), %rsi      # 0xb14c
leaq -0xa0(%rbp), %r15
movq %r15, %rdi
callq 0x8386
movl %eax, %r12d
movq %r15, %rdi
callq 0x30c0
testb %r12b, %r12b
je 0x6b4b
movq %rbx, %rdi
callq 0x857c
movq %rax, %r14
movq -0x40(%rbp), %rsi
addq $0x20, %rsi
leaq -0x120(%rbp), %rdi
callq 0x3210
leaq -0x120(%rbp), %rsi
movq %rbx, %rdi
callq 0x8590
movq %rax, %r15
leaq -0x120(%rbp), %rdi
callq 0x30c0
movq -0x40(%rbp), %rsi
addq $0x20, %rsi
leaq -0xa0(%rbp), %rdi
callq 0x3574
leaq 0x45b4(%rip), %rsi      # 0xb14c
leaq -0xa0(%rbp), %r12
movq %r12, %rdi
callq 0x8386
movl %eax, %r13d
movq %r12, %rdi
callq 0x30c0
testb %r13b, %r13b
je 0x6bc2
movq %rbx, %rdi
callq 0x857c
movq %rax, %r15
leaq -0x100(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x3210
leaq -0x100(%rbp), %rsi
movq %r14, %rdi
callq 0x90c2
movl %eax, %r12d
testq %r15, %r15
setne %r13b
leaq -0x100(%rbp), %rdi
callq 0x30c0
testb %r12b, %r13b
je 0x6c85
leaq -0xe0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x3210
leaq -0xe0(%rbp), %rsi
movq %r14, %rdi
callq 0x94a4
movq %rax, %r13
leaq -0xe0(%rbp), %rdi
callq 0x30c0
testq %r13, %r13
je 0x6d09
leaq 0xa12a(%rip), %rsi      # 0x10d68
leaq 0xa133(%rip), %rdx      # 0x10d78
movq %r13, %rdi
xorl %ecx, %ecx
callq 0x31c0
testq %rax, %rax
je 0x6ca5
movq %rax, %r12
movq %rbx, %rdi
callq 0x857c
movq %rax, %rbx
movq %rbx, %rdi
callq 0x901c
testq %rax, %rax
je 0x6ce8
cmpq %rbx, %r12
je 0x6d4b
movq %rbx, %rdi
callq 0x901c
movq %rax, %rbx
jmp 0x6c62
movq 0xa33c(%rip), %rdi      # 0x10fc8
leaq 0x45d6(%rip), %rsi      # 0xb269
callq 0x3180
movq %rax, %rdi
callq 0x30d0
jmp 0x6d34
leaq 0xa0bc(%rip), %rsi      # 0x10d68
leaq 0xa0dd(%rip), %rdx      # 0x10d90
movq %r13, %rdi
xorl %ecx, %ecx
callq 0x31c0
testq %rax, %rax
je 0x6d09
movq %rax, %rbx
pushq $0x30
popq %rdi
callq 0x3190
movq %rax, %r12
movq %rax, %rdi
movq %rbx, %rsi
callq 0x83c4
movq %r15, %rdi
movq %r12, %rsi
callq 0x9030
jmp 0x6d09
pushq $0x48
popq %rdi
callq 0x3190
movq %rax, %r13
movq %rax, %rdi
movq %r12, %rsi
callq 0x8b0c
movq %r15, %rdi
movq %r13, %rsi
callq 0x9030
leaq -0xc0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x3210
leaq -0xc0(%rbp), %rsi
movq %r14, %rdi
callq 0x9142
leaq -0xc0(%rbp), %rdi
callq 0x30c0
leaq -0x80(%rbp), %rdi
callq 0x30c0
leaq -0x60(%rbp), %rdi
callq 0x30c0
jmp 0x6a14
movq 0xa276(%rip), %rdi      # 0x10fc8
leaq 0x455f(%rip), %rsi      # 0xb2b8
callq 0x3180
movq %rax, %rdi
callq 0x30d0
jmp 0x6d34
movq %rax, %rbx
movq %r12, %rdi
jmp 0x6d76
movq %rax, %rbx
movq %r13, %rdi
callq 0x3170
jmp 0x6e1f
jmp 0x6e1c
jmp 0x6e1c
jmp 0x6e1c
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
jmp 0x6dcf
movq %rax, %rbx
leaq -0xe0(%rbp), %rdi
jmp 0x6dcf
jmp 0x6e1c
movq %rax, %rbx
leaq -0x100(%rbp), %rdi
jmp 0x6dcf
jmp 0x6e1c
movq %rax, %rbx
leaq -0x120(%rbp), %rdi
jmp 0x6dcf
jmp 0x6e1c
movq %rax, %rbx
leaq -0x140(%rbp), %rdi
callq 0x30c0
jmp 0x6e1f
movq %rax, %rbx
leaq -0xa0(%rbp), %rdi
jmp 0x6e23
jmp 0x6e13
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x3614
jmp 0x6df5
movq %rax, %rbx
leaq -0x160(%rbp), %rdi
callq 0x30c0
jmp 0x6e06
movq %rax, %rbx
leaq -0x180(%rbp), %rdi
jmp 0x6e2c
jmp 0x6e55
jmp 0x6e1c
movq %rax, %rbx
jmp 0x6e28
jmp 0x6e1c
jmp 0x6e55
movq %rax, %rbx
leaq -0x80(%rbp), %rdi
callq 0x30c0
leaq -0x60(%rbp), %rdi
callq 0x30c0
jmp 0x6e58
movq %rax, %rbx
leaq -0x1a0(%rbp), %rdi
callq 0x30c0
jmp 0x6e47
movq %rax, %rbx
leaq -0x1c0(%rbp), %rdi
callq 0x30c0
jmp 0x6e61
movq %rax, %rbx
leaq -0x40(%rbp), %rdi
callq 0x3614
movq %rbx, %rdi
callq 0x3290
nop