// File: dmonaldo[P]simple-network-file-system/FileSys.cpp
// Function: FileSys::parse_command(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
FileSys::Command FileSys::parse_command(string command_str)
{
  //cout << "Running command: " << command_str << endl;

  // empty command struct returned for errors
  struct FileSys::Command empty = {"", "", ""};

  // grab each of the tokens (if they exist)
  struct Command command;
  istringstream ss(command_str);
  int num_tokens = 0;
  if (ss >> command.name) {
    num_tokens++;
    if (ss >> command.file_name) {
      num_tokens++;
      if (ss >> command.append_data) {
        num_tokens++;
        string junk;
        if (ss >> junk) {
          num_tokens++;
        }
      }
    }
  }

  // Check for empty command line
  if (num_tokens == 0) {
    return empty;
  }

  // Check for invalid command lines
  if (command.name == "ls" || command.name == "home" ||
      command.name == "quit")
  {
    if (num_tokens != 1) {
      cerr << "Invalid command line: " << command.name;
      cerr << " has improper number of arguments" << endl;
      return empty;
    }
  }
  else if (command.name == "mkdir" ||
      command.name == "cd"    ||
      command.name == "rmdir" ||
      command.name == "create"||
      command.name == "cat"   ||
      command.name == "rm"    ||
      command.name == "stat")
  {
    if (num_tokens != 2) {
      cerr << "Invalid command line: " << command.name;
      cerr << " has improper number of arguments" << endl;
      return empty;
    }
  }
  else if (command.name == "append" || command.name == "head")
  {
    if (num_tokens != 3) {
      cerr << "Invalid command line: " << command.name;
      cerr << " has improper number of arguments" << endl;
      return empty;
    }
  }
  else {
    cerr << "Invalid command line: " << command.name;
    cerr << " is not a command" << endl;
    return empty;
  }

  return command;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x380, %rsp            # imm = 0x380
movq %rdx, -0x2c8(%rbp)
movq %rdi, -0x2c0(%rbp)
movq %rdi, %rax
movq %rax, -0x2b8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x78(%rbp), %rax
movq %rax, -0x2b0(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0x2a8(%rbp)
callq 0x3570
movq -0x2b0(%rbp), %rdi
movq -0x2a8(%rbp), %rdx
leaq 0x66ab(%rip), %rsi      # 0xc0ea
callq 0x69b0
jmp 0x5a46
movb $0x1, -0x90(%rbp)
leaq -0x58(%rbp), %rax
movq %rax, -0x2d8(%rbp)
leaq -0x8d(%rbp), %rdi
movq %rdi, -0x2d0(%rbp)
callq 0x3570
movq -0x2d8(%rbp), %rdi
movq -0x2d0(%rbp), %rdx
leaq 0x666a(%rip), %rsi      # 0xc0ea
callq 0x69b0
jmp 0x5a87
movb $0x1, -0x8f(%rbp)
leaq -0x38(%rbp), %rax
movq %rax, -0x2e8(%rbp)
leaq -0x8e(%rbp), %rdi
movq %rdi, -0x2e0(%rbp)
callq 0x3570
movq -0x2e8(%rbp), %rdi
movq -0x2e0(%rbp), %rdx
leaq 0x6629(%rip), %rsi      # 0xc0ea
callq 0x69b0
jmp 0x5ac8
movb $0x0, -0x8f(%rbp)
movb $0x0, -0x90(%rbp)
leaq -0x8e(%rbp), %rdi
callq 0x33b0
leaq -0x8d(%rbp), %rdi
callq 0x33b0
leaq -0x79(%rbp), %rdi
callq 0x33b0
leaq -0xf0(%rbp), %rdi
callq 0x6ad0
movq -0x2c8(%rbp), %rsi
leaq -0x270(%rbp), %rdi
movl $0x8, %edx
callq 0x3530
jmp 0x5b1d
movl $0x0, -0x274(%rbp)
leaq -0x270(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x3440
movq %rax, -0x2f0(%rbp)
jmp 0x5b43
movq -0x2f0(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3430
movb %al, -0x2f1(%rbp)
jmp 0x5b61
movb -0x2f1(%rbp), %al
testb $0x1, %al
jne 0x5b70
jmp 0x5d8f
movl -0x274(%rbp), %eax
incl %eax
movl %eax, -0x274(%rbp)
leaq -0xd0(%rbp), %rsi
leaq -0x270(%rbp), %rdi
callq 0x3440
movq %rax, -0x300(%rbp)
jmp 0x5b9a
movq -0x300(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3430
movb %al, -0x301(%rbp)
jmp 0x5bb8
movb -0x301(%rbp), %al
testb $0x1, %al
jne 0x5bc7
jmp 0x5d8d
movl -0x274(%rbp), %eax
incl %eax
movl %eax, -0x274(%rbp)
leaq -0xb0(%rbp), %rsi
leaq -0x270(%rbp), %rdi
callq 0x3440
movq %rax, -0x310(%rbp)
jmp 0x5bf1
movq -0x310(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3430
movb %al, -0x311(%rbp)
jmp 0x5c0f
movb -0x311(%rbp), %al
testb $0x1, %al
jne 0x5c1e
jmp 0x5d8b
movl -0x274(%rbp), %eax
incl %eax
movl %eax, -0x274(%rbp)
leaq -0x298(%rbp), %rdi
movq %rdi, -0x328(%rbp)
callq 0x3470
movq -0x328(%rbp), %rsi
leaq -0x270(%rbp), %rdi
callq 0x3440
movq %rax, -0x320(%rbp)
jmp 0x5c5b
movq -0x320(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3430
movb %al, -0x329(%rbp)
jmp 0x5c79
movb -0x329(%rbp), %al
testb $0x1, %al
jne 0x5c88
jmp 0x5d7f
movl -0x274(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x274(%rbp)
jmp 0x5d7f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x5d20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x5cfb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x8e(%rbp), %rdi
callq 0x33b0
testb $0x1, -0x8f(%rbp)
jne 0x5ced
jmp 0x5cf9
movq -0x2d8(%rbp), %rdi
callq 0x3628
jmp 0x5cfb
leaq -0x8d(%rbp), %rdi
callq 0x33b0
testb $0x1, -0x90(%rbp)
jne 0x5d12
jmp 0x5d1e
movq -0x2b0(%rbp), %rdi
callq 0x3628
jmp 0x5d20
leaq -0x79(%rbp), %rdi
callq 0x33b0
jmp 0x6247
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x6232
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x6226
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x298(%rbp), %rdi
callq 0x3628
jmp 0x6226
leaq -0x298(%rbp), %rdi
callq 0x3628
jmp 0x5d8d
jmp 0x5d8f
cmpl $0x0, -0x274(%rbp)
jne 0x5db7
movq -0x2c0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x6b10
movl $0x1, -0x29c(%rbp)
jmp 0x61f5
leaq 0x64a4(%rip), %rsi      # 0xc262
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x32a(%rbp)
jmp 0x5dd2
movb -0x32a(%rbp), %al
testb $0x1, %al
jne 0x5e2f
jmp 0x5dde
leaq 0x6472(%rip), %rsi      # 0xc257
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x32b(%rbp)
jmp 0x5df9
movb -0x32b(%rbp), %al
testb $0x1, %al
jne 0x5e2f
jmp 0x5e05
leaq 0x64af(%rip), %rsi      # 0xc2bb
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x32c(%rbp)
jmp 0x5e20
movb -0x32c(%rbp), %al
testb $0x1, %al
jne 0x5e2f
jmp 0x5ec2
cmpl $0x1, -0x274(%rbp)
je 0x5ebd
movq 0xa1ad(%rip), %rdi      # 0xfff0
leaq 0x6432(%rip), %rsi      # 0xc27c
callq 0x3340
movq %rax, -0x338(%rbp)
jmp 0x5e58
movq -0x338(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x3310
jmp 0x5e6d
movq 0xa17c(%rip), %rdi      # 0xfff0
leaq 0x6445(%rip), %rsi      # 0xc2c0
callq 0x3340
movq %rax, -0x340(%rbp)
jmp 0x5e89
movq -0x340(%rbp), %rdi
movq 0xa111(%rip), %rsi      # 0xffa8
callq 0x3390
jmp 0x5e9e
movq -0x2c0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x6b10
movl $0x1, -0x29c(%rbp)
jmp 0x61f5
jmp 0x61d8
leaq 0x6385(%rip), %rsi      # 0xc24e
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x341(%rbp)
jmp 0x5edd
movb -0x341(%rbp), %al
testb $0x1, %al
jne 0x5fe2
jmp 0x5eed
leaq 0x6360(%rip), %rsi      # 0xc254
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x342(%rbp)
jmp 0x5f08
movb -0x342(%rbp), %al
testb $0x1, %al
jne 0x5fe2
jmp 0x5f18
leaq 0x633d(%rip), %rsi      # 0xc25c
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x343(%rbp)
jmp 0x5f33
movb -0x343(%rbp), %al
testb $0x1, %al
jne 0x5fe2
jmp 0x5f43
leaq 0x631b(%rip), %rsi      # 0xc265
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x344(%rbp)
jmp 0x5f5e
movb -0x344(%rbp), %al
testb $0x1, %al
jne 0x5fe2
jmp 0x5f6a
leaq 0x6302(%rip), %rsi      # 0xc273
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x345(%rbp)
jmp 0x5f85
movb -0x345(%rbp), %al
testb $0x1, %al
jne 0x5fe2
jmp 0x5f91
leaq 0x631b(%rip), %rsi      # 0xc2b3
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x346(%rbp)
jmp 0x5fac
movb -0x346(%rbp), %al
testb $0x1, %al
jne 0x5fe2
jmp 0x5fb8
leaq 0x62f7(%rip), %rsi      # 0xc2b6
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x347(%rbp)
jmp 0x5fd3
movb -0x347(%rbp), %al
testb $0x1, %al
jne 0x5fe2
jmp 0x6075
cmpl $0x2, -0x274(%rbp)
je 0x6070
movq 0x9ffa(%rip), %rdi      # 0xfff0
leaq 0x627f(%rip), %rsi      # 0xc27c
callq 0x3340
movq %rax, -0x350(%rbp)
jmp 0x600b
movq -0x350(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x3310
jmp 0x6020
movq 0x9fc9(%rip), %rdi      # 0xfff0
leaq 0x6292(%rip), %rsi      # 0xc2c0
callq 0x3340
movq %rax, -0x358(%rbp)
jmp 0x603c
movq -0x358(%rbp), %rdi
movq 0x9f5e(%rip), %rsi      # 0xffa8
callq 0x3390
jmp 0x6051
movq -0x2c0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x6b10
movl $0x1, -0x29c(%rbp)
jmp 0x61f5
jmp 0x61d6
leaq 0x61f0(%rip), %rsi      # 0xc26c
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x359(%rbp)
jmp 0x6090
movb -0x359(%rbp), %al
testb $0x1, %al
jne 0x60c6
jmp 0x609c
leaq 0x61d4(%rip), %rsi      # 0xc277
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x35a(%rbp)
jmp 0x60b7
movb -0x35a(%rbp), %al
testb $0x1, %al
jne 0x60c6
jmp 0x6156
cmpl $0x3, -0x274(%rbp)
je 0x6154
movq 0x9f16(%rip), %rdi      # 0xfff0
leaq 0x619b(%rip), %rsi      # 0xc27c
callq 0x3340
movq %rax, -0x368(%rbp)
jmp 0x60ef
movq -0x368(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x3310
jmp 0x6104
movq 0x9ee5(%rip), %rdi      # 0xfff0
leaq 0x61ae(%rip), %rsi      # 0xc2c0
callq 0x3340
movq %rax, -0x370(%rbp)
jmp 0x6120
movq -0x370(%rbp), %rdi
movq 0x9e7a(%rip), %rsi      # 0xffa8
callq 0x3390
jmp 0x6135
movq -0x2c0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x6b10
movl $0x1, -0x29c(%rbp)
jmp 0x61f5
jmp 0x61d4
movq 0x9e93(%rip), %rdi      # 0xfff0
leaq 0x6118(%rip), %rsi      # 0xc27c
callq 0x3340
movq %rax, -0x378(%rbp)
jmp 0x6172
movq -0x378(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x3310
jmp 0x6187
movq 0x9e62(%rip), %rdi      # 0xfff0
leaq 0x614d(%rip), %rsi      # 0xc2e2
callq 0x3340
movq %rax, -0x380(%rbp)
jmp 0x61a3
movq -0x380(%rbp), %rdi
movq 0x9df7(%rip), %rsi      # 0xffa8
callq 0x3390
jmp 0x61b8
movq -0x2c0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x6b10
movl $0x1, -0x29c(%rbp)
jmp 0x61f5
jmp 0x61d6
jmp 0x61d8
movq -0x2c0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x6b10
movl $0x1, -0x29c(%rbp)
leaq -0x270(%rbp), %rdi
callq 0x31c0
leaq -0xf0(%rbp), %rdi
callq 0x6a90
leaq -0x78(%rbp), %rdi
callq 0x6a90
movq -0x2b8(%rbp), %rax
addq $0x380, %rsp            # imm = 0x380
popq %rbp
retq
leaq -0x270(%rbp), %rdi
callq 0x31c0
leaq -0xf0(%rbp), %rdi
callq 0x6a90
leaq -0x78(%rbp), %rdi
callq 0x6a90
movq -0x88(%rbp), %rdi
callq 0x3560
nopw %cs:(%rax,%rax)