#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 fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, char *);
int 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];
  if (pipe(pipeFd) == -1) {
    perror("Erro ao criar pipe");
    return -1;
  }
  int idProcesso = fork();
  if (idProcesso == 0) {
    close(pipeFd[1]);
    char buffer[255];
    while (1) {
      int bytesLidos = read(pipeFd[0], buffer, 255);
      buffer[bytesLidos] = '\0';
      printf("%s\n", buffer);
    }
  } else {
    close(pipeFd[0]);
    char buffer[255];
    while (1) {
      scanf("%s", buffer);
      write(pipeFd[1], buffer, strlen(buffer));
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0xc(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11ce <main+0x3e>
lea    0xe45(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x230,%rsp
pop    %rbp
ret
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jne    122b <main+0x9b>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0xff,%edx
call   1080 <read@plt>
mov    %eax,-0x114(%rbp)
movslq -0x114(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rsi
lea    0xdf2(%rip),%rdi
call   1050 <printf@plt>
jmp    11ea <main+0x5a>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
lea    -0x220(%rbp),%rsi
lea    0xdd6(%rip),%rdi
call   1090 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x22c(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x228(%rbp)
lea    -0x220(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x22c(%rbp),%edi
mov    -0x228(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1233 <main+0xa3>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x110,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
je     1235 <main+0xa5>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     11f5 <main+0x65>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
lea    0xe4e(%rip),%r14
lea    0x10(%rsp),%rbx
xchg   %ax,%ax
mov    %r14,%rdi
mov    %rbx,%rsi
call   1090 <scanf@plt>
mov    0xc(%rsp),%ebp
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    11d0 <main+0x40>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
lea    0x10(%rsp),%rbx
lea    0xe09(%rip),%r14
nopw   0x0(%rax,%rax,1)
mov    0x8(%rsp),%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1080 <read@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
jmp    1210 <main+0x80>
lea    0xdc4(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"Erro ao criar pipe\00"
@.str.1 = [4 x i8] c"%s\0A\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 [255 x i8]
%5 = alloca i32
%6 = alloca [255 x i8]
store i32 0, i32* %1
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
ret i32 -1
12: ; preds = %0
%13 = call i32 (...) @fork()
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %12
%17 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%18 = load i32, i32* %17
%19 = call i32 @close(i32 %18)
br label %20
20: ; preds = %16, %20
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 255)
store i32 %24, i32* %5
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 %26
store i8 0, i8* %27
%28 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %20
30: ; preds = %12
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%32 = load i32, i32* %31
%33 = call i32 @close(i32 %32)
br label %34
34: ; preds = %30, %34
%35 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%36 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%38 = load i32, i32* %37
%39 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%40 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%41 = call i32 @strlen(i8* %40)
%42 = call i32 @write(i32 %38, i8* %39, i32 %41)
br label %34
}

LLVM IR (-O2):
@.str = [19 x i8] c"Erro ao criar pipe\00"
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [255 x i8]
%3 = alloca [255 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 i32 @pipe(i32* %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
ret i32 -1
10: ; preds = %0
%11 = call i32 (...) @fork()
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %10
%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)
%17 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
br label %18
18: ; preds = %18, %13
%19 = load i32, i32* %5
%20 = call i32 @read(i32 %19, i8* %17, i32 255)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 %21
store i8 0, i8* %22
%23 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %17)
br label %18
24: ; preds = %10
%25 = load i32, i32* %5
%26 = call i32 @close(i32 %25)
%27 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
br label %29
29: ; preds = %29, %24
%30 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %27)
%31 = load i32, i32* %28
%32 = call i32 @strlen(i8* %27)
%33 = call i32 @write(i32 %31, i8* %27, i32 %32)
br label %29
}