#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 STDOUT_FILENO;
int /*<<< orphan*/ close(int);
scalar_t__ fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ print_exit(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int n;
  int fd[2];
  pid_t pid;
  char line[20];
  if (pipe(fd) < 0) {
    print_exit("pipe error");
  }
  if ((pid = fork()) < 0) {
    print_exit("fork error");
  } else if (pid > 0) {
    close(fd[0]);
    write(fd[1], "hello world\n", 12);
  } else {
    close(fd[1]);
    n = read(fd[0], line, 20);
    write(STDOUT_FILENO, line, n);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1c(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jge    1195 <main+0x35>
lea    0xe70(%rip),%rdi
call   1070 <print_exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jge    11bb <main+0x5b>
lea    0xe5a(%rip),%rdi
call   1070 <print_exit@plt>
jmp    121d <main+0xbd>
cmpq   $0x0,-0x28(%rbp)
jle    11e7 <main+0x87>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
lea    0xe3e(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
jmp    1218 <main+0xb8>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x14,%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
jmp    121d <main+0xbd>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jns    1180 <main+0x20>
lea    0xe85(%rip),%rdi
call   1070 <print_exit@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11a9 <main+0x49>
je     11b7 <main+0x57>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
lea    0xe74(%rip),%rsi
mov    $0xc,%edx
jmp    11e4 <main+0x84>
lea    0xe5b(%rip),%rdi
call   1070 <print_exit@plt>
jmp    11e9 <main+0x89>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x14,%edx
call   1060 <read@plt>
mov    0x2e13(%rip),%rcx
mov    (%rcx),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret

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

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