#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 PIPE_BUF;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ err_quit(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ pipe(int *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int fd[2];
  char buf[PIPE_BUF];
  pid_t pid;
  int len;
  if ((pipe(fd)) < 0)
    err_quit("pipe");
  if ((pid = fork()) < 0)
    err_quit("fork");
  else if (pid == 0) {
    close(fd[0]);
    write(fd[1], "hello brother!\n", 15);
    exit(0);
  }
  if ((pid = fork()) < 0)
    err_quit("fork");
  else if (pid > 0) {
    close(fd[0]);
    close(fd[1]);
    exit(0);
  } else {
    close(fd[1]);
    len = read(fd[0], buf, PIPE_BUF);
    write(STDOUT_FILENO, buf, len);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
lea    -0xc(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
jge    11ca <main+0x5a>
lea    0xe3b(%rip),%rdi
call   1060 <err_quit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jge    11f0 <main+0x80>
lea    0xe1f(%rip),%rdi
call   1060 <err_quit@plt>
jmp    1223 <main+0xb3>
cmpq   $0x0,-0x28(%rbp)
jne    121e <main+0xae>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
lea    0xdfd(%rip),%rsi
mov    $0xf,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    1223 <main+0xb3>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jge    1249 <main+0xd9>
lea    0xdc6(%rip),%rdi
call   1060 <err_quit@plt>
jmp    12a7 <main+0x137>
cmpq   $0x0,-0x28(%rbp)
jle    126b <main+0xfb>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x38(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    0x2d57(%rip),%rax
mov    (%rax),%edx
call   1070 <read@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,-0x2c(%rbp)
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsp,%r15
mov    0x2e54(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x20(%rbp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jns    11b2 <main+0x42>
lea    0xe53(%rip),%rdi
call   1060 <err_quit@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
js     11d9 <main+0x69>
jne    11e5 <main+0x75>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
lea    0xe38(%rip),%rsi
mov    $0xf,%edx
jmp    1233 <main+0xc3>
lea    0xe25(%rip),%rdi
call   1060 <err_quit@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jns    120d <main+0x9d>
lea    0xe0d(%rip),%rdi
call   1060 <err_quit@plt>
mov    %r15,%rsp
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
jne    123f <main+0xcf>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%edi
mov    (%r14),%edx
mov    %rbx,%rsi
call   1070 <read@plt>
mov    0x2dc4(%rip),%rcx
mov    (%rcx),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@PIPE_BUF = global i32 0
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [16 x i8] c"hello brother!\0A\00"
@STDOUT_FILENO = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @PIPE_BUF
%8 = zext i32 %7 to i64
%9 = call i8* @llvm.stacksave()
store i8* %9, i8** %3
%10 = alloca i8, i64 %8
store i64 %8, i64* %4
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%12 = call i64 @pipe(i32* %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = call i32 @err_quit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %0
%17 = call i64 (...) @fork()
store i64 %17, i64* %5
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @err_quit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %33
21: ; preds = %16
%22 = load i64, i64* %5
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %32
24: ; preds = %21
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @write(i32 %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 15)
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %21
br label %33
33: ; preds = %32, %19
%34 = call i64 (...) @fork()
store i64 %34, i64* %5
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @err_quit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %61
38: ; preds = %33
%39 = load i64, i64* %5
%40 = icmp sgt i64 %39, 0
br i1 %40, label %41, label %49
41: ; preds = %38
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%46 = load i32, i32* %45
%47 = call i32 @close(i32 %46)
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %38
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%51 = load i32, i32* %50
%52 = call i32 @close(i32 %51)
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%54 = load i32, i32* %53
%55 = load i32, i32* @PIPE_BUF
%56 = call i32 @read(i32 %54, i8* %10, i32 %55)
store i32 %56, i32* %6
%57 = load i32, i32* @STDOUT_FILENO
%58 = load i32, i32* %6
%59 = call i32 @write(i32 %57, i8* %10, i32 %58)
%60 = call i32 @exit(i32 0)
unreachable
61: ; preds = %36
%62 = load i8*, i8** %3
%63 = load i32, i32* %1
ret i32 %63
}

LLVM IR (-O2):
@PIPE_BUF = global i32 0
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [16 x i8] c"hello brother!\0A\00"
@STDOUT_FILENO = global i32 0
define i32 @main() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = load i32, i32* @PIPE_BUF
%4 = zext i32 %3 to i64
%5 = tail call i8* @llvm.stacksave()
%6 = alloca i8, i64 %4
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, 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 = %0
%11 = call i32 @err_quit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %0
%13 = call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @err_quit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %26
17: ; preds = %12
%18 = icmp eq i64 %13, 0
br i1 %18, label %19, label %26
19: ; preds = %17
%20 = load i32, i32* %7
%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 ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 15)
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %17, %15
%27 = call i64 (...) @fork()
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @err_quit(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
31: ; preds = %26
%32 = icmp eq i64 %27, 0
br i1 %32, label %40, label %33
33: ; preds = %31
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %31
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %7
%45 = load i32, i32* @PIPE_BUF
%46 = call i32 @read(i32 %44, i8* %6, i32 %45)
%47 = load i32, i32* @STDOUT_FILENO
%48 = call i32 @write(i32 %47, i8* %6, i32 %46)
%49 = call i32 @exit(i32 0)
unreachable
}