#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int n, fd[2];
  pid_t pid;
  char line[4096];
  if (pipe(fd) < 0) {
    printf("!!Erro no Pipe"
           "\n");
    exit(0);
  }
  if ((pid = fork()) < 0) {
    printf("!!Erro no Pipe!!\n");
    exit(0);
  } else if (pid > 0) {
    close(fd[0]);
    write(1, "O pai mandou a mensagem para o filho\n", 38);
    write(fd[1], "Bom dia meu filho\n", 18);
  } else {
    close(fd[1]);
    write(1, "Filho esperando ....\n", 20);
    n = read(fd[0], line, 4096);
    write(1, line, n);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x10(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    11a8 <main+0x38>
lea    0xe64(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11d0 <main+0x60>
lea    0xe4c(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    1212 <main+0xa2>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
lea    0xe33(%rip),%rsi
mov    $0x26,%edx
call   1030 <write@plt>
mov    -0xc(%rbp),%edi
lea    0xe45(%rip),%rsi
mov    $0x12,%edx
call   1030 <write@plt>
jmp    125b <main+0xeb>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
lea    0xe35(%rip),%rsi
mov    $0x14,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    %eax,-0x8(%rbp)
lea    -0x1020(%rbp),%rsi
mov    -0x8(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    1260 <main+0xf0>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x1010,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
jns    1190 <main+0x20>
lea    0xed2(%rip),%rdi
jmp    11a3 <main+0x33>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
jns    11af <main+0x3f>
lea    0xeac(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jne    11f2 <main+0x82>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
lea    0xe78(%rip),%rsi
mov    $0x1,%edi
mov    $0x14,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1070 <read@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
jmp    1221 <main+0xb1>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
lea    0xdfe(%rip),%rsi
mov    $0x1,%edi
mov    $0x26,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
lea    0xe0a(%rip),%rsi
mov    $0x12,%edx
call   1040 <write@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"!!Erro no Pipe\0A\00"
@.str.1 = [18 x i8] c"!!Erro no Pipe!!\0A\00"
@.str.2 = [38 x i8] c"O pai mandou a mensagem para o filho\0A\00"
@.str.3 = [19 x i8] c"Bom dia meu filho\0A\00"
@.str.4 = [22 x i8] c"Filho esperando ....\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca i64
%5 = alloca [4096 x i8]
store i32 0, i32* %1
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%7 = call i64 @pipe(i32* %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 0)
unreachable
12: ; preds = %0
%13 = call i64 (...) @fork()
store i64 %13, i64* %4
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %12
%19 = load i64, i64* %4
%20 = icmp sgt i64 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %18
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = call i32 @write(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 38)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @write(i32 %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 18)
br label %41
29: ; preds = %18
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = call i32 @write(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 20)
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%35 = load i32, i32* %34
%36 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%37 = call i32 @read(i32 %35, i8* %36, i32 4096)
store i32 %37, i32* %2
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%39 = load i32, i32* %2
%40 = call i32 @write(i32 1, i8* %38, i32 %39)
br label %41
41: ; preds = %29, %21
br label %42
42: ; preds = %41
%43 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.2 = [38 x i8] c"O pai mandou a mensagem para o filho\0A\00"
@.str.3 = [19 x i8] c"Bom dia meu filho\0A\00"
@.str.4 = [22 x i8] c"Filho esperando ....\0A\00"
@str = [17 x i8] c"!!Erro no Pipe!!\00"
@str.5 = [15 x i8] c"!!Erro no Pipe\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [4096 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [4096 x i8], [4096 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 %8, label %11
8: ; preds = %0
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.5, i64 0, i64 0))
%10 = call i32 @exit(i32 0)
unreachable
11: ; preds = %0
%12 = call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %11
%18 = icmp eq i64 %12, 0
br i1 %18, label %26, label %19
19: ; preds = %17
%20 = load i32, i32* %5
%21 = call i32 @close(i32 %20)
%22 = call i32 @write(i32 1, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 38)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @write(i32 %24, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 18)
br label %34
26: ; preds = %17
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%28 = load i32, i32* %27
%29 = call i32 @close(i32 %28)
%30 = call i32 @write(i32 1, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 20)
%31 = load i32, i32* %5
%32 = call i32 @read(i32 %31, i8* %4, i32 4096)
%33 = call i32 @write(i32 1, i8* %4, i32 %32)
br label %34
34: ; preds = %19, %26
%35 = call i32 @exit(i32 0)
unreachable
}