#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 (*FUNC_PTR)(int *);

/* Variables and functions */
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ RTLD_LAZY;
char *dlerror();
void *dlopen(char *, int /*<<< orphan*/);
scalar_t__ dlsym(void *, char *);
int /*<<< orphan*/ exit(int);
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int pipe(int modus[2]) {
  typedef int (*FUNC_PTR)(int modus[2]);
  static FUNC_PTR libc_pipe;
  int ret = -1;
  if (!libc_pipe) {
    void *handle;
    char *error;
    handle = dlopen("libc.so.6", RTLD_LAZY);
    if (!handle) {
      fputs(dlerror(), stderr);
      exit(1);
    }
    libc_pipe = (FUNC_PTR)dlsym(handle, "pipe");
    if ((error = dlerror()) != NULL) {
      fprintf(stderr, "%s\n", error);
      exit(1);
    }
  }
  ret = libc_pipe(modus);
  if (!ret) {
    int fd_flags = fcntl(modus[0], F_GETFD, 0);
    if (fd_flags >= 0) {
      fd_flags |= FD_CLOEXEC;
      fcntl(modus[0], F_SETFD, fd_flags);
    }
    fd_flags = fcntl(modus[1], F_GETFD, 0);
    if (fd_flags >= 0) {
      fd_flags |= FD_CLOEXEC;
      fcntl(modus[1], F_SETFD, fd_flags);
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
cmpq   $0x0,0x2ed5(%rip)
jne    122b <pipe+0xbb>
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdi
call   1060 <dlopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d7 <pipe+0x67>
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,%rdi
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rdi
lea    0xe28(%rip),%rsi
call   1080 <dlsym@plt>
mov    %rax,0x2e72(%rip)
mov    $0x0,%al
call   1030 <dlerror@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1226 <pipe+0xb6>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdf8(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    122b <pipe+0xbb>
mov    0x2e2e(%rip),%rax
mov    -0x8(%rbp),%rdi
call   *%rax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    12de <pipe+0x16e>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     128e <pipe+0x11e>
mov    0x2d61(%rip),%rax
mov    (%rax),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2d54(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
call   1040 <fcntl@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
mov    0x2d2c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     12d9 <pipe+0x169>
mov    0x2d17(%rip),%rax
mov    (%rax),%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
mov    0x2d09(%rip),%rax
mov    (%rax),%esi
mov    -0x24(%rbp),%edx
call   1040 <fcntl@plt>
jmp    12de <pipe+0x16e>
mov    -0xc(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipe>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%rbx
mov    0x2ee2(%rip),%rax
test   %rax,%rax
jne    11ce <pipe+0x5e>
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
call   1060 <dlopen@plt>
test   %rax,%rax
je     123f <pipe+0xcf>
lea    0xe62(%rip),%rsi
mov    %rax,%rdi
call   1080 <dlsym@plt>
mov    %rax,0x2ea9(%rip)
xor    %eax,%eax
call   1030 <dlerror@plt>
test   %rax,%rax
jne    1261 <pipe+0xf1>
mov    0x2e92(%rip),%rax
mov    %rbx,%rdi
call   *%rax
mov    %eax,%ebp
test   %eax,%eax
jne    1238 <pipe+0xc8>
mov    (%rbx),%edi
mov    0x2de6(%rip),%r14
mov    (%r14),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     120b <pipe+0x9b>
mov    0x2dd9(%rip),%rcx
or     (%rcx),%eax
mov    (%rbx),%edi
mov    0x2dd6(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edx
call   1040 <fcntl@plt>
mov    0x4(%rbx),%edi
mov    (%r14),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
test   %eax,%eax
js     1238 <pipe+0xc8>
mov    0x2dad(%rip),%rcx
or     (%rcx),%eax
mov    0x4(%rbx),%edi
mov    0x2da9(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edx
call   1040 <fcntl@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
call   1030 <dlerror@plt>
mov    0x2d93(%rip),%rcx
mov    (%rcx),%esi
mov    %rax,%rdi
call   1050 <fputs@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d78(%rip),%rcx
mov    (%rcx),%edi
lea    0xd9e(%rip),%rsi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@pipe.libc_pipe = internal global i32 (i32*)* null
@.str = [10 x i8] c"libc.so.6\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [5 x i8] c"pipe\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
define i32 @pipe(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i32* %0, i32** %2
store i32 -1, i32* %3
%7 = load i32 (i32*)*, i32 (i32*)** @pipe.libc_pipe
%8 = icmp ne i32 (i32*)* %7, null
br i1 %8, label %31, label %9
9: ; preds = %1
%10 = load i32, i32* @RTLD_LAZY
%11 = call i8* @dlopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %10)
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %19, label %14
14: ; preds = %9
%15 = call i8* (...) @dlerror()
%16 = load i32, i32* @stderr
%17 = call i32 @fputs(i8* %15, i32 %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %9
%20 = load i8*, i8** %4
%21 = call i64 @dlsym(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = inttoptr i64 %21 to i32 (i32*)*
store i32 (i32*)* %22, i32 (i32*)** @pipe.libc_pipe
%23 = call i8* (...) @dlerror()
store i8* %23, i8** %5
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %30
25: ; preds = %19
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %5
%28 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %19
br label %31
31: ; preds = %30, %1
%32 = load i32 (i32*)*, i32 (i32*)** @pipe.libc_pipe
%33 = load i32*, i32** %2
%34 = call i32 %32(i32* %33)
store i32 %34, i32* %3
%35 = load i32, i32* %3
%36 = icmp ne i32 %35, 0
br i1 %36, label %74, label %37
37: ; preds = %31
%38 = load i32*, i32** %2
%39 = getelementptr inbounds i32, i32* %38, i64 0
%40 = load i32, i32* %39
%41 = load i32, i32* @F_GETFD
%42 = call i32 @fcntl(i32 %40, i32 %41, i32 0)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp sge i32 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %37
%46 = load i32, i32* @FD_CLOEXEC
%47 = load i32, i32* %6
%48 = or i32 %47, %46
store i32 %48, i32* %6
%49 = load i32*, i32** %2
%50 = getelementptr inbounds i32, i32* %49, i64 0
%51 = load i32, i32* %50
%52 = load i32, i32* @F_SETFD
%53 = load i32, i32* %6
%54 = call i32 @fcntl(i32 %51, i32 %52, i32 %53)
br label %55
55: ; preds = %45, %37
%56 = load i32*, i32** %2
%57 = getelementptr inbounds i32, i32* %56, i64 1
%58 = load i32, i32* %57
%59 = load i32, i32* @F_GETFD
%60 = call i32 @fcntl(i32 %58, i32 %59, i32 0)
store i32 %60, i32* %6
%61 = load i32, i32* %6
%62 = icmp sge i32 %61, 0
br i1 %62, label %63, label %73
63: ; preds = %55
%64 = load i32, i32* @FD_CLOEXEC
%65 = load i32, i32* %6
%66 = or i32 %65, %64
store i32 %66, i32* %6
%67 = load i32*, i32** %2
%68 = getelementptr inbounds i32, i32* %67, i64 1
%69 = load i32, i32* %68
%70 = load i32, i32* @F_SETFD
%71 = load i32, i32* %6
%72 = call i32 @fcntl(i32 %69, i32 %70, i32 %71)
br label %73
73: ; preds = %63, %55
br label %74
74: ; preds = %73, %31
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@pipe.libc_pipe = internal unnamed_addr global i32 (i32*)* null
@.str = [10 x i8] c"libc.so.6\00"
@RTLD_LAZY = global i32 0
@stderr = global i32 0
@.str.1 = [5 x i8] c"pipe\00"
@.str.2 = [4 x i8] c"%s\0A\00"
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFD = global i32 0
define i32 @pipe(i32* %0) {
%2 = load i32 (i32*)*, i32 (i32*)** @pipe.libc_pipe
%3 = icmp eq i32 (i32*)* %2, null
br i1 %3, label %4, label %24
4: ; preds = %1
%5 = load i32, i32* @RTLD_LAZY
%6 = tail call i8* @dlopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %13
8: ; preds = %4
%9 = tail call i8* (...) @dlerror()
%10 = load i32, i32* @stderr
%11 = tail call i32 @fputs(i8* %9, i32 %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %4
%14 = tail call i64 @dlsym(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = inttoptr i64 %14 to i32 (i32*)*
store i32 (i32*)* %15, i32 (i32*)** @pipe.libc_pipe
%16 = tail call i8* (...) @dlerror()
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = load i32 (i32*)*, i32 (i32*)** @pipe.libc_pipe
br label %24
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %18, %1
%25 = phi i32 (i32*)* [ %19, %18 ], [ %2, %1 ]
%26 = tail call i32 %25(i32* %0)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %51
28: ; preds = %24
%29 = load i32, i32* %0
%30 = load i32, i32* @F_GETFD
%31 = tail call i32 @fcntl(i32 %29, i32 %30, i32 0)
%32 = icmp sgt i32 %31, -1
br i1 %32, label %33, label %39
33: ; preds = %28
%34 = load i32, i32* @FD_CLOEXEC
%35 = or i32 %34, %31
%36 = load i32, i32* %0
%37 = load i32, i32* @F_SETFD
%38 = tail call i32 @fcntl(i32 %36, i32 %37, i32 %35)
br label %39
39: ; preds = %33, %28
%40 = getelementptr inbounds i32, i32* %0, i64 1
%41 = load i32, i32* %40
%42 = load i32, i32* @F_GETFD
%43 = tail call i32 @fcntl(i32 %41, i32 %42, i32 0)
%44 = icmp sgt i32 %43, -1
br i1 %44, label %45, label %51
45: ; preds = %39
%46 = load i32, i32* @FD_CLOEXEC
%47 = or i32 %46, %43
%48 = load i32, i32* %40
%49 = load i32, i32* @F_SETFD
%50 = tail call i32 @fcntl(i32 %48, i32 %49, i32 %47)
br label %51
51: ; preds = %39, %45, %24
ret i32 %26
}