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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ca <main+0x5a>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1307 <main+0x197>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
call   1040 <dup2@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1213 <main+0xa3>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1307 <main+0x197>
movl   $0x0,-0x20(%rbp)
cmpl   $0x14,-0x20(%rbp)
jge    1246 <main+0xd6>
mov    -0x14(%rbp),%edi
lea    0xdf4(%rip),%rsi
mov    $0xa,%edx
call   1030 <write@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    121a <main+0xaa>
movl   $0x0,-0x20(%rbp)
cmpl   $0x64,-0x20(%rbp)
jge    12f0 <main+0x180>
mov    -0x18(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    129c <main+0x12c>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %rax,%rsi
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12f0 <main+0x180>
cmpl   $0x0,-0x1c(%rbp)
jne    12b9 <main+0x149>
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12f0 <main+0x180>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
mov    -0x1c(%rbp),%esi
lea    -0x1020(%rbp),%rdx
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12e2 <main+0x172>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    124d <main+0xdd>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
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   1080 <pipe@plt>
test   %eax,%eax
js     1364 <main+0x1e4>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    0xc(%rsp),%esi
call   1050 <dup2@plt>
test   %eax,%eax
js     137b <main+0x1fb>
mov    0xc(%rsp),%edi
lea    0xe60(%rip),%rbx
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    $0xa,%edx
call   1040 <write@plt>
mov    $0x64,%ebp
lea    0x10(%rsp),%rbx
lea    0xd1a(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %eax,%eax
js     13a1 <main+0x221>
je     13c2 <main+0x242>
mov    %eax,%ecx
movb   $0x0,0x10(%rsp,%rcx,1)
mov    %r14,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
add    $0xffffffff,%ebp
jne    1330 <main+0x1b0>
jmp    13ce <main+0x24e>
mov    0x2c6d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xc87(%rip),%rdi
jmp    1390 <main+0x210>
mov    0x2c56(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xc81(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%eax
jmp    13e2 <main+0x262>
mov    0x2c30(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xc77(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    13ce <main+0x24e>
lea    0xc89(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [17 x i8] c"pipe failed: %s\0A\00"
@errno = global i32 0
@STDOUT_FILENO = global i32 0
@.str.1 = [17 x i8] c"dup2 failed: %s\0A\00"
@.str.2 = [11 x i8] c"hello pipe\00"
@.str.3 = [16 x i8] c"read error: %s\0A\00"
@.str.5 = [21 x i8] c"Read %d bytes: '%s'\0A\00"
@str = [9 x i8] c"Read EOF\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [4096 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %89
14: ; preds = %2
%15 = load i32, i32* @STDOUT_FILENO
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @dup2(i32 %15, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %61, label %20
20: ; preds = %14
%21 = load i32, i32* %16
%22 = call i32 @write(i32 %21, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%23 = load i32, i32* %16
%24 = call i32 @write(i32 %23, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%25 = load i32, i32* %16
%26 = call i32 @write(i32 %25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%27 = load i32, i32* %16
%28 = call i32 @write(i32 %27, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%29 = load i32, i32* %16
%30 = call i32 @write(i32 %29, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%31 = load i32, i32* %16
%32 = call i32 @write(i32 %31, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%33 = load i32, i32* %16
%34 = call i32 @write(i32 %33, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%35 = load i32, i32* %16
%36 = call i32 @write(i32 %35, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%37 = load i32, i32* %16
%38 = call i32 @write(i32 %37, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%39 = load i32, i32* %16
%40 = call i32 @write(i32 %39, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%41 = load i32, i32* %16
%42 = call i32 @write(i32 %41, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%43 = load i32, i32* %16
%44 = call i32 @write(i32 %43, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%45 = load i32, i32* %16
%46 = call i32 @write(i32 %45, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%47 = load i32, i32* %16
%48 = call i32 @write(i32 %47, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%49 = load i32, i32* %16
%50 = call i32 @write(i32 %49, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%51 = load i32, i32* %16
%52 = call i32 @write(i32 %51, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%53 = load i32, i32* %16
%54 = call i32 @write(i32 %53, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%55 = load i32, i32* %16
%56 = call i32 @write(i32 %55, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%57 = load i32, i32* %16
%58 = call i32 @write(i32 %57, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
%59 = load i32, i32* %16
%60 = call i32 @write(i32 %59, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 10)
br label %65
61: ; preds = %14
%62 = load i32, i32* @errno
%63 = call i8* @strerror(i32 %62)
%64 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %63)
br label %89
65: ; preds = %20, %78
%66 = phi i32 [ %82, %78 ], [ 0, %20 ]
%67 = load i32, i32* %7
%68 = call i32 @read(i32 %67, i8* %6, i32 1024)
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %65
%71 = load i32, i32* @errno
%72 = call i8* @strerror(i32 %71)
%73 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %72)
br label %84
74: ; preds = %65
%75 = icmp eq i32 %68, 0
br i1 %75, label %76, label %78
76: ; preds = %74
%77 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %84
78: ; preds = %74
%79 = zext i32 %68 to i64
%80 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %79
store i8 0, i8* %80
%81 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %68, i8* %6)
%82 = add nuw nsw i32 %66, 1
%83 = icmp eq i32 %82, 100
br i1 %83, label %84, label %65
84: ; preds = %78, %76, %70
%85 = load i32, i32* %16
%86 = call i32 @close(i32 %85)
%87 = load i32, i32* %7
%88 = call i32 @close(i32 %87)
br label %89
89: ; preds = %84, %61, %10
%90 = phi i32 [ 1, %10 ], [ 1, %61 ], [ 0, %84 ]
ret i32 %90
}