#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 int ssize_t;
typedef scalar_t__ pid_t;

/* Variables and functions */
int PIPE_BUF;
int STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
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;
  ssize_t len;
  if ((pipe(fd)) < 0) {
    perror("failed to pipe");
    exit(1);
  }
  if ((pid = fork()) < 0) {
    perror("failed to fork ");
    exit(1);
  } else if (pid > 0) {
    close(fd[0]);
    write(fd[1], "hello my son!\n", 14);
    exit(0);
  } else {
    close(fd[1]);
    len = read(fd[0], buf, PIPE_BUF);
    if (len < 0) {
      perror("process failed when read a pipe ");
      exit(1);
    } else
      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    11d4 <main+0x64>
lea    0xe3b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jge    11ff <main+0x8f>
lea    0xe1f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x28(%rbp)
jle    122d <main+0xbd>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
lea    0xe03(%rip),%rsi
mov    $0xe,%edx
call   1030 <write@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    0x2d95(%rip),%rax
mov    (%rax),%edx
call   1060 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    126d <main+0xfd>
lea    0xdd0(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%rsi
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e56(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jns    11ad <main+0x3d>
lea    0xe55(%rip),%rdi
jmp    11e5 <main+0x75>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jns    11c2 <main+0x52>
lea    0xe4f(%rip),%rdi
jmp    11e5 <main+0x75>
jne    11f4 <main+0x84>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
mov    (%r14),%edx
mov    %rbx,%rsi
call   1060 <read@plt>
test   %eax,%eax
jns    120d <main+0x9d>
lea    0xe49(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
lea    0xe19(%rip),%rsi
mov    $0xe,%edx
jmp    121b <main+0xab>
mov    0x2ddc(%rip),%rcx
mov    (%rcx),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@PIPE_BUF = global i32 0
@.str = [15 x i8] c"failed to pipe\00"
@.str.1 = [16 x i8] c"failed to fork \00"
@.str.2 = [15 x i8] c"hello my son!\0A\00"
@.str.3 = [33 x i8] c"process failed when read a pipe \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 %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = call i64 (...) @fork()
store i64 %18, i64* %5
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %17
%24 = load i64, i64* %5
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %23
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @write(i32 %31, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 14)
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %23
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%39 = load i32, i32* %38
%40 = load i32, i32* @PIPE_BUF
%41 = call i32 @read(i32 %39, i8* %10, i32 %40)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %34
%45 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %34
%48 = load i32, i32* @STDOUT_FILENO
%49 = load i32, i32* %6
%50 = call i32 @write(i32 %48, i8* %10, i32 %49)
br label %51
51: ; preds = %47
%52 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@PIPE_BUF = global i32 0
@.str = [15 x i8] c"failed to pipe\00"
@.str.1 = [16 x i8] c"failed to fork \00"
@.str.2 = [15 x i8] c"hello my son!\0A\00"
@.str.3 = [33 x i8] c"process failed when read a pipe \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 = alloca i8, i64 %4
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %1, 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 ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = call i64 (...) @fork()
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %12
%19 = icmp eq i64 %13, 0
br i1 %19, label %27, label %20
20: ; preds = %18
%21 = load i32, i32* %6
%22 = call i32 @close(i32 %21)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @write(i32 %24, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 14)
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %18
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %6
%32 = load i32, i32* @PIPE_BUF
%33 = call i32 @read(i32 %31, i8* %5, i32 %32)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %27
%36 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %27
%39 = load i32, i32* @STDOUT_FILENO
%40 = call i32 @write(i32 %39, i8* %5, i32 %33)
%41 = call i32 @exit(i32 0)
unreachable
}