#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*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int fcntl(int, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int write(int, char *, int);
int main(void) {
  int fd[2];
  pipe(fd);
  int flags = fcntl(fd[1], F_GETFL, 0);
  flags |= O_NONBLOCK;
  fcntl(fd[1], F_SETFL, flags);
  int i = 0;
  char x = 'x';
  while (write(fd[1], &x, 1) == 1)
    i++;
  perror("write() perror message");
  if (errno == EAGAIN || errno == EWOULDBLOCK)
    printf("errno indicates that we would have blocked\n");
  printf("wrote %d bytes into pipe before we couldn't write any more.\n", i);
  close(fd[0]);
  close(fd[1]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
mov    -0x8(%rbp),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%edx
call   1030 <fcntl@plt>
movl   $0x0,-0x14(%rbp)
movb   $0x78,-0x15(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0x1,%eax
jne    11e4 <main+0x84>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11bc <main+0x5c>
lea    0xe15(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%rax
mov    0x2daf(%rip),%rcx
cmp    (%rcx),%rax
je     1224 <main+0xc4>
mov    0x2dbf(%rip),%rax
mov    (%rax),%rax
mov    0x2dad(%rip),%rcx
cmp    (%rcx),%rax
jne    1232 <main+0xd2>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0x10(%rsp),%rdi
call   1080 <pipe@plt>
mov    0x14(%rsp),%edi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2e4c(%rip),%rcx
or     (%rcx),%eax
mov    0x14(%rsp),%edi
mov    0x2e2f(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edx
call   1040 <fcntl@plt>
movb   $0x78,0xf(%rsp)
mov    $0xffffffff,%ebx
lea    0xf(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x14(%rsp),%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
add    $0x1,%ebx
cmp    $0x1,%eax
je     11d0 <main+0x60>
lea    0xe10(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%rax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%rax
je     1217 <main+0xa7>
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%rax
jne    1223 <main+0xb3>
lea    0xe36(%rip),%rdi
call   1030 <puts@plt>
lea    0xded(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x10(%rsp),%edi
call   1070 <close@plt>
mov    0x14(%rsp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str = [23 x i8] c"write() perror message\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str.1 = [44 x i8] c"errno indicates that we would have blocked\0A\00"
@.str.2 = [61 x i8] c"wrote %d bytes into pipe before we couldn't write any more.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
store i32 0, i32* %1
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%9 = load i32, i32* %8
%10 = load i32, i32* @F_GETFL
%11 = call i32 @fcntl(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %3
%12 = load i32, i32* @O_NONBLOCK
%13 = load i32, i32* %3
%14 = or i32 %13, %12
store i32 %14, i32* %3
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%16 = load i32, i32* %15
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* %3
%19 = call i32 @fcntl(i32 %16, i32 %17, i32 %18)
store i32 0, i32* %4
store i8 120, i8* %5
br label %20
20: ; preds = %25, %0
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @write(i32 %22, i8* %5, i32 1)
%24 = icmp eq i32 %23, 1
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i32, i32* %4
%27 = add nsw i32 %26, 1
store i32 %27, i32* %4
br label %20
28: ; preds = %20
%29 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%30 = load i64, i64* @errno
%31 = load i64, i64* @EAGAIN
%32 = icmp eq i64 %30, %31
br i1 %32, label %37, label %33
33: ; preds = %28
%34 = load i64, i64* @errno
%35 = load i64, i64* @EWOULDBLOCK
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %33, %28
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %33
%40 = load i32, i32* %4
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
ret i32 0
}

LLVM IR (-O2):
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@.str = [23 x i8] c"write() perror message\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str.2 = [61 x i8] c"wrote %d bytes into pipe before we couldn't write any more.\0A\00"
@str = [43 x i8] c"errno indicates that we would have blocked\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i8
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%7 = load i32, i32* %6
%8 = load i32, i32* @F_GETFL
%9 = call i32 @fcntl(i32 %7, i32 %8, i32 0)
%10 = load i32, i32* @O_NONBLOCK
%11 = or i32 %10, %9
%12 = load i32, i32* %6
%13 = load i32, i32* @F_SETFL
%14 = call i32 @fcntl(i32 %12, i32 %13, i32 %11)
store i8 120, i8* %2
br label %15
15: ; preds = %15, %0
%16 = phi i32 [ 0, %0 ], [ %20, %15 ]
%17 = load i32, i32* %6
%18 = call i32 @write(i32 %17, i8* %2, i32 1)
%19 = icmp eq i32 %18, 1
%20 = add nuw nsw i32 %16, 1
br i1 %19, label %15, label %21
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%23 = load i64, i64* @errno
%24 = load i64, i64* @EAGAIN
%25 = icmp eq i64 %23, %24
%26 = load i64, i64* @EWOULDBLOCK
%27 = icmp eq i64 %23, %26
%28 = select i1 %25, i1 true, i1 %27
br i1 %28, label %29, label %31
29: ; preds = %21
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
br label %31
31: ; preds = %21, %29
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%33 = load i32, i32* %4
%34 = call i32 @close(i32 %33)
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
ret i32 0
}