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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jge    11bb <main+0x4b>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
jge    11f2 <main+0x82>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe29(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x428(%rbp)
jle    1221 <main+0xb1>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
lea    0xe06(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
jmp    1258 <main+0xe8>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
jmp    125d <main+0xed>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x410,%rsp
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
js     11ed <main+0x7d>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11ff <main+0x8f>
je     11b0 <main+0x40>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
lea    0xe6f(%rip),%rsi
mov    $0xc,%edx
jmp    11dd <main+0x6d>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
ret
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe03(%rip),%rsi
jmp    120f <main+0x9f>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xdfd(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [12 x i8] c"pipe error\0A\00"
@.str.1 = [12 x i8] c"fork error\0A\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 [1024 x i8]
%5 = bitcast [2 x i32]* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 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 %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = call i64 (...) @fork()
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %14
%22 = icmp eq i64 %15, 0
br i1 %22, label %29, label %23
23: ; preds = %21
%24 = load i32, i32* %7
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @write(i32 %27, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 12)
br label %37
29: ; preds = %21
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %7
%34 = call i32 @read(i32 %33, i8* %6, i32 1024)
%35 = load i32, i32* @STDOUT_FILENO
%36 = call i32 @write(i32 %35, i8* %6, i32 %34)
br label %37
37: ; preds = %23, %29
ret i32 0
}