#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 /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ EEXIST;
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int O_EXCL;
int O_NONBLOCK;
int O_RDWR;
int S_IFIFO;
int /*<<< orphan*/ S_ISFIFO(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ mkfifo(char const *, int);
int open(char const *, int);
int stat(char const *, struct stat *);
int open_fifo(const char *filename) {
  struct stat sb;
  int fd;
  if (mkfifo(filename, S_IFIFO | 0600) != 0) {
    if (errno == EEXIST) {
      if (stat(filename, &sb) == -1) {
        errno = EEXIST;
        return -1;
      } else if (!S_ISFIFO(sb.st_mode)) {
        return -1;
      }
    } else {
      return -1;
    }
  }
  fd = open(filename, O_RDWR | O_EXCL | O_NONBLOCK);
  if (fd == -1)
    return -1;
  fcntl(fd, F_SETFD, FD_CLOEXEC);
  return fd;
}


x86 assembly (clang, -O0):
<open_fifo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
or     $0x180,%esi
call   1050 <mkfifo@plt>
cmp    $0x0,%rax
je     1206 <open_fifo+0xb6>
mov    0x2e2b(%rip),%rax
mov    (%rax),%rax
mov    0x2e41(%rip),%rcx
cmp    (%rcx),%rax
jne    11f5 <open_fifo+0xa5>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    11ce <open_fifo+0x7e>
mov    0x2e1b(%rip),%rax
mov    (%rax),%rcx
mov    0x2df1(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <open_fifo+0x113>
mov    -0x18(%rbp),%edi
call   1040 <S_ISFIFO@plt>
cmp    $0x0,%eax
jne    11eb <open_fifo+0x9b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <open_fifo+0x113>
jmp    11f0 <open_fifo+0xa0>
jmp    1201 <open_fifo+0xb1>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <open_fifo+0x113>
jmp    1206 <open_fifo+0xb6>
mov    -0x10(%rbp),%rdi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    0x2dae(%rip),%rax
or     (%rax),%esi
mov    0x2dbd(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1243 <open_fifo+0xf3>
movl   $0xffffffff,-0x4(%rbp)
jmp    1263 <open_fifo+0x113>
mov    -0x1c(%rbp),%edi
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
mov    0x2d62(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_fifo>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e46(%rip),%rax
mov    $0x180,%esi
or     (%rax),%esi
call   1050 <mkfifo@plt>
test   %rax,%rax
je     11ab <open_fifo+0x5b>
mov    0x2e36(%rip),%r15
mov    (%r15),%rax
mov    0x2e4c(%rip),%r12
mov    $0xffffffff,%ebx
cmp    (%r12),%rax
jne    11fe <open_fifo+0xae>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     11f7 <open_fifo+0xa7>
mov    (%rsp),%edi
call   1040 <S_ISFIFO@plt>
test   %eax,%eax
je     11fe <open_fifo+0xae>
mov    0x2e36(%rip),%rax
mov    0x2e0f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e1c(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11f0 <open_fifo+0xa0>
mov    %eax,%ebx
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    0x2dd3(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
call   1030 <fcntl@plt>
jmp    11fe <open_fifo+0xae>
mov    $0xffffffff,%ebx
jmp    11fe <open_fifo+0xae>
mov    (%r12),%rax
mov    %rax,(%r15)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IFIFO = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @open_fifo(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @S_IFIFO
%8 = or i32 %7, 384
%9 = call i64 @mkfifo(i8* %6, i32 %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %31
11: ; preds = %1
%12 = load i64, i64* @errno
%13 = load i64, i64* @EEXIST
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %29
15: ; preds = %11
%16 = load i8*, i8** %3
%17 = call i32 @stat(i8* %16, %struct.stat* %4)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i64, i64* @EEXIST
store i64 %20, i64* @errno
store i32 -1, i32* %2
br label %48
21: ; preds = %15
%22 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @S_ISFIFO(i32 %23)
%25 = icmp ne i32 %24, 0
br i1 %25, label %27, label %26
26: ; preds = %21
store i32 -1, i32* %2
br label %48
27: ; preds = %21
br label %28
28: ; preds = %27
br label %30
29: ; preds = %11
store i32 -1, i32* %2
br label %48
30: ; preds = %28
br label %31
31: ; preds = %30, %1
%32 = load i8*, i8** %3
%33 = load i32, i32* @O_RDWR
%34 = load i32, i32* @O_EXCL
%35 = or i32 %33, %34
%36 = load i32, i32* @O_NONBLOCK
%37 = or i32 %35, %36
%38 = call i32 @open(i8* %32, i32 %37)
store i32 %38, i32* %5
%39 = load i32, i32* %5
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %42
41: ; preds = %31
store i32 -1, i32* %2
br label %48
42: ; preds = %31
%43 = load i32, i32* %5
%44 = load i32, i32* @F_SETFD
%45 = load i32, i32* @FD_CLOEXEC
%46 = call i32 @fcntl(i32 %43, i32 %44, i32 %45)
%47 = load i32, i32* %5
store i32 %47, i32* %2
br label %48
48: ; preds = %42, %41, %29, %26, %19
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IFIFO = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
define i32 @open_fifo(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @S_IFIFO
%5 = or i32 %4, 384
%6 = tail call i64 @mkfifo(i8* %0, i32 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %22, label %8
8: ; preds = %1
%9 = load i64, i64* @errno
%10 = load i64, i64* @EEXIST
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %34
12: ; preds = %8
%13 = call i32 @stat(i8* %0, %struct.stat* %2)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = load i64, i64* @EEXIST
store i64 %16, i64* @errno
br label %34
17: ; preds = %12
%18 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i32 @S_ISFIFO(i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %17, %1
%23 = load i32, i32* @O_RDWR
%24 = load i32, i32* @O_EXCL
%25 = or i32 %24, %23
%26 = load i32, i32* @O_NONBLOCK
%27 = or i32 %25, %26
%28 = tail call i32 @open(i8* %0, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %34, label %30
30: ; preds = %22
%31 = load i32, i32* @F_SETFD
%32 = load i32, i32* @FD_CLOEXEC
%33 = tail call i32 @fcntl(i32 %28, i32 %31, i32 %32)
br label %34
34: ; preds = %22, %8, %17, %30, %15
%35 = phi i32 [ -1, %15 ], [ %28, %30 ], [ -1, %17 ], [ -1, %8 ], [ -1, %22 ]
ret i32 %35
}