#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 */

/* Variables and functions */
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execvp(char *, char **);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int main(void) {
  int pipefd[2];
  if (pipe(pipefd) == -1) {
    perror("Couldn't create the pipe");
  }
  int f = fork();
  if (f == -1) {
    perror("Couldn't fork a child process");
  } else if (f == 0) {
    dup2(pipefd[1], STDOUT_FILENO);
    close(pipefd[1]);
    close(pipefd[0]);
    char *cmd[] = {"ls", "-l", (char *)0};
    execvp("ls", cmd);
  } else {
    close(pipefd[1]);
    int len = 5024;
    char str_buff[len];
    bzero(str_buff, len);
    read(pipefd[0], str_buff, len);
    printf("Receiving message from child...\n");
    printf("Message:\n%s", str_buff);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11bd <main+0x2d>
lea    0xe48(%rip),%rdi
call   1080 <perror@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    11e2 <main+0x52>
lea    0xe41(%rip),%rdi
call   1080 <perror@plt>
jmp    12bb <main+0x12b>
cmpl   $0x0,-0x10(%rbp)
jne    1243 <main+0xb3>
mov    -0x8(%rbp),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    0x2bec(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2be9(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2be6(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    -0x30(%rbp),%rsi
lea    0xdfe(%rip),%rdi
call   10a0 <execvp@plt>
jmp    12b6 <main+0x126>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x13a0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x50(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x48(%rbp)
mov    -0x34(%rbp),%esi
call   1090 <bzero@plt>
mov    -0x50(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    -0x34(%rbp),%edx
call   1070 <read@plt>
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x50(%rbp),%rsi
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
jmp    12bb <main+0x12b>
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
je     1278 <main+0xd8>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1226 <main+0x86>
cmp    $0xffffffff,%eax
je     1289 <main+0xe9>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    %rsp,%r14
lea    -0x13a0(%rsp),%rbx
ff
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    $0x13a0,%esi
call   10a0 <bzero@plt>
mov    -0x18(%rbp),%edi
mov    %rbx,%rsi
mov    $0x13a0,%edx
call   1080 <read@plt>
lea    0xe3e(%rip),%rdi
call   1030 <puts@plt>
lea    0xe26(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rsp
jmp    126d <main+0xcd>
mov    -0x14(%rbp),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    0x2bc2(%rip),%rax
mov    %rax,-0x20(%rbp)
movaps 0x2ba7(%rip),%xmm0
movaps %xmm0,-0x30(%rbp)
lea    0xdd3(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   10b0 <execvp@plt>
xor    %eax,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd81(%rip),%rdi
call   1090 <perror@plt>
jmp    11bd <main+0x1d>
lea    0xd89(%rip),%rdi
call   1090 <perror@plt>
jmp    126d <main+0xcd>

LLVM IR (-O0):
@.str = [25 x i8] c"Couldn't create the pipe\00"
@.str.1 = [30 x i8] c"Couldn't fork a child process\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [3 x i8] c"ls\00"
@.str.3 = [3 x i8] c"-l\00"
@__const.main.cmd = [3 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8* null]
@.str.4 = [33 x i8] c"Receiving message from child...\0A\00"
@.str.5 = [12 x i8] c"Message:\0A%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca [3 x i8*]
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store i32 0, i32* %1
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%9 = call i32 @pipe(i32* %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = call i32 (...) @fork()
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %54
19: ; preds = %13
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %36
22: ; preds = %19
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%24 = load i32, i32* %23
%25 = load i32, i32* @STDOUT_FILENO
%26 = call i32 @dup2(i32 %24, i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = bitcast [3 x i8*]* %4 to i8*
%34 = getelementptr inbounds [3 x i8*], [3 x i8*]* %4, i64 0, i64 0
%35 = call i32 @execvp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %34)
br label %53
36: ; preds = %19
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
store i32 5024, i32* %5
%40 = load i32, i32* %5
%41 = zext i32 %40 to i64
%42 = call i8* @llvm.stacksave()
store i8* %42, i8** %6
%43 = alloca i8, i64 %41
store i64 %41, i64* %7
%44 = load i32, i32* %5
%45 = call i32 @bzero(i8* %43, i32 %44)
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%47 = load i32, i32* %46
%48 = load i32, i32* %5
%49 = call i32 @read(i32 %47, i8* %43, i32 %48)
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %43)
%52 = load i8*, i8** %6
br label %53
53: ; preds = %36, %22
br label %54
54: ; preds = %53, %17
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
@.str = [25 x i8] c"Couldn't create the pipe\00"
@.str.1 = [30 x i8] c"Couldn't fork a child process\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [3 x i8] c"ls\00"
@.str.3 = [3 x i8] c"-l\00"
@__const.main.cmd = [3 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i32 0, i32 0), i8* null]
@.str.5 = [12 x i8] c"Message:\0A%s\00"
@str = [32 x i8] c"Receiving message from child...\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [3 x i8*]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %0
%10 = call i32 (...) @fork()
switch i32 %10, label %25 [
i32 -1, label %11
i32 0, label %13
]
11: ; preds = %9
%12 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %37
13: ; preds = %9
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%15 = load i32, i32* %14
%16 = load i32, i32* @STDOUT_FILENO
%17 = call i32 @dup2(i32 %15, i32 %16)
%18 = load i32, i32* %14
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
%22 = bitcast [3 x i8*]* %2 to i8*
%23 = getelementptr inbounds [3 x i8*], [3 x i8*]* %2, i64 0, i64 0
%24 = call i32 @execvp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8** %23)
br label %37
25: ; preds = %9
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = call i8* @llvm.stacksave()
%30 = alloca [5024 x i8]
%31 = getelementptr inbounds [5024 x i8], [5024 x i8]* %30, i64 0, i64 0
%32 = call i32 @bzero(i8* %31, i32 5024)
%33 = load i32, i32* %4
%34 = call i32 @read(i32 %33, i8* %31, i32 5024)
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %31)
br label %37
37: ; preds = %13, %25, %11
ret i32 0
}