#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 */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
char *strerror(int /*<<< orphan*/);
int write(int, char *, int);
int main() {
  int pipefd[2], err, i;
  char buffer[4096];
  pipe(pipefd);
  err = write(pipefd[1], buffer, 4096);
  if (err < 0) {
    printf("Write error 1: %s\n", strerror(errno));
  }
  close(pipefd[1]);
  for (i = 0; i < 100; i++) {
    err = read(pipefd[0], buffer, 4096);
    if (err < 0) {
      printf("read error 1: %s\n", strerror(errno));
      break;
    }
    if (err == 0) {
      printf("Read EOF\n");
      break;
    } else {
      printf("Read %d bytes\n", err);
    }
  }
  err = read(pipefd[0], buffer, 4096);
  if (err < 0) {
    printf("read error 1: %s\n", strerror(errno));
  }
  if (err == 0) {
    printf("Read EOF\n");
  } else {
    printf("Read %d bytes\n", err);
  }
  err = write(pipefd[0], "hello", 5);
  if (err < 0) {
    printf("Write error 2: %s\n", strerror(errno));
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
mov    -0x8(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11bb <main+0x5b>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe4c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x64,-0x14(%rbp)
jge    125a <main+0xfa>
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1219 <main+0xb9>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125a <main+0xfa>
cmpl   $0x0,-0x10(%rbp)
jne    1236 <main+0xd6>
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125a <main+0xfa>
mov    -0x10(%rbp),%esi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    124c <main+0xec>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ca <main+0x6a>
mov    -0xc(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    129a <main+0x13a>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x10(%rbp)
jne    12b7 <main+0x157>
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c8 <main+0x168>
mov    -0x10(%rbp),%esi
lea    0xd6e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
lea    0xd6c(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1314 <main+0x1b4>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xd43(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131b <main+0x1bb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1010,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
test   %eax,%eax
jns    11bb <main+0x4b>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe4f(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    $0x64,%ebp
lea    0x10(%rsp),%rbx
lea    0xe50(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
test   %eax,%eax
js     120a <main+0x9a>
je     122b <main+0xbb>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xffffffff,%ebp
jne    11e0 <main+0x70>
jmp    1237 <main+0xc7>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdf4(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1237 <main+0xc7>
lea    0xe1b(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x1000,%edx
call   1080 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1260 <main+0xf0>
jne    127f <main+0x10f>
lea    0xdf4(%rip),%rdi
call   1030 <puts@plt>
jmp    128f <main+0x11f>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd9e(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd9f(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rsp),%edi
lea    0xd9a(%rip),%rsi
mov    $0x5,%edx
call   1040 <write@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jns    12d0 <main+0x160>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd79(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"Write error 1: %s\0A\00"
@errno = global i32 0
@.str.1 = [18 x i8] c"read error 1: %s\0A\00"
@.str.2 = [10 x i8] c"Read EOF\0A\00"
@.str.3 = [15 x i8] c"Read %d bytes\0A\00"
@.str.4 = [6 x i8] c"hello\00"
@.str.5 = [19 x i8] c"Write error 2: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4096 x 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 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%11 = call i32 @write(i32 %9, i8* %10, i32 4096)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %14, %0
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
store i32 0, i32* %4
br label %22
22: ; preds = %45, %18
%23 = load i32, i32* %4
%24 = icmp slt i32 %23, 100
br i1 %24, label %25, label %48
25: ; preds = %22
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%27 = load i32, i32* %26
%28 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%29 = call i32 @read(i32 %27, i8* %28, i32 4096)
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %25
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %34)
br label %48
36: ; preds = %25
%37 = load i32, i32* %3
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %48
41: ; preds = %36
%42 = load i32, i32* %3
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %41
br label %45
45: ; preds = %44
%46 = load i32, i32* %4
%47 = add nsw i32 %46, 1
store i32 %47, i32* %4
br label %22
48: ; preds = %39, %32, %22
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%50 = load i32, i32* %49
%51 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%52 = call i32 @read(i32 %50, i8* %51, i32 4096)
store i32 %52, i32* %3
%53 = load i32, i32* %3
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %48
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %57)
br label %59
59: ; preds = %55, %48
%60 = load i32, i32* %3
%61 = icmp eq i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %59
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %67
64: ; preds = %59
%65 = load i32, i32* %3
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %65)
br label %67
67: ; preds = %64, %62
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%69 = load i32, i32* %68
%70 = call i32 @write(i32 %69, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 5)
store i32 %70, i32* %3
%71 = load i32, i32* %3
%72 = icmp slt i32 %71, 0
br i1 %72, label %73, label %77
73: ; preds = %67
%74 = load i32, i32* @errno
%75 = call i8* @strerror(i32 %74)
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %75)
store i32 1, i32* %1
br label %78
77: ; preds = %67
store i32 0, i32* %1
br label %78
78: ; preds = %77, %73
%79 = load i32, i32* %1
ret i32 %79
}

LLVM IR (-O2):
@.str = [19 x i8] c"Write error 1: %s\0A\00"
@errno = global i32 0
@.str.1 = [18 x i8] c"read error 1: %s\0A\00"
@.str.3 = [15 x i8] c"Read %d bytes\0A\00"
@.str.4 = [6 x i8] c"hello\00"
@.str.5 = [19 x i8] c"Write error 2: %s\0A\00"
@str.6 = [9 x i8] c"Read EOF\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 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%8 = load i32, i32* %7
%9 = call i32 @write(i32 %8, i8* %4, i32 4096)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = load i32, i32* @errno
%13 = call i8* @strerror(i32 %12)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %11, %0
%16 = load i32, i32* %7
%17 = call i32 @close(i32 %16)
br label %18
18: ; preds = %15, %31
%19 = phi i32 [ 0, %15 ], [ %33, %31 ]
%20 = load i32, i32* %5
%21 = call i32 @read(i32 %20, i8* %4, i32 4096)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %25)
br label %35
27: ; preds = %18
%28 = icmp eq i32 %21, 0
br i1 %28, label %29, label %31
29: ; preds = %27
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.6, i64 0, i64 0))
br label %35
31: ; preds = %27
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%33 = add nuw nsw i32 %19, 1
%34 = icmp eq i32 %33, 100
br i1 %34, label %35, label %18
35: ; preds = %31, %29, %23
%36 = load i32, i32* %5
%37 = call i32 @read(i32 %36, i8* %4, i32 4096)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = load i32, i32* @errno
%41 = call i8* @strerror(i32 %40)
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %41)
br label %47
43: ; preds = %35
%44 = icmp eq i32 %37, 0
br i1 %44, label %45, label %47
45: ; preds = %43
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.6, i64 0, i64 0))
br label %49
47: ; preds = %39, %43
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %37)
br label %49
49: ; preds = %47, %45
%50 = load i32, i32* %5
%51 = call i32 @write(i32 %50, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 5)
%52 = icmp slt i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %49
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i8* %55)
br label %57
57: ; preds = %49, %53
%58 = phi i32 [ 1, %53 ], [ 0, %49 ]
ret i32 %58
}