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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int /*<<< orphan*/, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execlp(char *, char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ fork();
int getpagesize();
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(int argc, char *argv[]) {
  pid_t pid;
  int rc, filedes[2], BSIZE = getpagesize();
  char *buffer = malloc(BSIZE);
  char *dirname = "/usr/bin";
  if (-1 == pipe(filedes)) {
    perror("pipe");
    exit(errno);
  };
  if (argc > 1)
    dirname = argv[1];
  printf("\nThis is the parent, about to fork\n");
  fflush(stdout);
  pid = fork();
  if (pid > 0) {
    printf("\nThis is the parent, after the fork\n");
    close(filedes[1]);
    printf("\nThe parent just read the following from the pipe:\n");
    while ((rc = read(filedes[0], buffer, BSIZE)) > 0)
      write(STDOUT_FILENO, buffer, rc);
    exit(EXIT_SUCCESS);
  }
  if (pid == 0) {
    printf("\nThis is the child, after the fork\n");
    dup2(STDOUT_FILENO, filedes[1]);
    close(filedes[0]);
    execlp("ls", "ls", "-l", dirname, NULL);
    exit(EXIT_FAILURE);
  }
  {
    perror("fork");
    exit(errno);
  };
}


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)
mov    $0x0,%al
call   10c0 <getpagesize@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x30(%rbp)
lea    0xdfd(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    -0x24(%rbp),%rdi
call   1070 <pipe@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1239 <main+0x69>
lea    0xde3(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpl   $0x1,-0x8(%rbp)
jle    124f <main+0x7f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
lea    0xdb8(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d66(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    12e2 <main+0x112>
lea    0xdac(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
lea    0xdbd(%rip),%rdi
call   1050 <printf@plt>
mov    -0x24(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    12d4 <main+0x104>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
jmp    129f <main+0xcf>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    133f <main+0x16f>
lea    0xd97(%rip),%rdi
call   1050 <printf@plt>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%esi
call   1040 <dup2@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    -0x38(%rbp),%rcx
lea    0xd92(%rip),%rsi
lea    0xd8e(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
mov    %rsi,%rdi
call   10e0 <execlp@plt>
mov    0x2c98(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0xd6f(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2c86(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %eax,%eax
call   10c0 <getpagesize@plt>
mov    %eax,%r12d
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1211 <main+0x41>
lea    0xdfd(%rip),%rdi
jmp    1318 <main+0x148>
cmp    $0x2,%r15d
jl     121d <main+0x4d>
mov    0x8(%r14),%r14
jmp    1224 <main+0x54>
lea    0xddc(%rip),%r14
lea    0xde9(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   10a0 <fflush@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
jle    12be <main+0xee>
lea    0xe09(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
lea    0xe18(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    12b0 <main+0xe0>
mov    0x2d6b(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     1290 <main+0xc0>
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jne    1311 <main+0x141>
lea    0xd70(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
mov    0xc(%rsp),%esi
call   1050 <dup2@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
lea    0xd20(%rip),%rdi
lea    0xd1c(%rip),%rdx
mov    %rdi,%rsi
mov    %r14,%rcx
xor    %r8d,%r8d
call   10e0 <execlp@plt>
mov    0x2cc6(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0xd3d(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cb4(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/usr/bin\00"
@.str.1 = [5 x i8] c"pipe\00"
@errno = global i32 0
@.str.2 = [36 x i8] c"\0AThis is the parent, about to fork\0A\00"
@stdout = global i32 0
@.str.3 = [37 x i8] c"\0AThis is the parent, after the fork\0A\00"
@.str.4 = [52 x i8] c"\0AThe parent just read the following from the pipe:\0A\00"
@STDOUT_FILENO = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.5 = [36 x i8] c"\0AThis is the child, after the fork\0A\00"
@.str.6 = [3 x i8] c"ls\00"
@.str.7 = [3 x i8] c"-l\00"
@EXIT_FAILURE = global i32 0
@.str.8 = [5 x i8] c"fork\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i32
%8 = alloca [2 x i32]
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 (...) @getpagesize()
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %10
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %11
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%16 = call i32 @pipe(i32* %15)
%17 = icmp eq i32 -1, %16
br i1 %17, label %18, label %22
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @errno
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
%23 = load i32, i32* %4
%24 = icmp sgt i32 %23, 1
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
store i8* %28, i8** %11
br label %29
29: ; preds = %25, %22
%30 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @stdout
%32 = call i32 @fflush(i32 %31)
%33 = call i64 (...) @fork()
store i64 %33, i64* %6
%34 = load i64, i64* %6
%35 = icmp sgt i64 %34, 0
br i1 %35, label %36, label %57
36: ; preds = %29
%37 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @close(i32 %39)
%41 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %49, %36
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%44 = load i32, i32* %43
%45 = load i8*, i8** %10
%46 = load i32, i32* %9
%47 = call i32 @read(i32 %44, i8* %45, i32 %46)
store i32 %47, i32* %7
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %42
%50 = load i32, i32* @STDOUT_FILENO
%51 = load i8*, i8** %10
%52 = load i32, i32* %7
%53 = call i32 @write(i32 %50, i8* %51, i32 %52)
br label %42
54: ; preds = %42
%55 = load i32, i32* @EXIT_SUCCESS
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %29
%58 = load i64, i64* %6
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %73
60: ; preds = %57
%61 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
%62 = load i32, i32* @STDOUT_FILENO
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 1
%64 = load i32, i32* %63
%65 = call i32 @dup2(i32 %62, i32 %64)
%66 = getelementptr inbounds [2 x i32], [2 x i32]* %8, i64 0, i64 0
%67 = load i32, i32* %66
%68 = call i32 @close(i32 %67)
%69 = load i8*, i8** %11
%70 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %69, i32* null)
%71 = load i32, i32* @EXIT_FAILURE
%72 = call i32 @exit(i32 %71)
unreachable
73: ; preds = %57
%74 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%75 = load i32, i32* @errno
%76 = call i32 @exit(i32 %75)
unreachable
}

LLVM IR (-O2):
@.str = [9 x i8] c"/usr/bin\00"
@.str.1 = [5 x i8] c"pipe\00"
@errno = global i32 0
@stdout = global i32 0
@STDOUT_FILENO = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.6 = [3 x i8] c"ls\00"
@.str.7 = [3 x i8] c"-l\00"
@EXIT_FAILURE = global i32 0
@.str.8 = [5 x i8] c"fork\00"
@str = [35 x i8] c"\0AThis is the parent, about to fork\00"
@str.9 = [35 x i8] c"\0AThis is the child, after the fork\00"
@str.10 = [36 x i8] c"\0AThis is the parent, after the fork\00"
@str.11 = [51 x i8] c"\0AThe parent just read the following from the pipe:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = tail call i32 (...) @getpagesize()
%6 = tail call i8* @malloc(i32 %5)
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @errno
%13 = call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = icmp sgt i32 %0, 1
br i1 %15, label %16, label %19
16: ; preds = %14
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
br label %19
19: ; preds = %16, %14
%20 = phi i8* [ %18, %16 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %14 ]
%21 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%22 = load i32, i32* @stdout
%23 = call i32 @fflush(i32 %22)
%24 = call i64 (...) @fork()
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %45
26: ; preds = %19
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.10, i64 0, i64 0))
%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 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.11, i64 0, i64 0))
%32 = load i32, i32* %7
%33 = call i32 @read(i32 %32, i8* %6, i32 %5)
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %26, %35
%36 = phi i32 [ %40, %35 ], [ %33, %26 ]
%37 = load i32, i32* @STDOUT_FILENO
%38 = call i32 @write(i32 %37, i8* %6, i32 %36)
%39 = load i32, i32* %7
%40 = call i32 @read(i32 %39, i8* %6, i32 %5)
%41 = icmp sgt i32 %40, 0
br i1 %41, label %35, label %42
42: ; preds = %35, %26
%43 = load i32, i32* @EXIT_SUCCESS
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %19
%46 = icmp eq i64 %24, 0
br i1 %46, label %47, label %58
47: ; preds = %45
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.9, i64 0, i64 0))
%49 = load i32, i32* @STDOUT_FILENO
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @dup2(i32 %49, i32 %51)
%53 = load i32, i32* %7
%54 = call i32 @close(i32 %53)
%55 = call i32 @execlp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* %20, i32* null)
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %45
%59 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i64 0, i64 0))
%60 = load i32, i32* @errno
%61 = call i32 @exit(i32 %60)
unreachable
}