#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 */
struct stat {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ O_WRONLY;
int PIPE_BUF;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stdin;
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main() {
  struct stat st;
  if (stat("meu_fifo", &st) == -1) {
    printf("Arquivo fifo nao existe. Execute o ./ler para cria-lo\n");
    exit(EXIT_FAILURE);
  }
  int fd = open("meu_fifo", O_WRONLY);
  if (fd == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  while (1) {
    char buf[PIPE_BUF];
    if (fgets(buf, PIPE_BUF, stdin) == NULL) {
      perror("fgets");
      break;
    }
    write(fd, buf, strlen(buf) + 1);
  }
  close(fd);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe5a(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    11d2 <main+0x42>
lea    0xe4a(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
lea    0xe1e(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    120e <main+0x7e>
lea    0xe45(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    1213 <main+0x83>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
mov    0x2d99(%rip),%rax
mov    (%rax),%edx
call   1070 <fgets@plt>
cmp    $0x0,%rax
jne    1278 <main+0xe8>
lea    0xdde(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x3,-0x24(%rbp)
jmp    12a1 <main+0x111>
mov    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%eax
mov    %eax,-0x34(%rbp)
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x24(%rbp),%eax
test   %eax,%eax
je     12bd <main+0x12d>
jmp    12b8 <main+0x128>
jmp    12c2 <main+0x132>
jmp    1213 <main+0x83>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xe5b(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    11c1 <main+0x31>
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
jmp    11e7 <main+0x57>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
lea    0xe2f(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
jne    11f5 <main+0x65>
lea    0xe27(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    %eax,%r14d
mov    %rsp,%r15
mov    0x2dc6(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    (%r12),%esi
mov    0x2dc6(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%r13),%edx
mov    %rbx,%rdi
call   1070 <fgets@plt>
test   %rax,%rax
je     1279 <main+0xe9>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <write@plt>
mov    %r15,%rsp
mov    %rsp,%r15
mov    (%r12),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    (%r12),%esi
jmp    1230 <main+0xa0>
lea    0xd8e(%rip),%rdi
call   10a0 <perror@plt>
mov    %r15,%rsp
mov    %r14d,%edi
call   1060 <close@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"meu_fifo\00"
@.str.1 = [55 x i8] c"Arquivo fifo nao existe. Execute o ./ler para cria-lo\0A\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@PIPE_BUF = global i32 0
@stdin = global i32 0
@.str.3 = [6 x i8] c"fgets\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.stat
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %13
br label %23
23: ; preds = %22, %42
%24 = load i32, i32* @PIPE_BUF
%25 = zext i32 %24 to i64
%26 = call i8* @llvm.stacksave()
store i8* %26, i8** %4
%27 = alloca i8, i64 %25
store i64 %25, i64* %5
%28 = load i32, i32* @PIPE_BUF
%29 = load i32, i32* @stdin
%30 = call i32* @fgets(i8* %27, i32 %28, i32 %29)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %34
32: ; preds = %23
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 3, i32* %6
br label %39
34: ; preds = %23
%35 = load i32, i32* %3
%36 = call i64 @strlen(i8* %27)
%37 = add nsw i64 %36, 1
%38 = call i32 @write(i32 %35, i8* %27, i64 %37)
store i32 0, i32* %6
br label %39
39: ; preds = %34, %32
%40 = load i8*, i8** %4
%41 = load i32, i32* %6
switch i32 %41, label %48 [
i32 0, label %42
i32 3, label %43
]
42: ; preds = %39
br label %23
43: ; preds = %39
%44 = load i32, i32* %3
%45 = call i32 @close(i32 %44)
%46 = load i32, i32* @EXIT_SUCCESS
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %39
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"meu_fifo\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@PIPE_BUF = global i32 0
@stdin = global i32 0
@.str.3 = [6 x i8] c"fgets\00"
@EXIT_SUCCESS = global i32 0
@str = [54 x i8] c"Arquivo fifo nao existe. Execute o ./ler para cria-lo\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = call i32 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %0
%10 = load i32, i32* @O_WRONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %22, label %13
13: ; preds = %9
%14 = load i32, i32* @PIPE_BUF
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
%17 = alloca i8, i64 %15
%18 = load i32, i32* @PIPE_BUF
%19 = load i32, i32* @stdin
%20 = call i32* @fgets(i8* %17, i32 %18, i32 %19)
%21 = icmp eq i32* %20, null
br i1 %21, label %40, label %26
22: ; preds = %9
%23 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %13, %26
%27 = phi i8* [ %35, %26 ], [ %17, %13 ]
%28 = phi i8* [ %34, %26 ], [ %16, %13 ]
%29 = call i64 @strlen(i8* %27)
%30 = add nsw i64 %29, 1
%31 = call i32 @write(i32 %11, i8* %27, i64 %30)
%32 = load i32, i32* @PIPE_BUF
%33 = zext i32 %32 to i64
%34 = call i8* @llvm.stacksave()
%35 = alloca i8, i64 %33
%36 = load i32, i32* @PIPE_BUF
%37 = load i32, i32* @stdin
%38 = call i32* @fgets(i8* %35, i32 %36, i32 %37)
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %26
40: ; preds = %26, %13
%41 = phi i8* [ %16, %13 ], [ %34, %26 ]
%42 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @close(i32 %11)
%44 = load i32, i32* @EXIT_SUCCESS
%45 = call i32 @exit(i32 %44)
unreachable
}