#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*/ r_buf;
typedef scalar_t__ pid_t;

/* Variables and functions */
int close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
scalar_t__ strlen(char *);
int write(int, char *, scalar_t__);
int main() {
  pid_t pid;
  int pipe_fd[2];
  int ret;
  int n;
  char r_buf[100];
  char w_buf[100];
  ret = pipe(pipe_fd);
  if (ret < 0) {
    printf("pipe create failed");
  }
  pid = fork();
  if (pid < 0) {
    printf("fork failed.\n");
    exit(-1);
  }
  if (pid == 0) {
    ret = close(pipe_fd[1]);
    if (ret < 0) {
      printf("close pipe fd 1 failed.\n");
      exit(-1);
    }
    while (1) {
      n = read(pipe_fd[0], r_buf, sizeof(r_buf));
      printf("read %d:  %s\n", n, r_buf);
    }
    close(pipe_fd[0]);
  } else {
    ret = close(pipe_fd[0]);
    if (ret < 0) {
      printf("close pipe fd 0 failed.\n");
    }
    while (1) {
      scanf("%s", w_buf);
      ret = write(pipe_fd[1], w_buf, strlen(w_buf) + 1);
      if (ret < 0) {
        printf("write failed.\n");
      }
    }
    close(pipe_fd[1]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
call   1070 <pipe@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11c6 <main+0x36>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11f4 <main+0x64>
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1265 <main+0xd5>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    122c <main+0x9c>
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
jmp    1231 <main+0xa1>
mov    -0x18(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x64,%edx
call   1080 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    -0x90(%rbp),%rdx
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1231 <main+0xa1>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1288 <main+0xf8>
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    128d <main+0xfd>
lea    -0x100(%rbp),%rsi
lea    0xdc6(%rip),%rdi
call   1090 <scanf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x10c(%rbp)
lea    -0x100(%rbp),%rax
mov    %rax,-0x108(%rbp)
lea    -0x100(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x10c(%rbp),%edi
mov    -0x108(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12f7 <main+0x167>
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    128d <main+0xfd>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x78,%rsp
lea    0x8(%rsp),%rdi
call   1080 <pipe@plt>
test   %eax,%eax
jns    11c6 <main+0x26>
lea    0xe41(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     1282 <main+0xe2>
je     1243 <main+0xa3>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
test   %eax,%eax
jns    11f1 <main+0x51>
lea    0xe46(%rip),%rdi
call   1030 <puts@plt>
lea    0xe29(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xe20(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <scanf@plt>
mov    0xc(%rsp),%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <write@plt>
test   %eax,%eax
jns    1210 <main+0x70>
mov    %r15,%rdi
call   1030 <puts@plt>
jmp    1210 <main+0x70>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
test   %eax,%eax
js     128b <main+0xeb>
lea    0x10(%rsp),%rbx
lea    0xdb7(%rip),%r14
nopl   0x0(%rax)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1090 <read@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1260 <main+0xc0>
lea    0xdd9(%rip),%rdi
jmp    1292 <main+0xf2>
lea    0xdb8(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
@.str = [19 x i8] c"pipe create failed\00"
@.str.3 = [14 x i8] c"read %d: %s\0A\00"
@.str.5 = [3 x i8] c"%s\00"
@str = [14 x i8] c"write failed.\00"
@str.7 = [24 x i8] c"close pipe fd 0 failed.\00"
@str.8 = [24 x i8] c"close pipe fd 1 failed.\00"
@str.9 = [13 x i8] c"fork failed.\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [100 x i8]
%3 = alloca [100 x i8]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, 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 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 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 %18
15: ; preds = %12
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str.9, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %12
%19 = icmp eq i64 %13, 0
br i1 %19, label %20, label %32
20: ; preds = %18
%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 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.8, i64 0, i64 0))
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %20, %28
%29 = load i32, i32* %7
%30 = call i32 @read(i32 %29, i8* %5, i32 100)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %30, i8* %5)
br label %28
32: ; preds = %18
%33 = load i32, i32* %7
%34 = call i32 @close(i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.7, i64 0, i64 0))
br label %38
38: ; preds = %36, %32
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
br label %40
40: ; preds = %49, %38
%41 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %6)
%42 = load i32, i32* %39
%43 = call i64 @strlen(i8* %6)
%44 = add nsw i64 %43, 1
%45 = call i32 @write(i32 %42, i8* %6, i64 %44)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %40
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %49
49: ; preds = %47, %40
br label %40
}