#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*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int strlen(char *);
int /*<<< orphan*/ write(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 child process");
  } else if (f == 0) {
    close(pipefd[0]);
    char str[] = "Hello, this is world!\n";
    int len = strlen(str) + 1;
    printf("Sending message to parent...\n");
    write(pipefd[1], str, len);
  } else {
    close(pipefd[1]);
    int len = 80;
    char str_buff[len];
    bzero(str_buff, len);
    read(pipefd[0], str_buff, len);
    printf("Receiving message from child...\n");
    printf("Message: %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   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11bd <main+0x2d>
lea    0xe48(%rip),%rdi
call   1090 <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   1090 <perror@plt>
jmp    12d0 <main+0x140>
cmpl   $0x0,-0x10(%rbp)
jne    1258 <main+0xc8>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    0xe85(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe82(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe80(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    0xe7a(%rip),%ax
mov    %ax,-0x1c(%rbp)
mov    0xe72(%rip),%al
mov    %al,-0x1a(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edx
call   1030 <write@plt>
jmp    12cb <main+0x13b>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x50,-0x38(%rbp)
mov    -0x38(%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    -0x38(%rbp),%esi
call   10a0 <bzero@plt>
mov    -0x50(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    -0x38(%rbp),%edx
call   1080 <read@plt>
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x50(%rbp),%rsi
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x40(%rbp),%rax
mov    %rax,%rsp
jmp    12d0 <main+0x140>
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   1080 <pipe@plt>
cmp    $0xffffffff,%eax
je     1277 <main+0xd7>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1223 <main+0x83>
cmp    $0xffffffff,%eax
je     1288 <main+0xe8>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    %rsp,%r14
lea    -0x50(%rsp),%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    $0x50,%esi
call   10b0 <bzero@plt>
mov    -0x18(%rbp),%edi
mov    %rbx,%rsi
mov    $0x50,%edx
call   1090 <read@plt>
lea    0xe39(%rip),%rdi
call   1030 <puts@plt>
lea    0xe21(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rsp
jmp    126c <main+0xcc>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
movaps 0xe4e(%rip),%xmm0
movaps %xmm0,-0x30(%rbp)
movabs $0xa21646c726f77,%rax
mov    %rax,-0x21(%rbp)
lea    -0x30(%rbp),%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%ebx
lea    0xe07(%rip),%rdi
call   1030 <puts@plt>
mov    -0x14(%rbp),%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
xor    %eax,%eax
lea    -0x10(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd82(%rip),%rdi
call   10a0 <perror@plt>
jmp    11bd <main+0x1d>
lea    0xd8a(%rip),%rdi
call   10a0 <perror@plt>
jmp    126c <main+0xcc>

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

LLVM IR (-O2):
@.str = [25 x i8] c"Couldn't create the pipe\00"
@.str.1 = [28 x i8] c"Couldn't fork child process\00"
@__const.main.str = [23 x i8] c"Hello, this is world!\0A\00"
@.str.4 = [12 x i8] c"Message: %s\00"
@str = [32 x i8] c"Receiving message from child...\00"
@str.5 = [29 x i8] c"Sending message to parent...\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [23 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 %23 [
i32 -1, label %11
i32 0, label %13
]
11: ; preds = %9
%12 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %35
13: ; preds = %9
%14 = load i32, i32* %4
%15 = call i32 @close(i32 %14)
%16 = getelementptr inbounds [23 x i8], [23 x i8]* %2, i64 0, i64 0
%17 = call i32 @strlen(i8* %16)
%18 = add nsw i32 %17, 1
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.5, i64 0, i64 0))
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @write(i32 %21, i8* %16, i32 %18)
br label %35
23: ; preds = %9
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = call i8* @llvm.stacksave()
%28 = alloca [80 x i8]
%29 = getelementptr inbounds [80 x i8], [80 x i8]* %28, i64 0, i64 0
%30 = call i32 @bzero(i8* %29, i32 80)
%31 = load i32, i32* %4
%32 = call i32 @read(i32 %31, i8* %29, i32 80)
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %29)
br label %35
35: ; preds = %13, %23, %11
ret i32 0
}