#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 STDOUT_FILENO;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int pipefd[2];
  pid_t cpid;
  char buf;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <string>\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) {
    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);
  } else {
    close(pipefd[0]);
    write(pipefd[1], argv[1], strlen(argv[1]));
    close(pipefd[1]);
    wait(NULL);
    exit(EXIT_SUCCESS);
  }
}


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     11fa <main+0x4a>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe19(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1226 <main+0x76>
lea    0xe01(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2db1(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1254 <main+0xa4>
lea    0xdd8(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    12cd <main+0x11d>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
cmp    $0x0,%rax
jle    129d <main+0xed>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    1266 <main+0xb6>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xd65(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <wait@plt>
mov    0x2cae(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11d6 <main+0x26>
lea    0x10(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11f0 <main+0x40>
lea    0xe40(%rip),%rdi
jmp    120b <main+0x5b>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe17(%rip),%rsi
call   1090 <fprintf@plt>
jmp    1210 <main+0x60>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
je     121e <main+0x6e>
cmp    $0xffffffff,%eax
jne    12a7 <main+0xf7>
lea    0xe0e(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x14(%rsp),%edi
call   1060 <close@plt>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %rax,%rax
jle    1276 <main+0xc6>
mov    0x2daa(%rip),%rbp
lea    0xf(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1080 <read@plt>
test   %rax,%rax
jg     1250 <main+0xa0>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
lea    0xd8c(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    0x2d28(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    0x10(%rsp),%edi
call   1060 <close@plt>
mov    0x14(%rsp),%ebp
mov    0x8(%rbx),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x14(%rsp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10c0 <wait@plt>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

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

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