#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef scalar_t__ pid_t;

/* Variables and functions */
int execvp(char *, char **);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ free(char **);
int /*<<< orphan*/ perror(char *);
char **rshell_parse_cmd(char *);
int /*<<< orphan*/ wait(int *);
int rshell_exec_cmd(char *cmd) {
  pid_t pid;
  int status;
  char **args = rshell_parse_cmd(cmd);
  pid = fork();
  if (pid == 0) {
    if (execvp(args[0], args) == -1) {
      perror("rshell");
    }
    exit(1);
  } else if (pid < 0) {
    perror("rshell");
    exit(1);
  } else {
    wait(&status);
  }
  free(args);
  return 1;
}


x86 assembly (clang, -O0):
<rshell_exec_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <rshell_parse_cmd@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ce <rshell_exec_cmd+0x5e>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <execvp@plt>
cmp    $0xffffffff,%eax
jne    11c4 <rshell_exec_cmd+0x54>
lea    0xe41(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jge    11ef <rshell_exec_cmd+0x7f>
lea    0xe20(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x14(%rbp),%rdi
call   1080 <wait@plt>
jmp    11fd <rshell_exec_cmd+0x8d>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rshell_exec_cmd>:
push   %rbx
sub    $0x10,%rsp
call   1040 <rshell_parse_cmd@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     11a8 <rshell_exec_cmd+0x38>
js     11ce <rshell_exec_cmd+0x5e>
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1060 <execvp@plt>
cmp    $0xffffffff,%eax
jne    11c4 <rshell_exec_cmd+0x54>
lea    0xe41(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe2b(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"rshell\00"
define i32 @rshell_exec_cmd(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8**
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i8** @rshell_parse_cmd(i8* %6)
store i8** %7, i8*** %5
%8 = call i64 (...) @fork()
store i64 %8, i64* %3
%9 = load i64, i64* %3
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %22
11: ; preds = %1
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = load i8**, i8*** %5
%16 = call i32 @execvp(i8* %14, i8** %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %11
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %1
%23 = load i64, i64* %3
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %22
%29 = call i32 @wait(i32* %4)
br label %30
30: ; preds = %28
br label %31
31: ; preds = %30
%32 = load i8**, i8*** %5
%33 = call i32 @free(i8** %32)
ret i32 1
}

LLVM IR (-O2):
@.str = [7 x i8] c"rshell\00"
define i32 @rshell_exec_cmd(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i8** @rshell_parse_cmd(i8* %0)
%5 = tail call i64 (...) @fork()
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %15
7: ; preds = %1
%8 = load i8*, i8** %4
%9 = tail call i32 @execvp(i8* %8, i8** %4)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %7
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = icmp slt i64 %5, 0
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %15
%21 = call i32 @wait(i32* %2)
%22 = call i32 @free(i8** %4)
ret i32 1
}