#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 ENOSYS;
int FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int O_CLOEXEC;
int O_NONBLOCK;
int __syscall(int, ...);
int __syscall_ret(int);
int pipe(int *);
int pipe2(int fd[2], int flag) {
  if (!flag)
    return pipe(fd);
  int ret = __syscall(359, fd, flag);
  if (ret != -ENOSYS)
    return __syscall_ret(ret);
  ret = pipe(fd);
  if (ret)
    return ret;
  if (flag & O_CLOEXEC) {
    __syscall(55, fd[0], F_SETFD, FD_CLOEXEC);
    __syscall(55, fd[1], F_SETFD, FD_CLOEXEC);
  }
  if (flag & O_NONBLOCK) {
    __syscall(55, fd[0], F_SETFL, O_NONBLOCK);
    __syscall(55, fd[1], F_SETFL, O_NONBLOCK);
  }
  return 0;
}


x86 assembly (clang, -O0):
<pipe2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    115a <pipe2+0x2a>
mov    -0x10(%rbp),%rdi
call   1040 <pipe@plt>
mov    %eax,-0x4(%rbp)
jmp    127a <pipe2+0x14a>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    $0x167,%edi
mov    $0x0,%al
call   1030 <__syscall@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
xor    %ecx,%ecx
mov    0x2e64(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
je     1196 <pipe2+0x66>
mov    -0x18(%rbp),%edi
call   1050 <__syscall_ret@plt>
mov    %eax,-0x4(%rbp)
jmp    127a <pipe2+0x14a>
mov    -0x10(%rbp),%rdi
call   1040 <pipe@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     11b7 <pipe2+0x87>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    127a <pipe2+0x14a>
mov    -0x14(%rbp),%eax
mov    0x2def(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1215 <pipe2+0xe5>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
mov    0x2dde(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
mov    $0x0,%al
call   1030 <__syscall@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    0x2dd2(%rip),%rax
mov    (%rax),%edx
mov    0x2db9(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
mov    $0x0,%al
call   1030 <__syscall@plt>
mov    -0x14(%rbp),%eax
mov    0x2dc9(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1273 <pipe2+0x143>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
mov    0x2da8(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
mov    $0x0,%al
call   1030 <__syscall@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
mov    0x2d83(%rip),%rax
mov    (%rax),%ecx
mov    $0x37,%edi
mov    $0x0,%al
call   1030 <__syscall@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipe2>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %esi,%esi
je     11fb <pipe2+0xcb>
mov    %esi,%ebp
mov    $0x167,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <__syscall@plt>
mov    0x2e84(%rip),%rcx
mov    (%rcx),%ecx
add    %eax,%ecx
jne    120d <pipe2+0xdd>
mov    %rbx,%rdi
call   1040 <pipe@plt>
mov    %eax,%r14d
test   %eax,%eax
jne    1221 <pipe2+0xf1>
mov    0x2e30(%rip),%rax
test   %ebp,(%rax)
je     11bb <pipe2+0x8b>
mov    (%rbx),%esi
mov    0x2e43(%rip),%r14
mov    (%r14),%edx
mov    0x2e29(%rip),%r15
mov    (%r15),%ecx
mov    $0x37,%edi
xor    %eax,%eax
call   1030 <__syscall@plt>
mov    0x4(%rbx),%esi
mov    (%r14),%edx
mov    (%r15),%ecx
mov    $0x37,%edi
xor    %eax,%eax
call   1030 <__syscall@plt>
mov    0x2e26(%rip),%r15
mov    (%r15),%ecx
test   %ebp,%ecx
je     121e <pipe2+0xee>
mov    (%rbx),%esi
mov    0x2df6(%rip),%rbp
mov    0x0(%rbp),%edx
xor    %r14d,%r14d
mov    $0x37,%edi
xor    %eax,%eax
call   1030 <__syscall@plt>
mov    0x4(%rbx),%esi
mov    0x0(%rbp),%edx
mov    (%r15),%ecx
mov    $0x37,%edi
xor    %eax,%eax
call   1030 <__syscall@plt>
jmp    1221 <pipe2+0xf1>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <pipe@plt>
mov    %eax,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <__syscall_ret@plt>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOSYS = global i32 0
@O_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @pipe2(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp ne i32 %7, 0
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i32*, i32** %4
%11 = call i32 @pipe(i32* %10)
store i32 %11, i32* %3
br label %67
12: ; preds = %2
%13 = load i32*, i32** %4
%14 = load i32, i32* %5
%15 = call i32 (i32, ...) @__syscall(i32 359, i32* %13, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = load i32, i32* @ENOSYS
%18 = sub nsw i32 0, %17
%19 = icmp ne i32 %16, %18
br i1 %19, label %20, label %23
20: ; preds = %12
%21 = load i32, i32* %6
%22 = call i32 @__syscall_ret(i32 %21)
store i32 %22, i32* %3
br label %67
23: ; preds = %12
%24 = load i32*, i32** %4
%25 = call i32 @pipe(i32* %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = load i32, i32* %6
store i32 %29, i32* %3
br label %67
30: ; preds = %23
%31 = load i32, i32* %5
%32 = load i32, i32* @O_CLOEXEC
%33 = and i32 %31, %32
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %48
35: ; preds = %30
%36 = load i32*, i32** %4
%37 = getelementptr inbounds i32, i32* %36, i64 0
%38 = load i32, i32* %37
%39 = load i32, i32* @F_SETFD
%40 = load i32, i32* @FD_CLOEXEC
%41 = call i32 (i32, ...) @__syscall(i32 55, i32 %38, i32 %39, i32 %40)
%42 = load i32*, i32** %4
%43 = getelementptr inbounds i32, i32* %42, i64 1
%44 = load i32, i32* %43
%45 = load i32, i32* @F_SETFD
%46 = load i32, i32* @FD_CLOEXEC
%47 = call i32 (i32, ...) @__syscall(i32 55, i32 %44, i32 %45, i32 %46)
br label %48
48: ; preds = %35, %30
%49 = load i32, i32* %5
%50 = load i32, i32* @O_NONBLOCK
%51 = and i32 %49, %50
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %66
53: ; preds = %48
%54 = load i32*, i32** %4
%55 = getelementptr inbounds i32, i32* %54, i64 0
%56 = load i32, i32* %55
%57 = load i32, i32* @F_SETFL
%58 = load i32, i32* @O_NONBLOCK
%59 = call i32 (i32, ...) @__syscall(i32 55, i32 %56, i32 %57, i32 %58)
%60 = load i32*, i32** %4
%61 = getelementptr inbounds i32, i32* %60, i64 1
%62 = load i32, i32* %61
%63 = load i32, i32* @F_SETFL
%64 = load i32, i32* @O_NONBLOCK
%65 = call i32 (i32, ...) @__syscall(i32 55, i32 %62, i32 %63, i32 %64)
br label %66
66: ; preds = %53, %48
store i32 0, i32* %3
br label %67
67: ; preds = %66, %28, %20, %9
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@ENOSYS = global i32 0
@O_CLOEXEC = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
define i32 @pipe2(i32* %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @pipe(i32* %0)
br label %43
6: ; preds = %2
%7 = tail call i32 (i32, ...) @__syscall(i32 359, i32* %0, i32 %1)
%8 = load i32, i32* @ENOSYS
%9 = sub nsw i32 0, %8
%10 = icmp eq i32 %7, %9
br i1 %10, label %13, label %11
11: ; preds = %6
%12 = tail call i32 @__syscall_ret(i32 %7)
br label %43
13: ; preds = %6
%14 = tail call i32 @pipe(i32* %0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %43
16: ; preds = %13
%17 = load i32, i32* @O_CLOEXEC
%18 = and i32 %17, %1
%19 = icmp eq i32 %18, 0
br i1 %19, label %30, label %20
20: ; preds = %16
%21 = load i32, i32* %0
%22 = load i32, i32* @F_SETFD
%23 = load i32, i32* @FD_CLOEXEC
%24 = tail call i32 (i32, ...) @__syscall(i32 55, i32 %21, i32 %22, i32 %23)
%25 = getelementptr inbounds i32, i32* %0, i64 1
%26 = load i32, i32* %25
%27 = load i32, i32* @F_SETFD
%28 = load i32, i32* @FD_CLOEXEC
%29 = tail call i32 (i32, ...) @__syscall(i32 55, i32 %26, i32 %27, i32 %28)
br label %30
30: ; preds = %20, %16
%31 = load i32, i32* @O_NONBLOCK
%32 = and i32 %31, %1
%33 = icmp eq i32 %32, 0
br i1 %33, label %43, label %34
34: ; preds = %30
%35 = load i32, i32* %0
%36 = load i32, i32* @F_SETFL
%37 = tail call i32 (i32, ...) @__syscall(i32 55, i32 %35, i32 %36, i32 %31)
%38 = getelementptr inbounds i32, i32* %0, i64 1
%39 = load i32, i32* %38
%40 = load i32, i32* @F_SETFL
%41 = load i32, i32* @O_NONBLOCK
%42 = tail call i32 (i32, ...) @__syscall(i32 55, i32 %39, i32 %40, i32 %41)
br label %43
43: ; preds = %11, %13, %34, %30, %4
%44 = phi i32 [ %5, %4 ], [ %12, %11 ], [ %14, %13 ], [ 0, %34 ], [ 0, %30 ]
ret i32 %44
}