#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ fork();
int fread(char *, int, int, int /*<<< orphan*/ *);
int getpagesize();
char *malloc(int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ *popen(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  pid_t pid;
  char *dirname = "/usr/bin";
  int rc, filedes[2], BSIZE = getpagesize();
  char *buffer = malloc(BSIZE);
  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) {
    FILE *cmd_fd;
    char cmd[1024] = "ls -l ";
    printf("\nThis is the child, after the fork\n");
    close(filedes[0]);
    cmd_fd = popen(strcat(cmd, dirname), "r");
    while ((rc = fread(buffer, 1, BSIZE, cmd_fd)) > 0)
      write(filedes[1], buffer, rc);
    pclose(cmd_fd);
    exit(EXIT_SUCCESS);
  }
  {
    perror("fork");
    exit(errno);
  };
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xde0(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1100 <getpagesize@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
call   10b0 <malloc@plt>
mov    %rax,-0x38(%rbp)
lea    -0x2c(%rbp),%rdi
call   1090 <pipe@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    126c <main+0x6c>
lea    0xdb0(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
cmpl   $0x1,-0x8(%rbp)
jle    1282 <main+0x82>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
lea    0xd85(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
mov    $0x0,%al
call   1120 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    1315 <main+0x115>
lea    0xd79(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%edi
call   1080 <close@plt>
lea    0xd8a(%rip),%rdi
call   1050 <printf@plt>
mov    -0x2c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   10a0 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    1307 <main+0x107>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
mov    -0x38(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <write@plt>
jmp    12d2 <main+0xd2>
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    13df <main+0x1df>
lea    -0x440(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1070 <memset@plt>
movb   $0x6c,-0x440(%rbp)
movb   $0x73,-0x43f(%rbp)
movb   $0x20,-0x43e(%rbp)
movb   $0x2d,-0x43d(%rbp)
movb   $0x6c,-0x43c(%rbp)
movb   $0x20,-0x43b(%rbp)
lea    0xd27(%rip),%rdi
call   1050 <printf@plt>
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
lea    -0x440(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10f0 <strcat@plt>
mov    %eax,%edi
lea    0xd25(%rip),%rsi
call   10d0 <popen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    13c8 <main+0x1c8>
mov    -0x28(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <write@plt>
jmp    1393 <main+0x193>
mov    -0x40(%rbp),%rdi
call   1060 <pclose@plt>
mov    0x2bf0(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
lea    0xccb(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2be6(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   1100 <getpagesize@plt>
mov    %eax,%r15d
mov    %eax,%edi
call   10b0 <malloc@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
call   1090 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1241 <main+0x41>
lea    0xdcd(%rip),%rdi
jmp    137e <main+0x17e>
cmp    $0x2,%ebp
jl     124c <main+0x4c>
mov    0x8(%r14),%r14
jmp    1253 <main+0x53>
lea    0xdad(%rip),%r14
lea    0xdb6(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
call   10c0 <fflush@plt>
xor    %eax,%eax
call   1120 <fork@plt>
test   %rax,%rax
jle    12e5 <main+0xe5>
lea    0xdd6(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   1080 <close@plt>
lea    0xde5(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   10a0 <read@plt>
test   %eax,%eax
jle    1399 <main+0x199>
mov    0x2d38(%rip),%rbp
nopl   0x0(%rax,%rax,1)
mov    0x0(%rbp),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   10a0 <read@plt>
test   %eax,%eax
jg     12c0 <main+0xc0>
jmp    1399 <main+0x199>
jne    1377 <main+0x177>
lea    0x10(%rsp),%rbp
mov    $0x400,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
movl   $0x2d20736c,0x10(%rsp)
2d
movw   $0x206c,0x14(%rsp)
lea    0xd1e(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
call   10f0 <strcat@plt>
lea    0xcd9(%rip),%rsi
mov    %eax,%edi
call   10d0 <popen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rax,%rcx
nopl   (%rax)
call   1040 <fread@plt>
test   %eax,%eax
jle    1391 <main+0x191>
mov    0xc(%rsp),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %rbp,%rcx
jmp    1350 <main+0x150>
lea    0xcd3(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2c4e(%rip),%rax
mov    (%rax),%edi
call   1110 <exit@plt>
mov    %rbp,%rdi
call   1060 <pclose@plt>
mov    0x2c28(%rip),%rax
mov    (%rax),%edi
call   1110 <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 = [2 x i8] c"r\00"
@.str.7 = [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 i8*
%8 = alloca i32
%9 = alloca [2 x i32]
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32*
%13 = alloca [1024 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %7
%14 = call i32 (...) @getpagesize()
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %11
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%18 = call i32 @pipe(i32* %17)
%19 = icmp eq i32 -1, %18
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @errno
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
%25 = load i32, i32* %4
%26 = icmp sgt i32 %25, 1
br i1 %26, label %27, label %31
27: ; preds = %24
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
store i8* %30, i8** %7
br label %31
31: ; preds = %27, %24
%32 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @stdout
%34 = call i32 @fflush(i32 %33)
%35 = call i64 (...) @fork()
store i64 %35, i64* %6
%36 = load i64, i64* %6
%37 = icmp sgt i64 %36, 0
br i1 %37, label %38, label %59
38: ; preds = %31
%39 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0))
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%41 = load i32, i32* %40
%42 = call i32 @close(i32 %41)
%43 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %51, %38
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%46 = load i32, i32* %45
%47 = load i8*, i8** %11
%48 = load i32, i32* %10
%49 = call i32 @read(i32 %46, i8* %47, i32 %48)
store i32 %49, i32* %8
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %44
%52 = load i32, i32* @STDOUT_FILENO
%53 = load i8*, i8** %11
%54 = load i32, i32* %8
%55 = call i32 @write(i32 %52, i8* %53, i32 %54)
br label %44
56: ; preds = %44
%57 = load i32, i32* @EXIT_SUCCESS
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %31
%60 = load i64, i64* %6
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %96
62: ; preds = %59
%63 = bitcast [1024 x i8]* %13 to i8*
%64 = bitcast i8* %63 to [1024 x i8]*
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %64, i32 0, i32 0
store i8 108, i8* %65
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %64, i32 0, i32 1
store i8 115, i8* %66
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %64, i32 0, i32 2
store i8 32, i8* %67
%68 = getelementptr inbounds [1024 x i8], [1024 x i8]* %64, i32 0, i32 3
store i8 45, i8* %68
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %64, i32 0, i32 4
store i8 108, i8* %69
%70 = getelementptr inbounds [1024 x i8], [1024 x i8]* %64, i32 0, i32 5
store i8 32, i8* %70
%71 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
%72 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%73 = load i32, i32* %72
%74 = call i32 @close(i32 %73)
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%76 = load i8*, i8** %7
%77 = call i32 @strcat(i8* %75, i8* %76)
%78 = call i32* @popen(i32 %77, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %78, i32** %12
br label %79
79: ; preds = %85, %62
%80 = load i8*, i8** %11
%81 = load i32, i32* %10
%82 = load i32*, i32** %12
%83 = call i32 @fread(i8* %80, i32 1, i32 %81, i32* %82)
store i32 %83, i32* %8
%84 = icmp sgt i32 %83, 0
br i1 %84, label %85, label %91
85: ; preds = %79
%86 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%87 = load i32, i32* %86
%88 = load i8*, i8** %11
%89 = load i32, i32* %8
%90 = call i32 @write(i32 %87, i8* %88, i32 %89)
br label %79
91: ; preds = %79
%92 = load i32*, i32** %12
%93 = call i32 @pclose(i32* %92)
%94 = load i32, i32* @EXIT_SUCCESS
%95 = call i32 @exit(i32 %94)
unreachable
96: ; preds = %59
%97 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%98 = load i32, i32* @errno
%99 = call i32 @exit(i32 %98)
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 = [2 x i8] c"r\00"
@.str.7 = [5 x i8] c"fork\00"
@str = [35 x i8] c"\0AThis is the parent, about to fork\00"
@str.8 = [35 x i8] c"\0AThis is the child, after the fork\00"
@str.9 = [36 x i8] c"\0AThis is the parent, after the fork\00"
@str.10 = [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 = alloca [1024 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = tail call i32 (...) @getpagesize()
%7 = tail call i8* @malloc(i32 %6)
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @errno
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = icmp sgt i32 %0, 1
br i1 %16, label %17, label %20
17: ; preds = %15
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
br label %20
20: ; preds = %17, %15
%21 = phi i8* [ %19, %17 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %15 ]
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%23 = load i32, i32* @stdout
%24 = call i32 @fflush(i32 %23)
%25 = call i64 (...) @fork()
%26 = icmp sgt i64 %25, 0
br i1 %26, label %27, label %46
27: ; preds = %20
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.9, i64 0, i64 0))
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.10, i64 0, i64 0))
%33 = load i32, i32* %8
%34 = call i32 @read(i32 %33, i8* %7, i32 %6)
%35 = icmp sgt i32 %34, 0
br i1 %35, label %36, label %43
36: ; preds = %27, %36
%37 = phi i32 [ %41, %36 ], [ %34, %27 ]
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i32 @write(i32 %38, i8* %7, i32 %37)
%40 = load i32, i32* %8
%41 = call i32 @read(i32 %40, i8* %7, i32 %6)
%42 = icmp sgt i32 %41, 0
br i1 %42, label %36, label %43
43: ; preds = %36, %27
%44 = load i32, i32* @EXIT_SUCCESS
%45 = call i32 @exit(i32 %44)
unreachable
46: ; preds = %20
%47 = icmp eq i64 %25, 0
br i1 %47, label %48, label %74
48: ; preds = %46
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
store i8 108, i8* %49
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 1
store i8 115, i8* %50
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 2
store i8 32, i8* %51
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 3
store i8 45, i8* %52
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 4
store i8 108, i8* %53
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 5
store i8 32, i8* %54
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str.8, i64 0, i64 0))
%56 = load i32, i32* %8
%57 = call i32 @close(i32 %56)
%58 = call i32 @strcat(i8* %49, i8* %21)
%59 = call i32* @popen(i32 %58, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%60 = call i32 @fread(i8* %7, i32 1, i32 %6, i32* %59)
%61 = icmp sgt i32 %60, 0
br i1 %61, label %62, label %70
62: ; preds = %48
%63 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
br label %64
64: ; preds = %62, %64
%65 = phi i32 [ %60, %62 ], [ %68, %64 ]
%66 = load i32, i32* %63
%67 = call i32 @write(i32 %66, i8* %7, i32 %65)
%68 = call i32 @fread(i8* %7, i32 1, i32 %6, i32* %59)
%69 = icmp sgt i32 %68, 0
br i1 %69, label %64, label %70
70: ; preds = %64, %48
%71 = call i32 @pclose(i32* %59)
%72 = load i32, i32* @EXIT_SUCCESS
%73 = call i32 @exit(i32 %72)
unreachable
74: ; preds = %46
%75 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%76 = load i32, i32* @errno
%77 = call i32 @exit(i32 %76)
unreachable
}