#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 */
int /*<<< orphan*/ O_RDONLY;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(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)) < 0) {
    perror("open p1");
    return 1;
  }
  if ((fds[1] = open("p2", O_RDONLY)) < 0) {
    perror("open p2");
    return 1;
  }
  fd = 0;
  while (1) {
    i = read(fds[fd], buf, sizeof(buf) - 1);
    if (i < 0) {
      perror("read");
      return 1;
    } else if (!i) {
      printf("pipe closed\n");
      return 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    0x2e8f(%rip),%rax
mov    (%rax),%esi
lea    0xea3(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    118b <main+0x4b>
lea    0xe86(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1281 <main+0x141>
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11c4 <main+0x84>
lea    0xe55(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1281 <main+0x141>
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    1215 <main+0xd5>
lea    0xe0c(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1281 <main+0x141>
cmpl   $0x0,-0x1014(%rbp)
jne    123c <main+0xfc>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1281 <main+0x141>
jmp    1241 <main+0x101>
movslq -0x1014(%rbp),%rax
movb   $0x0,-0x1010(%rbp,%rax,1)
lea    -0x1010(%rbp),%rsi
lea    0xdc4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1018(%rbp),%eax
add    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %edx,-0x1018(%rbp)
jmp    11ce <main+0x8e>
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    0x2e83(%rip),%r14
mov    (%r14),%esi
lea    0xe96(%rip),%rdi
call   1060 <open@plt>
mov    %eax,0x8(%rsp)
test   %eax,%eax
js     122f <main+0xdf>
mov    %eax,%ebx
mov    (%r14),%esi
lea    0xe81(%rip),%rdi
call   1060 <open@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
js     1238 <main+0xe8>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xfff,%edx
call   1050 <read@plt>
test   %eax,%eax
js     1216 <main+0xc6>
lea    0xe5c(%rip),%r14
lea    0x10(%rsp),%r15
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
test   %eax,%eax
je     121f <main+0xcf>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12d,%ebx
add    $0x1,%ebx
mov    %ebx,%eax
shr    $0x1f,%eax
add    %r12d,%eax
add    $0x1,%eax
and    $0xfffffffe,%eax
sub    %eax,%ebx
mov    0x8(%rsp,%rbx,4),%edi
mov    %r15,%rsi
mov    $0xfff,%edx
call   1050 <read@plt>
mov    %ebx,%r12d
test   %eax,%eax
jns    11d0 <main+0x80>
lea    0xdf3(%rip),%rdi
jmp    123f <main+0xef>
lea    0xdf8(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1249 <main+0xf9>
lea    0xdca(%rip),%rdi
jmp    123f <main+0xef>
lea    0xdc9(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"p1\00"
@O_RDONLY = 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"
@.str.4 = [5 x i8] c"read\00"
@.str.5 = [13 x i8] c"pipe closed\0A\00"
@.str.6 = [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 = call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
store i32 %7, i32* %8
%9 = icmp slt i32 %7, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = 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 %46
12: ; preds = %0
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
store i32 %14, i32* %15
%16 = icmp slt i32 %14, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = 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 %46
19: ; preds = %12
store i32 0, i32* %5
br label %20
20: ; preds = %19, %37
%21 = load i32, i32* %5
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 %22
%24 = load i32, i32* %23
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 4095)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %20
%30 = 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 %46
31: ; preds = %20
%32 = load i32, i32* %4
%33 = icmp ne i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %31
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* %1
br label %46
36: ; preds = %31
br label %37
37: ; preds = %36
%38 = load i32, i32* %4
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %39
store i8 0, i8* %40
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %41)
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1
%45 = srem i32 %44, 2
store i32 %45, i32* %5
br label %20
46: ; preds = %34, %29, %17, %10
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
@.str = [3 x i8] c"p1\00"
@O_RDONLY = 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"
@.str.4 = [5 x i8] c"read\00"
@.str.6 = [9 x i8] c"read: %s\00"
@str = [12 x i8] c"pipe closed\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 = tail call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
store i32 %6, i32* %7
%8 = icmp slt i32 %6, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %40
11: ; preds = %0
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
store i32 %13, i32* %14
%15 = icmp slt i32 %13, 0
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = call i32 @read(i32 %6, i8* %4, i32 4095)
%18 = icmp slt i32 %17, 0
br i1 %18, label %21, label %23
19: ; preds = %11
%20 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
br label %40
21: ; preds = %29, %16
%22 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %40
23: ; preds = %16, %29
%24 = phi i32 [ %38, %29 ], [ %17, %16 ]
%25 = phi i32 [ %34, %29 ], [ 0, %16 ]
%26 = icmp eq i32 %24, 0
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %40
29: ; preds = %23
%30 = zext i32 %24 to i64
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %30
store i8 0, i8* %31
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %4)
%33 = add nsw i32 %25, 1
%34 = srem i32 %33, 2
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 %35
%37 = load i32, i32* %36
%38 = call i32 @read(i32 %37, i8* %4, i32 4095)
%39 = icmp slt i32 %38, 0
br i1 %39, label %21, label %23
40: ; preds = %27, %21, %19, %9
%41 = phi i32 [ 1, %9 ], [ 1, %19 ], [ 1, %21 ], [ 0, %27 ]
ret i32 %41
}