// File: dmonaldo[P]simple-network-file-system/Shell.cpp
// Function: Shell::parse_command(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
Shell::Command Shell::parse_command(string command_str)
{
  // empty command struct returned for errors
  struct 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 0x28eb(%rip), %rsi      # 0xc0ea
callq 0x69b0
jmp 0x9806
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 0x28aa(%rip), %rsi      # 0xc0ea
callq 0x69b0
jmp 0x9847
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 0x2869(%rip), %rsi      # 0xc0ea
callq 0x69b0
jmp 0x9888
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 0xa4c0
movq -0x2c8(%rbp), %rsi
leaq -0x270(%rbp), %rdi
movl $0x8, %edx
callq 0x3530
jmp 0x98dd
movl $0x0, -0x274(%rbp)
leaq -0x270(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x3440
movq %rax, -0x2f0(%rbp)
jmp 0x9903
movq -0x2f0(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3430
movb %al, -0x2f1(%rbp)
jmp 0x9921
movb -0x2f1(%rbp), %al
testb $0x1, %al
jne 0x9930
jmp 0x9b4f
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 0x995a
movq -0x300(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3430
movb %al, -0x301(%rbp)
jmp 0x9978
movb -0x301(%rbp), %al
testb $0x1, %al
jne 0x9987
jmp 0x9b4d
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 0x99b1
movq -0x310(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3430
movb %al, -0x311(%rbp)
jmp 0x99cf
movb -0x311(%rbp), %al
testb $0x1, %al
jne 0x99de
jmp 0x9b4b
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 0x9a1b
movq -0x320(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x3430
movb %al, -0x329(%rbp)
jmp 0x9a39
movb -0x329(%rbp), %al
testb $0x1, %al
jne 0x9a48
jmp 0x9b3f
movl -0x274(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x274(%rbp)
jmp 0x9b3f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x9ae0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x9abb
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 0x9aad
jmp 0x9ab9
movq -0x2d8(%rbp), %rdi
callq 0x3628
jmp 0x9abb
leaq -0x8d(%rbp), %rdi
callq 0x33b0
testb $0x1, -0x90(%rbp)
jne 0x9ad2
jmp 0x9ade
movq -0x2b0(%rbp), %rdi
callq 0x3628
jmp 0x9ae0
leaq -0x79(%rbp), %rdi
callq 0x33b0
jmp 0xa007
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x9ff2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
jmp 0x9fe6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x88(%rbp)
movl %eax, -0x8c(%rbp)
leaq -0x298(%rbp), %rdi
callq 0x3628
jmp 0x9fe6
leaq -0x298(%rbp), %rdi
callq 0x3628
jmp 0x9b4d
jmp 0x9b4f
cmpl $0x0, -0x274(%rbp)
jne 0x9b77
movq -0x2c0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xa500
movl $0x1, -0x29c(%rbp)
jmp 0x9fb5
leaq 0x26e4(%rip), %rsi      # 0xc262
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x32a(%rbp)
jmp 0x9b92
movb -0x32a(%rbp), %al
testb $0x1, %al
jne 0x9bef
jmp 0x9b9e
leaq 0x26b2(%rip), %rsi      # 0xc257
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x32b(%rbp)
jmp 0x9bb9
movb -0x32b(%rbp), %al
testb $0x1, %al
jne 0x9bef
jmp 0x9bc5
leaq 0x26ef(%rip), %rsi      # 0xc2bb
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x32c(%rbp)
jmp 0x9be0
movb -0x32c(%rbp), %al
testb $0x1, %al
jne 0x9bef
jmp 0x9c82
cmpl $0x1, -0x274(%rbp)
je 0x9c7d
movq 0x63ed(%rip), %rdi      # 0xfff0
leaq 0x2672(%rip), %rsi      # 0xc27c
callq 0x3340
movq %rax, -0x338(%rbp)
jmp 0x9c18
movq -0x338(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x3310
jmp 0x9c2d
movq 0x63bc(%rip), %rdi      # 0xfff0
leaq 0x2685(%rip), %rsi      # 0xc2c0
callq 0x3340
movq %rax, -0x340(%rbp)
jmp 0x9c49
movq -0x340(%rbp), %rdi
movq 0x6351(%rip), %rsi      # 0xffa8
callq 0x3390
jmp 0x9c5e
movq -0x2c0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xa500
movl $0x1, -0x29c(%rbp)
jmp 0x9fb5
jmp 0x9f98
leaq 0x25c5(%rip), %rsi      # 0xc24e
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x341(%rbp)
jmp 0x9c9d
movb -0x341(%rbp), %al
testb $0x1, %al
jne 0x9da2
jmp 0x9cad
leaq 0x25a0(%rip), %rsi      # 0xc254
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x342(%rbp)
jmp 0x9cc8
movb -0x342(%rbp), %al
testb $0x1, %al
jne 0x9da2
jmp 0x9cd8
leaq 0x257d(%rip), %rsi      # 0xc25c
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x343(%rbp)
jmp 0x9cf3
movb -0x343(%rbp), %al
testb $0x1, %al
jne 0x9da2
jmp 0x9d03
leaq 0x255b(%rip), %rsi      # 0xc265
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x344(%rbp)
jmp 0x9d1e
movb -0x344(%rbp), %al
testb $0x1, %al
jne 0x9da2
jmp 0x9d2a
leaq 0x2542(%rip), %rsi      # 0xc273
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x345(%rbp)
jmp 0x9d45
movb -0x345(%rbp), %al
testb $0x1, %al
jne 0x9da2
jmp 0x9d51
leaq 0x255b(%rip), %rsi      # 0xc2b3
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x346(%rbp)
jmp 0x9d6c
movb -0x346(%rbp), %al
testb $0x1, %al
jne 0x9da2
jmp 0x9d78
leaq 0x2537(%rip), %rsi      # 0xc2b6
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x347(%rbp)
jmp 0x9d93
movb -0x347(%rbp), %al
testb $0x1, %al
jne 0x9da2
jmp 0x9e35
cmpl $0x2, -0x274(%rbp)
je 0x9e30
movq 0x623a(%rip), %rdi      # 0xfff0
leaq 0x24bf(%rip), %rsi      # 0xc27c
callq 0x3340
movq %rax, -0x350(%rbp)
jmp 0x9dcb
movq -0x350(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x3310
jmp 0x9de0
movq 0x6209(%rip), %rdi      # 0xfff0
leaq 0x24d2(%rip), %rsi      # 0xc2c0
callq 0x3340
movq %rax, -0x358(%rbp)
jmp 0x9dfc
movq -0x358(%rbp), %rdi
movq 0x619e(%rip), %rsi      # 0xffa8
callq 0x3390
jmp 0x9e11
movq -0x2c0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xa500
movl $0x1, -0x29c(%rbp)
jmp 0x9fb5
jmp 0x9f96
leaq 0x2430(%rip), %rsi      # 0xc26c
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x359(%rbp)
jmp 0x9e50
movb -0x359(%rbp), %al
testb $0x1, %al
jne 0x9e86
jmp 0x9e5c
leaq 0x2414(%rip), %rsi      # 0xc277
leaq -0xf0(%rbp), %rdi
callq 0x6a60
movb %al, -0x35a(%rbp)
jmp 0x9e77
movb -0x35a(%rbp), %al
testb $0x1, %al
jne 0x9e86
jmp 0x9f16
cmpl $0x3, -0x274(%rbp)
je 0x9f14
movq 0x6156(%rip), %rdi      # 0xfff0
leaq 0x23db(%rip), %rsi      # 0xc27c
callq 0x3340
movq %rax, -0x368(%rbp)
jmp 0x9eaf
movq -0x368(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x3310
jmp 0x9ec4
movq 0x6125(%rip), %rdi      # 0xfff0
leaq 0x23ee(%rip), %rsi      # 0xc2c0
callq 0x3340
movq %rax, -0x370(%rbp)
jmp 0x9ee0
movq -0x370(%rbp), %rdi
movq 0x60ba(%rip), %rsi      # 0xffa8
callq 0x3390
jmp 0x9ef5
movq -0x2c0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xa500
movl $0x1, -0x29c(%rbp)
jmp 0x9fb5
jmp 0x9f94
movq 0x60d3(%rip), %rdi      # 0xfff0
leaq 0x2358(%rip), %rsi      # 0xc27c
callq 0x3340
movq %rax, -0x378(%rbp)
jmp 0x9f32
movq -0x378(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x3310
jmp 0x9f47
movq 0x60a2(%rip), %rdi      # 0xfff0
leaq 0x238d(%rip), %rsi      # 0xc2e2
callq 0x3340
movq %rax, -0x380(%rbp)
jmp 0x9f63
movq -0x380(%rbp), %rdi
movq 0x6037(%rip), %rsi      # 0xffa8
callq 0x3390
jmp 0x9f78
movq -0x2c0(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xa500
movl $0x1, -0x29c(%rbp)
jmp 0x9fb5
jmp 0x9f96
jmp 0x9f98
movq -0x2c0(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0xa500
movl $0x1, -0x29c(%rbp)
leaq -0x270(%rbp), %rdi
callq 0x31c0
leaq -0xf0(%rbp), %rdi
callq 0xa480
leaq -0x78(%rbp), %rdi
callq 0xa480
movq -0x2b8(%rbp), %rax
addq $0x380, %rsp            # imm = 0x380
popq %rbp
retq
leaq -0x270(%rbp), %rdi
callq 0x31c0
leaq -0xf0(%rbp), %rdi
callq 0xa480
leaq -0x78(%rbp), %rdi
callq 0xa480
movq -0x88(%rbp), %rdi
callq 0x3560
nopw %cs:(%rax,%rax)
nopl (%rax)