#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 int pid_t;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ free(char **);
char **parse_cmd(char *);
int /*<<< orphan*/ perror(int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
void exec_cmd(char *cmd) {
  int str_end = strlen(cmd) - 1, background = 0;
  if (cmd[str_end] == '&') {
    background = 1;
    cmd[str_end] = '\0';
  }
  char **args = parse_cmd(cmd);
  pid_t pid = fork();
  if (pid == -1) {
    perror(strerror(errno));
    exit(errno);
  }
  if (pid && !background) {
    wait(NULL);
  } else if (!pid) {
    execvp(args[0], args);
    perror(strerror(errno));
  }
  free(args);
}


x86 assembly (clang, -O0):
<exec_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x26,%eax
jne    11da <exec_cmd+0x4a>
movl   $0x1,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
call   1050 <parse_cmd@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    121e <exec_cmd+0x8e>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%edi
call   1060 <perror@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
je     1240 <exec_cmd+0xb0>
cmpl   $0x0,-0x10(%rbp)
jne    1240 <exec_cmd+0xb0>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
jmp    1274 <exec_cmd+0xe4>
cmpl   $0x0,-0x1c(%rbp)
jne    126f <exec_cmd+0xdf>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <execvp@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%edi
call   1060 <perror@plt>
jmp    1274 <exec_cmd+0xe4>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_cmd>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <strlen@plt>
cltq
mov    -0x1(%rax,%rbx,1),%bpl
cmp    $0x26,%bpl
jne    11ad <exec_cmd+0x1d>
movb   $0x0,-0x1(%rbx,%rax,1)
mov    %rbx,%rdi
call   1050 <parse_cmd@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   10b0 <fork@plt>
cmp    $0xffffffff,%eax
je     1209 <exec_cmd+0x79>
test   %eax,%eax
je     11d7 <exec_cmd+0x47>
cmp    $0x26,%bpl
je     11d7 <exec_cmd+0x47>
xor    %edi,%edi
call   10a0 <wait@plt>
jmp    11fb <exec_cmd+0x6b>
test   %eax,%eax
jne    11fb <exec_cmd+0x6b>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1070 <execvp@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%edi
call   1060 <perror@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <free@plt>
mov    0x2dd0(%rip),%rbx
mov    (%rbx),%edi
call   1090 <strerror@plt>
mov    %eax,%edi
call   1060 <perror@plt>
mov    (%rbx),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@errno = global i32 0
define void @exec_cmd(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @strlen(i8* %7)
%9 = sub nsw i32 %8, 1
store i32 %9, i32* %3
store i32 0, i32* %4
%10 = load i8*, i8** %2
%11 = load i32, i32* %3
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 38
br i1 %16, label %17, label %22
17: ; preds = %1
store i32 1, i32* %4
%18 = load i8*, i8** %2
%19 = load i32, i32* %3
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %17, %1
%23 = load i8*, i8** %2
%24 = call i8** @parse_cmd(i8* %23)
store i8** %24, i8*** %5
%25 = call i32 (...) @fork()
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %34
28: ; preds = %22
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 @perror(i32 %30)
%32 = load i32, i32* @errno
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %22
%35 = load i32, i32* %6
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = load i32, i32* %4
%39 = icmp ne i32 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = call i32 @wait(i32* null)
br label %55
42: ; preds = %37, %34
%43 = load i32, i32* %6
%44 = icmp ne i32 %43, 0
br i1 %44, label %54, label %45
45: ; preds = %42
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 0
%48 = load i8*, i8** %47
%49 = load i8**, i8*** %5
%50 = call i32 @execvp(i8* %48, i8** %49)
%51 = load i32, i32* @errno
%52 = call i32 @strerror(i32 %51)
%53 = call i32 @perror(i32 %52)
br label %54
54: ; preds = %45, %42
br label %55
55: ; preds = %54, %40
%56 = load i8**, i8*** %5
%57 = call i32 @free(i8** %56)
ret void
}

LLVM IR (-O2):
@errno = global i32 0
define void @exec_cmd(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = add nsw i32 %2, -1
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 38
br i1 %7, label %8, label %9
8: ; preds = %1
store i8 0, i8* %5
br label %9
9: ; preds = %8, %1
%10 = tail call i8** @parse_cmd(i8* %0)
%11 = tail call i32 (...) @fork()
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = load i32, i32* @errno
%15 = tail call i32 @strerror(i32 %14)
%16 = tail call i32 @perror(i32 %15)
%17 = load i32, i32* @errno
%18 = tail call i32 @exit(i32 %17)
unreachable
19: ; preds = %9
%20 = icmp eq i32 %11, 0
%21 = select i1 %20, i1 true, i1 %7
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = tail call i32 @wait(i32* null)
br label %31
24: ; preds = %19
br i1 %20, label %25, label %31
25: ; preds = %24
%26 = load i8*, i8** %10
%27 = tail call i32 @execvp(i8* %26, i8** %10)
%28 = load i32, i32* @errno
%29 = tail call i32 @strerror(i32 %28)
%30 = tail call i32 @perror(i32 %29)
br label %31
31: ; preds = %24, %25, %22
%32 = tail call i32 @free(i8** %10)
ret void
}