#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 *, ...);
int read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int write(int, char *, int /*<<< orphan*/);
int main() {
  int ret;
  pid_t pid;
  int fd[2];
  ret = pipe(fd);
  if (ret < 0) {
    perror("pipe");
    return -1;
  }
  if (!(pid = fork())) {
    close(fd[0]);
    char buff[1024] = {0};
    printf("Input a message:\n");
    scanf("%s", buff);
    ret = write(fd[1], buff, strlen(buff));
    if (ret < 0) {
      perror("write");
      return -1;
    }
    printf("child : %s\n", buff);
    close(fd[1]);
    return 0;
  }
  close(fd[1]);
  char buffer[1024] = {0};
  ret = read(fd[0], buffer, 1023);
  if (ret < 0) {
    perror("write");
    return -1;
  }
  printf("parent : %s\n", buffer);
  close(fd[0]);
  wait(&pid);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x14(%rbp),%rdi
call   1080 <pipe@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11f0 <main+0x40>
lea    0xe21(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1345 <main+0x195>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jne    12c4 <main+0x114>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x420(%rbp),%rsi
lea    0xddd(%rip),%rdi
call   10a0 <scanf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x82c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x828(%rbp)
lea    -0x420(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x82c(%rbp),%edi
mov    -0x828(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    129b <main+0xeb>
lea    0xd90(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1345 <main+0x195>
lea    -0x420(%rbp),%rsi
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1345 <main+0x195>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x3ff,%edx
call   1090 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1318 <main+0x168>
lea    0xd13(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1345 <main+0x195>
lea    -0x820(%rbp),%rsi
lea    0xd06(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
lea    -0xc(%rbp),%rdi
call   10c0 <wait@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x418,%rsp
lea    0x4(%rsp),%rdi
call   1090 <pipe@plt>
test   %eax,%eax
js     12c1 <main+0x101>
xor    %eax,%eax
call   10e0 <fork@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
je     124d <main+0x8d>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
lea    0x10(%rsp),%rbp
xor    %ebx,%ebx
mov    $0x400,%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    0x4(%rsp),%edi
mov    %rbp,%rsi
mov    $0x3ff,%edx
call   10a0 <read@plt>
test   %eax,%eax
js     12ca <main+0x10a>
lea    0xdf1(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x4(%rsp),%edi
call   1080 <close@plt>
lea    0xc(%rsp),%rdi
call   10d0 <wait@plt>
jmp    12db <main+0x11b>
mov    0x4(%rsp),%edi
call   1080 <close@plt>
lea    0x10(%rsp),%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
lea    0xdb6(%rip),%rdi
call   1030 <puts@plt>
lea    0xd88(%rip),%rdi
mov    %rbx,%rsi
call   10b0 <scanf@plt>
mov    0x8(%rsp),%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %eax,%eax
js     12ca <main+0x10a>
lea    0xd66(%rip),%rdi
xor    %ebx,%ebx
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x8(%rsp),%edi
call   1080 <close@plt>
jmp    12db <main+0x11b>
lea    0xd38(%rip),%rdi
jmp    12d1 <main+0x111>
lea    0xd37(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x418,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [18 x i8] c"Input a message:\0A\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [12 x i8] c"child : %s\0A\00"
@.str.5 = [13 x i8] c"parent : %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]
%6 = alloca [1024 x i8]
store i32 0, i32* %1
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %60
13: ; preds = %0
%14 = call i32 (...) @fork()
store i32 %14, i32* %3
%15 = icmp ne i32 %14, 0
br i1 %15, label %40, label %16
16: ; preds = %13
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%18 = load i32, i32* %17
%19 = call i32 @close(i32 %18)
%20 = bitcast [1024 x i8]* %5 to i8*
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%23 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%25 = load i32, i32* %24
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%28 = call i32 @strlen(i8* %27)
%29 = call i32 @write(i32 %25, i8* %26, i32 %28)
store i32 %29, i32* %2
%30 = load i32, i32* %2
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %16
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %60
34: ; preds = %16
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %35)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%38 = load i32, i32* %37
%39 = call i32 @close(i32 %38)
store i32 0, i32* %1
br label %60
40: ; preds = %13
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = bitcast [1024 x i8]* %6 to i8*
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%46 = load i32, i32* %45
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%48 = call i32 @read(i32 %46, i8* %47, i32 1023)
store i32 %48, i32* %2
%49 = load i32, i32* %2
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %40
%52 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %1
br label %60
53: ; preds = %40
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %54)
%56 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%57 = load i32, i32* %56
%58 = call i32 @close(i32 %57)
%59 = call i32 @wait(i32* %3)
store i32 0, i32* %1
br label %60
60: ; preds = %53, %51, %34, %32, %11
%61 = load i32, i32* %1
ret i32 %61
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [12 x i8] c"child : %s\0A\00"
@.str.5 = [13 x i8] c"parent : %s\0A\00"
@str = [17 x i8] c"Input a message:\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = bitcast i32* %1 to i8*
%6 = bitcast [2 x i32]* %2 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%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))
br label %51
12: ; preds = %0
%13 = call i32 (...) @fork()
store i32 %13, i32* %1
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %34
15: ; preds = %12
%16 = load i32, i32* %7
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%20 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @strlen(i8* %18)
%24 = call i32 @write(i32 %22, i8* %18, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %32
28: ; preds = %15
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8* %18)
%30 = load i32, i32* %21
%31 = call i32 @close(i32 %30)
br label %32
32: ; preds = %28, %26
%33 = phi i32 [ -1, %26 ], [ 0, %28 ]
br label %51
34: ; preds = %12
%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 [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%39 = load i32, i32* %7
%40 = call i32 @read(i32 %39, i8* %38, i32 1023)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %34
%43 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %49
44: ; preds = %34
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i8* %38)
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
%48 = call i32 @wait(i32* %1)
br label %49
49: ; preds = %44, %42
%50 = phi i32 [ -1, %42 ], [ 0, %44 ]
br label %51
51: ; preds = %49, %32, %10
%52 = phi i32 [ -1, %10 ], [ %50, %49 ], [ %33, %32 ]
ret i32 %52
}