#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 getpid();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
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);
  }
  pid_t ppid = getpid();
  printf("\n01 Main starting Parent pid %d", ppid);
  printf("\n");
  if (pipe(pipefd) == -1) {
    perror("pipe");
    exit(EXIT_FAILURE);
  }
  cpid = fork();
  pid_t currentpid = getpid();
  printf("\nParent pid %d, child pid %d, current pid %d", ppid, cpid,
         currentpid);
  printf("\n");
  if (cpid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if (cpid == 0) {
    printf("\nInside child %d, %d", cpid, currentpid);
    printf("\n");
    close(pipefd[1]);
    while (read(pipefd[0], &buf, 1) > 0)
      write(STDOUT_FILENO, &buf, 1);
    write(STDOUT_FILENO, "\n", 1);
    printf("\n");
    close(pipefd[0]);
    _exit(EXIT_SUCCESS);
  } else {
    printf("\nInside parent %d, %d, %d", ppid, cpid, currentpid);
    printf("\n");
    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    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     121a <main+0x4a>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdf9(%rip),%rsi
call   10b0 <fprintf@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xde6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x18(%rbp),%rdi
call   1090 <pipe@plt>
cmp    $0xffffffff,%eax
jne    126f <main+0x9f>
lea    0xdd8(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xda6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xd71(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12cc <main+0xfc>
lea    0xdad(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpl   $0x0,-0x1c(%rbp)
jne    1375 <main+0x1a5>
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xd21(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   10a0 <read@plt>
cmp    $0x0,%rax
jle    1337 <main+0x167>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
jmp    1300 <main+0x130>
mov    0x2cb2(%rip),%rax
mov    (%rax),%edi
lea    0xccb(%rip),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
lea    0xcba(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    0x2c5a(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x24(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x28(%rbp),%ecx
lea    0xcfb(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xc7f(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10e0 <wait@plt>
mov    0x2be1(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
cmp    $0x2,%edi
jne    122d <main+0x4d>
xor    %eax,%eax
call   1060 <getpid@plt>
mov    %eax,%ebp
lea    0xe12(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0x10(%rsp),%rdi
call   10a0 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1247 <main+0x67>
lea    0xe09(%rip),%rdi
jmp    128c <main+0xac>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%rdx
lea    0xdc0(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    1291 <main+0xb1>
xor    %eax,%eax
call   1100 <fork@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <getpid@plt>
mov    %eax,%r14d
lea    0xdd8(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
test   %ebx,%ebx
je     129f <main+0xbf>
cmp    $0xffffffff,%ebx
jne    1351 <main+0x171>
lea    0xdda(%rip),%rdi
call   10d0 <perror@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
lea    0xdc5(%rip),%rdi
xor    %esi,%esi
mov    %r14d,%edx
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x14(%rsp),%edi
call   1090 <close@plt>
mov    0x10(%rsp),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   10b0 <read@plt>
test   %rax,%rax
jle    1316 <main+0x136>
mov    0x2d0c(%rip),%rbp
lea    0xf(%rsp),%rbx
nopl   0x0(%rax)
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   10b0 <read@plt>
test   %rax,%rax
jg     12f0 <main+0x110>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
lea    0xcec(%rip),%rsi
mov    $0x1,%edx
call   1050 <write@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x10(%rsp),%edi
call   1090 <close@plt>
mov    0x2c7e(%rip),%rax
mov    (%rax),%edi
call   1040 <_exit@plt>
lea    0xd28(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x10(%rsp),%edi
call   1090 <close@plt>
mov    0x14(%rsp),%ebx
mov    0x8(%r15),%rbp
mov    %rbp,%rdi
call   1070 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    0x14(%rsp),%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10f0 <wait@plt>
mov    0x2c1c(%rip),%rax
mov    (%rax),%edi
call   10e0 <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 = [32 x i8] c"\0A01 Main starting Parent pid %d\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [5 x i8] c"pipe\00"
@.str.4 = [45 x i8] c"\0AParent pid %d, child pid %d, current pid %d\00"
@.str.5 = [5 x i8] c"fork\00"
@.str.6 = [21 x i8] c"\0AInside child %d, %d\00"
@STDOUT_FILENO = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.7 = [26 x i8] c"\0AInside parent %d, %d, %d\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 i8
%9 = alloca i32
%10 = alloca i32
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 ([20 x i8], [20 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 = call i32 (...) @getpid()
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%27 = call i32 @pipe(i32* %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %21
%34 = call i32 (...) @fork()
store i32 %34, i32* %7
%35 = call i32 (...) @getpid()
store i32 %35, i32* %10
%36 = load i32, i32* %9
%37 = load i32, i32* %7
%38 = load i32, i32* %10
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %36, i32 %37, i32 %38)
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%41 = load i32, i32* %7
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %47
43: ; preds = %33
%44 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %33
%48 = load i32, i32* %7
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %75
50: ; preds = %47
%51 = load i32, i32* %7
%52 = load i32, i32* %10
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %51, i32 %52)
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%55 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%56 = load i32, i32* %55
%57 = call i32 @close(i32 %56)
br label %58
58: ; preds = %63, %50
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%60 = load i32, i32* %59
%61 = call i64 @read(i32 %60, i8* %8, i32 1)
%62 = icmp sgt i64 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %58
%64 = load i32, i32* @STDOUT_FILENO
%65 = call i32 @write(i32 %64, i8* %8, i32 1)
br label %58
66: ; preds = %58
%67 = load i32, i32* @STDOUT_FILENO
%68 = call i32 @write(i32 %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%71 = load i32, i32* %70
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* @EXIT_SUCCESS
%74 = call i32 @_exit(i32 %73)
unreachable
75: ; preds = %47
%76 = load i32, i32* %9
%77 = load i32, i32* %7
%78 = load i32, i32* %10
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i32 %76, i32 %77, i32 %78)
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%81 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%82 = load i32, i32* %81
%83 = call i32 @close(i32 %82)
%84 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%85 = load i32, i32* %84
%86 = load i8**, i8*** %5
%87 = getelementptr inbounds i8*, i8** %86, i64 1
%88 = load i8*, i8** %87
%89 = load i8**, i8*** %5
%90 = getelementptr inbounds i8*, i8** %89, i64 1
%91 = load i8*, i8** %90
%92 = call i32 @strlen(i8* %91)
%93 = call i32 @write(i32 %85, i8* %88, i32 %92)
%94 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%95 = load i32, i32* %94
%96 = call i32 @close(i32 %95)
%97 = call i32 @wait(i32* null)
%98 = load i32, i32* @EXIT_SUCCESS
%99 = call i32 @exit(i32 %98)
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 = [32 x i8] c"\0A01 Main starting Parent pid %d\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [5 x i8] c"pipe\00"
@.str.4 = [45 x i8] c"\0AParent pid %d, child pid %d, current pid %d\00"
@.str.5 = [5 x i8] c"fork\00"
@.str.6 = [21 x i8] c"\0AInside child %d, %d\00"
@STDOUT_FILENO = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.7 = [26 x i8] c"\0AInside parent %d, %d, %d\00"
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 = tail call i32 (...) @getpid()
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = tail call i32 @putchar(i32 10)
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%18 = call i32 @pipe(i32* %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %13
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %13
%25 = call i32 (...) @fork()
%26 = call i32 (...) @getpid()
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %14, i32 %25, i32 %26)
%28 = call i32 @putchar(i32 10)
switch i32 %25, label %56 [
i32 -1, label %29
i32 0, label %33
]
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 0, i32 %26)
%35 = call i32 @putchar(i32 10)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %17
%40 = call i64 @read(i32 %39, i8* %4, i32 1)
%41 = icmp sgt i64 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %33, %42
%43 = load i32, i32* @STDOUT_FILENO
%44 = call i32 @write(i32 %43, i8* %4, i32 1)
%45 = load i32, i32* %17
%46 = call i64 @read(i32 %45, i8* %4, i32 1)
%47 = icmp sgt i64 %46, 0
br i1 %47, label %42, label %48
48: ; preds = %42, %33
%49 = load i32, i32* @STDOUT_FILENO
%50 = call i32 @write(i32 %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1)
%51 = call i32 @putchar(i32 10)
%52 = load i32, i32* %17
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* @EXIT_SUCCESS
%55 = call i32 @_exit(i32 %54)
unreachable
56: ; preds = %24
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i32 %14, i32 %25, i32 %26)
%58 = call i32 @putchar(i32 10)
%59 = load i32, i32* %17
%60 = call i32 @close(i32 %59)
%61 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%62 = load i32, i32* %61
%63 = getelementptr inbounds i8*, i8** %1, i64 1
%64 = load i8*, i8** %63
%65 = call i32 @strlen(i8* %64)
%66 = call i32 @write(i32 %62, i8* %64, i32 %65)
%67 = load i32, i32* %61
%68 = call i32 @close(i32 %67)
%69 = call i32 @wait(i32* null)
%70 = load i32, i32* @EXIT_SUCCESS
%71 = call i32 @exit(i32 %70)
unreachable
}