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

/* Variables and functions */
int /*<<< orphan*/ child_answers(char *);
int /*<<< orphan*/ close(int);
int fork();
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
scalar_t__ read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
int main() {
  int pipefd[2];
  int pid = -1;
  char buf[(1024)];
  if (pipe(pipefd) < 0) {
    perror("error when creating pipe");
    return -1;
  }
  pid = fork();
  if (pid < 0) {
    perror("error when creating child process");
    return -1;
  }
  if (pid == 0) {
    close(pipefd[1]);
    while (1) {
      memset(buf, 0x00, (1024));
      if (read(pipefd[0], buf, sizeof(buf)) > 0) {
        child_answers(buf);
      }
    }
  } else {
    close(pipefd[0]);
    while (1) {
      memset(buf, 0x00, (1024));
      scanf("%s", buf);
      write(pipefd[1], buf, strlen(buf));
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x10(%rbp)
lea    -0xc(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0x0,%rax
jge    11e4 <main+0x44>
lea    0xe2d(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <main+0x12e>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1210 <main+0x70>
lea    0xe1a(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ce <main+0x12e>
cmpl   $0x0,-0x10(%rbp)
jne    1264 <main+0xc4>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    -0xc(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1090 <read@plt>
cmp    $0x0,%rax
jle    125f <main+0xbf>
lea    -0x410(%rbp),%rdi
call   1040 <child_answers@plt>
jmp    1222 <main+0x82>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x410(%rbp),%rsi
lea    0xdae(%rip),%rdi
call   10a0 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x41c(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
lea    -0x410(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x41c(%rbp),%edi
mov    -0x418(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    126c <main+0xcc>
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    0x8(%rsp),%rdi
call   1080 <pipe@plt>
test   %rax,%rax
js     126f <main+0xcf>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     1278 <main+0xd8>
je     1224 <main+0x84>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
lea    0x10(%rsp),%rbx
lea    0xe57(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <scanf@plt>
mov    0xc(%rsp),%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    11f0 <main+0x50>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
lea    0x10(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1090 <read@plt>
test   %rax,%rax
jle    1240 <main+0xa0>
mov    %rbx,%rdi
call   1040 <child_answers@plt>
jmp    1240 <main+0xa0>
lea    0xd8a(%rip),%rdi
jmp    127f <main+0xdf>
lea    0xd9a(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"error when creating pipe\00"
@.str.1 = [34 x i8] c"error when creating child process\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca [1024 x i8]
store i32 0, i32* %1
store i32 -1, i32* %3
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %50
10: ; preds = %0
%11 = call i32 (...) @fork()
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %50
16: ; preds = %10
%17 = load i32, i32* %3
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %35
19: ; preds = %16
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @close(i32 %21)
br label %23
23: ; preds = %19, %34
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%25 = call i32 @memset(i8* %24, i32 0, i32 1024)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%27 = load i32, i32* %26
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%29 = call i64 @read(i32 %27, i8* %28, i32 1024)
%30 = icmp sgt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %23
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%33 = call i32 @child_answers(i8* %32)
br label %34
34: ; preds = %31, %23
br label %23
35: ; preds = %16
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %35, %39
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%41 = call i32 @memset(i8* %40, i32 0, i32 1024)
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%43 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %42)
%44 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%45 = load i32, i32* %44
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%48 = call i32 @strlen(i8* %47)
%49 = call i32 @write(i32 %45, i8* %46, i32 %48)
br label %39
50: ; preds = %14, %8
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
@.str = [25 x i8] c"error when creating pipe\00"
@.str.1 = [34 x i8] c"error when creating child process\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [1024 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%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 %35, label %8
8: ; preds = %0
%9 = call i32 (...) @fork()
%10 = icmp slt i32 %9, 0
br i1 %10, label %35, label %11
11: ; preds = %8
%12 = icmp eq i32 %9, 0
br i1 %12, label %13, label %25
13: ; preds = %11
%14 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%15 = load i32, i32* %14
%16 = call i32 @close(i32 %15)
br label %17
17: ; preds = %24, %13
%18 = call i32 @memset(i8* %4, i32 0, i32 1024)
%19 = load i32, i32* %5
%20 = call i64 @read(i32 %19, i8* %4, i32 1024)
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @child_answers(i8* %4)
br label %24
24: ; preds = %22, %17
br label %17
25: ; preds = %11
%26 = load i32, i32* %5
%27 = call i32 @close(i32 %26)
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
br label %29
29: ; preds = %29, %25
%30 = call i32 @memset(i8* %4, i32 0, i32 1024)
%31 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%32 = load i32, i32* %28
%33 = call i32 @strlen(i8* %4)
%34 = call i32 @write(i32 %32, i8* %4, i32 %33)
br label %29
35: ; preds = %8, %0
%36 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), %0 ], [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), %8 ]
%37 = call i32 @perror(i8* %36)
ret i32 -1
}