#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*/ O_NONBLOCK;
int /*<<< orphan*/ err_sys(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ set_fl(int, int /*<<< orphan*/);
int write(int, char *, int);
int main(void) {
  int i, n;
  int fd[2];
  if (pipe(fd) < 0)
    err_sys("pipe error");
  set_fl(fd[1], O_NONBLOCK);
  for (n = 0;; n++) {
    if ((i = write(fd[1], "a", 1)) != 1) {
      printf("write ret %d, ", i);
      break;
    }
  }
  printf("pipe capacity = %d\n", n);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x14(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    118e <main+0x2e>
lea    0xe77(%rip),%rdi
call   1040 <err_sys@plt>
mov    -0x10(%rbp),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
call   1030 <set_fl@plt>
movl   $0x0,-0xc(%rbp)
mov    -0x10(%rbp),%edi
lea    0xe5b(%rip),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x1,%eax
je     11da <main+0x7a>
mov    -0x8(%rbp),%esi
lea    0xe3d(%rip),%rdi
call   1060 <printf@plt>
jmp    11ed <main+0x8d>
jmp    11df <main+0x7f>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11a6 <main+0x46>
mov    -0xc(%rbp),%esi
lea    0xe25(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsp,%rdi
call   1070 <pipe@plt>
test   %rax,%rax
jns    117d <main+0x1d>
lea    0xe88(%rip),%rdi
call   1040 <err_sys@plt>
mov    0x4(%rsp),%edi
mov    0x2e60(%rip),%rax
mov    (%rax),%esi
call   1030 <set_fl@plt>
mov    $0xffffffff,%ebx
lea    0xe70(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    0x4(%rsp),%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
add    $0x1,%ebx
cmp    $0x1,%eax
je     11a0 <main+0x40>
lea    0xe4d(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
lea    0xe4e(%rip),%rdi
mov    %ebx,%esi
call   1060 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"pipe error\00"
@O_NONBLOCK = global i32 0
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [15 x i8] c"write ret %d, \00"
@.str.3 = [20 x i8] c"pipe capacity = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x i32]
store i32 0, i32* %1
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%12 = load i32, i32* %11
%13 = load i32, i32* @O_NONBLOCK
%14 = call i32 @set_fl(i32 %12, i32 %13)
store i32 0, i32* %3
br label %15
15: ; preds = %24, %10
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @write(i32 %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
store i32 %18, i32* %2
%19 = icmp ne i32 %18, 1
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i32, i32* %2
%22 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %27
23: ; preds = %15
br label %24
24: ; preds = %23
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
br label %15
27: ; preds = %20
%28 = load i32, i32* %3
%29 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [11 x i8] c"pipe error\00"
@O_NONBLOCK = global i32 0
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [15 x i8] c"write ret %d, \00"
@.str.3 = [20 x i8] c"pipe capacity = %d\0A\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 %8
6: ; preds = %0
%7 = call i32 @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%10 = load i32, i32* %9
%11 = load i32, i32* @O_NONBLOCK
%12 = call i32 @set_fl(i32 %10, i32 %11)
br label %13
13: ; preds = %13, %8
%14 = phi i32 [ 0, %8 ], [ %18, %13 ]
%15 = load i32, i32* %9
%16 = call i32 @write(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%17 = icmp eq i32 %16, 1
%18 = add nuw nsw i32 %14, 1
br i1 %17, label %13, label %19
19: ; preds = %13
%20 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%21 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%22 = call i32 @exit(i32 0)
unreachable
}