#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execl(char *, char *, char *, char *, char *);
int /*<<< orphan*/ exit(int);
int fork();
int pipe(int *);
int open_pipe_fd(char *command) {
  int pipe_fds[2];
  int pid;
  if (pipe(pipe_fds) == -1)
    return -1;
  pid = fork();
  if (pid == 0) {
    close(pipe_fds[1]);
    if (pipe_fds[0] != 0) {
      dup2(pipe_fds[0], 0);
      close(pipe_fds[0]);
    }
    execl("/bin/sh", "sh", "-c", command, (char *)0);
    exit(127);
  }
  close(pipe_fds[0]);
  if (pid < 0) {
    close(pipe_fds[1]);
    return -1;
  }
  return pipe_fds[1];
}


x86 assembly (clang, -O0):
<open_pipe_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    118a <open_pipe_fd+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <open_pipe_fd+0xbb>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    11ef <open_pipe_fd+0x8f>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x18(%rbp)
je     11c2 <open_pipe_fd+0x62>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rcx
lea    0xe33(%rip),%rdi
lea    0xe31(%rip),%rsi
lea    0xe2d(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1070 <execl@plt>
mov    $0x7f,%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x0,-0x1c(%rbp)
jge    1215 <open_pipe_fd+0xb5>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121b <open_pipe_fd+0xbb>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_pipe_fd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
mov    $0xffffffff,%ebp
cmp    $0xffffffff,%eax
je     11a6 <open_pipe_fd+0x46>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11b1 <open_pipe_fd+0x51>
mov    %eax,%ebx
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
test   %ebx,%ebx
js     11a1 <open_pipe_fd+0x41>
mov    %edi,%ebp
jmp    11a6 <open_pipe_fd+0x46>
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
test   %edi,%edi
je     11d2 <open_pipe_fd+0x72>
xor    %esi,%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
lea    0xe27(%rip),%rdi
lea    0xe25(%rip),%rsi
lea    0xe21(%rip),%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
call   1070 <execl@plt>
mov    $0x7f,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
define i32 @open_pipe_fd(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [2 x i32]
%5 = alloca i32
store i8* %0, i8** %3
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %45
10: ; preds = %1
%11 = call i32 (...) @fork()
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %32
14: ; preds = %10
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%16 = load i32, i32* %15
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%19 = load i32, i32* %18
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %14
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @dup2(i32 %23, i32 0)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %21, %14
%29 = load i8*, i8** %3
%30 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %29, i8* null)
%31 = call i32 @exit(i32 127)
unreachable
32: ; preds = %10
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* %5
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %32
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
store i32 -1, i32* %2
br label %45
42: ; preds = %32
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%44 = load i32, i32* %43
store i32 %44, i32* %2
br label %45
45: ; preds = %42, %38, %9
%46 = load i32, i32* %2
ret i32 %46
}

LLVM IR (-O2):
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\00"
@.str.2 = [3 x i8] c"-c\00"
define i32 @open_pipe_fd(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 i32 @pipe(i32* %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %31, label %7
7: ; preds = %1
%8 = call i32 (...) @fork()
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %23
10: ; preds = %7
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%12 = load i32, i32* %11
%13 = call i32 @close(i32 %12)
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %10
%17 = call i32 @dup2(i32 %14, i32 0)
%18 = load i32, i32* %4
%19 = call i32 @close(i32 %18)
br label %20
20: ; preds = %16, %10
%21 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* null)
%22 = call i32 @exit(i32 127)
unreachable
23: ; preds = %7
%24 = load i32, i32* %4
%25 = call i32 @close(i32 %24)
%26 = icmp slt i32 %8, 0
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%28 = load i32, i32* %27
br i1 %26, label %29, label %31
29: ; preds = %23
%30 = call i32 @close(i32 %28)
br label %31
31: ; preds = %23, %1, %29
%32 = phi i32 [ -1, %29 ], [ -1, %1 ], [ %28, %23 ]
ret i32 %32
}