#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_saving_errno(int);
scalar_t__ merge_flags(int, int);
scalar_t__ pipe(int *);
int pipe2(int fd[2], int flags) {
  int xfd[2];
  if (pipe(xfd) < 0)
    return -1;
  for (int i = 0; i < 2; ++i)
    if (merge_flags(xfd[i], flags) < 0)
      goto fail;
  fd[0] = xfd[0];
  fd[1] = xfd[1];
  return 0;
fail:
  close_saving_errno(xfd[0]);
  close_saving_errno(xfd[1]);
  return -1;
}


x86 assembly (clang, -O0):
<pipe2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <pipe@plt>
cmp    $0x0,%rax
jge    115e <pipe2+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <pipe2+0xa7>
movl   $0x0,-0x20(%rbp)
cmpl   $0x2,-0x20(%rbp)
jge    11a1 <pipe2+0x71>
movslq -0x20(%rbp),%rax
mov    -0x1c(%rbp,%rax,4),%edi
mov    -0x14(%rbp),%esi
call   1050 <merge_flags@plt>
cmp    $0x0,%rax
jge    118e <pipe2+0x5e>
jmp    11c0 <pipe2+0x90>
jmp    1193 <pipe2+0x63>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1165 <pipe2+0x35>
mov    -0x1c(%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)
jmp    11d7 <pipe2+0xa7>
mov    -0x1c(%rbp),%edi
call   1030 <close_saving_errno@plt>
mov    -0x18(%rbp),%edi
call   1030 <close_saving_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipe2>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
call   1040 <pipe@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     1196 <pipe2+0x66>
mov    0x8(%rsp),%edi
mov    %ebp,%esi
call   1050 <merge_flags@plt>
test   %rax,%rax
js     1184 <pipe2+0x54>
mov    0xc(%rsp),%edi
mov    %ebp,%esi
call   1050 <merge_flags@plt>
test   %rax,%rax
js     1184 <pipe2+0x54>
mov    0x8(%rsp),%eax
mov    %eax,(%r14)
mov    0xc(%rsp),%eax
mov    %eax,0x4(%r14)
xor    %ebx,%ebx
jmp    1196 <pipe2+0x66>
mov    0x8(%rsp),%edi
call   1030 <close_saving_errno@plt>
mov    0xc(%rsp),%edi
call   1030 <close_saving_errno@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @pipe2(i32* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [2 x i32]
%7 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %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 %45
12: ; preds = %2
store i32 0, i32* %7
br label %13
13: ; preds = %26, %12
%14 = load i32, i32* %7
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %29
16: ; preds = %13
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 %18
%20 = load i32, i32* %19
%21 = load i32, i32* %5
%22 = call i64 @merge_flags(i32 %20, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %16
br label %38
25: ; preds = %16
br label %26
26: ; preds = %25
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
br label %13
29: ; preds = %13
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%31 = load i32, i32* %30
%32 = load i32*, i32** %4
%33 = getelementptr inbounds i32, i32* %32, i64 0
store i32 %31, i32* %33
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%35 = load i32, i32* %34
%36 = load i32*, i32** %4
%37 = getelementptr inbounds i32, i32* %36, i64 1
store i32 %35, i32* %37
store i32 0, i32* %3
br label %45
38: ; preds = %24
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%40 = load i32, i32* %39
%41 = call i32 @close_saving_errno(i32 %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%43 = load i32, i32* %42
%44 = call i32 @close_saving_errno(i32 %43)
store i32 -1, i32* %3
br label %45
45: ; preds = %38, %29, %11
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
define i32 @pipe2(i32* nocapture writeonly %0, i32 %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 %27, label %8
8: ; preds = %2
%9 = load i32, i32* %5
%10 = call i64 @merge_flags(i32 %9, i32 %1)
%11 = icmp slt i64 %10, 0
br i1 %11, label %21, label %12
12: ; preds = %8
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%14 = load i32, i32* %13
%15 = call i64 @merge_flags(i32 %14, i32 %1)
%16 = icmp slt i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %12
%18 = load i32, i32* %5
store i32 %18, i32* %0
%19 = load i32, i32* %13
%20 = getelementptr inbounds i32, i32* %0, i64 1
store i32 %19, i32* %20
br label %27
21: ; preds = %12, %8
%22 = load i32, i32* %5
%23 = call i32 @close_saving_errno(i32 %22)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @close_saving_errno(i32 %25)
br label %27
27: ; preds = %2, %21, %17
%28 = phi i32 [ -1, %21 ], [ 0, %17 ], [ -1, %2 ]
ret i32 %28
}