// File: /workspace/github_cpp_makefile_star2_O1/AndreiBarsan[P]uShell/shell.cc
// Function: microshell::core::Shell::parse_command(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::shared_ptr<microshell::core::Command>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*) const
bool Shell::parse_command(const string& command_text,
                          shared_ptr<Command> &command,
                          string *error) const {
  // TODO(andrei) YACC this.
  // TODO(andrei) Customizable IFS variable.
  vector<string> argv = util::split(command_text, ' ');

  // Perform expansion for every parameter.
  for(string& arg : argv) {
    arg = expand(arg);
  }

  const string& program_name = argv[0];

  if(is_builtin(argv[0])) {
    command = construct_builtin(argv);
  }
  else {
    // If the path actually points to a directory, we want to catch that.
    string full_path = resolve_path(program_name);
    if (util::is_directory(full_path)) {
      // TODO(andrei) zsh-like auto-cd functionality could go here.
      *error = "Cannot execute [" + argv[0] + "]. It's a directory.";
      return false;
    }

    string binary_path;
    if(resolve_binary_name(argv[0], &binary_path)) {
      argv[0] = binary_path;
      command = make_shared<DiskCommand>(new DiskCommand(argv));
    } else {
      *error = "Command not found: [" + argv[0] + "]";
      return false;
    }
  }

  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rcx, -0x98(%rbp)
movq %rdx, -0xa0(%rbp)
movq %rdi, %r15
leaq -0x48(%rbp), %r13
movq %r13, %rdi
movl $0x20, %edx
callq 0xbfc9
movq (%r13), %r12
movq 0x8(%r13), %rbx
cmpq %rbx, %r12
je 0xa721
leaq -0x80(%rbp), %r14
leaq -0x90(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xaae8
movq %r12, %rdi
movq %r13, %rsi
callq 0x5300
movq -0x90(%rbp), %rdi
cmpq %r14, %rdi
je 0xa718
callq 0x5230
addq $0x20, %r12
cmpq %rbx, %r12
jne 0xa6ee
movq -0x48(%rbp), %r12
callq 0x6886
movq %rax, %r13
movq %rax, %rdi
movq %r12, %rsi
callq 0xba0a
addq $0x8, %r13
cmpq %rax, %r13
je 0xa7a6
callq 0x6886
movq -0x48(%rbp), %rsi
movq %rax, %rdi
callq 0x72f2
movq (%rax), %rsi
movq (%rsi), %rax
leaq -0x90(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq *(%rax)
movaps -0x90(%rbp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, -0x90(%rbp)
movq -0xa0(%rbp), %rax
movq 0x8(%rax), %rdi
movups %xmm0, (%rax)
testq %rdi, %rdi
je 0xa78e
callq 0x6a96
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0xa79f
callq 0x6a96
movb $0x1, %bl
jmp 0xaa32
leaq -0x90(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xaba2
leaq -0x90(%rbp), %rdi
callq 0xc32d
testb %al, %al
je 0xa820
movq -0x48(%rbp), %rdx
leaq 0x3d74(%rip), %rsi      # 0xe547
leaq -0x70(%rbp), %rdi
callq 0x67fb
leaq 0x3d75(%rip), %rsi      # 0xe558
leaq -0x70(%rbp), %rdi
callq 0x5450
leaq -0xb0(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xa948
movq %rdx, -0xc0(%rbp)
movq (%rcx), %rdx
movq %rdx, -0xb0(%rbp)
jmp 0xa94e
leaq -0xb0(%rbp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
movq -0x48(%rbp), %rsi
leaq -0xc0(%rbp), %rdx
movq %r15, %rdi
callq 0xb03a
movl %eax, %r15d
testb %al, %al
je 0xa8fa
movq -0x48(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x5170
movl $0x20, %edi
callq 0x5260
movq %rax, %r12
leaq 0x995e(%rip), %rax      # 0x141d8
movq %rax, (%r12)
movq %r12, %rdi
addq $0x8, %rdi
leaq -0x48(%rbp), %rsi
callq 0x6cae
leaq 0x96e3(%rip), %rax      # 0x13f78
addq $0x10, %rax
movq %rax, (%r12)
leaq -0xe0(%rbp), %rcx
movq %r12, (%rcx)
leaq -0x68(%rbp), %rdi
movq $0x0, -0x8(%rdi)
leaq -0x70(%rbp), %rsi
leaq -0x29(%rbp), %rdx
callq 0xba5a
movaps -0x70(%rbp), %xmm0
xorps %xmm1, %xmm1
movaps %xmm1, -0x70(%rbp)
movq -0xa0(%rbp), %rax
movq 0x8(%rax), %rdi
movups %xmm0, (%rax)
testq %rdi, %rdi
je 0xa8e3
callq 0x6a96
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0xaa04
callq 0x6a96
jmp 0xaa04
movq -0x48(%rbp), %rdx
leaq 0x3c68(%rip), %rsi      # 0xe56d
leaq -0xe0(%rbp), %rdi
callq 0x67fb
leaq 0x2724(%rip), %rsi      # 0xd03c
leaq -0xe0(%rbp), %rdi
callq 0x5450
leaq -0x60(%rbp), %rbx
movq %rbx, -0x10(%rbx)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xa9b2
movq %rdx, -0x70(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x60(%rbp)
jmp 0xa9b8
movups (%rcx), %xmm0
movups %xmm0, (%rbx)
movq 0x8(%rax), %rdx
leaq -0xc0(%rbp), %r14
movq %rdx, 0x8(%r14)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq -0x98(%rbp), %rdi
movq %r14, %rsi
callq 0x5300
movq (%r14), %rdi
cmpq %rbx, %rdi
je 0xa988
callq 0x5230
leaq -0x60(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xa99a
callq 0x5230
leaq -0x80(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xaa30
callq 0x5230
jmp 0xaa30
movups (%rcx), %xmm0
movups %xmm0, (%rbx)
movq 0x8(%rax), %rdx
leaq -0x70(%rbp), %r14
movq %rdx, 0x8(%r14)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq -0x98(%rbp), %rdi
movq %r14, %rsi
callq 0x5300
movq (%r14), %rdi
cmpq %rbx, %rdi
je 0xa9ef
callq 0x5230
leaq -0xd0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xaa04
callq 0x5230
movq -0xc0(%rbp), %rdi
cmpq %r13, %rdi
je 0xaa15
callq 0x5230
leaq -0x80(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xaa27
callq 0x5230
testb %r15b, %r15b
jne 0xa79f
xorl %ebx, %ebx
leaq -0x48(%rbp), %rdi
callq 0x6c60
movl %ebx, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r12, %rdi
jmp 0xaa6a
movq %rax, %rbx
leaq -0xd0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xaa8c
callq 0x5230
jmp 0xaa8c
jmp 0xaa89
jmp 0xaa89
movq %rax, %rbx
leaq -0x60(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0xaa98
jmp 0xaaa2
jmp 0xaa9f
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
cmpq %r13, %rdi
je 0xaaa2
callq 0x5230
jmp 0xaaa2
movq %rax, %rbx
leaq -0x80(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xaabf
callq 0x5230
jmp 0xaabf
jmp 0xaabc
jmp 0xaabc
jmp 0xaabc
movq %rax, %rbx
leaq -0x48(%rbp), %rdi
callq 0x6c60
movq %rbx, %rdi
callq 0x5400