#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*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *const, char *const *);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *const);
scalar_t__ pipe(int *);
int /*<<< orphan*/ stderr;
int pipe_open(char *const argv[], pid_t *child_pid) {
  int pipefd[2];
  if (pipe(pipefd) < 0)
    return -1;
  pid_t pid = fork();
  if (pid == 0) {
    close(pipefd[0]);
    dup2(pipefd[1], STDOUT_FILENO);
    execvp(argv[0], argv);
    fprintf(stderr, "can't execute '%s'\n", argv[0]);
    fflush(stderr);
    _exit(EXIT_FAILURE);
  }
  if (child_pid)
    *child_pid = pid;
  close(pipefd[1]);
  fcntl(pipefd[0], F_SETFD, FD_CLOEXEC);
  return pipefd[0];
}


x86 assembly (clang, -O0):
<pipe_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11bf <pipe_open+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1274 <pipe_open+0xe4>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1236 <pipe_open+0xa6>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rsi
call   10a0 <execvp@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdeb(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
cmpq   $0x0,-0x18(%rbp)
je     124c <pipe_open+0xbc>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
mov    0x2d61(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipe_open>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsp,%rdi
call   1070 <pipe@plt>
test   %rax,%rax
js     11e3 <pipe_open+0x53>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     11f0 <pipe_open+0x60>
test   %rbx,%rbx
je     11bb <pipe_open+0x2b>
mov    %rax,(%rbx)
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    (%rsp),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    (%rsp),%eax
jmp    11e8 <pipe_open+0x58>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    (%rsp),%edi
call   1060 <close@plt>
mov    0x4(%rsp),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   10a0 <execvp@plt>
mov    0x2dbc(%rip),%rbx
mov    (%rbx),%edi
mov    (%r14),%rdx
lea    0xdd8(%rip),%rsi
call   1080 <fprintf@plt>
mov    (%rbx),%edi
call   1090 <fflush@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

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

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@stderr = global i32 0
@.str = [20 x i8] c"can't execute '%s'\0A\00"
@EXIT_FAILURE = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @pipe_open(i8** %0, i64* writeonly %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %39, label %8
8: ; preds = %2
%9 = call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %27
11: ; preds = %8
%12 = load i32, i32* %5
%13 = call i32 @close(i32 %12)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%15 = load i32, i32* %14
%16 = load i32, i32* @STDOUT_FILENO
%17 = call i32 @dup2(i32 %15, i32 %16)
%18 = load i8*, i8** %0
%19 = call i32 @execvp(i8* %18, i8** %0)
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %0
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @stderr
%24 = call i32 @fflush(i32 %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @_exit(i32 %25)
unreachable
27: ; preds = %8
%28 = icmp eq i64* %1, null
br i1 %28, label %30, label %29
29: ; preds = %27
store i64 %9, i64* %1
br label %30
30: ; preds = %29, %27
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %5
%35 = load i32, i32* @F_SETFD
%36 = load i32, i32* @FD_CLOEXEC
%37 = call i32 @fcntl(i32 %34, i32 %35, i32 %36)
%38 = load i32, i32* %5
br label %39
39: ; preds = %2, %30
%40 = phi i32 [ %38, %30 ], [ -1, %2 ]
ret i32 %40
}