#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 getpid();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, int, 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[3];
  if (pipe(pipeFd) == -1) {
    perror("Erro ao criar pipe");
    return -1;
  }
  int idProcesso = fork();
  if (idProcesso == 0) {
    idProcesso = fork();
    if (idProcesso == 0) {
      close(pipeFd[2]);
      close(pipeFd[0]);
      char buffer[255];
      while (1) {
        int bytesLidos = read(pipeFd[0], buffer, 255);
        buffer[bytesLidos] = '\0';
        printf("%d: %s\n", getpid(), buffer);
      }
    } else {
      close(pipeFd[1]);
      close(pipeFd[2]);
      char buffer[255];
      while (1) {
        int bytesLidos = read(pipeFd[0], buffer, 255);
        buffer[bytesLidos] = '\0';
        printf("%d: %s\n", getpid(), 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    $0x350,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11de <main+0x3e>
lea    0xe35(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x350,%rsp
pop    %rbp
ret
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    12ba <main+0x11a>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1260 <main+0xc0>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%edi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1090 <read@plt>
mov    %eax,-0x124(%rbp)
movslq -0x124(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    -0x120(%rbp),%rdx
lea    0xdbd(%rip),%rdi
call   1060 <printf@plt>
jmp    1216 <main+0x76>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0xff,%edx
call   1090 <read@plt>
mov    %eax,-0x234(%rbp)
movslq -0x234(%rbp),%rax
movb   $0x0,-0x230(%rbp,%rax,1)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    -0x230(%rbp),%rdx
lea    0xd63(%rip),%rdi
call   1060 <printf@plt>
jmp    1270 <main+0xd0>
mov    -0x10(%rbp),%edi
call   1070 <close@plt>
lea    -0x340(%rbp),%rsi
lea    0xd4b(%rip),%rdi
call   10a0 <scanf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x34c(%rbp)
lea    -0x340(%rbp),%rax
mov    %rax,-0x348(%rbp)
lea    -0x340(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x34c(%rbp),%edi
mov    -0x348(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    12c2 <main+0x122>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x110,%rsp
lea    0x4(%rsp),%rdi
call   1080 <pipe@plt>
cmp    $0xffffffff,%eax
je     12ae <main+0x10e>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1205 <main+0x65>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
lea    0xe42(%rip),%r14
lea    0x10(%rsp),%rbx
xchg   %ax,%ax
mov    %r14,%rdi
mov    %rbx,%rsi
call   10a0 <scanf@plt>
mov    0x8(%rsp),%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    11e0 <main+0x40>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     125e <main+0xbe>
mov    0x8(%rsp),%edi
call   1070 <close@plt>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
lea    0x10(%rsp),%rbx
lea    0xde5(%rip),%r14
xchg   %ax,%ax
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1090 <read@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
call   1060 <printf@plt>
jmp    1230 <main+0x90>
mov    0xc(%rsp),%edi
call   1070 <close@plt>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
lea    0x10(%rsp),%rbx
lea    0xd97(%rip),%r14
nopl   0x0(%rax)
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1090 <read@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
call   1060 <printf@plt>
jmp    1280 <main+0xe0>
lea    0xd4b(%rip),%rdi
call   10b0 <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 = [8 x i8] c"%d: %s\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [3 x i32]
%3 = alloca i32
%4 = alloca [255 x i8]
%5 = alloca i32
%6 = alloca [255 x i8]
%7 = alloca i32
%8 = alloca [255 x i8]
store i32 0, i32* %1
%9 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
ret i32 -1
14: ; preds = %0
%15 = call i32 (...) @fork()
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %58
18: ; preds = %14
%19 = call i32 (...) @fork()
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %40
22: ; preds = %18
%23 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 2
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 0
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
br label %29
29: ; preds = %22, %29
%30 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 0
%31 = load i32, i32* %30
%32 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 255)
store i32 %33, i32* %5
%34 = load i32, i32* %5
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 %35
store i8 0, i8* %36
%37 = call i32 (...) @getpid()
%38 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%39 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %37, i8* %38)
br label %29
40: ; preds = %18
%41 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @close(i32 %42)
%44 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 2
%45 = load i32, i32* %44
%46 = call i32 @close(i32 %45)
br label %47
47: ; preds = %40, %47
%48 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 0
%49 = load i32, i32* %48
%50 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%51 = call i32 @read(i32 %49, i8* %50, i32 255)
store i32 %51, i32* %7
%52 = load i32, i32* %7
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 %53
store i8 0, i8* %54
%55 = call i32 (...) @getpid()
%56 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%57 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %55, i8* %56)
br label %47
58: ; preds = %14
%59 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 0
%60 = load i32, i32* %59
%61 = call i32 @close(i32 %60)
br label %62
62: ; preds = %58, %62
%63 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%64 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %63)
%65 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 1
%66 = load i32, i32* %65
%67 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%68 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%69 = call i32 @strlen(i8* %68)
%70 = call i32 @write(i32 %66, i8* %67, i32 %69)
br label %62
}

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