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

/* Variables and functions */
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
scalar_t__ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  int fd[2];
  char buf;
  if (pipe(fd) != 0) {
    perror("Error creating pipe!");
  }
  pid_t pid;
  if ((pid = fork()) == -1) {
    perror("Error forking!");
  }
  if (pid == 0) {
    close(fd[1]);
    dup2(fd[0], STDIN_FILENO);
  } else {
    close(fd[0]);
    dup2(fd[1], STDOUT_FILENO);
  }
  while (read(STDIN_FILENO, &buf, 1) > 0) {
    write(STDOUT_FILENO, &buf, 1);
  }
  if (pid == 0) {
    close(fd[0]);
  } else {
    close(fd[1]);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     11a5 <main+0x35>
lea    0xe60(%rip),%rdi
call   1080 <perror@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x20(%rbp)
cmp    $0xffffffff,%eax
jne    11c4 <main+0x54>
lea    0xe56(%rip),%rdi
call   1080 <perror@plt>
cmpl   $0x0,-0x20(%rbp)
jne    11ec <main+0x7c>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
jmp    1205 <main+0x95>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
jmp    120a <main+0x9a>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jle    1247 <main+0xd7>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    120a <main+0x9a>
cmpl   $0x0,-0x20(%rbp)
jne    125e <main+0xee>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
jmp    1266 <main+0xf6>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%r14
mov    %r14,%rdi
call   1060 <pipe@plt>
test   %rax,%rax
jne    1258 <main+0xe8>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
je     11bf <main+0x4f>
cmp    $0xffffffff,%eax
je     1269 <main+0xf9>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x2e3a(%rip),%rax
xor    %r15d,%r15d
mov    0xc(%rsp),%edi
jmp    11d6 <main+0x66>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    $0x1,%r15b
mov    0x2e06(%rip),%rax
mov    0x8(%rsp),%edi
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    0x2df4(%rip),%r12
mov    (%r12),%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
jle    1236 <main+0xc6>
mov    0x2ded(%rip),%rbp
lea    0x7(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    (%r12),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
jg     1210 <main+0xa0>
lea    0xc(%rsp),%rax
test   %r15b,%r15b
cmovne %r14,%rax
mov    (%rax),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda1(%rip),%rdi
call   1080 <perror@plt>
jmp    1192 <main+0x22>
lea    0xda5(%rip),%rdi
call   1080 <perror@plt>
jmp    11a6 <main+0x36>

LLVM IR (-O0):
@.str = [21 x i8] c"Error creating pipe!\00"
@.str.1 = [15 x i8] c"Error forking!\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i8
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%10 = call i64 @pipe(i32* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = call i32 (...) @fork()
store i32 %15, i32* %8
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %19
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%27 = load i32, i32* %26
%28 = load i32, i32* @STDIN_FILENO
%29 = call i32 @dup2(i32 %27, i32 %28)
br label %38
30: ; preds = %19
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%35 = load i32, i32* %34
%36 = load i32, i32* @STDOUT_FILENO
%37 = call i32 @dup2(i32 %35, i32 %36)
br label %38
38: ; preds = %30, %22
br label %39
39: ; preds = %43, %38
%40 = load i32, i32* @STDIN_FILENO
%41 = call i64 @read(i32 %40, i8* %7, i32 1)
%42 = icmp sgt i64 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = load i32, i32* @STDOUT_FILENO
%45 = call i32 @write(i32 %44, i8* %7, i32 1)
br label %39
46: ; preds = %39
%47 = load i32, i32* %8
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
br label %57
53: ; preds = %46
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%55 = load i32, i32* %54
%56 = call i32 @close(i32 %55)
br label %57
57: ; preds = %53, %49
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@.str = [21 x i8] c"Error creating pipe!\00"
@.str.1 = [15 x i8] c"Error forking!\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca i8
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = call i32 (...) @fork()
switch i32 %12, label %20 [
i32 -1, label %13
i32 0, label %15
]
13: ; preds = %11
%14 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %20
15: ; preds = %11
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* %6
br label %25
20: ; preds = %11, %13
%21 = load i32, i32* %6
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%24 = load i32, i32* %23
br label %25
25: ; preds = %20, %15
%26 = phi i32* [ @STDOUT_FILENO, %20 ], [ @STDIN_FILENO, %15 ]
%27 = phi i32 [ %24, %20 ], [ %19, %15 ]
%28 = phi i1 [ false, %20 ], [ true, %15 ]
%29 = load i32, i32* %26
%30 = call i32 @dup2(i32 %27, i32 %29)
%31 = load i32, i32* @STDIN_FILENO
%32 = call i64 @read(i32 %31, i8* %4, i32 1)
%33 = icmp sgt i64 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %25, %34
%35 = load i32, i32* @STDOUT_FILENO
%36 = call i32 @write(i32 %35, i8* %4, i32 1)
%37 = load i32, i32* @STDIN_FILENO
%38 = call i64 @read(i32 %37, i8* %4, i32 1)
%39 = icmp sgt i64 %38, 0
br i1 %39, label %34, label %40
40: ; preds = %34, %25
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%42 = load i32, i32* %41
%43 = load i32, i32* %6
%44 = select i1 %28, i32 %43, i32 %42
%45 = call i32 @close(i32 %44)
ret i32 0
}