#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 */
typedef int /*<<< orphan*/ testchar;

/* Variables and functions */
scalar_t__ EAGAIN;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
scalar_t__ errno;
scalar_t__ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ pipe(int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(scalar_t__);
scalar_t__ write(int, char *, int);
int main(void) {
  int fds[2];
  int wrote;
  int counter = 0;
  char testchar[1] = "c";
  if (pipe(fds) < 0) {
    fprintf(stderr, "Failed to create a pipe: %s\n", strerror(errno));
    return -1;
  }
  if (fcntl(fds[1], F_SETFL, O_NONBLOCK) < 0) {
    fprintf(stderr, "Could not set NONBLOCK flag: %s\n", strerror(errno));
    return -1;
  }
  while (wrote = write(fds[1], testchar, sizeof(testchar)) > 0) {
    counter += wrote;
  }
  if (errno == EAGAIN)
    fprintf(stdout, "Capacity of pipe(in bytes): %d\n", counter);
  else
    fprintf(stderr, "Error while writing to input of pipe: %s\n",
            strerror(errno));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0xe94(%rip),%al
mov    %al,-0x15(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jge    11bd <main+0x6d>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe57(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <main+0x17e>
mov    -0x8(%rbp),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    121c <main+0xcc>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <main+0x17e>
jmp    1221 <main+0xd1>
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
setg   %al
mov    %al,%cl
and    $0x1,%cl
movzbl %cl,%ecx
mov    %ecx,-0x10(%rbp)
test   $0x1,%al
jne    1251 <main+0x101>
jmp    125f <main+0x10f>
mov    -0x10(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1221 <main+0xd1>
mov    0x2d62(%rip),%rax
mov    (%rax),%rax
mov    0x2d40(%rip),%rcx
cmp    (%rcx),%rax
jne    1298 <main+0x148>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xdb3(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12c7 <main+0x177>
mov    0x2d39(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d1d(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movb   $0x63,0xf(%rsp)
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
js     11ec <main+0x9c>
mov    0x14(%rsp),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
mov    0x2e66(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
test   %rax,%rax
js     120d <main+0xbd>
mov    $0xffffffff,%ebx
lea    0xf(%rsp),%rbp
nopl   0x0(%rax)
mov    0x14(%rsp),%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
add    $0x1,%ebx
test   %rax,%rax
jg     11a0 <main+0x50>
mov    0x2e08(%rip),%rax
mov    (%rax),%rdi
mov    0x2de6(%rip),%rax
cmp    (%rax),%rdi
jne    123f <main+0xef>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
xor    %ebp,%ebp
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1262 <main+0x112>
mov    0x2de5(%rip),%rax
mov    (%rax),%ebx
mov    0x2dcc(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
lea    0xdf5(%rip),%rsi
jmp    122c <main+0xdc>
mov    0x2dc4(%rip),%rax
mov    (%rax),%ebx
mov    0x2dab(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
lea    0xdf1(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1262 <main+0x112>
mov    0x2d92(%rip),%rax
mov    (%rax),%ebx
call   1070 <strerror@plt>
lea    0xe0a(%rip),%rsi
xor    %ebp,%ebp
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.testchar = [1 x i8] c"c"
@stderr = global i32 0
@.str = [29 x i8] c"Failed to create a pipe: %s\0A\00"
@errno = global i64 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [33 x i8] c"Could not set NONBLOCK flag: %s\0A\00"
@EAGAIN = global i64 0
@stdout = global i32 0
@.str.2 = [32 x i8] c"Capacity of pipe(in bytes): %d\0A\00"
@.str.3 = [42 x i8] c"Error while writing to input of pipe: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1 x i8]
store i32 0, i32* %1
store i32 0, i32* %4
%6 = bitcast [1 x i8]* %5 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = load i64, i64* @errno
%13 = call i8* @strerror(i64 %12)
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 -1, i32* %1
br label %53
15: ; preds = %0
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%17 = load i32, i32* %16
%18 = load i32, i32* @F_SETFL
%19 = load i32, i32* @O_NONBLOCK
%20 = call i64 @fcntl(i32 %17, i32 %18, i32 %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %15
%23 = load i32, i32* @stderr
%24 = load i64, i64* @errno
%25 = call i8* @strerror(i64 %24)
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %25)
store i32 -1, i32* %1
br label %53
27: ; preds = %15
br label %28
28: ; preds = %35, %27
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds [1 x i8], [1 x i8]* %5, i64 0, i64 0
%32 = call i64 @write(i32 %30, i8* %31, i32 1)
%33 = icmp sgt i64 %32, 0
%34 = zext i1 %33 to i32
store i32 %34, i32* %3
br i1 %33, label %35, label %39
35: ; preds = %28
%36 = load i32, i32* %3
%37 = load i32, i32* %4
%38 = add nsw i32 %37, %36
store i32 %38, i32* %4
br label %28
39: ; preds = %28
%40 = load i64, i64* @errno
%41 = load i64, i64* @EAGAIN
%42 = icmp eq i64 %40, %41
br i1 %42, label %43, label %47
43: ; preds = %39
%44 = load i32, i32* @stdout
%45 = load i32, i32* %4
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %45)
br label %52
47: ; preds = %39
%48 = load i32, i32* @stderr
%49 = load i64, i64* @errno
%50 = call i8* @strerror(i64 %49)
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %47, %43
store i32 0, i32* %1
br label %53
53: ; preds = %52, %22, %10
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"Failed to create a pipe: %s\0A\00"
@errno = global i64 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [33 x i8] c"Could not set NONBLOCK flag: %s\0A\00"
@EAGAIN = global i64 0
@stdout = global i32 0
@.str.2 = [32 x i8] c"Capacity of pipe(in bytes): %d\0A\00"
@.str.3 = [42 x i8] c"Error while writing to input of pipe: %s\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [1 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [1 x i8], [1 x i8]* %2, i64 0, i64 0
store i8 99, i8* %4
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = load i64, i64* @errno
%11 = call i8* @strerror(i64 %10)
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %11)
br label %42
13: ; preds = %0
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%15 = load i32, i32* %14
%16 = load i32, i32* @F_SETFL
%17 = load i32, i32* @O_NONBLOCK
%18 = call i64 @fcntl(i32 %15, i32 %16, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %13
%21 = load i32, i32* @stderr
%22 = load i64, i64* @errno
%23 = call i8* @strerror(i64 %22)
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %23)
br label %42
25: ; preds = %13, %25
%26 = phi i32 [ %30, %25 ], [ 0, %13 ]
%27 = load i32, i32* %14
%28 = call i64 @write(i32 %27, i8* %4, i32 1)
%29 = icmp sgt i64 %28, 0
%30 = add nuw nsw i32 %26, 1
br i1 %29, label %25, label %31
31: ; preds = %25
%32 = load i64, i64* @errno
%33 = load i64, i64* @EAGAIN
%34 = icmp eq i64 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* @stdout
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %26)
br label %42
38: ; preds = %31
%39 = load i32, i32* @stderr
%40 = call i8* @strerror(i64 %32)
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0), i8* %40)
br label %42
42: ; preds = %35, %38, %20, %8
%43 = phi i32 [ -1, %8 ], [ -1, %20 ], [ 0, %38 ], [ 0, %35 ]
ret i32 %43
}