#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*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ close(int);
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int pipeutils_create_nonblocking(int pipefds[2]) {
  int r = pipe(pipefds);
  if (r == -1) {
    perror("pipe");
    return -1;
  }
  int flags = fcntl(pipefds[0], F_GETFL);
  if (flags == -1) {
    perror("fcntl");
    close(pipefds[0]);
    close(pipefds[1]);
    return -1;
  }
  r = fcntl(pipefds[0], F_SETFL, flags | O_NONBLOCK);
  if (r == -1) {
    perror("fcntl");
    close(pipefds[0]);
    close(pipefds[1]);
    return -1;
  }
  flags = fcntl(pipefds[1], F_GETFL);
  if (flags == -1) {
    perror("fcntl");
    close(pipefds[0]);
    close(pipefds[1]);
    return -1;
  }
  r = fcntl(pipefds[1], F_SETFL, flags | O_NONBLOCK);
  if (r == -1) {
    perror("fcntl");
    close(pipefds[0]);
    close(pipefds[1]);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<pipeutils_create_nonblocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <pipe@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    117a <pipeutils_create_nonblocking+0x3a>
lea    0xe97(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <pipeutils_create_nonblocking+0x1a3>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11cc <pipeutils_create_nonblocking+0x8c>
lea    0xe61(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <pipeutils_create_nonblocking+0x1a3>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    0x2e03(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    122a <pipeutils_create_nonblocking+0xea>
lea    0xe03(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <pipeutils_create_nonblocking+0x1a3>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    127d <pipeutils_create_nonblocking+0x13d>
lea    0xdb0(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <pipeutils_create_nonblocking+0x1a3>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
mov    0x2d51(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12dc <pipeutils_create_nonblocking+0x19c>
lea    0xd51(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e3 <pipeutils_create_nonblocking+0x1a3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipeutils_create_nonblocking>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     11dc <pipeutils_create_nonblocking+0x9c>
mov    (%rbx),%edi
mov    0x2e6f(%rip),%rbp
mov    0x0(%rbp),%esi
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11bf <pipeutils_create_nonblocking+0x7f>
mov    (%rbx),%edi
mov    0x2e5f(%rip),%r14
mov    (%r14),%esi
mov    0x2e65(%rip),%r15
or     (%r15),%eax
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11bf <pipeutils_create_nonblocking+0x7f>
mov    0x4(%rbx),%edi
mov    0x0(%rbp),%esi
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11bf <pipeutils_create_nonblocking+0x7f>
mov    0x4(%rbx),%edi
mov    (%r14),%esi
or     (%r15),%eax
mov    %eax,%edx
xor    %eax,%eax
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11ed <pipeutils_create_nonblocking+0xad>
lea    0xe3f(%rip),%rdi
call   1060 <perror@plt>
mov    (%rbx),%edi
call   1040 <close@plt>
mov    0x4(%rbx),%edi
call   1040 <close@plt>
jmp    11e8 <pipeutils_create_nonblocking+0xa8>
lea    0xe1d(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@F_GETFL = global i32 0
@.str.1 = [6 x i8] c"fcntl\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
define i32 @pipeutils_create_nonblocking(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %3
%6 = load i32*, i32** %3
%7 = call i32 @pipe(i32* %6)
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %91
12: ; preds = %1
%13 = load i32*, i32** %3
%14 = getelementptr inbounds i32, i32* %13, i64 0
%15 = load i32, i32* %14
%16 = load i32, i32* @F_GETFL
%17 = call i32 (i32, i32, ...) @fcntl(i32 %15, i32 %16)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %30
20: ; preds = %12
%21 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32*, i32** %3
%23 = getelementptr inbounds i32, i32* %22, i64 0
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = load i32*, i32** %3
%27 = getelementptr inbounds i32, i32* %26, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
store i32 -1, i32* %2
br label %91
30: ; preds = %12
%31 = load i32*, i32** %3
%32 = getelementptr inbounds i32, i32* %31, i64 0
%33 = load i32, i32* %32
%34 = load i32, i32* @F_SETFL
%35 = load i32, i32* %5
%36 = load i32, i32* @O_NONBLOCK
%37 = or i32 %35, %36
%38 = call i32 (i32, i32, ...) @fcntl(i32 %33, i32 %34, i32 %37)
store i32 %38, i32* %4
%39 = load i32, i32* %4
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %51
41: ; preds = %30
%42 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%43 = load i32*, i32** %3
%44 = getelementptr inbounds i32, i32* %43, i64 0
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
%47 = load i32*, i32** %3
%48 = getelementptr inbounds i32, i32* %47, i64 1
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %2
br label %91
51: ; preds = %30
%52 = load i32*, i32** %3
%53 = getelementptr inbounds i32, i32* %52, i64 1
%54 = load i32, i32* %53
%55 = load i32, i32* @F_GETFL
%56 = call i32 (i32, i32, ...) @fcntl(i32 %54, i32 %55)
store i32 %56, i32* %5
%57 = load i32, i32* %5
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %69
59: ; preds = %51
%60 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%61 = load i32*, i32** %3
%62 = getelementptr inbounds i32, i32* %61, i64 0
%63 = load i32, i32* %62
%64 = call i32 @close(i32 %63)
%65 = load i32*, i32** %3
%66 = getelementptr inbounds i32, i32* %65, i64 1
%67 = load i32, i32* %66
%68 = call i32 @close(i32 %67)
store i32 -1, i32* %2
br label %91
69: ; preds = %51
%70 = load i32*, i32** %3
%71 = getelementptr inbounds i32, i32* %70, i64 1
%72 = load i32, i32* %71
%73 = load i32, i32* @F_SETFL
%74 = load i32, i32* %5
%75 = load i32, i32* @O_NONBLOCK
%76 = or i32 %74, %75
%77 = call i32 (i32, i32, ...) @fcntl(i32 %72, i32 %73, i32 %76)
store i32 %77, i32* %4
%78 = load i32, i32* %4
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %90
80: ; preds = %69
%81 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%82 = load i32*, i32** %3
%83 = getelementptr inbounds i32, i32* %82, i64 0
%84 = load i32, i32* %83
%85 = call i32 @close(i32 %84)
%86 = load i32*, i32** %3
%87 = getelementptr inbounds i32, i32* %86, i64 1
%88 = load i32, i32* %87
%89 = call i32 @close(i32 %88)
store i32 -1, i32* %2
br label %91
90: ; preds = %69
store i32 0, i32* %2
br label %91
91: ; preds = %90, %80, %59, %41, %20, %10
%92 = load i32, i32* %2
ret i32 %92
}

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