#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*/ buf;

/* Variables and functions */
scalar_t__ EAGAIN;
int O_NONBLOCK;
int O_RDONLY;
scalar_t__ errno;
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int main(void) {
  int fds[2];
  char buf[4096];
  int i;
  int fd;
  if ((fds[0] = open("p1", O_RDONLY | O_NONBLOCK)) < 0) {
    perror("open p1");
    return 1;
  }
  if ((fds[1] = open("p2", O_RDONLY | O_NONBLOCK)) < 0) {
    perror("open p2");
    return 1;
  }
  fd = 0;
  while (1) {
    i = read(fds[fd], buf, sizeof(buf) - 1);
    if ((i < 0) && (errno != EAGAIN)) {
      perror("read");
      return 1;
    } else if (i > 0) {
      buf[i] = '\0';
      printf("read: %s", buf);
    }
    fd = (fd + 1) % 2;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
mov    0x2e86(%rip),%rax
or     (%rax),%esi
lea    0xe9a(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1194 <main+0x54>
lea    0xe7d(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1291 <main+0x151>
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
mov    0x2e44(%rip),%rax
or     (%rax),%esi
lea    0xe60(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11d6 <main+0x96>
lea    0xe43(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1291 <main+0x151>
movl   $0x0,-0x1018(%rbp)
movslq -0x1018(%rbp),%rax
mov    -0xc(%rbp,%rax,4),%edi
lea    -0x1010(%rbp),%rsi
mov    $0xfff,%edx
call   1040 <read@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jge    1241 <main+0x101>
mov    0x2dba(%rip),%rax
mov    (%rax),%rax
mov    0x2da0(%rip),%rcx
cmp    (%rcx),%rax
je     1241 <main+0x101>
lea    0xde0(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1291 <main+0x151>
cmpl   $0x0,-0x1014(%rbp)
jle    1270 <main+0x130>
movslq -0x1014(%rbp),%rax
movb   $0x0,-0x1010(%rbp,%rax,1)
lea    -0x1010(%rbp),%rsi
lea    0xdaa(%rip),%rdi
call   1030 <printf@plt>
jmp    1275 <main+0x135>
mov    -0x1018(%rbp),%eax
add    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %edx,-0x1018(%rbp)
jmp    11e0 <main+0xa0>
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    0x2e88(%rip),%r14
mov    0x2e89(%rip),%rbx
mov    (%rbx),%esi
or     (%r14),%esi
lea    0xe9a(%rip),%rdi
call   1050 <open@plt>
mov    %eax,0x8(%rsp)
test   %eax,%eax
jns    1184 <main+0x44>
lea    0xe81(%rip),%rdi
jmp    1223 <main+0xe3>
mov    %eax,%ebp
mov    (%rbx),%esi
or     (%r14),%esi
lea    0xe7b(%rip),%rdi
call   1050 <open@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
jns    11a8 <main+0x68>
lea    0xe62(%rip),%rdi
jmp    1223 <main+0xe3>
xor    %ebx,%ebx
lea    0x10(%rsp),%r13
mov    0x2e1a(%rip),%r15
mov    0x2e03(%rip),%r12
lea    0xe51(%rip),%r14
jmp    11f3 <main+0xb3>
cs nopw 0x0(%rax,%rax,1)
mov    (%r15),%rax
cmp    (%r12),%rax
jne    121c <main+0xdc>
mov    %ebx,%eax
add    $0x1,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %ebx,%ecx
add    $0x1,%ecx
and    $0xfffffffe,%ecx
sub    %ecx,%eax
mov    0x8(%rsp,%rax,4),%ebp
mov    %eax,%ebx
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0xfff,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11d0 <main+0x90>
je     11d9 <main+0x99>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %r13,%rsi
call   1030 <printf@plt>
jmp    11d9 <main+0x99>
lea    0xded(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"p1\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [8 x i8] c"open p1\00"
@.str.2 = [3 x i8] c"p2\00"
@.str.3 = [8 x i8] c"open p2\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [9 x i8] c"read: %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [4096 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @O_RDONLY
%7 = load i32, i32* @O_NONBLOCK
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
store i32 %9, i32* %10
%11 = icmp slt i32 %9, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %53
14: ; preds = %0
%15 = load i32, i32* @O_RDONLY
%16 = load i32, i32* @O_NONBLOCK
%17 = or i32 %15, %16
%18 = call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
store i32 %18, i32* %19
%20 = icmp slt i32 %18, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %53
23: ; preds = %14
store i32 0, i32* %5
br label %24
24: ; preds = %23, %49
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 %26
%28 = load i32, i32* %27
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%30 = call i32 @read(i32 %28, i8* %29, i32 4095)
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %24
%34 = load i64, i64* @errno
%35 = load i64, i64* @EAGAIN
%36 = icmp ne i64 %34, %35
br i1 %36, label %37, label %39
37: ; preds = %33
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %53
39: ; preds = %33, %24
%40 = load i32, i32* %4
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %39
%43 = load i32, i32* %4
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %44
store i8 0, i8* %45
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%47 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %42, %39
br label %49
49: ; preds = %48
%50 = load i32, i32* %5
%51 = add nsw i32 %50, 1
%52 = srem i32 %51, 2
store i32 %52, i32* %5
br label %24
53: ; preds = %37, %21, %12
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
@.str = [3 x i8] c"p1\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [8 x i8] c"open p1\00"
@.str.2 = [3 x i8] c"p2\00"
@.str.3 = [8 x i8] c"open p2\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [9 x i8] c"read: %s\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [4096 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_RDONLY
%6 = load i32, i32* @O_NONBLOCK
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
store i32 %8, i32* %9
%10 = icmp slt i32 %8, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %45
13: ; preds = %0
%14 = load i32, i32* @O_RDONLY
%15 = load i32, i32* @O_NONBLOCK
%16 = or i32 %15, %14
%17 = tail call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
store i32 %17, i32* %18
%19 = icmp slt i32 %17, 0
br i1 %19, label %20, label %22
20: ; preds = %13
%21 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %45
22: ; preds = %13, %39
%23 = phi i32 [ %44, %39 ], [ %8, %13 ]
%24 = phi i32 [ %41, %39 ], [ 0, %13 ]
%25 = call i32 @read(i32 %23, i8* %4, i32 4095)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %22
%28 = load i64, i64* @errno
%29 = load i64, i64* @EAGAIN
%30 = icmp eq i64 %28, %29
br i1 %30, label %39, label %31
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %45
33: ; preds = %22
%34 = icmp eq i32 %25, 0
br i1 %34, label %39, label %35
35: ; preds = %33
%36 = zext i32 %25 to i64
%37 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %36
store i8 0, i8* %37
%38 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %4)
br label %39
39: ; preds = %27, %33, %35
%40 = add nsw i32 %24, 1
%41 = srem i32 %40, 2
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 %42
%44 = load i32, i32* %43
br label %22
45: ; preds = %31, %20, %11
ret i32 1
}