#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*/ SIGKILL;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ kill(scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ pause();
scalar_t__ pipe(int *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int n, fd[2];
  pid_t pid;
  char line[128];
  char msg[9] = {'B', 'o', 'm', ' ', 'd', 'i', 'a', '\n', '\0'};
  if (pipe(fd) < 0) {
    fprintf(stderr, "Erro no tubo\n");
    _exit(1);
  }
  if ((pid = fork()) < 0) {
    fprintf(stderr, "Erro no fork\n");
    _exit(1);
  }
  if (pid > 0) {
    close(fd[1]);
    n = read(fd[0], line, 128);
    write(1, &line[0], n);
    close(fd[0]);
    kill(pid, SIGKILL);
    _exit(0);
  }
  if (pid == 0) {
    close(fd[0]);
    write(fd[1], &msg[0], 9);
    close(fd[1]);
    pause();
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe57(%rip),%rax
mov    %rax,-0xa9(%rbp)
mov    0xe52(%rip),%al
mov    %al,-0xa1(%rbp)
lea    -0x10(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
jge    11ee <main+0x5e>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    1222 <main+0x92>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xe04(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
cmpq   $0x0,-0x18(%rbp)
jle    1281 <main+0xf1>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,-0x8(%rbp)
lea    -0xa0(%rbp),%rsi
mov    -0x8(%rbp),%edx
mov    $0x1,%edi
call   1040 <write@plt>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
call   1090 <kill@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12b7 <main+0x127>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0xc(%rbp),%edi
lea    -0xa9(%rbp),%rsi
mov    $0x9,%edx
call   1040 <write@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    $0x0,%al
call   10a0 <pause@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0xa8,%rsp
movabs $0xa616964206d6f42,%rax
69 61 0a
mov    %rax,0x10(%rsp)
movb   $0x0,0x18(%rsp)
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
js     1204 <main+0x74>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1216 <main+0x86>
jne    1235 <main+0xa5>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0xc(%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x9,%edx
call   1040 <write@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
xor    %eax,%eax
call   10a0 <pause@plt>
xor    %eax,%eax
add    $0xa8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
jmp    1226 <main+0x96>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    %rax,%rbx
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    0x8(%rsp),%edi
lea    0x20(%rsp),%r14
mov    %r14,%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x8(%rsp),%edi
call   1050 <close@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <kill@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@__const.main.msg = [9 x i8] c"Bom dia\0A\00"
@stderr = global i32 0
@.str = [14 x i8] c"Erro no tubo\0A\00"
@.str.1 = [14 x i8] c"Erro no fork\0A\00"
@SIGKILL = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x i32]
%4 = alloca i64
%5 = alloca [128 x i8]
%6 = alloca [9 x i8]
store i32 0, i32* %1
%7 = bitcast [9 x i8]* %6 to i8*
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @_exit(i32 1)
unreachable
15: ; preds = %0
%16 = call i64 (...) @fork()
store i64 %16, i64* %4
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @_exit(i32 1)
unreachable
22: ; preds = %15
%23 = load i64, i64* %4
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %43
25: ; preds = %22
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%30 = load i32, i32* %29
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%32 = call i32 @read(i32 %30, i8* %31, i32 128)
store i32 %32, i32* %2
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%34 = load i32, i32* %2
%35 = call i32 @write(i32 1, i8* %33, i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = load i64, i64* %4
%40 = load i32, i32* @SIGKILL
%41 = call i32 @kill(i64 %39, i32 %40)
%42 = call i32 @_exit(i32 0)
unreachable
43: ; preds = %22
%44 = load i64, i64* %4
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %58
46: ; preds = %43
%47 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%48 = load i32, i32* %47
%49 = call i32 @close(i32 %48)
%50 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%51 = load i32, i32* %50
%52 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%53 = call i32 @write(i32 %51, i8* %52, i32 9)
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%55 = load i32, i32* %54
%56 = call i32 @close(i32 %55)
%57 = call i32 (...) @pause()
br label %58
58: ; preds = %46, %43
ret i32 0
}

LLVM IR (-O2):
@__const.main.msg = [9 x i8] c"Bom dia\0A\00"
@stderr = global i32 0
@.str = [14 x i8] c"Erro no tubo\0A\00"
@.str.1 = [14 x i8] c"Erro no fork\0A\00"
@SIGKILL = global i32 0
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [128 x i8]
%3 = alloca [9 x i8]
%4 = bitcast [2 x i32]* %1 to i8*
%5 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @_exit(i32 1)
unreachable
14: ; preds = %0
%15 = call i64 (...) @fork()
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, 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 = icmp eq i64 %15, 0
br i1 %22, label %35, label %23
23: ; preds = %21
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%25 = load i32, i32* %24
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %7
%28 = call i32 @read(i32 %27, i8* %5, i32 128)
%29 = call i32 @write(i32 1, i8* %5, i32 %28)
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* @SIGKILL
%33 = call i32 @kill(i64 %15, i32 %32)
%34 = call i32 @_exit(i32 0)
unreachable
35: ; preds = %21
%36 = load i32, i32* %7
%37 = call i32 @close(i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @write(i32 %39, i8* %6, i32 9)
%41 = load i32, i32* %38
%42 = call i32 @close(i32 %41)
%43 = call i32 (...) @pause()
ret i32 0
}