#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int pipefd[2];
  int fileid;
  pid_t cpid;
  char buf;
  char buf2;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if (pipe(pipefd) == -1) {
    perror("pipe");
    exit(EXIT_FAILURE);
  }
  cpid = fork();
  if (cpid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (cpid == 0) {
    fileid = open(argv[1], O_RDONLY);
    close(pipefd[0]);
    while (read(fileid, &buf2, 1) > 0)
      write(pipefd[1], &buf2, 1);
    close(pipefd[1]);
    exit(EXIT_SUCCESS);
  } else {
    close(pipefd[1]);
    while (read(pipefd[0], &buf, 1) > 0)
      write(STDOUT_FILENO, &buf, 1);
    write(STDOUT_FILENO, "\n", 1);
    close(pipefd[0]);
    exit(EXIT_SUCCESS);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11da <main+0x4a>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1206 <main+0x76>
lea    0xe23(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1234 <main+0xa4>
lea    0xdfa(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpl   $0x0,-0x20(%rbp)
jne    12a6 <main+0x116>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jle    1290 <main+0x100>
mov    -0x14(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    125f <main+0xcf>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jle    12e5 <main+0x155>
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    12ae <main+0x11e>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
lea    0xd1f(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11b6 <main+0x26>
lea    0x10(%rsp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11d0 <main+0x40>
lea    0xe62(%rip),%rdi
jmp    11eb <main+0x5b>
mov    0x2e13(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe37(%rip),%rsi
call   1070 <fprintf@plt>
jmp    11f0 <main+0x60>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11fe <main+0x6e>
cmp    $0xffffffff,%eax
jne    126b <main+0xdb>
lea    0xe30(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x8(%rbx),%rdi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%ebp
mov    0x10(%rsp),%edi
call   1040 <close@plt>
lea    0xe(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    1265 <main+0xd5>
lea    0xe(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    0x14(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %rax,%rax
jg     1240 <main+0xb0>
mov    0x14(%rsp),%edi
jmp    12e4 <main+0x154>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    12c6 <main+0x136>
mov    0x2d5d(%rip),%rbp
lea    0xf(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %rax,%rax
jg     12a0 <main+0x110>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
lea    0xd3e(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
mov    0x2cd0(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"pipe\00"
@.str.2 = [5 x i8] c"fork\00"
@O_RDONLY = global i32 0
@EXIT_SUCCESS = global i32 0
@STDOUT_FILENO = global i32 0
@.str.3 = [2 x i8] c"\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 i8
%10 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %21
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %2
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%23 = call i32 @pipe(i32* %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %21
%30 = call i32 (...) @fork()
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %29
%38 = load i32, i32* %8
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %63
40: ; preds = %37
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = load i32, i32* @O_RDONLY
%45 = call i32 @open(i8* %43, i32 %44)
store i32 %45, i32* %7
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
br label %49
49: ; preds = %53, %40
%50 = load i32, i32* %7
%51 = call i64 @read(i32 %50, i8* %10, i32 1)
%52 = icmp sgt i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %49
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%55 = load i32, i32* %54
%56 = call i32 @write(i32 %55, i8* %10, i32 1)
br label %49
57: ; preds = %49
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%59 = load i32, i32* %58
%60 = call i32 @close(i32 %59)
%61 = load i32, i32* @EXIT_SUCCESS
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %37
%64 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%65 = load i32, i32* %64
%66 = call i32 @close(i32 %65)
br label %67
67: ; preds = %72, %63
%68 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%69 = load i32, i32* %68
%70 = call i64 @read(i32 %69, i8* %9, i32 1)
%71 = icmp sgt i64 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %67
%73 = load i32, i32* @STDOUT_FILENO
%74 = call i32 @write(i32 %73, i8* %9, i32 1)
br label %67
75: ; preds = %67
%76 = load i32, i32* @STDOUT_FILENO
%77 = call i32 @write(i32 %76, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%78 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%79 = load i32, i32* %78
%80 = call i32 @close(i32 %79)
%81 = load i32, i32* @EXIT_SUCCESS
%82 = call i32 @exit(i32 %81)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <filename>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"pipe\00"
@.str.2 = [5 x i8] c"fork\00"
@O_RDONLY = global i32 0
@EXIT_SUCCESS = global i32 0
@STDOUT_FILENO = global i32 0
@.str.3 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = alloca i8
%5 = alloca i8
%6 = bitcast [2 x i32]* %3 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %14
%23 = call i32 (...) @fork()
switch i32 %23, label %50 [
i32 -1, label %24
i32 0, label %28
]
24: ; preds = %22
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %22
%29 = getelementptr inbounds i8*, i8** %1, i64 1
%30 = load i8*, i8** %29
%31 = load i32, i32* @O_RDONLY
%32 = call i32 @open(i8* %30, i32 %31)
%33 = load i32, i32* %15
%34 = call i32 @close(i32 %33)
%35 = call i64 @read(i32 %32, i8* %5, i32 1)
%36 = icmp sgt i64 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %28
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
br label %39
39: ; preds = %37, %39
%40 = load i32, i32* %38
%41 = call i32 @write(i32 %40, i8* %5, i32 1)
%42 = call i64 @read(i32 %32, i8* %5, i32 1)
%43 = icmp sgt i64 %42, 0
br i1 %43, label %39, label %44
44: ; preds = %39, %28
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* @EXIT_SUCCESS
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %22
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%52 = load i32, i32* %51
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %15
%55 = call i64 @read(i32 %54, i8* %4, i32 1)
%56 = icmp sgt i64 %55, 0
br i1 %56, label %57, label %63
57: ; preds = %50, %57
%58 = load i32, i32* @STDOUT_FILENO
%59 = call i32 @write(i32 %58, i8* %4, i32 1)
%60 = load i32, i32* %15
%61 = call i64 @read(i32 %60, i8* %4, i32 1)
%62 = icmp sgt i64 %61, 0
br i1 %62, label %57, label %63
63: ; preds = %57, %50
%64 = load i32, i32* @STDOUT_FILENO
%65 = call i32 @write(i32 %64, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
%66 = load i32, i32* %15
%67 = call i32 @close(i32 %66)
%68 = load i32, i32* @EXIT_SUCCESS
%69 = call i32 @exit(i32 %68)
unreachable
}