#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 */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int PIPE_BUF;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, int);
int write(int, char *, int);
int main(void) {
  int fildes[2];
  if (pipe(fildes) < 0) {
    perror("pipe(2) failed");
    exit(EXIT_FAILURE);
  }
  if (fcntl(fildes[1], F_SETFL, O_NONBLOCK) < 0) {
    perror("fcntl(2) failed");
    exit(EXIT_FAILURE);
  }
  printf("PIPE_BUF = %d\n", PIPE_BUF);
  int i;
  for (i = 0; write(fildes[1], "a", 1) != -1; i++)
    ;
  if (errno == EAGAIN || errno == EWOULDBLOCK) {
    printf("write(2) would block (%d bytes written)\n", i);
  } else {
    perror("write(2) failed");
    exit(EXIT_FAILURE);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    119c <main+0x3c>
lea    0xe77(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    11da <main+0x7a>
lea    0xe48(%rip),%rdi
call   1070 <perror@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
lea    0xe35(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    0xe2e(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1226 <main+0xc6>
jmp    1218 <main+0xb8>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11f6 <main+0x96>
mov    0x2da3(%rip),%rax
mov    (%rax),%rax
mov    0x2d71(%rip),%rcx
cmp    (%rcx),%rax
je     125a <main+0xfa>
mov    0x2d89(%rip),%rax
mov    (%rax),%rax
mov    0x2d77(%rip),%rcx
cmp    (%rcx),%rax
jne    126e <main+0x10e>
mov    -0x10(%rbp),%esi
lea    0xdcc(%rip),%rdi
call   1050 <printf@plt>
jmp    1288 <main+0x128>
lea    0xde4(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsp,%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     1213 <main+0xb3>
mov    0x4(%rsp),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
test   %rax,%rax
js     121c <main+0xbc>
mov    0x2e18(%rip),%rax
mov    (%rax),%esi
lea    0xe76(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
lea    0xe74(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    0x4(%rsp),%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
add    $0x1,%ebx
cmp    $0xffffffff,%eax
jne    11c0 <main+0x60>
mov    0x2df0(%rip),%rax
mov    (%rax),%rax
mov    0x2dbe(%rip),%rcx
cmp    (%rcx),%rax
je     11fb <main+0x9b>
mov    0x2dd2(%rip),%rcx
cmp    (%rcx),%rax
jne    1225 <main+0xc5>
lea    0xe2e(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xde6(%rip),%rdi
jmp    122c <main+0xcc>
lea    0xdec(%rip),%rdi
jmp    122c <main+0xcc>
lea    0xe2d(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [15 x i8] c"pipe(2) failed\00"
@EXIT_FAILURE = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [16 x i8] c"fcntl(2) failed\00"
@.str.2 = [15 x i8] c"PIPE_BUF = %d\0A\00"
@PIPE_BUF = global i32 0
@.str.3 = [2 x i8] c"a\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str.4 = [41 x i8] c"write(2) would block (%d bytes written)\0A\00"
@.str.5 = [16 x i8] c"write(2) failed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
store i32 0, i32* %1
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%5 = call i64 @pipe(i32* %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%13 = load i32, i32* %12
%14 = load i32, i32* @F_SETFL
%15 = load i32, i32* @O_NONBLOCK
%16 = call i64 @fcntl(i32 %13, i32 %14, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %11
%19 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %11
%23 = load i32, i32* @PIPE_BUF
%24 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %23)
store i32 0, i32* %3
br label %25
25: ; preds = %31, %22
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @write(i32 %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%29 = icmp ne i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %25
br label %31
31: ; preds = %30
%32 = load i32, i32* %3
%33 = add nsw i32 %32, 1
store i32 %33, i32* %3
br label %25
34: ; preds = %25
%35 = load i64, i64* @errno
%36 = load i64, i64* @EAGAIN
%37 = icmp eq i64 %35, %36
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = load i64, i64* @errno
%40 = load i64, i64* @EWOULDBLOCK
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %45
42: ; preds = %38, %34
%43 = load i32, i32* %3
%44 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %43)
br label %49
45: ; preds = %38
%46 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %42
ret i32 0
}

LLVM IR (-O2):
@.str = [15 x i8] c"pipe(2) failed\00"
@EXIT_FAILURE = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [16 x i8] c"fcntl(2) failed\00"
@.str.2 = [15 x i8] c"PIPE_BUF = %d\0A\00"
@PIPE_BUF = global i32 0
@.str.3 = [2 x i8] c"a\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str.4 = [41 x i8] c"write(2) would block (%d bytes written)\0A\00"
@.str.5 = [16 x i8] c"write(2) failed\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%4 = call i64 @pipe(i32* %3)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @EXIT_FAILURE
%9 = call i32 @exit(i32 %8)
unreachable
10: ; preds = %0
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%12 = load i32, i32* %11
%13 = load i32, i32* @F_SETFL
%14 = load i32, i32* @O_NONBLOCK
%15 = call i64 @fcntl(i32 %12, i32 %13, i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %10
%18 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %10
%22 = load i32, i32* @PIPE_BUF
%23 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %24, %21
%25 = phi i32 [ 0, %21 ], [ %29, %24 ]
%26 = load i32, i32* %11
%27 = call i32 @write(i32 %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%28 = icmp eq i32 %27, -1
%29 = add nuw nsw i32 %25, 1
br i1 %28, label %30, label %24
30: ; preds = %24
%31 = load i64, i64* @errno
%32 = load i64, i64* @EAGAIN
%33 = icmp eq i64 %31, %32
%34 = load i64, i64* @EWOULDBLOCK
%35 = icmp eq i64 %31, %34
%36 = select i1 %33, i1 true, i1 %35
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %25)
ret i32 0
39: ; preds = %30
%40 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
}