#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 STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ sleep(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]);
  }
  if (pipe(pipefd) == -1) {
    perror("pipe");
  }
  cpid = fork();
  if (cpid == -1) {
    perror("Fork");
  }
  if (cpid == 0) {
    close(pipefd[1]);
    while (read(pipefd[0], &buf, 1) > 0)
      write(STDOUT_FILENO, &buf, 1);
    close(pipefd[0]);
  } else {
    close(pipefd[0]);
    write(pipefd[1], argv[1], strlen(argv[1]));
    sleep(1);
    close(pipefd[1]);
    wait(NULL);
  }
}


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     11dc <main+0x3c>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe29(%rip),%rsi
call   1080 <fprintf@plt>
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11fa <main+0x5a>
lea    0xe1e(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    121a <main+0x7a>
lea    0xe03(%rip),%rdi
call   1090 <perror@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    1270 <main+0xd0>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x0,%rax
jle    1263 <main+0xc3>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    122c <main+0x8c>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
jmp    12c0 <main+0x120>
mov    -0x18(%rbp),%edi
call   1050 <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   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     11c6 <main+0x26>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe3f(%rip),%rsi
call   1080 <fprintf@plt>
lea    0x10(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     1298 <main+0xf8>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     122e <main+0x8e>
cmp    $0xffffffff,%eax
je     12a9 <main+0x109>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
mov    0x14(%rsp),%ebp
mov    0x8(%rbx),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    0x14(%rsp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10b0 <wait@plt>
jmp    128f <main+0xef>
mov    0x14(%rsp),%edi
call   1050 <close@plt>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
jle    1286 <main+0xe6>
mov    0x2d9a(%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   1070 <read@plt>
test   %rax,%rax
jg     1260 <main+0xc0>
mov    0x10(%rsp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd74(%rip),%rdi
call   1090 <perror@plt>
jmp    11d9 <main+0x39>
lea    0xd68(%rip),%rdi
call   1090 <perror@plt>
jmp    11ed <main+0x4d>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"Usage:%s <string>\0A\00"
@.str.1 = [5 x i8] c"pipe\00"
@.str.2 = [5 x i8] c"Fork\00"
@STDOUT_FILENO = 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 %11, 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 ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %11
11: ; preds = %7, %2
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%13 = call i32 @pipe(i32* %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = call i32 (...) @fork()
switch i32 %18, label %37 [
i32 -1, label %19
i32 0, label %21
]
19: ; preds = %17
%20 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %37
21: ; preds = %17
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* %12
%26 = call i64 @read(i32 %25, i8* %4, i32 1)
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %34
28: ; preds = %21, %28
%29 = load i32, i32* @STDOUT_FILENO
%30 = call i32 @write(i32 %29, i8* %4, i32 1)
%31 = load i32, i32* %12
%32 = call i64 @read(i32 %31, i8* %4, i32 1)
%33 = icmp sgt i64 %32, 0
br i1 %33, label %28, label %34
34: ; preds = %28, %21
%35 = load i32, i32* %12
%36 = call i32 @close(i32 %35)
br label %50
37: ; preds = %19, %17
%38 = load i32, i32* %12
%39 = call i32 @close(i32 %38)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%41 = load i32, i32* %40
%42 = getelementptr inbounds i8*, i8** %1, i64 1
%43 = load i8*, i8** %42
%44 = call i32 @strlen(i8* %43)
%45 = call i32 @write(i32 %41, i8* %43, i32 %44)
%46 = call i32 @sleep(i32 1)
%47 = load i32, i32* %40
%48 = call i32 @close(i32 %47)
%49 = call i32 @wait(i32* null)
br label %50
50: ; preds = %37, %34
ret i32 0
}