#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 */
scalar_t__ ENOENT;
int /*<<< orphan*/ EPOLLIN;
int /*<<< orphan*/ F_OK;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ S_IRWXU;
int access(char *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fatal(char *);
scalar_t__ mkfifo(char *, int /*<<< orphan*/);
int open(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ register_epoll(int, int, int /*<<< orphan*/);
int open_fifo(char *path, int epfd) {
  if (access(path, F_OK) == -1) {
    if (errno != ENOENT) {
      fatal("access() failed");
    }
    if (mkfifo(path, S_IRWXU)) {
      fatal("mkfifo() failed");
    }
  }
  int fd = open(path, O_RDONLY | O_NONBLOCK, 0);
  if (fd == -1) {
    fatal("open() on fifo failed");
  }
  register_epoll(epfd, fd, EPOLLIN);
  return fd;
}


x86 assembly (clang, -O0):
<open_fifo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
cmp    $0xffffffff,%eax
jne    11cd <open_fifo+0x7d>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
je     11a0 <open_fifo+0x50>
lea    0xe65(%rip),%rdi
call   1070 <fatal@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
call   1030 <mkfifo@plt>
cmp    $0x0,%rax
je     11c8 <open_fifo+0x78>
lea    0xe4d(%rip),%rdi
call   1070 <fatal@plt>
jmp    11cd <open_fifo+0x7d>
mov    -0x8(%rbp),%rdi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    0x2dff(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1040 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1203 <open_fifo+0xb3>
lea    0xe22(%rip),%rdi
call   1070 <fatal@plt>
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
mov    0x2dc8(%rip),%rax
mov    (%rax),%edx
call   1060 <register_epoll@plt>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_fifo>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
cmp    $0xffffffff,%eax
jne    11af <open_fifo+0x5f>
mov    0x2e4e(%rip),%rax
mov    (%rax),%rax
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
je     118d <open_fifo+0x3d>
lea    0xe78(%rip),%rdi
call   1070 <fatal@plt>
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1030 <mkfifo@plt>
test   %rax,%rax
je     11af <open_fifo+0x5f>
lea    0xe66(%rip),%rdi
call   1070 <fatal@plt>
mov    0x2e1a(%rip),%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1040 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11de <open_fifo+0x8e>
lea    0xe47(%rip),%rdi
call   1070 <fatal@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %ebx,%esi
call   1060 <register_epoll@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@F_OK = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str = [16 x i8] c"access() failed\00"
@S_IRWXU = global i32 0
@.str.1 = [16 x i8] c"mkfifo() failed\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [22 x i8] c"open() on fifo failed\00"
@EPOLLIN = global i32 0
define i32 @open_fifo(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = load i8*, i8** %3
%7 = load i32, i32* @F_OK
%8 = call i32 @access(i8* %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %24
10: ; preds = %2
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp ne i64 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = load i8*, i8** %3
%18 = load i32, i32* @S_IRWXU
%19 = call i64 @mkfifo(i8* %17, i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %16
br label %24
24: ; preds = %23, %2
%25 = load i8*, i8** %3
%26 = load i32, i32* @O_RDONLY
%27 = load i32, i32* @O_NONBLOCK
%28 = or i32 %26, %27
%29 = call i32 @open(i8* %25, i32 %28, i32 0)
store i32 %29, i32* %5
%30 = load i32, i32* %5
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %24
%33 = call i32 @fatal(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %24
%35 = load i32, i32* %4
%36 = load i32, i32* %5
%37 = load i32, i32* @EPOLLIN
%38 = call i32 @register_epoll(i32 %35, i32 %36, i32 %37)
%39 = load i32, i32* %5
ret i32 %39
}

LLVM IR (-O2):
@F_OK = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str = [16 x i8] c"access() failed\00"
@S_IRWXU = global i32 0
@.str.1 = [16 x i8] c"mkfifo() failed\00"
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [22 x i8] c"open() on fifo failed\00"
@EPOLLIN = global i32 0
define i32 @open_fifo(i8* %0, i32 %1) {
%3 = load i32, i32* @F_OK
%4 = tail call i32 @access(i8* %0, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %18
6: ; preds = %2
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %6
%13 = load i32, i32* @S_IRWXU
%14 = tail call i64 @mkfifo(i8* %0, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = tail call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %12, %16, %2
%19 = load i32, i32* @O_RDONLY
%20 = load i32, i32* @O_NONBLOCK
%21 = or i32 %20, %19
%22 = tail call i32 @open(i8* %0, i32 %21, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %18
%25 = tail call i32 @fatal(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %18
%27 = load i32, i32* @EPOLLIN
%28 = tail call i32 @register_epoll(i32 %1, i32 %22, i32 %27)
ret i32 %22
}