#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*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int fd[2];
  pid_t pid;
  pipe(fd);
  pid = fork();
  if (pid == 0) {
    char buf[] = "hello parent!";
    close(fd[0]);
    sleep(1);
    write(fd[1], buf, sizeof buf);
    exit(0);
  } else if (pid > 0) {
    char buf[32];
    close(fd[1]);
    read(fd[0], buf, 32);
    printf("%s\n", buf);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    -0x8(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f0 <main+0x70>
mov    0xe52(%rip),%rax
mov    %rax,-0x1e(%rbp)
mov    0xe50(%rip),%eax
mov    %eax,-0x16(%rbp)
mov    0xe4a(%rip),%ax
mov    %ax,-0x12(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    -0x4(%rbp),%edi
lea    -0x1e(%rbp),%rsi
mov    $0xe,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jle    122b <main+0xab>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1070 <read@plt>
lea    -0x40(%rbp),%rsi
lea    0xdef(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    1230 <main+0xb0>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     11a5 <main+0x25>
jg     11f0 <main+0x70>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret
movabs $0x21746e65726170,%rax
mov    %rax,0x16(%rsp)
movabs $0x6170206f6c6c6568,%rax
20 70 61
mov    %rax,0x10(%rsp)
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0xe,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x20,%edx
call   1070 <read@plt>
lea    0xdf8(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@__const.main.buf = [14 x i8] c"hello parent!\00"
@.str = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca i64
%3 = alloca [14 x i8]
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i32 @pipe(i32* %5)
%7 = call i64 (...) @fork()
store i64 %7, i64* %2
%8 = load i64, i64* %2
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %21
10: ; preds = %0
%11 = bitcast [14 x i8]* %3 to i8*
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%13 = load i32, i32* %12
%14 = call i32 @close(i32 %13)
%15 = call i32 @sleep(i32 1)
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%19 = call i32 @write(i32 %17, i8* %18, i32 14)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %0
%22 = load i64, i64* %2
%23 = icmp sgt i64 %22, 0
br i1 %23, label %24, label %35
24: ; preds = %21
%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 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%29 = load i32, i32* %28
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%31 = call i32 @read(i32 %29, i8* %30, i32 32)
%32 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%33 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %21
br label %36
36: ; preds = %35
ret i32 0
}

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