#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int write(int, char *, int);
int main() {
  pid_t pid;
  int ret;
  int fd[2];
  ret = pipe(fd);
  if (ret < 0) {
    perror("pipe");
    return -1;
  }
  if (!(pid = fork())) {
    close(fd[1]);
    char buff[1024] = {0};
    ret = read(fd[0], buff, 1023);
    if (ret < 0) {
      perror("read");
      return -1;
    }
    printf("child recv a msg:%s\n", buff);
    close(fd[0]);
    return 0;
  }
  close(fd[0]);
  ret = write(fd[1], "hello", 5);
  printf("parent send a msg:%s\n", "hello");
  wait(&pid);
  close(fd[1]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x14(%rbp),%rdi
call   1070 <pipe@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11d0 <main+0x40>
lea    0xe41(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a8 <main+0x118>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jne    125e <main+0xce>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x3ff,%edx
call   1080 <read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1237 <main+0xa7>
lea    0xddf(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a8 <main+0x118>
lea    -0x420(%rbp),%rsi
lea    0xdc5(%rip),%rdi
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a8 <main+0x118>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
lea    0xdaf(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    %eax,-0xc(%rbp)
lea    0xda1(%rip),%rdi
lea    0xd94(%rip),%rsi
call   1040 <printf@plt>
lea    -0x8(%rbp),%rdi
call   10a0 <wait@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x410,%rsp
lea    0x4(%rsp),%rdi
call   1070 <pipe@plt>
test   %eax,%eax
js     125a <main+0xca>
xor    %ebp,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
je     11fe <main+0x6e>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
lea    0xe4d(%rip),%rbx
mov    %rbx,%rsi
mov    $0x5,%edx
call   1030 <write@plt>
lea    0xe3f(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
lea    0xc(%rsp),%rdi
call   10a0 <wait@plt>
mov    0x8(%rsp),%edi
jmp    1247 <main+0xb7>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
lea    0x10(%rsp),%r14
xor    %ebp,%ebp
mov    $0x400,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x4(%rsp),%edi
mov    %r14,%rsi
mov    $0x3ff,%edx
call   1080 <read@plt>
test   %eax,%eax
js     1263 <main+0xd3>
lea    0xdd1(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1040 <printf@plt>
mov    0x4(%rsp),%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd9f(%rip),%rdi
jmp    126a <main+0xda>
lea    0xd9b(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebp
jmp    124c <main+0xbc>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [21 x i8] c"child recv a msg:%s\0A\00"
@.str.3 = [6 x i8] c"hello\00"
@.str.4 = [22 x i8] c"parent send a msg:%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x i32]
%5 = alloca [1024 x i8]
store i32 0, i32* %1
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %46
12: ; preds = %0
%13 = call i32 (...) @fork()
store i32 %13, i32* %2
%14 = icmp ne i32 %13, 0
br i1 %14, label %34, label %15
15: ; preds = %12
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @close(i32 %17)
%19 = bitcast [1024 x i8]* %5 to i8*
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%23 = call i32 @read(i32 %21, i8* %22, i32 1023)
store i32 %23, i32* %3
%24 = load i32, i32* %3
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %46
28: ; preds = %15
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%30 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
store i32 0, i32* %1
br label %46
34: ; preds = %12
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%36 = load i32, i32* %35
%37 = call i32 @close(i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @write(i32 %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
store i32 %40, i32* %3
%41 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @wait(i32* %2)
%43 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%44 = load i32, i32* %43
%45 = call i32 @close(i32 %44)
store i32 0, i32* %1
br label %46
46: ; preds = %34, %28, %26, %10
%47 = load i32, i32* %1
ret i32 %47
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"read\00"
@.str.2 = [21 x i8] c"child recv a msg:%s\0A\00"
@.str.3 = [6 x i8] c"hello\00"
@.str.4 = [22 x i8] c"parent send a msg:%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [1024 x i8]
%4 = bitcast i32* %1 to i8*
%5 = bitcast [2 x i32]* %2 to i8*
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%7 = call i32 @pipe(i32* %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %40
11: ; preds = %0
%12 = call i32 (...) @fork()
store i32 %12, i32* %1
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %30
14: ; preds = %11
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%16 = load i32, i32* %15
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%19 = load i32, i32* %6
%20 = call i32 @read(i32 %19, i8* %18, i32 1023)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %14
%23 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %28
24: ; preds = %14
%25 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %24, %22
%29 = phi i32 [ -1, %22 ], [ 0, %24 ]
br label %40
30: ; preds = %11
%31 = load i32, i32* %6
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @write(i32 %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%36 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%37 = call i32 @wait(i32* %1)
%38 = load i32, i32* %33
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %30, %28, %9
%41 = phi i32 [ -1, %9 ], [ 0, %30 ], [ %29, %28 ]
ret i32 %41
}