#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;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int BUFSIZ;
int /*<<< orphan*/ EXIT_FAILURE;
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
scalar_t__ WIFSIGNALED(int);
int /*<<< orphan*/ WTERMSIG(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ execv(char *const, char *const *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
size_t read(int, char *, int);
char *strsignal(int /*<<< orphan*/);
int /*<<< orphan*/ wait(int *);
int pipe_exec(int argc, char *const argv[]) {
  char buf[BUFSIZ];
  size_t s, size;
  int status;
  int fd[2];
  pid_t pid;
  if (pipe(fd) == -1) {
    perror("pipe");
    return -1;
  }
  pid = fork();
  if (pid == -1) {
    perror("fork");
    return -1;
  }
  if (pid) {
    close(fd[1]);
    for (;;) {
      s = read(fd[0], buf, sizeof(buf));
      if (s == -1) {
        perror("read");
        return -1;
      } else if (!s) {
        break;
      }
      buf[s] = 0;
      printf("%s", buf);
    }
    close(fd[0]);
    wait(&status);
    if (WIFEXITED(status))
      return WEXITSTATUS(status);
    else if (WIFSIGNALED(status))
      printf("%s\n", strsignal(WTERMSIG(status)));
    return status;
  }
  close(fd[0]);
  execv(argv[0], argv);
  perror("execv");
  exit(EXIT_FAILURE);
}


x86 assembly (clang, -O0):
<pipe_exec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x58(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
lea    -0x3c(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1250 <pipe_exec+0x70>
lea    0xdc8(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13ba <pipe_exec+0x1da>
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    1283 <pipe_exec+0xa3>
lea    0xd9a(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13ba <pipe_exec+0x1da>
cmpl   $0x0,-0x40(%rbp)
je     1388 <pipe_exec+0x1a8>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
mov    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rax
mov    -0x3c(%rbp),%edi
mov    %eax,%edx
call   1060 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    12d5 <pipe_exec+0xf5>
lea    0xd4d(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13ba <pipe_exec+0x1da>
cmpq   $0x0,-0x28(%rbp)
jne    12e5 <pipe_exec+0x105>
jmp    1307 <pipe_exec+0x127>
jmp    12ea <pipe_exec+0x10a>
mov    -0x50(%rbp),%rsi
mov    -0x28(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
lea    0xd12(%rip),%rdi
call   1030 <printf@plt>
jmp    1295 <pipe_exec+0xb5>
mov    -0x3c(%rbp),%edi
call   1040 <close@plt>
lea    -0x34(%rbp),%rdi
call   10e0 <wait@plt>
mov    -0x34(%rbp),%edi
call   10c0 <WIFEXITED@plt>
cmp    $0x0,%rax
je     1341 <pipe_exec+0x161>
mov    -0x34(%rbp),%edi
call   10a0 <WEXITSTATUS@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13ba <pipe_exec+0x1da>
mov    -0x34(%rbp),%edi
call   1070 <WIFSIGNALED@plt>
cmp    $0x0,%rax
je     1371 <pipe_exec+0x191>
mov    -0x34(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    %eax,%edi
call   1090 <strsignal@plt>
mov    %rax,%rsi
lea    0xca6(%rip),%rdi
call   1030 <printf@plt>
jmp    1376 <pipe_exec+0x196>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    13ba <pipe_exec+0x1da>
mov    -0x3c(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   10f0 <execv@plt>
lea    0xc6f(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c25(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipe_exec>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    0x2de9(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x24(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     12cf <pipe_exec+0xef>
xor    %eax,%eax
call   1100 <fork@plt>
cmp    $0xffffffff,%eax
je     12d8 <pipe_exec+0xf8>
test   %eax,%eax
je     12fd <pipe_exec+0x11d>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
lea    0xdcb(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    -0x24(%rbp),%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1060 <read@plt>
test   %rax,%rax
je     127a <pipe_exec+0x9a>
cmp    $0xffffffffffffffff,%rax
je     12e1 <pipe_exec+0x101>
movb   $0x0,(%rbx,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
jmp    1250 <pipe_exec+0x70>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
lea    -0x1c(%rbp),%rdi
call   10e0 <wait@plt>
mov    -0x1c(%rbp),%edi
call   10c0 <WIFEXITED@plt>
mov    -0x1c(%rbp),%edi
test   %rax,%rax
je     12a2 <pipe_exec+0xc2>
call   10a0 <WEXITSTATUS@plt>
jmp    12f2 <pipe_exec+0x112>
call   1070 <WIFSIGNALED@plt>
test   %rax,%rax
je     12ca <pipe_exec+0xea>
mov    -0x1c(%rbp),%edi
call   1080 <WTERMSIG@plt>
mov    %eax,%edi
call   1090 <strsignal@plt>
lea    0xd50(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    -0x1c(%rbp),%eax
jmp    12f2 <pipe_exec+0x112>
lea    0xd2a(%rip),%rdi
jmp    12e8 <pipe_exec+0x108>
lea    0xd26(%rip),%rdi
jmp    12e8 <pipe_exec+0x108>
lea    0xd22(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    (%r15),%rdi
mov    %r15,%rsi
call   10f0 <execv@plt>
lea    0xcff(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@BUFSIZ = global i32 0
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [6 x i8] c"execv\00"
@EXIT_FAILURE = global i32 0
define i32 @pipe_exec(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca [2 x i32]
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* @BUFSIZ
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %6
%17 = alloca i8, i64 %15
store i64 %15, i64* %7
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%19 = call i32 @pipe(i32* %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %13
br label %89
23: ; preds = %2
%24 = call i32 (...) @fork()
store i32 %24, i32* %12
%25 = load i32, i32* %12
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %13
br label %89
29: ; preds = %23
%30 = load i32, i32* %12
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %77
32: ; preds = %29
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
br label %36
36: ; preds = %50, %32
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%38 = load i32, i32* %37
%39 = trunc i64 %15 to i32
%40 = call i64 @read(i32 %38, i8* %17, i32 %39)
store i64 %40, i64* %8
%41 = load i64, i64* %8
%42 = icmp eq i64 %41, -1
br i1 %42, label %43, label %45
43: ; preds = %36
%44 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %13
br label %89
45: ; preds = %36
%46 = load i64, i64* %8
%47 = icmp ne i64 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %45
br label %54
49: ; preds = %45
br label %50
50: ; preds = %49
%51 = load i64, i64* %8
%52 = getelementptr inbounds i8, i8* %17, i64 %51
store i8 0, i8* %52
%53 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %17)
br label %36
54: ; preds = %48
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
%58 = call i32 @wait(i32* %10)
%59 = load i32, i32* %10
%60 = call i64 @WIFEXITED(i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %54
%63 = load i32, i32* %10
%64 = call i32 @WEXITSTATUS(i32 %63)
store i32 %64, i32* %3
store i32 1, i32* %13
br label %89
65: ; preds = %54
%66 = load i32, i32* %10
%67 = call i64 @WIFSIGNALED(i32 %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %74
69: ; preds = %65
%70 = load i32, i32* %10
%71 = call i32 @WTERMSIG(i32 %70)
%72 = call i8* @strsignal(i32 %71)
%73 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %72)
br label %74
74: ; preds = %69, %65
br label %75
75: ; preds = %74
%76 = load i32, i32* %10
store i32 %76, i32* %3
store i32 1, i32* %13
br label %89
77: ; preds = %29
%78 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%79 = load i32, i32* %78
%80 = call i32 @close(i32 %79)
%81 = load i8**, i8*** %5
%82 = getelementptr inbounds i8*, i8** %81, i64 0
%83 = load i8*, i8** %82
%84 = load i8**, i8*** %5
%85 = call i32 @execv(i8* %83, i8** %84)
%86 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%87 = load i32, i32* @EXIT_FAILURE
%88 = call i32 @exit(i32 %87)
unreachable
89: ; preds = %75, %62, %43, %27, %21
%90 = load i8*, i8** %6
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
@BUFSIZ = global i32 0
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [6 x i8] c"execv\00"
@EXIT_FAILURE = global i32 0
define i32 @pipe_exec(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca [2 x i32]
%5 = load i32, i32* @BUFSIZ
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast i32* %3 to i8*
%9 = bitcast [2 x i32]* %4 to i8*
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%11 = call i32 @pipe(i32* %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %59
15: ; preds = %2
%16 = call i32 (...) @fork()
switch i32 %16, label %19 [
i32 -1, label %17
i32 0, label %51
]
17: ; preds = %15
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %59
19: ; preds = %15
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
br label %23
23: ; preds = %28, %19
%24 = load i32, i32* %10
%25 = call i64 @read(i32 %24, i8* %7, i32 %5)
switch i64 %25, label %28 [
i64 -1, label %26
i64 0, label %31
]
26: ; preds = %23
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %59
28: ; preds = %23
%29 = getelementptr inbounds i8, i8* %7, i64 %25
store i8 0, i8* %29
%30 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %7)
br label %23
31: ; preds = %23
%32 = load i32, i32* %10
%33 = call i32 @close(i32 %32)
%34 = call i32 @wait(i32* %3)
%35 = load i32, i32* %3
%36 = call i64 @WIFEXITED(i32 %35)
%37 = icmp eq i64 %36, 0
%38 = load i32, i32* %3
br i1 %37, label %41, label %39
39: ; preds = %31
%40 = call i32 @WEXITSTATUS(i32 %38)
br label %59
41: ; preds = %31
%42 = call i64 @WIFSIGNALED(i32 %38)
%43 = icmp eq i64 %42, 0
br i1 %43, label %49, label %44
44: ; preds = %41
%45 = load i32, i32* %3
%46 = call i32 @WTERMSIG(i32 %45)
%47 = call i8* @strsignal(i32 %46)
%48 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %47)
br label %49
49: ; preds = %41, %44
%50 = load i32, i32* %3
br label %59
51: ; preds = %15
%52 = load i32, i32* %10
%53 = call i32 @close(i32 %52)
%54 = load i8*, i8** %1
%55 = call i32 @execv(i8* %54, i8** %1)
%56 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %49, %39, %26, %17, %13
%60 = phi i32 [ -1, %13 ], [ -1, %17 ], [ -1, %26 ], [ %40, %39 ], [ %50, %49 ]
ret i32 %60
}