#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 FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ die(char *);
int fcntl(int, int /*<<< orphan*/, ...);
scalar_t__ pipe(int *);
void setup_pipe(int *fds, int nonblocking) {
  if (pipe(fds) < 0)
    die("pipe: %m");
  for (int i = 0; i < 2; i++)
    if (fcntl(fds[i], F_SETFD, fcntl(fds[i], F_GETFD) | FD_CLOEXEC) < 0 ||
        nonblocking &&
            fcntl(fds[i], F_SETFL, fcntl(fds[i], F_GETFL) | O_NONBLOCK) < 0)
      die("fcntl on pipe: %m");
}


x86 assembly (clang, -O0):
<setup_pipe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <pipe@plt>
cmp    $0x0,%rax
jge    115e <setup_pipe+0x2e>
lea    0xeb0(%rip),%rdi
call   1050 <die@plt>
movl   $0x0,-0x10(%rbp)
cmpl   $0x2,-0x10(%rbp)
jge    1244 <setup_pipe+0x114>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e54(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x18(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    %eax,%edx
mov    0x2e15(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jl     1225 <setup_pipe+0xf5>
cmpl   $0x0,-0xc(%rbp)
je     1231 <setup_pipe+0x101>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    -0x20(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    %eax,%edx
mov    0x2dd5(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    1231 <setup_pipe+0x101>
lea    0xdd4(%rip),%rdi
call   1050 <die@plt>
jmp    1236 <setup_pipe+0x106>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1165 <setup_pipe+0x35>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_pipe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
call   1040 <pipe@plt>
test   %rax,%rax
jns    1157 <setup_pipe+0x27>
lea    0xeb7(%rip),%rdi
call   1050 <die@plt>
mov    (%r15),%ebp
mov    0x2e77(%rip),%rax
mov    (%rax),%ebx
mov    0x2e56(%rip),%r13
mov    0x0(%r13),%esi
mov    %ebp,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2e4a(%rip),%r12
or     (%r12),%eax
mov    %ebp,%edi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     11d0 <setup_pipe+0xa0>
test   %r14d,%r14d
je     11dc <setup_pipe+0xac>
mov    (%r15),%ebx
mov    0x2e2e(%rip),%rax
mov    (%rax),%ebp
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2e2b(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
jns    11dc <setup_pipe+0xac>
lea    0xe29(%rip),%rdi
call   1050 <die@plt>
mov    0x4(%r15),%ebx
mov    0x2df1(%rip),%rax
mov    (%rax),%ebp
mov    0x0(%r13),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
or     (%r12),%eax
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     1258 <setup_pipe+0x128>
test   %r14d,%r14d
je     1249 <setup_pipe+0x119>
mov    0x4(%r15),%ebx
mov    0x2db5(%rip),%rax
mov    (%rax),%ebp
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <fcntl@plt>
mov    0x2db2(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
test   %eax,%eax
js     1258 <setup_pipe+0x128>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda1(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <die@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"pipe: %m\00"
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [18 x i8] c"fcntl on pipe: %m\00"
define void @setup_pipe(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%6 = load i32*, i32** %3
%7 = call i64 @pipe(i32* %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
store i32 0, i32* %5
br label %12
12: ; preds = %57, %11
%13 = load i32, i32* %5
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %60
15: ; preds = %12
%16 = load i32*, i32** %3
%17 = load i32, i32* %5
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %16, i64 %18
%20 = load i32, i32* %19
%21 = load i32, i32* @F_SETFD
%22 = load i32*, i32** %3
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %22, i64 %24
%26 = load i32, i32* %25
%27 = load i32, i32* @F_GETFD
%28 = call i32 (i32, i32, ...) @fcntl(i32 %26, i32 %27)
%29 = load i32, i32* @FD_CLOEXEC
%30 = or i32 %28, %29
%31 = call i32 (i32, i32, ...) @fcntl(i32 %20, i32 %21, i32 %30)
%32 = icmp slt i32 %31, 0
br i1 %32, label %54, label %33
33: ; preds = %15
%34 = load i32, i32* %4
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %56
36: ; preds = %33
%37 = load i32*, i32** %3
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %37, i64 %39
%41 = load i32, i32* %40
%42 = load i32, i32* @F_SETFL
%43 = load i32*, i32** %3
%44 = load i32, i32* %5
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
%47 = load i32, i32* %46
%48 = load i32, i32* @F_GETFL
%49 = call i32 (i32, i32, ...) @fcntl(i32 %47, i32 %48)
%50 = load i32, i32* @O_NONBLOCK
%51 = or i32 %49, %50
%52 = call i32 (i32, i32, ...) @fcntl(i32 %41, i32 %42, i32 %51)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %36, %15
%55 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %56
56: ; preds = %54, %36, %33
br label %57
57: ; preds = %56
%58 = load i32, i32* %5
%59 = add nsw i32 %58, 1
store i32 %59, i32* %5
br label %12
60: ; preds = %12
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"pipe: %m\00"
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@F_SETFL = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [18 x i8] c"fcntl on pipe: %m\00"
define void @setup_pipe(i32* %0, i32 %1) {
%3 = tail call i64 @pipe(i32* %0)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @die(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = icmp eq i32 %1, 0
%9 = load i32, i32* %0
%10 = load i32, i32* @F_SETFD
%11 = load i32, i32* @F_GETFD
%12 = tail call i32 (i32, i32, ...) @fcntl(i32 %9, i32 %11)
%13 = load i32, i32* @FD_CLOEXEC
%14 = or i32 %13, %12
%15 = tail call i32 (i32, i32, ...) @fcntl(i32 %9, i32 %10, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %27, label %17
17: ; preds = %7
br i1 %8, label %29, label %18
18: ; preds = %17
%19 = load i32, i32* %0
%20 = load i32, i32* @F_SETFL
%21 = load i32, i32* @F_GETFL
%22 = tail call i32 (i32, i32, ...) @fcntl(i32 %19, i32 %21)
%23 = load i32, i32* @O_NONBLOCK
%24 = or i32 %23, %22
%25 = tail call i32 (i32, i32, ...) @fcntl(i32 %19, i32 %20, i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %18, %7
%28 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %17, %18, %27
%30 = getelementptr inbounds i32, i32* %0, i64 1
%31 = load i32, i32* %30
%32 = load i32, i32* @F_SETFD
%33 = load i32, i32* @F_GETFD
%34 = tail call i32 (i32, i32, ...) @fcntl(i32 %31, i32 %33)
%35 = load i32, i32* @FD_CLOEXEC
%36 = or i32 %35, %34
%37 = tail call i32 (i32, i32, ...) @fcntl(i32 %31, i32 %32, i32 %36)
%38 = icmp slt i32 %37, 0
br i1 %38, label %49, label %39
39: ; preds = %29
br i1 %8, label %51, label %40
40: ; preds = %39
%41 = load i32, i32* %30
%42 = load i32, i32* @F_SETFL
%43 = load i32, i32* @F_GETFL
%44 = tail call i32 (i32, i32, ...) @fcntl(i32 %41, i32 %43)
%45 = load i32, i32* @O_NONBLOCK
%46 = or i32 %45, %44
%47 = tail call i32 (i32, i32, ...) @fcntl(i32 %41, i32 %42, i32 %46)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %40, %29
%50 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %51
51: ; preds = %49, %40, %39
ret void
}