// File: /workspace/github_cpp_makefile_star2_O3/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 0xbeed
movq (%r13), %r12
movq 0x8(%r13), %rbx
cmpq %rbx, %r12
je 0xa655
leaq -0x80(%rbp), %r14
leaq -0x90(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xaa22
movq %r12, %rdi
movq %r13, %rsi
callq 0x5300
movq -0x90(%rbp), %rdi
cmpq %r14, %rdi
je 0xa648
callq 0x5230
addq $0x20, %r12
cmpq %rbx, %r12
jne 0xa61e
movq -0x48(%rbp), %r12
callq 0x68b2
movq %rax, %r13
movq %rax, %rdi
movq %r12, %rsi
callq 0xb93e
addq $0x8, %r13
cmpq %rax, %r13
je 0xa6dc
callq 0x68b2
movq -0x48(%rbp), %rsi
movq %rax, %rdi
callq 0x72f6
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 0xa966
callq 0x6abe
movq -0x88(%rbp), %rdi
testq %rdi, %rdi
je 0xa966
callq 0x6abe
jmp 0xa966
leaq -0x90(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xaadc
leaq -0x90(%rbp), %rdi
callq 0xc251
testb %al, %al
je 0xa756
movq -0x48(%rbp), %rdx
leaq 0x3e3e(%rip), %rsi      # 0xe547
leaq -0x70(%rbp), %rdi
callq 0x6827
leaq 0x3e3f(%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 0xa882
movq %rdx, -0xc0(%rbp)
movq (%rcx), %rdx
movq %rdx, -0xb0(%rbp)
jmp 0xa888
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 0xaf74
movl %eax, %r15d
testb %al, %al
je 0xa834
movq -0x48(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x5170
movl $0x20, %edi
callq 0x5260
movq %rax, %r12
leaq 0x9a28(%rip), %rax      # 0x141d8
movq %rax, (%r12)
movq %r12, %rdi
addq $0x8, %rdi
leaq -0x48(%rbp), %rsi
callq 0x6cc4
leaq 0x97e5(%rip), %rax      # 0x13fb0
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 0xb98e
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 0xa93e
callq 0x6abe
movq -0x68(%rbp), %rdi
testq %rdi, %rdi
je 0xa93e
callq 0x6abe
jmp 0xa93e
movq -0x48(%rbp), %rdx
leaq 0x3d2e(%rip), %rsi      # 0xe56d
leaq -0xe0(%rbp), %rdi
callq 0x6827
leaq 0x27ea(%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 0xa8ec
movq %rdx, -0x70(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x60(%rbp)
jmp 0xa8f2
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 0xa8c2
callq 0x5230
leaq -0x60(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xa8d4
callq 0x5230
leaq -0x80(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xa96a
callq 0x5230
jmp 0xa96a
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 0xa929
callq 0x5230
leaq -0xd0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xa93e
callq 0x5230
movq -0xc0(%rbp), %rdi
cmpq %r13, %rdi
je 0xa94f
callq 0x5230
leaq -0x80(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xa961
callq 0x5230
testb %r15b, %r15b
je 0xa96a
movb $0x1, %bl
jmp 0xa96c
xorl %ebx, %ebx
leaq -0x48(%rbp), %rdi
callq 0x6c6c
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 0xa9a4
movq %rax, %rbx
leaq -0xd0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xa9c6
callq 0x5230
jmp 0xa9c6
jmp 0xa9c3
jmp 0xa9c3
movq %rax, %rbx
leaq -0x60(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0xa9d2
jmp 0xa9dc
jmp 0xa9d9
movq %rax, %rbx
movq -0xc0(%rbp), %rdi
cmpq %r13, %rdi
je 0xa9dc
callq 0x5230
jmp 0xa9dc
movq %rax, %rbx
leaq -0x80(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xa9f9
callq 0x5230
jmp 0xa9f9
jmp 0xa9f6
jmp 0xa9f6
jmp 0xa9f6
movq %rax, %rbx
leaq -0x48(%rbp), %rdi
callq 0x6c6c
movq %rbx, %rdi
callq 0x5400