#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*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd[2], pid;
  char msgout[20] = "Hello world\n";
  char msgin[20];
  if (pipe(fd) == -1) {
    perror("pipe()");
    exit(1);
  }
  if ((pid = fork()) > 0) {
    close(fd[0]);
    write(fd[1], msgout, 20);
  } else if (pid == 0) {
    close(fd[1]);
    read(fd[0], msgin, 20);
    puts(msgin);
  } else {
    perror("fork()");
    exit(2);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe67(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe65(%rip),%eax
mov    %eax,-0x20(%rbp)
lea    -0xc(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11d6 <main+0x56>
lea    0xe4d(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jle    1207 <main+0x87>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x14,%edx
call   1040 <write@plt>
jmp    1253 <main+0xd3>
cmpl   $0x0,-0x10(%rbp)
jne    1238 <main+0xb8>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x14,%edx
call   1070 <read@plt>
lea    -0x50(%rbp),%rdi
call   1030 <puts@plt>
jmp    124e <main+0xce>
lea    0xddc(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
jmp    1253 <main+0xd3>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x50,%rsp
movaps 0xe74(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x20(%rsp)
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
cmp    $0xffffffff,%eax
je     1202 <main+0x82>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jle    11d1 <main+0x51>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x14,%edx
call   1040 <write@plt>
jmp    11fa <main+0x7a>
jne    1218 <main+0x98>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
lea    0x30(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x14,%edx
call   1070 <read@plt>
mov    %rbx,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbx
ret
lea    0xe0b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdfc(%rip),%rdi
call   1080 <perror@plt>
mov    $0x2,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@__const.main.msgout = [20 x i8] c"Hello world\0A\00\00\00\00\00\00\00\00"
@.str = [7 x i8] c"pipe()\00"
@.str.1 = [7 x i8] c"fork()\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca [20 x i8]
%5 = alloca [20 x i8]
store i32 0, i32* %1
%6 = bitcast [20 x i8]* %4 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i32 (...) @fork()
store i32 %14, i32* %3
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %13
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%18 = load i32, i32* %17
%19 = call i32 @close(i32 %18)
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%21 = load i32, i32* %20
%22 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%23 = call i32 @write(i32 %21, i8* %22, i32 20)
br label %41
24: ; preds = %13
%25 = load i32, i32* %3
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %37
27: ; preds = %24
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%32 = load i32, i32* %31
%33 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%34 = call i32 @read(i32 %32, i8* %33, i32 20)
%35 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
%36 = call i32 @puts(i8* %35)
br label %40
37: ; preds = %24
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @exit(i32 2)
unreachable
40: ; preds = %27
br label %41
41: ; preds = %40, %16
%42 = load i32, i32* %1
ret i32 %42
}

LLVM IR (-O2):
@__const.main.msgout = [20 x i8] c"Hello world\0A\00\00\00\00\00\00\00\00"
@.str = [7 x i8] c"pipe()\00"
@.str.1 = [7 x i8] c"fork()\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [20 x i8]
%3 = alloca [20 x i8]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i32 (...) @fork()
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = load i32, i32* %7
%18 = call i32 @close(i32 %17)
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @write(i32 %20, i8* %5, i32 20)
br label %34
22: ; preds = %13
%23 = icmp eq i32 %14, 0
br i1 %23, label %24, label %31
24: ; preds = %22
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %7
%29 = call i32 @read(i32 %28, i8* %6, i32 20)
%30 = call i32 @puts(i8* %6)
br label %34
31: ; preds = %22
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 2)
unreachable
34: ; preds = %24, %16
ret i32 0
}