// File: 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
subq $0x3a0, %rsp            # imm = 0x3A0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x7c70
leaq -0x69(%rbp), %rdi
movq %rdi, -0x2b0(%rbp)
callq 0x3310
movq -0x2b0(%rbp), %rdx
leaq 0x950d(%rip), %rsi      # 0x1414b
leaq -0x68(%rbp), %rdi
callq 0x3260
jmp 0xac49
leaq -0x28(%rbp), %rdi
leaq -0x48(%rbp), %rsi
leaq -0x68(%rbp), %rdx
callq 0x3550
jmp 0xac5c
leaq -0x68(%rbp), %rdi
callq 0x3130
leaq -0x69(%rbp), %rdi
callq 0x3200
leaq -0x48(%rbp), %rdi
callq 0x3130
leaq -0x28(%rbp), %rdi
callq 0x4020
cmpq $0x2, %rax
je 0xad12
movq 0x11337(%rip), %rdi     # 0x1bfc8
leaq 0x95eb(%rip), %rsi      # 0x14283
callq 0x31b0
movq %rax, -0x2b8(%rbp)
jmp 0xaca6
movq -0x2b8(%rbp), %rdi
movq 0x11304(%rip), %rsi     # 0x1bfb8
callq 0x31e0
jmp 0xacbb
movl $0x1, -0x80(%rbp)
jmp 0xb661
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xacea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x3130
leaq -0x69(%rbp), %rdi
callq 0x3200
leaq -0x48(%rbp), %rdi
callq 0x3130
jmp 0xb6ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xb6a3
xorl %eax, %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
callq 0xd9b0
movq %rax, %rsi
movb $0x0, -0xc1(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x36c0
jmp 0xad37
leaq 0x940e(%rip), %rsi      # 0x1414c
leaq -0xa0(%rbp), %rdi
callq 0xd980
movb %al, -0x2b9(%rbp)
jmp 0xad52
movb -0x2b9(%rbp), %cl
movb $0x1, %al
testb $0x1, %cl
movb %al, -0x2ba(%rbp)
jne 0xadb6
jmp 0xad67
leaq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0xd9b0
movq %rax, %rsi
leaq -0xc0(%rbp), %rdi
callq 0x36c0
jmp 0xad86
movb $0x1, -0xc1(%rbp)
leaq 0x93b8(%rip), %rsi      # 0x1414c
leaq -0xc0(%rbp), %rdi
callq 0xd980
movb %al, -0x2bb(%rbp)
jmp 0xada8
movb -0x2bb(%rbp), %al
movb %al, -0x2ba(%rbp)
jmp 0xadb6
movb -0x2ba(%rbp), %al
movb %al, -0x2bc(%rbp)
testb $0x1, -0xc1(%rbp)
jne 0xadcd
jmp 0xadd9
leaq -0xc0(%rbp), %rdi
callq 0x3130
leaq -0xa0(%rbp), %rdi
callq 0x3130
movb -0x2bc(%rbp), %al
testb $0x1, %al
jne 0xadf4
jmp 0xae75
movq 0x111cd(%rip), %rdi     # 0x1bfc8
leaq 0x9481(%rip), %rsi      # 0x14283
callq 0x31b0
movq %rax, -0x2c8(%rbp)
jmp 0xae10
movq -0x2c8(%rbp), %rdi
movq 0x1119a(%rip), %rsi     # 0x1bfb8
callq 0x31e0
jmp 0xae25
movl $0x1, -0x80(%rbp)
jmp 0xb661
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xae64
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
testb $0x1, -0xc1(%rbp)
jne 0xae56
jmp 0xae62
leaq -0xc0(%rbp), %rdi
callq 0x3130
jmp 0xae64
leaq -0xa0(%rbp), %rdi
callq 0x3130
jmp 0xb6a3
xorl %eax, %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
callq 0xd9b0
movq %rax, %rsi
leaq -0x120(%rbp), %rdi
callq 0x36c0
jmp 0xae93
leaq -0x141(%rbp), %rdi
movq %rdi, -0x2d0(%rbp)
callq 0x3310
movq -0x2d0(%rbp), %rdx
leaq 0x9327(%rip), %rsi      # 0x141db
leaq -0x140(%rbp), %rdi
callq 0x3260
jmp 0xaec2
leaq -0x100(%rbp), %rdi
leaq -0x120(%rbp), %rsi
leaq -0x140(%rbp), %rdx
callq 0x3550
jmp 0xaede
leaq -0x100(%rbp), %rdi
callq 0xda00
movq %rax, %rsi
leaq -0xe8(%rbp), %rdi
callq 0x30e0
jmp 0xaefb
leaq -0x100(%rbp), %rdi
callq 0x37e0
leaq -0x140(%rbp), %rdi
callq 0x3130
leaq -0x141(%rbp), %rdi
callq 0x3200
leaq -0x120(%rbp), %rdi
callq 0x3130
xorl %eax, %eax
movl %eax, %esi
leaq -0x28(%rbp), %rdi
callq 0xd9b0
movq %rax, %rsi
leaq -0x188(%rbp), %rdi
callq 0x36c0
jmp 0xaf49
xorl %eax, %eax
movl %eax, %esi
movq %rsi, -0x2d8(%rbp)
leaq -0x28(%rbp), %rdi
callq 0xd9b0
movq %rax, %rdi
callq 0x32a0
movq %rax, -0x2e0(%rbp)
leaq -0xe8(%rbp), %rdi
callq 0x32a0
movq -0x2e0(%rbp), %rcx
movq -0x2d8(%rbp), %rdx
subq %rax, %rcx
leaq -0x168(%rbp), %rdi
leaq -0x188(%rbp), %rsi
callq 0x31d0
jmp 0xaf9e
leaq -0x188(%rbp), %rdi
callq 0x3130
movq -0x10(%rbp), %rax
movq %rax, -0x2e8(%rbp)
leaq -0x1b0(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x30e0
jmp 0xafca
movq -0x2e8(%rbp), %rdi
leaq -0x1b0(%rbp), %rsi
callq 0xe1f0
movq %rax, -0x2f0(%rbp)
jmp 0xafe6
leaq -0x1b0(%rbp), %rdi
callq 0x3130
movq -0x2f0(%rbp), %rax
movq %rax, -0x190(%rbp)
leaq -0x1d0(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x36c0
jmp 0xb015
leaq 0x9130(%rip), %rsi      # 0x1414c
leaq -0x1d0(%rbp), %rdi
callq 0xd980
movb %al, -0x2f1(%rbp)
jmp 0xb030
leaq -0x1d0(%rbp), %rdi
callq 0x3130
movb -0x2f1(%rbp), %al
testb $0x1, %al
jne 0xb04b
jmp 0xb146
movq -0x10(%rbp), %rdi
callq 0xe1b0
movq %rax, -0x300(%rbp)
jmp 0xb05d
movq -0x300(%rbp), %rax
movq %rax, -0x190(%rbp)
jmp 0xb146
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xb0b0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xb0a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x37e0
leaq -0x140(%rbp), %rdi
callq 0x3130
leaq -0x141(%rbp), %rdi
callq 0x3200
leaq -0x120(%rbp), %rdi
callq 0x3130
jmp 0xb6a3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xb697
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x188(%rbp), %rdi
callq 0x3130
jmp 0xb697
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
jmp 0xb68b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x3130
jmp 0xb68b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x1d0(%rbp), %rdi
callq 0x3130
jmp 0xb68b
movq -0x10(%rbp), %rax
movq %rax, -0x308(%rbp)
leaq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0xd9b0
movq %rax, %rsi
leaq -0x1f8(%rbp), %rdi
callq 0x30e0
jmp 0xb170
movq -0x308(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
callq 0xe1f0
movq %rax, -0x310(%rbp)
jmp 0xb18c
leaq -0x1f8(%rbp), %rdi
callq 0x3130
movq -0x310(%rbp), %rax
movq %rax, -0x1d8(%rbp)
leaq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0xd9b0
movq %rax, %rsi
leaq -0x218(%rbp), %rdi
callq 0x36c0
jmp 0xb1c5
leaq 0x8f80(%rip), %rsi      # 0x1414c
leaq -0x218(%rbp), %rdi
callq 0xd980
movb %al, -0x311(%rbp)
jmp 0xb1e0
leaq -0x218(%rbp), %rdi
callq 0x3130
movb -0x311(%rbp), %al
testb $0x1, %al
jne 0xb1f8
jmp 0xb254
movq -0x10(%rbp), %rdi
callq 0xe1b0
movq %rax, -0x320(%rbp)
jmp 0xb20a
movq -0x320(%rbp), %rax
movq %rax, -0x1d8(%rbp)
jmp 0xb254
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x1f8(%rbp), %rdi
callq 0x3130
jmp 0xb68b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x3130
jmp 0xb68b
movq -0x190(%rbp), %rax
movq %rax, -0x328(%rbp)
leaq -0x238(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x30e0
jmp 0xb277
movq -0x328(%rbp), %rdi
leaq -0x238(%rbp), %rsi
callq 0xf6a0
movb %al, -0x329(%rbp)
jmp 0xb292
movb -0x329(%rbp), %cl
movb $0x1, %al
testb $0x1, %cl
movb %al, -0x32a(%rbp)
jne 0xb2a7
jmp 0xb2ba
cmpq $0x0, -0x1d8(%rbp)
setne %al
xorb $-0x1, %al
movb %al, -0x32a(%rbp)
movb -0x32a(%rbp), %al
movb %al, -0x32b(%rbp)
leaq -0x238(%rbp), %rdi
callq 0x3130
movb -0x32b(%rbp), %al
testb $0x1, %al
jne 0xb2de
jmp 0xb338
movq 0x10ce3(%rip), %rdi     # 0x1bfc8
leaq 0x8f7d(%rip), %rsi      # 0x14269
callq 0x31b0
movq %rax, -0x338(%rbp)
jmp 0xb2fa
movq -0x338(%rbp), %rdi
movq 0x10cb0(%rip), %rsi     # 0x1bfb8
callq 0x31e0
jmp 0xb30f
movl $0x1, -0x80(%rbp)
jmp 0xb649
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x238(%rbp), %rdi
callq 0x3130
jmp 0xb68b
movq -0x190(%rbp), %rax
movq %rax, -0x340(%rbp)
leaq -0x260(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x30e0
jmp 0xb35b
movq -0x340(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0xfde0
movq %rax, -0x348(%rbp)
jmp 0xb377
leaq -0x260(%rbp), %rdi
callq 0x3130
movq -0x348(%rbp), %rax
movq %rax, -0x240(%rbp)
movq -0x240(%rbp), %rax
movq %rax, -0x350(%rbp)
cmpq $0x0, %rax
je 0xb3e9
movq -0x350(%rbp), %rdi
leaq 0x109ad(%rip), %rsi     # 0x1bd60
leaq 0x109b6(%rip), %rdx     # 0x1bd70
xorl %eax, %eax
movl %eax, %ecx
callq 0x3210
movq %rax, -0x358(%rbp)
jmp 0xb3f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x260(%rbp), %rdi
callq 0x3130
jmp 0xb68b
xorl %eax, %eax
movq %rax, -0x358(%rbp)
jmp 0xb3f4
movq -0x358(%rbp), %rax
movq %rax, -0x268(%rbp)
cmpq $0x0, -0x268(%rbp)
je 0xb534
movq -0x10(%rbp), %rdi
callq 0xe1b0
movq %rax, -0x360(%rbp)
jmp 0xb422
movq -0x360(%rbp), %rax
movq %rax, -0x270(%rbp)
movq -0x270(%rbp), %rdi
callq 0xf570
movq %rax, -0x368(%rbp)
jmp 0xb445
movq -0x368(%rbp), %rax
cmpq $0x0, %rax
je 0xb4c7
movq -0x268(%rbp), %rax
cmpq -0x270(%rbp), %rax
jne 0xb49f
movq 0x10b5f(%rip), %rdi     # 0x1bfc8
leaq 0x8e48(%rip), %rsi      # 0x142b8
callq 0x31b0
movq %rax, -0x370(%rbp)
jmp 0xb47e
movq -0x370(%rbp), %rdi
movq 0x10b2c(%rip), %rsi     # 0x1bfb8
callq 0x31e0
jmp 0xb493
movl $0x1, -0x80(%rbp)
jmp 0xb649
movq -0x270(%rbp), %rdi
callq 0xf570
movq %rax, -0x378(%rbp)
jmp 0xb4b4
movq -0x378(%rbp), %rax
movq %rax, -0x270(%rbp)
jmp 0xb430
movl $0x48, %edi
callq 0x31c0
movq %rax, -0x380(%rbp)
jmp 0xb4da
movq -0x380(%rbp), %rdi
movq -0x268(%rbp), %rsi
callq 0xecd0
jmp 0xb4ef
movq -0x380(%rbp), %rax
movq %rax, -0x278(%rbp)
movq -0x1d8(%rbp), %rdi
movq -0x278(%rbp), %rsi
callq 0xf5b0
jmp 0xb512
jmp 0xb5fe
movq -0x380(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
callq 0x31a0
jmp 0xb68b
movq -0x240(%rbp), %rax
movq %rax, -0x388(%rbp)
cmpq $0x0, %rax
je 0xb56f
movq -0x388(%rbp), %rdi
leaq 0x1080a(%rip), %rsi     # 0x1bd60
leaq 0x1082b(%rip), %rdx     # 0x1bd88
xorl %eax, %eax
movl %eax, %ecx
callq 0x3210
movq %rax, -0x390(%rbp)
jmp 0xb57a
xorl %eax, %eax
movq %rax, -0x390(%rbp)
jmp 0xb57a
movq -0x390(%rbp), %rax
movq %rax, -0x280(%rbp)
cmpq $0x0, -0x280(%rbp)
je 0xb5fc
movl $0x30, %edi
callq 0x31c0
movq %rax, -0x398(%rbp)
jmp 0xb5a5
movq -0x398(%rbp), %rdi
movq -0x280(%rbp), %rsi
callq 0xdc40
jmp 0xb5ba
movq -0x398(%rbp), %rax
movq %rax, -0x288(%rbp)
movq -0x1d8(%rbp), %rdi
movq -0x288(%rbp), %rsi
callq 0xf5b0
jmp 0xb5dd
jmp 0xb5fc
movq -0x398(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
callq 0x31a0
jmp 0xb68b
jmp 0xb5fe
movq -0x190(%rbp), %rax
movq %rax, -0x3a0(%rbp)
leaq -0x2a8(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x30e0
jmp 0xb621
movq -0x3a0(%rbp), %rdi
leaq -0x2a8(%rbp), %rsi
callq 0xf780
jmp 0xb636
leaq -0x2a8(%rbp), %rdi
callq 0x3130
movl $0x0, -0x80(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x3130
leaq -0xe8(%rbp), %rdi
callq 0x3130
leaq -0x28(%rbp), %rdi
callq 0x37e0
addq $0x3a0, %rsp            # imm = 0x3A0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x2a8(%rbp), %rdi
callq 0x3130
leaq -0x168(%rbp), %rdi
callq 0x3130
leaq -0xe8(%rbp), %rdi
callq 0x3130
leaq -0x28(%rbp), %rdi
callq 0x37e0
movq -0x78(%rbp), %rdi
callq 0x3300
nopw %cs:(%rax,%rax)