#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int dup2(int, int);
int errno;
int open(char *, int /*<<< orphan*/);
int pipe(int pipefd[2]) {
  int fd1 = open("/dev/pipe/0", O_RDWR);
  int fd2 = -1;
  int saveerr;
  fd2 = dup2(fd1, -1);
  if (fd2 == -1) {
    saveerr = errno;
    close(fd1);
    errno = saveerr;
    return -1;
  }
  pipefd[0] = fd1;
  pipefd[1] = fd2;
  return 0;
}


x86 assembly (clang, -O0):
<pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ea5(%rip),%rax
mov    (%rax),%esi
lea    0xeb4(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    $0xffffffff,%esi
call   1030 <dup2@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11a1 <pipe+0x71>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2e45(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11bb <pipe+0x8b>
mov    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x4(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipe>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2ea7(%rip),%rax
mov    (%rax),%esi
lea    0xeb6(%rip),%rdi
call   1050 <open@plt>
mov    %eax,%ebx
mov    $0xffffffff,%ebp
mov    %eax,%edi
mov    $0xffffffff,%esi
call   1030 <dup2@plt>
cmp    $0xffffffff,%eax
je     1172 <pipe+0x42>
mov    %ebx,(%r14)
mov    %eax,0x4(%r14)
xor    %ebp,%ebp
jmp    1186 <pipe+0x56>
mov    0x2e5f(%rip),%r14
mov    (%r14),%r15d
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r15d,(%r14)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/dev/pipe/0\00"
@O_RDWR = global i32 0
@errno = global i32 0
define i32 @pipe(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
store i32 -1, i32* %5
%9 = load i32, i32* %4
%10 = call i32 @dup2(i32 %9, i32 -1)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %18
13: ; preds = %1
%14 = load i32, i32* @errno
store i32 %14, i32* %6
%15 = load i32, i32* %4
%16 = call i32 @close(i32 %15)
%17 = load i32, i32* %6
store i32 %17, i32* @errno
store i32 -1, i32* %2
br label %25
18: ; preds = %1
%19 = load i32, i32* %4
%20 = load i32*, i32** %3
%21 = getelementptr inbounds i32, i32* %20, i64 0
store i32 %19, i32* %21
%22 = load i32, i32* %5
%23 = load i32*, i32** %3
%24 = getelementptr inbounds i32, i32* %23, i64 1
store i32 %22, i32* %24
store i32 0, i32* %2
br label %25
25: ; preds = %18, %13
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@.str = [12 x i8] c"/dev/pipe/0\00"
@O_RDWR = global i32 0
@errno = global i32 0
define i32 @pipe(i32* nocapture writeonly %0) {
%2 = load i32, i32* @O_RDWR
%3 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = tail call i32 @dup2(i32 %3, i32 -1)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = tail call i32 @close(i32 %3)
store i32 %7, i32* @errno
br label %11
9: ; preds = %1
store i32 %3, i32* %0
%10 = getelementptr inbounds i32, i32* %0, i64 1
store i32 %4, i32* %10
br label %11
11: ; preds = %9, %6
%12 = phi i32 [ -1, %6 ], [ 0, %9 ]
ret i32 %12
}