// File: /workspace/github_cpp_makefile_star2_O2/Matan-Vinkler[P]Basic-File-System/myfs_main.cpp
// Function: main
int main(int argc, char **argv) {

	if (argc != 2) {
		std::cerr << "Please provide the file to operate on" << std::endl;
		return -1;
	}

	BlockDeviceSimulator *blkdevptr = new BlockDeviceSimulator(argv[1]);
	MyFs myfs(blkdevptr);
	bool exit = false;

	std::cout << "Welcome to " << FS_NAME << std::endl;
	std::cout << "To get help, please type 'help' on the prompt below." << std::endl;
	std::cout << std::endl;

	while (!exit) {
		try {
			std::string cmdline;
			std::cout << FS_NAME << "$ ";
			std::getline(std::cin, cmdline, '\n');
			if (cmdline == std::string(""))
				continue;

			std::vector<std::string> cmd = split_cmd(cmdline);

			if (cmd[0] == LIST_CMD) {
				MyFs::dir_list dlist;
				if (cmd.size() == 1)
					dlist = myfs.list_dir("/");
				else if (cmd.size() == 2)
					dlist = myfs.list_dir(cmd[1]);
				else
					std::cout << LIST_CMD << ": one or zero arguments requested" << std::endl;

				for (size_t i=0; i < dlist.size(); i++) {
					std::cout << std::setw(15) << std::left
						<< dlist[i].name + (dlist[i].is_dir ? "/":"")
						<< std::setw(10) << std::right
						<< dlist[i].file_size << std::endl;
				}
			} else if (cmd[0] == EXIT_CMD) {
				exit = true;
			} else if (cmd[0] == HELP_CMD) {
				std::cout << HELP_STRING;
			} else if (cmd[0] == CREATE_FILE_CMD) {
				if (cmd.size() == 2)
					myfs.create_file(cmd[1], false);
				else
					std::cout << CREATE_FILE_CMD << ": file path requested" << std::endl;
			} else if (cmd[0] == CONTENT_CMD) {
				if (cmd.size() == 2)
					std::cout << myfs.get_content(cmd[1]) << std::endl;
				else
					std::cout << CONTENT_CMD << ": file path requested" << std::endl;
			} else if (cmd[0] == TREE_CMD) {
				recursive_print(myfs, "");
			} else if (cmd[0] == EDIT_CMD) {
				if (cmd.size() == 2) {
					std::cout << "Enter new file content" << std::endl;
					std::string content;
					std::string curr_line;
					std::getline(std::cin, curr_line);
					while (curr_line != "") {
						content += curr_line + "\n";
						std::getline(std::cin, curr_line);
					}
					myfs.set_content(cmd[1], content);
				} else {
					std::cout << EDIT_CMD << ": file path requested" << std::endl;
				}
			} else if (cmd[0] == CREATE_DIR_CMD) {
				if (cmd.size() == 2)
					myfs.create_file(cmd[1], true);
				else
					std::cout << CREATE_DIR_CMD << ": one argument requested" << std::endl;
			} else {
				std::cout << "unknown command: " << cmd[0] << std::endl;
			}
		} catch (std::runtime_error &e) {
			std::cout << e.what() << std::endl;
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
cmpl $0x2, %edi
jne 0x3d49
movq %rsi, %r14
pushq $0x10
popq %rdi
callq 0x21f0
movq %rax, %rbx
movq 0x8(%r14), %rsi
leaq -0xf0(%rbp), %rdi
leaq -0x90(%rbp), %rdx
callq 0x2bc6
leaq -0xf0(%rbp), %rsi
movq %rbx, %rdi
callq 0x292c
leaq -0xf0(%rbp), %rdi
callq 0x2368
leaq -0x90(%rbp), %rdi
movq %rbx, %rsi
callq 0x2cb0
movq 0x4a2e(%rip), %rdi      # 0x7fc0
leaq 0x1cb2(%rip), %rsi      # 0x524b
callq 0x21e0
leaq 0x4c43(%rip), %rsi      # 0x81e8
movq %rax, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x2100
movq 0x4a04(%rip), %rdi      # 0x7fc0
leaq 0x1c94(%rip), %rsi      # 0x5257
callq 0x21e0
movq %rax, %rdi
callq 0x2100
movq 0x49e9(%rip), %rbx      # 0x7fc0
movq %rbx, %rdi
callq 0x2100
leaq -0xd0(%rbp), %r14
movl $0xffffff4f, %r12d      # imm = 0xFFFFFF4F
leaq -0xa0(%rbp), %rax
movq %rax, -0xb0(%rbp)
andq $0x0, -0xa8(%rbp)
movb $0x0, -0xa0(%rbp)
movq %rbx, %rdi
leaq 0x4bd5(%rip), %rsi      # 0x81e8
callq 0x21d0
movq %rax, %rdi
leaq 0x1c6a(%rip), %rsi      # 0x528c
callq 0x21e0
movq 0x49a2(%rip), %rdi      # 0x7fd0
leaq -0xb0(%rbp), %rsi
pushq $0xa
popq %rdx
callq 0x2310
movq %r14, %rdi
leaq 0x1bc7(%rip), %rsi      # 0x520e
leaq -0x70(%rbp), %rdx
callq 0x2bc6
leaq -0xb0(%rbp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x30ec
movl %eax, %r13d
movq %r14, %rdi
callq 0x2368
testb %r13b, %r13b
je 0x367a
movq %r15, %rdi
jmp 0x3aed
leaq -0x250(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x2240
leaq -0x48(%rbp), %rdi
movq %r13, %rsi
callq 0x345c
movq %r13, %rdi
callq 0x2368
movq -0x48(%rbp), %rdi
leaq 0x4b5d(%rip), %rsi      # 0x8208
callq 0x30ec
testb %al, %al
je 0x3717
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
andq $0x0, -0x60(%rbp)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rax
subq %rsi, %rax
sarq $0x5, %rax
cmpq $0x2, %rax
je 0x37b7
cmpq $0x1, %rax
jne 0x37fa
leaq -0x230(%rbp), %r15
movq %r15, %rdi
leaq 0x1b9b(%rip), %rsi      # 0x528f
leaq -0x190(%rbp), %rdx
callq 0x2bc6
movq %r14, %rdi
leaq -0x90(%rbp), %rsi
movq %r15, %rdx
callq 0x2fce
jmp 0x37dc
movq -0x48(%rbp), %rdi
leaq 0x4bc6(%rip), %rsi      # 0x82e8
callq 0x30ec
testb %al, %al
jne 0x3d69
movq -0x48(%rbp), %rdi
leaq 0x4b8e(%rip), %rsi      # 0x82c8
callq 0x30ec
testb %al, %al
je 0x3757
movq %rbx, %rdi
leaq 0x4bbb(%rip), %rsi      # 0x8308
callq 0x21d0
jmp 0x3add
movq -0x48(%rbp), %rdi
leaq 0x4ae6(%rip), %r15      # 0x8248
movq %r15, %rsi
callq 0x30ec
testb %al, %al
je 0x38f3
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
subq %rax, %rcx
cmpq $0x40, %rcx
jne 0x3abb
addq $0x20, %rax
leaq -0x1b0(%rbp), %rdi
movq %rax, %rsi
callq 0x2240
leaq -0x90(%rbp), %rdi
leaq -0x1b0(%rbp), %r15
movq %r15, %rsi
xorl %edx, %edx
callq 0x2db6
jmp 0x39cf
addq $0x20, %rsi
leaq -0x210(%rbp), %r15
movq %r15, %rdi
callq 0x2240
movq %r14, %rdi
leaq -0x90(%rbp), %rsi
movq %r15, %rdx
callq 0x2fce
leaq -0x70(%rbp), %rdi
movq %r14, %rsi
callq 0x43d4
movq %r14, %rdi
callq 0x3064
movq %r15, %rdi
callq 0x2368
jmp 0x3820
movq %rbx, %rdi
leaq 0x4a04(%rip), %rsi      # 0x8208
callq 0x21d0
movq %rax, %rdi
leaq 0x1a7e(%rip), %rsi      # 0x5291
callq 0x21e0
movq %rax, %rdi
callq 0x2100
xorl %r13d, %r13d
xorl %r15d, %r15d
movq -0x70(%rbp), %rcx
movq -0x68(%rbp), %rax
subq %rcx, %rax
cqto
pushq $0x48
popq %rsi
idivq %rsi
cmpq %rax, %r15
jae 0x38e5
movq (%rbx), %rax
movq -0x18(%rax), %rdx
movq $0xf, 0x10(%rbx,%rdx)
movq -0x18(%rax), %rax
movl 0x18(%rbx,%rax), %edx
andl %r12d, %edx
orl $0x20, %edx
movl %edx, 0x18(%rbx,%rax)
cmpb $0x0, 0x20(%rcx,%r13)
leaq (%rcx,%r13), %rsi
leaq 0x1999(%rip), %rdx      # 0x520e
leaq 0x1a13(%rip), %rax      # 0x528f
cmovneq %rax, %rdx
movq %r14, %rdi
callq 0x4133
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21d0
movq (%rax), %rcx
movq -0x18(%rcx), %rdx
movq $0xa, 0x10(%rax,%rdx)
movq -0x18(%rcx), %rcx
movl 0x18(%rax,%rcx), %edx
andl %r12d, %edx
orl $0x80, %edx
movl %edx, 0x18(%rax,%rcx)
movq -0x70(%rbp), %rcx
movl 0x24(%rcx,%r13), %esi
movq %rax, %rdi
callq 0x22d0
movq %rax, %rdi
callq 0x2100
movq %r14, %rdi
callq 0x2368
incq %r15
addq $0x48, %r13
jmp 0x3826
leaq -0x70(%rbp), %rdi
callq 0x3064
jmp 0x3add
movq -0x48(%rbp), %rdi
leaq 0x492a(%rip), %r15      # 0x8228
movq %r15, %rsi
callq 0x30ec
testb %al, %al
je 0x396b
movq -0x48(%rbp), %rax
movq -0x40(%rbp), %rcx
subq %rax, %rcx
cmpq $0x40, %rcx
jne 0x3abb
addq $0x20, %rax
leaq -0x150(%rbp), %r15
movq %r15, %rdi
movq %rax, %rsi
callq 0x2240
movq %r14, %rdi
leaq -0x90(%rbp), %rsi
movq %r15, %rdx
callq 0x2e2e
movq %rbx, %rdi
movq %r14, %rsi
callq 0x21d0
movq %rax, %rdi
callq 0x2100
movq %r14, %rdi
callq 0x2368
leaq -0x150(%rbp), %rdi
jmp 0x39d2
movq -0x48(%rbp), %rdi
leaq 0x4932(%rip), %rsi      # 0x82a8
callq 0x30ec
testb %al, %al
je 0x39dc
leaq -0x130(%rbp), %rdi
leaq 0x1881(%rip), %r15      # 0x520e
movq %r15, %rsi
movq %r14, %rdx
callq 0x2bc6
leaq -0x1f0(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
leaq -0x70(%rbp), %rdx
callq 0x2bc6
leaq -0x90(%rbp), %rdi
leaq -0x130(%rbp), %r15
movq %r15, %rsi
movq %r13, %rdx
callq 0x3dea
movq %r13, %rdi
callq 0x2368
movq %r15, %rdi
callq 0x2368
jmp 0x3add
movq -0x48(%rbp), %rdi
leaq 0x48a1(%rip), %r15      # 0x8288
movq %r15, %rsi
callq 0x30ec
testb %al, %al
je 0x3af7
movq -0x40(%rbp), %rax
subq -0x48(%rbp), %rax
cmpq $0x40, %rax
jne 0x3abb
movq %rbx, %rdi
leaq 0x18b6(%rip), %rsi      # 0x52c9
callq 0x21e0
movq %rax, %rdi
callq 0x2100
leaq -0xc0(%rbp), %rax
movq %rax, -0xd0(%rbp)
andq $0x0, -0xc8(%rbp)
xorl %ecx, %ecx
movb %cl, -0xc0(%rbp)
leaq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
andq $0x0, -0x68(%rbp)
movb %cl, -0x60(%rbp)
movq 0x457b(%rip), %rdi      # 0x7fd0
leaq -0x70(%rbp), %rsi
callq 0x2050
leaq -0x70(%rbp), %r15
movq %r15, %rdi
leaq 0x17a2(%rip), %rsi      # 0x520e
callq 0x416b
testb %al, %al
je 0x3b6e
leaq -0x190(%rbp), %r15
movq %r15, %rdi
leaq -0x70(%rbp), %rsi
leaq 0x177f(%rip), %rdx      # 0x520d
callq 0x4133
movq %r14, %rdi
movq %r15, %rsi
callq 0x2340
movq %r15, %rdi
callq 0x2368
movq 0x4523(%rip), %rdi      # 0x7fd0
leaq -0x70(%rbp), %r15
movq %r15, %rsi
callq 0x2050
jmp 0x3a62
movq %rbx, %rdi
movq %r15, %rsi
callq 0x21d0
leaq 0x17e6(%rip), %rsi      # 0x52b3
movq %rax, %rdi
callq 0x21e0
movq %rax, %rdi
callq 0x2100
leaq -0x48(%rbp), %rdi
callq 0x4108
leaq -0xb0(%rbp), %rdi
callq 0x2368
jmp 0x35ec
movq -0x48(%rbp), %rdi
leaq 0x4766(%rip), %rsi      # 0x8268
callq 0x30ec
testb %al, %al
je 0x3b4e
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rax
subq %rsi, %rax
cmpq $0x40, %rax
jne 0x3bce
addq $0x20, %rsi
leaq -0x170(%rbp), %rdi
callq 0x2240
leaq -0x90(%rbp), %rdi
leaq -0x170(%rbp), %r15
movq %r15, %rsi
pushq $0x1
popq %rdx
callq 0x2db6
jmp 0x39cf
movq %rbx, %rdi
leaq 0x17a1(%rip), %rsi      # 0x52f9
callq 0x21e0
movq -0x48(%rbp), %rsi
movq %rax, %rdi
callq 0x21d0
jmp 0x3ad5
movq -0x48(%rbp), %rsi
addq $0x20, %rsi
leaq -0x110(%rbp), %rdi
callq 0x2240
leaq -0x1d0(%rbp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x2240
leaq -0x90(%rbp), %rdi
leaq -0x110(%rbp), %r15
movq %r15, %rsi
movq %r13, %rdx
callq 0x2eec
movq %r13, %rdi
callq 0x2368
movq %r15, %rdi
callq 0x2368
leaq -0x70(%rbp), %rdi
callq 0x2368
movq %r14, %rdi
jmp 0x39d2
movq %rbx, %rdi
leaq 0x4690(%rip), %rsi      # 0x8268
callq 0x21d0
leaq 0x16fc(%rip), %rsi      # 0x52e0
jmp 0x3acd
jmp 0x3c5c
movq %rdx, -0x30(%rbp)
movq %rax, -0x50(%rbp)
movq %r13, %rdi
callq 0x2368
jmp 0x3c05
movq %rdx, -0x30(%rbp)
movq %rax, -0x50(%rbp)
leaq -0x110(%rbp), %rdi
jmp 0x3c8b
jmp 0x3c94
movq %rdx, -0x30(%rbp)
movq %rax, %r15
movq %r13, %rdi
callq 0x2368
jmp 0x3c2b
movq %rdx, -0x30(%rbp)
movq %rax, %r15
leaq -0x130(%rbp), %rdi
callq 0x2368
jmp 0x3cb1
movq %rdx, %r13
movq %rax, %r15
jmp 0x3c53
jmp 0x3cb9
jmp 0x3cb9
movq %rdx, %r13
movq %rax, %r15
movq %r14, %rdi
callq 0x2368
leaq -0x150(%rbp), %rdi
jmp 0x3c65
movq %rdx, %r13
movq %r15, %rdi
movq %rax, %r15
callq 0x2368
jmp 0x3cfc
jmp 0x3c71
movq %rdx, %r13
movq %r15, %rdi
movq %rax, %r15
jmp 0x3cee
jmp 0x3cdd
jmp 0x3cdd
movq %rdx, -0x30(%rbp)
movq %rax, -0x50(%rbp)
movq %r15, %rdi
callq 0x2368
jmp 0x3c9c
jmp 0x3c94
movq %rdx, -0x30(%rbp)
movq %rax, -0x50(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x2368
movq %r14, %rdi
callq 0x2368
movq -0x50(%rbp), %r15
movq -0x30(%rbp), %r13
jmp 0x3cfc
jmp 0x3cdd
movq %rdx, %r13
movq %rax, %r15
jmp 0x3cfc
movq %rax, %r15
movq %r13, %rdi
movq %rdx, %r13
callq 0x2368
jmp 0x3d05
jmp 0x3cd5
jmp 0x3cd5
movq %rdx, %r13
movq %rax, %r15
jmp 0x3d05
movq %rdx, %r13
movq %rax, %r15
jmp 0x3cf3
movq %rdx, %r13
movq %rax, %r15
movq %r14, %rdi
callq 0x2368
leaq -0x70(%rbp), %rdi
callq 0x3064
leaq -0x48(%rbp), %rdi
callq 0x4108
leaq -0xb0(%rbp), %rdi
callq 0x2368
cmpl $0x1, %r13d
jne 0x3dd6
movq %r15, %rdi
callq 0x20c0
movq (%rax), %rcx
movq %rax, %rdi
callq *0x10(%rcx)
movq %rbx, %rdi
movq %rax, %rsi
callq 0x21e0
movq %rax, %rdi
callq 0x2100
callq 0x22a0
jmp 0x35ec
movq 0x4298(%rip), %rdi      # 0x7fe8
leaq 0x14ce(%rip), %rsi      # 0x5225
callq 0x21e0
movq %rax, %rdi
callq 0x2100
pushq $-0x1
popq %rax
jmp 0x3d8c
leaq -0x48(%rbp), %rdi
callq 0x4108
leaq -0xb0(%rbp), %rdi
callq 0x2368
leaq -0x88(%rbp), %rdi
callq 0x3064
xorl %eax, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3dd3
movq %rax, %r15
callq 0x22a0
jmp 0x3dd6
movq %rax, %rdi
callq 0x2c01
movq %rax, %r15
leaq -0xf0(%rbp), %rdi
callq 0x2368
jmp 0x3dc6
movq %rax, %r15
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0x2200
jmp 0x3de2
movq %rax, %r15
leaq -0x88(%rbp), %rdi
callq 0x3064
movq %r15, %rdi
callq 0x22e0