#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 */

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ wait(int);
int write(int, char *, int /*<<< orphan*/);
int main() {
  int fd[2];
  int pid;
  if (pipe(fd) < 0) {
    printf("pipe error\n");
    exit(0);
  }
  pid = fork();
  if (pid < 0) {
    printf("fork error\n");
    exit(0);
  }
  switch (pid) {
  case 0:
    printf("fork success\n");
    close(fd[1]);
    while (1) {
      char buf[1024];
      int len;
      len = read(fd[0], buf, 1024);
      if (len < 0)
        break;
      buf[len] = '\0';
      printf("child: %s\n", buf);
      if (buf[0] == 'E') {
        break;
      }
    }
    close(fd[0]);
    break;
  default:
    close(fd[0]);
    while (1) {
      char data[1024];
      scanf("%s\n", data);
      int len = write(fd[1], data, strlen(data));
      if (data[0] == 'E')
        break;
    }
    close(fd[1]);
    wait(pid);
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0x0,%rax
jge    11da <main+0x3a>
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1203 <main+0x63>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%eax
test   %eax,%eax
jne    12a0 <main+0x100>
jmp    1213 <main+0x73>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jge    1255 <main+0xb5>
jmp    1293 <main+0xf3>
movslq -0x414(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rsi
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movsbl -0x410(%rbp),%eax
cmp    $0x45,%eax
jne    128e <main+0xee>
jmp    1293 <main+0xf3>
jmp    1229 <main+0x89>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
jmp    1322 <main+0x182>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
lea    -0x820(%rbp),%rsi
lea    0xd77(%rip),%rdi
call   1090 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x834(%rbp)
lea    -0x820(%rbp),%rax
mov    %rax,-0x830(%rbp)
lea    -0x820(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x834(%rbp),%edi
mov    -0x830(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x824(%rbp)
movsbl -0x820(%rbp),%eax
cmp    $0x45,%eax
jne    130d <main+0x16d>
jmp    1312 <main+0x172>
jmp    12a8 <main+0x108>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%edi
call   10b0 <wait@plt>
xor    %eax,%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x418,%rsp
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
js     12c9 <main+0x119>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
js     12d2 <main+0x122>
je     123d <main+0x8d>
mov    %eax,%r14d
mov    0x8(%rsp),%edi
call   1070 <close@plt>
lea    0xe13(%rip),%r15
lea    0x10(%rsp),%rbp
nopl   0x0(%rax)
mov    %r15,%rdi
mov    %rbp,%rsi
call   10a0 <scanf@plt>
mov    0xc(%rsp),%ebx
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmpb   $0x45,0x10(%rsp)
jne    1200 <main+0x50>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    %r14d,%edi
call   10c0 <wait@plt>
jmp    12b9 <main+0x109>
lea    0xdc7(%rip),%rdi
call   1030 <puts@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %eax,%eax
js     12b0 <main+0x100>
lea    0xd90(%rip),%r14
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
cmpb   $0x45,0x10(%rsp)
je     12b0 <main+0x100>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %eax,%eax
jns    1280 <main+0xd0>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd53(%rip),%rdi
jmp    12d9 <main+0x129>
lea    0xd3f(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"pipe error\0A\00"
@.str.1 = [12 x i8] c"fork error\0A\00"
@.str.2 = [14 x i8] c"fork success\0A\00"
@.str.3 = [11 x i8] c"child: %s\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca i32
store i32 0, i32* %1
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 0)
unreachable
14: ; preds = %0
%15 = call i32 (...) @fork()
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %14
%22 = load i32, i32* %3
switch i32 %22, label %52 [
i32 0, label %23
]
23: ; preds = %21
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
br label %28
28: ; preds = %23, %47
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%30 = load i32, i32* %29
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 1024)
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %28
br label %48
36: ; preds = %28
%37 = load i32, i32* %5
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %38
store i8 0, i8* %39
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %40)
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %44, 69
br i1 %45, label %46, label %47
46: ; preds = %36
br label %48
47: ; preds = %36
br label %28
48: ; preds = %46, %35
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%50 = load i32, i32* %49
%51 = call i32 @close(i32 %50)
br label %77
52: ; preds = %21
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%54 = load i32, i32* %53
%55 = call i32 @close(i32 %54)
br label %56
56: ; preds = %52, %70
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%58 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %57)
%59 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%60 = load i32, i32* %59
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%63 = call i32 @strlen(i8* %62)
%64 = call i32 @write(i32 %60, i8* %61, i32 %63)
store i32 %64, i32* %7
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%66 = load i8, i8* %65
%67 = sext i8 %66 to i32
%68 = icmp eq i32 %67, 69
br i1 %68, label %69, label %70
69: ; preds = %56
br label %71
70: ; preds = %56
br label %56
71: ; preds = %69
%72 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%73 = load i32, i32* %72
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* %3
%76 = call i32 @wait(i32 %75)
br label %77
77: ; preds = %71, %48
ret i32 0
}

LLVM IR (-O2):
@.str.3 = [11 x i8] c"child: %s\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@str = [13 x i8] c"fork success\00"
@str.5 = [11 x i8] c"fork error\00"
@str.6 = [11 x i8] c"pipe error\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.6, i64 0, i64 0))
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %0
%12 = call i32 (...) @fork()
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str.5, i64 0, i64 0))
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %11
%18 = icmp eq i32 %12, 0
br i1 %18, label %19, label %42
19: ; preds = %17
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @close(i32 %22)
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%25 = load i32, i32* %5
%26 = call i32 @read(i32 %25, i8* %24, i32 1024)
%27 = icmp slt i32 %26, 0
br i1 %27, label %39, label %28
28: ; preds = %19, %35
%29 = phi i32 [ %37, %35 ], [ %26, %19 ]
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %30
store i8 0, i8* %31
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %24)
%33 = load i8, i8* %24
%34 = icmp eq i8 %33, 69
br i1 %34, label %39, label %35
35: ; preds = %28
%36 = load i32, i32* %5
%37 = call i32 @read(i32 %36, i8* %24, i32 1024)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %28
39: ; preds = %35, %28, %19
%40 = load i32, i32* %5
%41 = call i32 @close(i32 %40)
br label %58
42: ; preds = %17
%43 = load i32, i32* %5
%44 = call i32 @close(i32 %43)
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
br label %47
47: ; preds = %47, %42
%48 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %45)
%49 = load i32, i32* %46
%50 = call i32 @strlen(i8* %45)
%51 = call i32 @write(i32 %49, i8* %45, i32 %50)
%52 = load i8, i8* %45
%53 = icmp eq i8 %52, 69
br i1 %53, label %54, label %47
54: ; preds = %47
%55 = load i32, i32* %46
%56 = call i32 @close(i32 %55)
%57 = call i32 @wait(i32 %12)
br label %58
58: ; preds = %54, %39
ret i32 0
}