#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ F_DUPFD_CLOEXEC;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ close(int);
scalar_t__ do_redirect(char **);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
scalar_t__ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe_exec(char **);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
void execute_cmd(char *argv[]) {
  pid_t f;
  size_t i, j;
  int stdin_saved = fcntl(STDIN_FILENO, F_DUPFD_CLOEXEC, 0);
  if (stdin_saved < 0) {
    perror("Unable to dup stdin");
    return;
  }
  for (i = 0, j = 0; argv[i] != NULL; i++) {
    if (!strcmp(argv[i], "|")) {
      argv[i] = NULL;
      pipe_exec(&argv[j]);
      j = i + 1;
    }
  }
  if ((f = fork()) < 0) {
    perror("Couldn't fork");
    return;
  }
  if (f == 0) {
    if (do_redirect(&argv[j]) < 0) {
      exit(EXIT_FAILURE);
    }
    if (execvp(argv[j], &argv[j]) < 0) {
      perror("execvp() failed");
      exit(0);
    }
  } else {
    dup2(stdin_saved, STDIN_FILENO);
    close(stdin_saved);
    waitpid(f, NULL, 0);
  }
}


x86 assembly (clang, -O0):
<execute_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11f3 <execute_cmd+0x43>
lea    0xe17(%rip),%rdi
call   1090 <perror@plt>
jmp    1344 <execute_cmd+0x194>
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     127d <execute_cmd+0xcd>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xdeb(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    1267 <execute_cmd+0xb7>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
call   10c0 <pipe_exec@plt>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    126c <execute_cmd+0xbc>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    1203 <execute_cmd+0x53>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    12a3 <execute_cmd+0xf3>
lea    0xd7d(%rip),%rdi
call   1090 <perror@plt>
jmp    1344 <execute_cmd+0x194>
cmpq   $0x0,-0x10(%rbp)
jne    131c <execute_cmd+0x16c>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
call   1070 <do_redirect@plt>
cmp    $0x0,%rax
jge    12da <execute_cmd+0x12a>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
call   10a0 <execvp@plt>
cmp    $0x0,%rax
jge    1317 <execute_cmd+0x167>
lea    0xd19(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
jmp    1344 <execute_cmd+0x194>
mov    -0x24(%rbp),%edi
mov    0x2caa(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute_cmd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
js     12ad <execute_cmd+0xfd>
mov    %eax,%r14d
mov    (%r15),%rdi
test   %rdi,%rdi
je     1236 <execute_cmd+0x86>
xor    %ebp,%ebp
lea    0xe21(%rip),%r12
mov    %r15,%rbx
xor    %r13d,%r13d
jmp    1224 <execute_cmd+0x74>
nopl   0x0(%rax,%rax,1)
movq   $0x0,(%rbx)
lea    (%r15,%r13,8),%rdi
call   10c0 <pipe_exec@plt>
add    $0x1,%rbp
mov    %rbp,%r13
lea    (%r15,%rbp,8),%rbx
mov    (%r15,%rbp,8),%rdi
test   %rdi,%rdi
je     1239 <execute_cmd+0x89>
mov    %r12,%rsi
call   1060 <strcmp@plt>
test   %eax,%eax
je     1200 <execute_cmd+0x50>
add    $0x1,%rbp
jmp    1217 <execute_cmd+0x67>
xor    %r13d,%r13d
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     12b6 <execute_cmd+0x106>
je     127d <execute_cmd+0xcd>
mov    %rax,%r12
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1040 <dup2@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    %r12,%rdi
xor    %esi,%esi
xor    %edx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <waitpid@plt>
lea    (%r15,%r13,8),%rbx
mov    %rbx,%rdi
call   1070 <do_redirect@plt>
test   %rax,%rax
js     12d0 <execute_cmd+0x120>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   10a0 <execvp@plt>
test   %rax,%rax
js     12de <execute_cmd+0x12e>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd4c(%rip),%rdi
jmp    12bd <execute_cmd+0x10d>
lea    0xd59(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <perror@plt>
mov    0x2d01(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xd3f(%rip),%rdi
call   1090 <perror@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@F_DUPFD_CLOEXEC = global i32 0
@.str = [20 x i8] c"Unable to dup stdin\00"
@.str.1 = [2 x i8] c"|\00"
@.str.2 = [14 x i8] c"Couldn't fork\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [16 x i8] c"execvp() failed\00"
define void @execute_cmd(i8** %0) {
%2 = alloca i8**
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i8** %0, i8*** %2
%7 = load i32, i32* @STDIN_FILENO
%8 = load i32, i32* @F_DUPFD_CLOEXEC
%9 = call i32 @fcntl(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %81
14: ; preds = %1
store i64 0, i64* %4
store i64 0, i64* %5
br label %15
15: ; preds = %39, %14
%16 = load i8**, i8*** %2
%17 = load i64, i64* %4
%18 = getelementptr inbounds i8*, i8** %16, i64 %17
%19 = load i8*, i8** %18
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %42
21: ; preds = %15
%22 = load i8**, i8*** %2
%23 = load i64, i64* %4
%24 = getelementptr inbounds i8*, i8** %22, i64 %23
%25 = load i8*, i8** %24
%26 = call i32 @strcmp(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp ne i32 %26, 0
br i1 %27, label %38, label %28
28: ; preds = %21
%29 = load i8**, i8*** %2
%30 = load i64, i64* %4
%31 = getelementptr inbounds i8*, i8** %29, i64 %30
store i8* null, i8** %31
%32 = load i8**, i8*** %2
%33 = load i64, i64* %5
%34 = getelementptr inbounds i8*, i8** %32, i64 %33
%35 = call i32 @pipe_exec(i8** %34)
%36 = load i64, i64* %4
%37 = add i64 %36, 1
store i64 %37, i64* %5
br label %38
38: ; preds = %28, %21
br label %39
39: ; preds = %38
%40 = load i64, i64* %4
%41 = add i64 %40, 1
store i64 %41, i64* %4
br label %15
42: ; preds = %15
%43 = call i64 (...) @fork()
store i64 %43, i64* %3
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %81
47: ; preds = %42
%48 = load i64, i64* %3
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %73
50: ; preds = %47
%51 = load i8**, i8*** %2
%52 = load i64, i64* %5
%53 = getelementptr inbounds i8*, i8** %51, i64 %52
%54 = call i64 @do_redirect(i8** %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %50
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %50
%60 = load i8**, i8*** %2
%61 = load i64, i64* %5
%62 = getelementptr inbounds i8*, i8** %60, i64 %61
%63 = load i8*, i8** %62
%64 = load i8**, i8*** %2
%65 = load i64, i64* %5
%66 = getelementptr inbounds i8*, i8** %64, i64 %65
%67 = call i64 @execvp(i8* %63, i8** %66)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %59
%70 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%71 = call i32 @exit(i32 0)
unreachable
72: ; preds = %59
br label %81
73: ; preds = %47
%74 = load i32, i32* %6
%75 = load i32, i32* @STDIN_FILENO
%76 = call i32 @dup2(i32 %74, i32 %75)
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
%79 = load i64, i64* %3
%80 = call i32 @waitpid(i64 %79, i32* null, i32 0)
br label %81
81: ; preds = %12, %45, %73, %72
ret void
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@F_DUPFD_CLOEXEC = global i32 0
@.str = [20 x i8] c"Unable to dup stdin\00"
@.str.1 = [2 x i8] c"|\00"
@.str.2 = [14 x i8] c"Couldn't fork\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [16 x i8] c"execvp() failed\00"
define void @execute_cmd(i8** %0) {
%2 = load i32, i32* @STDIN_FILENO
%3 = load i32, i32* @F_DUPFD_CLOEXEC
%4 = tail call i32 @fcntl(i32 %2, i32 %3, i32 0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i8*, i8** %0
%8 = icmp eq i8* %7, null
br i1 %8, label %30, label %11
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %57
11: ; preds = %6, %24
%12 = phi i8* [ %28, %24 ], [ %7, %6 ]
%13 = phi i8** [ %27, %24 ], [ %0, %6 ]
%14 = phi i64 [ %25, %24 ], [ 0, %6 ]
%15 = phi i64 [ %26, %24 ], [ 0, %6 ]
%16 = tail call i32 @strcmp(i8* %12, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %11
%19 = add i64 %14, 1
br label %24
20: ; preds = %11
store i8* null, i8** %13
%21 = getelementptr inbounds i8*, i8** %0, i64 %15
%22 = tail call i32 @pipe_exec(i8** %21)
%23 = add i64 %14, 1
br label %24
24: ; preds = %18, %20
%25 = phi i64 [ %19, %18 ], [ %23, %20 ]
%26 = phi i64 [ %15, %18 ], [ %23, %20 ]
%27 = getelementptr inbounds i8*, i8** %0, i64 %25
%28 = load i8*, i8** %27
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %11
30: ; preds = %24, %6
%31 = phi i64 [ 0, %6 ], [ %26, %24 ]
%32 = tail call i64 (...) @fork()
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %57
36: ; preds = %30
%37 = icmp eq i64 %32, 0
br i1 %37, label %38, label %52
38: ; preds = %36
%39 = getelementptr inbounds i8*, i8** %0, i64 %31
%40 = tail call i64 @do_redirect(i8** %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* @EXIT_FAILURE
%44 = tail call i32 @exit(i32 %43)
unreachable
45: ; preds = %38
%46 = load i8*, i8** %39
%47 = tail call i64 @execvp(i8* %46, i8** %39)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %57
49: ; preds = %45
%50 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%51 = tail call i32 @exit(i32 0)
unreachable
52: ; preds = %36
%53 = load i32, i32* @STDIN_FILENO
%54 = tail call i32 @dup2(i32 %4, i32 %53)
%55 = tail call i32 @close(i32 %4)
%56 = tail call i32 @waitpid(i64 %32, i32* null, i32 0)
br label %57
57: ; preds = %52, %45, %34, %9
ret void
}