#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_RDONLY;
int PIPE_BUF;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int mkfifo(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, char **, int);
int stat(char *, struct stat *);
int main() {
  struct stat st;
  if (stat("meu_fifo", &st) == -1) {
    if (mkfifo("meu_fifo", 0666) == -1) {
      perror("mkfifo");
      exit(EXIT_FAILURE);
    }
    printf("fifo criado\n");
  } else {
    printf("fifo meu_fifo previamente criado\n");
  }
  int fd = open("meu_fifo", O_RDONLY);
  if (fd == -1) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  char buf[PIPE_BUF];
  while (1) {
    if (read(fd, &buf, PIPE_BUF) == 0) {
      printf("Fifo fechado na outra ponta\n");
      break;
    }
    printf("Lido: %s", buf);
  }
  close(fd);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    11ef <main+0x6f>
lea    0xe51(%rip),%rdi
mov    $0x1b6,%esi
call   1060 <mkfifo@plt>
cmp    $0xffffffff,%eax
jne    11dc <main+0x5c>
lea    0xe40(%rip),%rdi
call   1090 <perror@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11fd <main+0x7d>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
lea    0xdf3(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1239 <main+0xb9>
lea    0xe19(%rip),%rdi
call   1090 <perror@plt>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
jmp    126a <main+0xea>
mov    -0x28(%rbp),%rsi
mov    -0xc(%rbp),%edi
mov    0x2d50(%rip),%rax
mov    (%rax),%edx
call   1050 <read@plt>
cmp    $0x0,%rax
jne    129c <main+0x11c>
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12b3 <main+0x133>
mov    -0x28(%rbp),%rsi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1265 <main+0xe5>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xe5a(%rip),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    11d3 <main+0x43>
lea    0xe45(%rip),%rdi
mov    $0x1b6,%esi
call   1070 <mkfifo@plt>
cmp    $0xffffffff,%eax
jne    11dc <main+0x4c>
lea    0xe38(%rip),%rdi
jmp    1209 <main+0x79>
lea    0xe44(%rip),%rdi
jmp    11e3 <main+0x53>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
lea    0xe08(%rip),%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
jne    121c <main+0x8c>
lea    0xe07(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    %eax,%r14d
mov    0x2da2(%rip),%r12
mov    (%r12),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r14d,%edi
mov    %rbx,%rsi
call   1060 <read@plt>
test   %rax,%rax
je     1281 <main+0xf1>
lea    0xdc0(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r12),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1060 <read@plt>
test   %rax,%rax
jne    1260 <main+0xd0>
lea    0xdb7(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1050 <close@plt>
mov    0x2d34(%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 = [7 x i8] c"mkfifo\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [13 x i8] c"fifo criado\0A\00"
@.str.3 = [34 x i8] c"fifo meu_fifo previamente criado\0A\00"
@O_RDONLY = global i32 0
@.str.4 = [5 x i8] c"open\00"
@PIPE_BUF = global i32 0
@.str.5 = [29 x i8] c"Fifo fechado na outra ponta\0A\00"
@.str.6 = [9 x i8] c"Lido: %s\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
store i32 0, i32* %1
%6 = call i32 @stat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %17
8: ; preds = %0
%9 = call i32 @mkfifo(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 438)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %8
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %19
17: ; preds = %0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %17, %15
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 %21, i32* %3
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %19
%29 = load i32, i32* @PIPE_BUF
%30 = zext i32 %29 to i64
%31 = call i8* @llvm.stacksave()
store i8* %31, i8** %4
%32 = alloca i8, i64 %30
store i64 %30, i64* %5
br label %33
33: ; preds = %42, %28
br label %34
34: ; preds = %33
%35 = load i32, i32* %3
%36 = bitcast i8* %32 to i8**
%37 = load i32, i32* @PIPE_BUF
%38 = call i64 @read(i32 %35, i8** %36, i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
br label %44
42: ; preds = %34
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %32)
br label %33
44: ; preds = %40
%45 = load i32, i32* %3
%46 = call i32 @close(i32 %45)
%47 = load i32, i32* @EXIT_SUCCESS
%48 = call i32 @exit(i32 %47)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"meu_fifo\00"
@.str.1 = [7 x i8] c"mkfifo\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.4 = [5 x i8] c"open\00"
@PIPE_BUF = global i32 0
@.str.6 = [9 x i8] c"Lido: %s\00"
@EXIT_SUCCESS = global i32 0
@str = [33 x i8] c"fifo meu_fifo previamente criado\00"
@str.7 = [28 x i8] c"Fifo fechado na outra ponta\00"
@str.8 = [12 x i8] c"fifo criado\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 %12
5: ; preds = %0
%6 = tail call i32 @mkfifo(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 438)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %12
8: ; preds = %5
%9 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %0, %5
%13 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @str.8, i64 0, i64 0), %5 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0), %0 ]
%14 = tail call i32 @puts(i8* dereferenceable(1) %13)
%15 = load i32, i32* @O_RDONLY
%16 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %12
%23 = load i32, i32* @PIPE_BUF
%24 = zext i32 %23 to i64
%25 = alloca i8, i64 %24
%26 = bitcast i8* %25 to i8**
%27 = call i64 @read(i32 %16, i8** %26, i32 %23)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %34, %22
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
%31 = call i32 @close(i32 %16)
%32 = load i32, i32* @EXIT_SUCCESS
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %22, %34
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %25)
%36 = load i32, i32* @PIPE_BUF
%37 = call i64 @read(i32 %16, i8** %26, i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %29, label %34
}