#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*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_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*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ stderr;
int pipe_exec(char *argv[]) {
  int pipefd[2];
  pid_t f;
  if (pipe(pipefd) < 0) {
    fprintf(stderr, "%s: couldn't create pipe", argv[0]);
    perror(NULL);
    return -1;
  }
  if ((f = fork()) < 0) {
    fprintf(stderr, "%s", argv[0]);
    perror(NULL);
    return -1;
  }
  if (f == 0) {
    dup2(pipefd[1], STDOUT_FILENO);
    close(pipefd[0]);
    if (do_redirect(argv) < 0) {
      exit(EXIT_FAILURE);
    }
    if (execvp(argv[0], argv) < 0) {
      perror("execvp() failed");
      exit(EXIT_FAILURE);
    }
  } else {
    dup2(pipefd[0], STDIN_FILENO);
    close(pipefd[1]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<pipe_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jge    11e0 <pipe_exec+0x50>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe3a(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <pipe_exec+0x134>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jge    1226 <pipe_exec+0x96>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe0d(%rip),%rsi
call   1060 <fprintf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <pipe_exec+0x134>
cmpq   $0x0,-0x20(%rbp)
jne    12a4 <pipe_exec+0x114>
mov    -0x14(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rdi
call   1070 <do_redirect@plt>
cmp    $0x0,%rax
jge    126b <pipe_exec+0xdb>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <execvp@plt>
cmp    $0x0,%rax
jge    129f <pipe_exec+0x10f>
lea    0xd90(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    12bd <pipe_exec+0x12d>
mov    -0x18(%rbp),%edi
mov    0x2d1a(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipe_exec>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
js     1236 <pipe_exec+0xa6>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     124b <pipe_exec+0xbb>
je     11df <pipe_exec+0x4f>
mov    0x8(%rsp),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    126f <pipe_exec+0xdf>
mov    0xc(%rsp),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    %rbx,%rdi
call   1070 <do_redirect@plt>
test   %rax,%rax
js     1228 <pipe_exec+0x98>
mov    (%rbx),%rdi
mov    %rbx,%rsi
call   1090 <execvp@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    126f <pipe_exec+0xdf>
lea    0xdf9(%rip),%rdi
call   1080 <perror@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdb7(%rip),%rsi
jmp    125e <pipe_exec+0xce>
mov    0x2d86(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xdbb(%rip),%rsi
call   1060 <fprintf@plt>
xor    %edi,%edi
call   1080 <perror@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [25 x i8] c"%s: couldn't create pipe\00"
@.str.1 = [3 x i8] c"%s\00"
@STDOUT_FILENO = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [16 x i8] c"execvp() failed\00"
@STDIN_FILENO = global i32 0
define i32 @pipe_exec(i8** %0) {
%2 = alloca i32
%3 = alloca i8**
%4 = alloca [2 x i32]
%5 = alloca i64
store i8** %0, i8*** %3
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = load i8**, i8*** %3
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i32 @perror(i8* null)
store i32 -1, i32* %2
br label %64
16: ; preds = %1
%17 = call i64 (...) @fork()
store i64 %17, i64* %5
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = load i8**, i8*** %3
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = call i32 @perror(i8* null)
store i32 -1, i32* %2
br label %64
26: ; preds = %16
%27 = load i64, i64* %5
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %55
29: ; preds = %26
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%31 = load i32, i32* %30
%32 = load i32, i32* @STDOUT_FILENO
%33 = call i32 @dup2(i32 %31, i32 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = load i8**, i8*** %3
%38 = call i64 @do_redirect(i8** %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %29
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %29
%44 = load i8**, i8*** %3
%45 = getelementptr inbounds i8*, i8** %44, i64 0
%46 = load i8*, i8** %45
%47 = load i8**, i8*** %3
%48 = call i64 @execvp(i8* %46, i8** %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %43
%51 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%52 = load i32, i32* @EXIT_FAILURE
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %43
br label %63
55: ; preds = %26
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%57 = load i32, i32* %56
%58 = load i32, i32* @STDIN_FILENO
%59 = call i32 @dup2(i32 %57, i32 %58)
%60 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%61 = load i32, i32* %60
%62 = call i32 @close(i32 %61)
br label %63
63: ; preds = %55, %54
store i32 0, i32* %2
br label %64
64: ; preds = %63, %19, %9
%65 = load i32, i32* %2
ret i32 %65
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [25 x i8] c"%s: couldn't create pipe\00"
@.str.1 = [3 x i8] c"%s\00"
@STDOUT_FILENO = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [16 x i8] c"execvp() failed\00"
@STDIN_FILENO = global i32 0
define i32 @pipe_exec(i8** %0) {
%2 = alloca [2 x i32]
%3 = bitcast [2 x i32]* %2 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%5 = call i64 @pipe(i32* %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %0
%10 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = call i32 @perror(i8* null)
br label %49
12: ; preds = %1
%13 = call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %0
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i32 @perror(i8* null)
br label %49
20: ; preds = %12
%21 = icmp eq i64 %13, 0
br i1 %21, label %22, label %42
22: ; preds = %20
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%24 = load i32, i32* %23
%25 = load i32, i32* @STDOUT_FILENO
%26 = call i32 @dup2(i32 %24, i32 %25)
%27 = load i32, i32* %4
%28 = call i32 @close(i32 %27)
%29 = call i64 @do_redirect(i8** %0)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %22
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %22
%35 = load i8*, i8** %0
%36 = call i64 @execvp(i8* %35, i8** %0)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %49
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = call i32 @exit(i32 %40)
unreachable
42: ; preds = %20
%43 = load i32, i32* %4
%44 = load i32, i32* @STDIN_FILENO
%45 = call i32 @dup2(i32 %43, i32 %44)
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %42, %34, %15, %7
%50 = phi i32 [ -1, %7 ], [ -1, %15 ], [ 0, %34 ], [ 0, %42 ]
ret i32 %50
}