#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 STDIN_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ read(int, char *, int);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd[2];
  pid_t pid;
  char buf[20] = {0};
  if (pipe(fd) < 0) {
    perror("pipe");
    exit(-1);
  }
  if ((pid = fork()) < 0) {
    perror("fork");
    exit(-1);
  } else if (pid > 0) {
    close(fd[0]);
    write(fd[1], "hello world!", sizeof("hello world!"));
  } else {
    close(fd[1]);
    read(fd[0], buf, 20);
    write(STDIN_FILENO, buf, strlen(buf));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1050 <memset@plt>
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11d8 <main+0x48>
lea    0xe37(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1203 <main+0x73>
lea    0xe11(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    122f <main+0x9f>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
lea    0xdea(%rip),%rsi
mov    $0xd,%edx
call   1030 <write@plt>
jmp    1273 <main+0xe3>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x14,%edx
call   1080 <read@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1278 <main+0xe8>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x20(%rsp)
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     1213 <main+0x93>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     121c <main+0x9c>
je     11ce <main+0x4e>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
lea    0xe43(%rip),%rsi
mov    $0xd,%edx
jmp    1205 <main+0x85>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x14,%edx
call   1070 <read@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xde6(%rip),%rdi
jmp    1223 <main+0xa3>
lea    0xde2(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

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

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