#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 STDIN_FILENO;
int STDOUT_FILENO;
int close(int);
int dup2(int, int);
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ strlen(char *);
char *strrchr(char *, char);
void execute_command(char **arguments, int *fd, int pipe_action) {
  pid_t pid;
  char *command;
  pid = fork();
  if (pid < 0) {
    perror("Unable to fork");
    exit(EXIT_FAILURE);
  }
  if (pid == 0) {
    if (arguments[0] == NULL || strlen(arguments[0]) == 0) {
      exit(EXIT_FAILURE);
    }
    command = arguments[0];
    if (strrchr(arguments[0], '/') != NULL) {
      arguments[0] = strrchr(arguments[0], '/') + 1;
    }
    if (pipe_action == 1) {
      if (close(fd[0]) == -1) {
        perror(NULL);
        exit(EXIT_FAILURE);
      }
      if (close(STDOUT_FILENO) == -1) {
        perror(NULL);
        exit(EXIT_FAILURE);
      }
      if (dup2(fd[1], STDOUT_FILENO) == -1) {
        perror(NULL);
        exit(EXIT_FAILURE);
      }
    }
    if (pipe_action == 2) {
      if (close(fd[1]) == -1) {
        perror(NULL);
        exit(EXIT_FAILURE);
      }
      if (close(STDIN_FILENO) == -1) {
        perror(NULL);
        exit(EXIT_FAILURE);
      }
      if (dup2(fd[0], STDIN_FILENO) == -1) {
        perror(NULL);
        exit(EXIT_FAILURE);
      }
    }
    execvp(command, arguments);
    perror(command);
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<execute_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11c3 <execute_command+0x43>
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x20(%rbp)
jne    13a3 <execute_command+0x223>
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
je     11f2 <execute_command+0x72>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
jne    1200 <execute_command+0x80>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
cmp    $0x0,%rax
je     1245 <execute_command+0xc5>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,%rcx
add    $0x1,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
cmpl   $0x1,-0x14(%rbp)
jne    12e2 <execute_command+0x162>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    127a <execute_command+0xfa>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    12a8 <execute_command+0x128>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
mov    0x2d3a(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    12dd <execute_command+0x15d>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    12e2 <execute_command+0x162>
cmpl   $0x2,-0x14(%rbp)
jne    137f <execute_command+0x1ff>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    1318 <execute_command+0x198>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    1346 <execute_command+0x1c6>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2c7d(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
cmp    $0xffffffff,%eax
jne    137a <execute_command+0x1fa>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <perror@plt>
mov    0x2c65(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
jmp    137f <execute_command+0x1ff>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <execvp@plt>
mov    -0x28(%rbp),%rdi
call   1070 <perror@plt>
mov    0x2c3c(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute_command>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r15
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     1198 <execute_command+0x28>
je     11a6 <execute_command+0x36>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe61(%rip),%rdi
call   1060 <perror@plt>
jmp    11b4 <execute_command+0x44>
mov    (%r15),%r14
test   %r14,%r14
je     11b4 <execute_command+0x44>
cmpb   $0x0,(%r14)
jne    11c2 <execute_command+0x52>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     11db <execute_command+0x6b>
add    $0x1,%rax
mov    %rax,(%r15)
cmp    $0x2,%ebp
je     120a <execute_command+0x9a>
cmp    $0x1,%ebp
jne    1241 <execute_command+0xd1>
mov    (%rbx),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     123a <execute_command+0xca>
mov    0x2df8(%rip),%rbp
mov    0x0(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     123a <execute_command+0xca>
mov    0x4(%rbx),%edi
jmp    122d <execute_command+0xbd>
mov    0x4(%rbx),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     123a <execute_command+0xca>
mov    0x2db2(%rip),%rbp
mov    0x0(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     123a <execute_command+0xca>
mov    (%rbx),%edi
mov    0x0(%rbp),%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
jne    1241 <execute_command+0xd1>
xor    %edi,%edi
jmp    119f <execute_command+0x2f>
mov    %r14,%rdi
mov    %r15,%rsi
call   1070 <execvp@plt>
mov    %r14,%rdi
jmp    119f <execute_command+0x2f>

LLVM IR (-O0):
@.str = [15 x i8] c"Unable to fork\00"
@EXIT_FAILURE = global i32 0
@STDOUT_FILENO = global i32 0
@STDIN_FILENO = global i32 0
define void @execute_command(i8** %0, i32* %1, i32 %2) {
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
store i8** %0, i8*** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%9 = call i64 (...) @fork()
store i64 %9, i64* %7
%10 = load i64, i64* %7
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %3
%17 = load i64, i64* %7
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %124
19: ; preds = %16
%20 = load i8**, i8*** %4
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = icmp eq i8* %22, null
br i1 %23, label %30, label %24
24: ; preds = %19
%25 = load i8**, i8*** %4
%26 = getelementptr inbounds i8*, i8** %25, i64 0
%27 = load i8*, i8** %26
%28 = call i64 @strlen(i8* %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %24, %19
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
%34 = load i8**, i8*** %4
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
store i8* %36, i8** %8
%37 = load i8**, i8*** %4
%38 = getelementptr inbounds i8*, i8** %37, i64 0
%39 = load i8*, i8** %38
%40 = call i8* @strrchr(i8* %39, i8 signext 47)
%41 = icmp ne i8* %40, null
br i1 %41, label %42, label %50
42: ; preds = %33
%43 = load i8**, i8*** %4
%44 = getelementptr inbounds i8*, i8** %43, i64 0
%45 = load i8*, i8** %44
%46 = call i8* @strrchr(i8* %45, i8 signext 47)
%47 = getelementptr inbounds i8, i8* %46, i64 1
%48 = load i8**, i8*** %4
%49 = getelementptr inbounds i8*, i8** %48, i64 0
store i8* %47, i8** %49
br label %50
50: ; preds = %42, %33
%51 = load i32, i32* %6
%52 = icmp eq i32 %51, 1
br i1 %52, label %53, label %83
53: ; preds = %50
%54 = load i32*, i32** %5
%55 = getelementptr inbounds i32, i32* %54, i64 0
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %63
59: ; preds = %53
%60 = call i32 @perror(i8* null)
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %53
%64 = load i32, i32* @STDOUT_FILENO
%65 = call i32 @close(i32 %64)
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %71
67: ; preds = %63
%68 = call i32 @perror(i8* null)
%69 = load i32, i32* @EXIT_FAILURE
%70 = call i32 @exit(i32 %69)
unreachable
71: ; preds = %63
%72 = load i32*, i32** %5
%73 = getelementptr inbounds i32, i32* %72, i64 1
%74 = load i32, i32* %73
%75 = load i32, i32* @STDOUT_FILENO
%76 = call i32 @dup2(i32 %74, i32 %75)
%77 = icmp eq i32 %76, -1
br i1 %77, label %78, label %82
78: ; preds = %71
%79 = call i32 @perror(i8* null)
%80 = load i32, i32* @EXIT_FAILURE
%81 = call i32 @exit(i32 %80)
unreachable
82: ; preds = %71
br label %83
83: ; preds = %82, %50
%84 = load i32, i32* %6
%85 = icmp eq i32 %84, 2
br i1 %85, label %86, label %116
86: ; preds = %83
%87 = load i32*, i32** %5
%88 = getelementptr inbounds i32, i32* %87, i64 1
%89 = load i32, i32* %88
%90 = call i32 @close(i32 %89)
%91 = icmp eq i32 %90, -1
br i1 %91, label %92, label %96
92: ; preds = %86
%93 = call i32 @perror(i8* null)
%94 = load i32, i32* @EXIT_FAILURE
%95 = call i32 @exit(i32 %94)
unreachable
96: ; preds = %86
%97 = load i32, i32* @STDIN_FILENO
%98 = call i32 @close(i32 %97)
%99 = icmp eq i32 %98, -1
br i1 %99, label %100, label %104
100: ; preds = %96
%101 = call i32 @perror(i8* null)
%102 = load i32, i32* @EXIT_FAILURE
%103 = call i32 @exit(i32 %102)
unreachable
104: ; preds = %96
%105 = load i32*, i32** %5
%106 = getelementptr inbounds i32, i32* %105, i64 0
%107 = load i32, i32* %106
%108 = load i32, i32* @STDIN_FILENO
%109 = call i32 @dup2(i32 %107, i32 %108)
%110 = icmp eq i32 %109, -1
br i1 %110, label %111, label %115
111: ; preds = %104
%112 = call i32 @perror(i8* null)
%113 = load i32, i32* @EXIT_FAILURE
%114 = call i32 @exit(i32 %113)
unreachable
115: ; preds = %104
br label %116
116: ; preds = %115, %83
%117 = load i8*, i8** %8
%118 = load i8**, i8*** %4
%119 = call i32 @execvp(i8* %117, i8** %118)
%120 = load i8*, i8** %8
%121 = call i32 @perror(i8* %120)
%122 = load i32, i32* @EXIT_FAILURE
%123 = call i32 @exit(i32 %122)
unreachable
124: ; preds = %16
ret void
}

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