#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 */
int O_NONBLOCK;
int O_RDWR;
int S_IRUSR;
int /*<<< orphan*/ S_ISFIFO(int /*<<< orphan*/);
int S_IWUSR;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
scalar_t__ mkfifo(char const *, int);
int open(char const *, int);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
int open_fifo(const char *filename) {
  int fd = -1;
  struct stat ss;
  if (NULL == filename)
    return 0;
  if (0 == stat(filename, &ss)) {
    if (!S_ISFIFO(ss.st_mode)) {
      fprintf(stderr, "File [%s] exists, but is not a fifo.\n", filename);
      return 0;
    }
  } else {
    if (0 != mkfifo(filename, S_IRUSR | S_IWUSR)) {
      fprintf(stderr, "Failed to create new fifo [%s]\n", filename);
      return 0;
    }
  }
  fd = open(filename, O_RDWR | O_NONBLOCK);
  if (0 >= fd) {
    fprintf(stderr, "Failed to open fifo [%s] for writing.\n", filename);
    return 0;
  }
  return fd;
}


x86 assembly (clang, -O0):
<open_fifo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    117b <open_fifo+0x2b>
movl   $0x0,-0x4(%rbp)
jmp    1278 <open_fifo+0x128>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11d1 <open_fifo+0x81>
mov    -0x18(%rbp),%edi
call   1030 <S_ISFIFO@plt>
cmp    $0x0,%eax
jne    11cc <open_fifo+0x7c>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe45(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1278 <open_fifo+0x128>
jmp    1224 <open_fifo+0xd4>
mov    -0x10(%rbp),%rdi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    0x2df3(%rip),%rax
or     (%rax),%esi
call   1050 <mkfifo@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     121f <open_fifo+0xcf>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe18(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1278 <open_fifo+0x128>
jmp    1224 <open_fifo+0xd4>
mov    -0x10(%rbp),%rdi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    0x2da8(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x14(%rbp),%eax
jl     1272 <open_fifo+0x122>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xde5(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1278 <open_fifo+0x128>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_fifo>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
test   %rdi,%rdi
je     11e8 <open_fifo+0x98>
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
je     119a <open_fifo+0x4a>
mov    0x2e47(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1050 <mkfifo@plt>
test   %rax,%rax
je     11a7 <open_fifo+0x57>
lea    0xe8e(%rip),%rsi
jmp    11d5 <open_fifo+0x85>
mov    0x8(%rsp),%edi
call   1030 <S_ISFIFO@plt>
test   %eax,%eax
je     11ce <open_fifo+0x7e>
mov    0x2e3a(%rip),%rax
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
jg     11e8 <open_fifo+0x98>
lea    0xe7a(%rip),%rsi
jmp    11d5 <open_fifo+0x85>
lea    0xe2b(%rip),%rsi
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [38 x i8] c"File [%s] exists, but is not a fifo.\0A\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [32 x i8] c"Failed to create new fifo [%s]\0A\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [39 x i8] c"Failed to open fifo [%s] for writing.\0A\00"
define i32 @open_fifo(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
store i8* %0, i8** %3
store i32 -1, i32* %4
%6 = load i8*, i8** %3
%7 = icmp eq i8* null, %6
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 0, i32* %2
br label %49
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i64 @stat(i8* %10, %struct.stat* %5)
%12 = icmp eq i64 0, %11
br i1 %12, label %13, label %23
13: ; preds = %9
%14 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @S_ISFIFO(i32 %15)
%17 = icmp ne i32 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %3
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 0, i32* %2
br label %49
22: ; preds = %13
br label %35
23: ; preds = %9
%24 = load i8*, i8** %3
%25 = load i32, i32* @S_IRUSR
%26 = load i32, i32* @S_IWUSR
%27 = or i32 %25, %26
%28 = call i64 @mkfifo(i8* %24, i32 %27)
%29 = icmp ne i64 0, %28
br i1 %29, label %30, label %34
30: ; preds = %23
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %3
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %32)
store i32 0, i32* %2
br label %49
34: ; preds = %23
br label %35
35: ; preds = %34, %22
%36 = load i8*, i8** %3
%37 = load i32, i32* @O_RDWR
%38 = load i32, i32* @O_NONBLOCK
%39 = or i32 %37, %38
%40 = call i32 @open(i8* %36, i32 %39)
store i32 %40, i32* %4
%41 = load i32, i32* %4
%42 = icmp sge i32 0, %41
br i1 %42, label %43, label %47
43: ; preds = %35
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %3
%46 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %45)
store i32 0, i32* %2
br label %49
47: ; preds = %35
%48 = load i32, i32* %4
store i32 %48, i32* %2
br label %49
49: ; preds = %47, %43, %30, %18, %8
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [38 x i8] c"File [%s] exists, but is not a fifo.\0A\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [32 x i8] c"Failed to create new fifo [%s]\0A\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.2 = [39 x i8] c"Failed to open fifo [%s] for writing.\0A\00"
define i32 @open_fifo(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = icmp eq i8* %0, null
br i1 %4, label %29, label %5
5: ; preds = %1
%6 = call i64 @stat(i8* %0, %struct.stat* %2)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %5
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = call i32 @S_ISFIFO(i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %25, label %19
13: ; preds = %5
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %15, %14
%17 = call i64 @mkfifo(i8* %0, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %13, %8
%20 = load i32, i32* @O_RDWR
%21 = load i32, i32* @O_NONBLOCK
%22 = or i32 %21, %20
%23 = call i32 @open(i8* %0, i32 %22)
%24 = icmp slt i32 %23, 1
br i1 %24, label %25, label %29
25: ; preds = %19, %13, %8
%26 = phi i8* [ getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), %8 ], [ getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), %13 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), %19 ]
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* %26, i8* %0)
br label %29
29: ; preds = %25, %19, %1
%30 = phi i32 [ 0, %1 ], [ %23, %19 ], [ 0, %25 ]
ret i32 %30
}