#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 ssize_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EWOULDBLOCK;
int O_CLOEXEC;
int O_NONBLOCK;
int /*<<< orphan*/ err(int, char *);
scalar_t__ errno;
int /*<<< orphan*/ errx(int, char *, ...);
scalar_t__ pipe2(int *, int);
int /*<<< orphan*/ puts(char *);
int read(int, char *, int);
int write(int, char *, int);
int main(void) {
  int p[2];
  if (pipe2(p, O_NONBLOCK | O_CLOEXEC) < 0) {
    err(1, "pipe2");
  }
  ssize_t ret = write(p[1], "a", 1);
  if (ret < 0) {
    err(1, "write");
  } else if (ret != 1) {
    errx(1, "invalid return value from write: %zd\n", ret);
  }
  char c;
  ret = read(p[0], &c, 1);
  if (ret < 0) {
    err(1, "read");
  } else if (ret != 1) {
    errx(1, "invalid return value from read: %zd\n", ret);
  }
  ret = read(p[0], &c, 1);
  if (ret > 0) {
    errx(1, "read returned unexpected data: %zd\n", ret);
  } else if (ret == 0) {
    errx(1, "read returned 0 instead of EAGAIN\n");
  } else if (errno != EAGAIN && errno != EWOULDBLOCK) {
    err(1, "unexpected read failure");
  }
  puts("TEST OK");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    0x2e3d(%rip),%rax
or     (%rax),%esi
call   1080 <pipe2@plt>
cmp    $0x0,%rax
jge    11a5 <main+0x45>
mov    $0x1,%edi
lea    0xe60(%rip),%rsi
call   1070 <err@plt>
mov    -0x8(%rbp),%edi
lea    0xe57(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11dc <main+0x7c>
mov    $0x1,%edi
lea    0xe36(%rip),%rsi
call   1070 <err@plt>
jmp    1201 <main+0xa1>
cmpl   $0x1,-0x10(%rbp)
je     11fc <main+0x9c>
mov    -0x10(%rbp),%edx
mov    $0x1,%edi
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1050 <errx@plt>
jmp    1201 <main+0xa1>
mov    -0xc(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1235 <main+0xd5>
mov    $0x1,%edi
lea    0xe09(%rip),%rsi
call   1070 <err@plt>
jmp    125a <main+0xfa>
cmpl   $0x1,-0x10(%rbp)
je     1255 <main+0xf5>
mov    -0x10(%rbp),%edx
mov    $0x1,%edi
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1050 <errx@plt>
jmp    125a <main+0xfa>
mov    -0xc(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1293 <main+0x133>
mov    -0x10(%rbp),%edx
mov    $0x1,%edi
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1050 <errx@plt>
jmp    1304 <main+0x1a4>
cmpl   $0x0,-0x10(%rbp)
jne    12b5 <main+0x155>
mov    $0x1,%edi
lea    0xdd9(%rip),%rsi
mov    $0x0,%al
call   1050 <errx@plt>
jmp    12ff <main+0x19f>
mov    0x2d1c(%rip),%rax
mov    (%rax),%rax
mov    0x2cf2(%rip),%rcx
cmp    (%rcx),%rax
je     12fa <main+0x19a>
mov    0x2d02(%rip),%rax
mov    (%rax),%rax
mov    0x2cf0(%rip),%rcx
cmp    (%rcx),%rax
je     12fa <main+0x19a>
mov    $0x1,%edi
lea    0xdb0(%rip),%rsi
call   1070 <err@plt>
jmp    12ff <main+0x19f>
jmp    1304 <main+0x1a4>
lea    0xdb2(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x18,%rsp
mov    0x2e7d(%rip),%rax
mov    0x2e4e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0x10(%rsp),%rdi
call   1080 <pipe2@plt>
test   %rax,%rax
jns    1196 <main+0x36>
lea    0xe74(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    0x14(%rsp),%edi
lea    0xe65(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
test   %eax,%eax
js     11cb <main+0x6b>
cmp    $0x1,%eax
je     11dc <main+0x7c>
lea    0xe53(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <errx@plt>
jmp    11dc <main+0x7c>
lea    0xe36(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
js     120f <main+0xaf>
cmp    $0x1,%eax
je     1220 <main+0xc0>
lea    0xe3a(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <errx@plt>
jmp    1220 <main+0xc0>
lea    0xe1e(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    125f <main+0xff>
lea    0xe20(%rip),%rsi
mov    $0x1,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <errx@plt>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
ret
je     1296 <main+0x136>
mov    0x2d70(%rip),%rax
mov    (%rax),%rax
mov    0x2d46(%rip),%rcx
cmp    (%rcx),%rax
je     124c <main+0xec>
mov    0x2d52(%rip),%rcx
cmp    (%rcx),%rax
je     124c <main+0xec>
lea    0xe1b(%rip),%rsi
mov    $0x1,%edi
call   1070 <err@plt>
jmp    124c <main+0xec>
lea    0xde5(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1050 <errx@plt>
jmp    124c <main+0xec>

LLVM IR (-O0):
@O_NONBLOCK = global i32 0
@O_CLOEXEC = global i32 0
@.str = [6 x i8] c"pipe2\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [38 x i8] c"invalid return value from write: %zd\0A\00"
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [37 x i8] c"invalid return value from read: %zd\0A\00"
@.str.6 = [36 x i8] c"read returned unexpected data: %zd\0A\00"
@.str.7 = [35 x i8] c"read returned 0 instead of EAGAIN\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str.8 = [24 x i8] c"unexpected read failure\00"
@.str.9 = [8 x i8] c"TEST OK\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i8
store i32 0, i32* %1
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%6 = load i32, i32* @O_NONBLOCK
%7 = load i32, i32* @O_CLOEXEC
%8 = or i32 %6, %7
%9 = call i64 @pipe2(i32* %5, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%15 = load i32, i32* %14
%16 = call i32 @write(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %28
21: ; preds = %13
%22 = load i32, i32* %3
%23 = icmp ne i32 %22, 1
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* %3
%26 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %24, %21
br label %28
28: ; preds = %27, %19
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%30 = load i32, i32* %29
%31 = call i32 @read(i32 %30, i8* %4, i32 1)
store i32 %31, i32* %3
%32 = load i32, i32* %3
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %43
36: ; preds = %28
%37 = load i32, i32* %3
%38 = icmp ne i32 %37, 1
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32, i32* %3
%41 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %39, %36
br label %43
43: ; preds = %42, %34
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%45 = load i32, i32* %44
%46 = call i32 @read(i32 %45, i8* %4, i32 1)
store i32 %46, i32* %3
%47 = load i32, i32* %3
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %43
%50 = load i32, i32* %3
%51 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i32 %50)
br label %69
52: ; preds = %43
%53 = load i32, i32* %3
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0))
br label %68
57: ; preds = %52
%58 = load i64, i64* @errno
%59 = load i64, i64* @EAGAIN
%60 = icmp ne i64 %58, %59
br i1 %60, label %61, label %67
61: ; preds = %57
%62 = load i64, i64* @errno
%63 = load i64, i64* @EWOULDBLOCK
%64 = icmp ne i64 %62, %63
br i1 %64, label %65, label %67
65: ; preds = %61
%66 = call i32 @err(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
br label %67
67: ; preds = %65, %61, %57
br label %68
68: ; preds = %67, %55
br label %69
69: ; preds = %68, %49
%70 = call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@O_NONBLOCK = global i32 0
@O_CLOEXEC = global i32 0
@.str = [6 x i8] c"pipe2\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [6 x i8] c"write\00"
@.str.3 = [38 x i8] c"invalid return value from write: %zd\0A\00"
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [37 x i8] c"invalid return value from read: %zd\0A\00"
@.str.6 = [36 x i8] c"read returned unexpected data: %zd\0A\00"
@.str.7 = [35 x i8] c"read returned 0 instead of EAGAIN\0A\00"
@errno = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@.str.8 = [24 x i8] c"unexpected read failure\00"
@.str.9 = [8 x i8] c"TEST OK\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 = load i32, i32* @O_NONBLOCK
%6 = load i32, i32* @O_CLOEXEC
%7 = or i32 %6, %5
%8 = call i64 @pipe2(i32* %4, i32 %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%14 = load i32, i32* %13
%15 = call i32 @write(i32 %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 1)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @err(i32 1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %23
19: ; preds = %12
%20 = icmp eq i32 %15, 1
br i1 %20, label %23, label %21
21: ; preds = %19
%22 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.3, i64 0, i64 0), i32 %15)
br label %23
23: ; preds = %19, %21, %17
%24 = load i32, i32* %4
%25 = call i32 @read(i32 %24, i8* %2, i32 1)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %33
29: ; preds = %23
%30 = icmp eq i32 %25, 1
br i1 %30, label %33, label %31
31: ; preds = %29
%32 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0), i32 %25)
br label %33
33: ; preds = %29, %31, %27
%34 = load i32, i32* %4
%35 = call i32 @read(i32 %34, i8* %2, i32 1)
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.6, i64 0, i64 0), i32 %35)
br label %52
39: ; preds = %33
%40 = icmp eq i32 %35, 0
br i1 %40, label %41, label %43
41: ; preds = %39
%42 = call i32 (i32, i8*, ...) @errx(i32 1, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0))
br label %52
43: ; preds = %39
%44 = load i64, i64* @errno
%45 = load i64, i64* @EAGAIN
%46 = icmp eq i64 %44, %45
%47 = load i64, i64* @EWOULDBLOCK
%48 = icmp eq i64 %44, %47
%49 = select i1 %46, i1 true, i1 %48
br i1 %49, label %52, label %50
50: ; preds = %43
%51 = call i32 @err(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
br label %52
52: ; preds = %41, %50, %43, %37
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
ret i32 0
}