#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 EINVAL;
int O_CLOEXEC;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ S_ISFIFO(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ fstat(int, struct stat *);
int open(char const *, int);
int ls_fifo_open(int *fd, const char *fifo) {
  int saved_errno;
  if (*fd >= 0)
    return 0;
  if (!fifo)
    return 0;
  *fd = open(fifo, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
  if (*fd < 0)
    goto error;
  struct stat sb;
  if (fstat(*fd, &sb) < 0)
    goto error;
  if (!S_ISFIFO(sb.st_mode)) {
    errno = -EINVAL;
    goto error;
  }
  return 0;
error:
  saved_errno = errno;
  close(*fd);
  *fd = -1;
  errno = saved_errno;
  return -1;
}


x86 assembly (clang, -O0):
<ls_fifo_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jl     1169 <ls_fifo_open+0x29>
movl   $0x0,-0x4(%rbp)
jmp    1246 <ls_fifo_open+0x106>
cmpq   $0x0,-0x18(%rbp)
jne    1180 <ls_fifo_open+0x40>
movl   $0x0,-0x4(%rbp)
jmp    1246 <ls_fifo_open+0x106>
mov    -0x18(%rbp),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    0x2e24(%rip),%rax
or     (%rax),%esi
mov    0x2e4b(%rip),%rax
or     (%rax),%esi
call   1050 <open@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jge    11be <ls_fifo_open+0x7e>
jmp    1212 <ls_fifo_open+0xd2>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
jge    11dc <ls_fifo_open+0x9c>
jmp    1212 <ls_fifo_open+0xd2>
mov    -0x20(%rbp),%edi
call   1030 <S_ISFIFO@plt>
cmp    $0x0,%eax
jne    1206 <ls_fifo_open+0xc6>
xor    %ecx,%ecx
mov    0x2dd2(%rip),%rax
sub    (%rax),%ecx
mov    0x2dd1(%rip),%rax
mov    %ecx,(%rax)
jmp    1212 <ls_fifo_open+0xd2>
movl   $0x0,-0x4(%rbp)
jmp    1246 <ls_fifo_open+0x106>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
mov    -0x1c(%rbp),%ecx
mov    0x2d93(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ls_fifo_open>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xor    %ebp,%ebp
test   %rsi,%rsi
je     11d7 <ls_fifo_open+0x97>
mov    %rdi,%rbx
cmpl   $0x0,(%rdi)
jns    11d7 <ls_fifo_open+0x97>
mov    0x2e7e(%rip),%rcx
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e74(%rip),%rcx
or     (%rcx),%eax
mov    %rsi,%rdi
mov    %eax,%esi
call   1050 <open@plt>
mov    %eax,(%rbx)
test   %eax,%eax
js     11b8 <ls_fifo_open+0x78>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
test   %rax,%rax
js     11b8 <ls_fifo_open+0x78>
mov    0x8(%rsp),%edi
call   1030 <S_ISFIFO@plt>
test   %eax,%eax
jne    11d7 <ls_fifo_open+0x97>
mov    0x2e1d(%rip),%rax
xor    %ecx,%ecx
sub    (%rax),%ecx
mov    0x2e1a(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e11(%rip),%r14
mov    (%r14),%ebp
mov    (%rbx),%edi
call   1040 <close@plt>
movl   $0xffffffff,(%rbx)
mov    %ebp,(%r14)
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@O_NONBLOCK = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @ls_fifo_open(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca %struct.stat
store i32* %0, i32** %4
store i8* %1, i8** %5
%8 = load i32*, i32** %4
%9 = load i32, i32* %8
%10 = icmp sge i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 0, i32* %3
br label %51
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %12
store i32 0, i32* %3
br label %51
16: ; preds = %12
%17 = load i8*, i8** %5
%18 = load i32, i32* @O_RDONLY
%19 = load i32, i32* @O_CLOEXEC
%20 = or i32 %18, %19
%21 = load i32, i32* @O_NONBLOCK
%22 = or i32 %20, %21
%23 = call i32 @open(i8* %17, i32 %22)
%24 = load i32*, i32** %4
store i32 %23, i32* %24
%25 = load i32*, i32** %4
%26 = load i32, i32* %25
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %16
br label %44
29: ; preds = %16
%30 = load i32*, i32** %4
%31 = load i32, i32* %30
%32 = call i64 @fstat(i32 %31, %struct.stat* %7)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
br label %44
35: ; preds = %29
%36 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @S_ISFIFO(i32 %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %35
%41 = load i32, i32* @EINVAL
%42 = sub nsw i32 0, %41
store i32 %42, i32* @errno
br label %44
43: ; preds = %35
store i32 0, i32* %3
br label %51
44: ; preds = %40, %34, %28
%45 = load i32, i32* @errno
store i32 %45, i32* %6
%46 = load i32*, i32** %4
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
%49 = load i32*, i32** %4
store i32 -1, i32* %49
%50 = load i32, i32* %6
store i32 %50, i32* @errno
store i32 -1, i32* %3
br label %51
51: ; preds = %44, %43, %15, %11
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
@O_NONBLOCK = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
define i32 @ls_fifo_open(i32* nocapture %0, i8* %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* %0
%5 = icmp sgt i32 %4, -1
%6 = icmp eq i8* %1, null
%7 = or i1 %6, %5
br i1 %7, label %31, label %8
8: ; preds = %2
%9 = load i32, i32* @O_RDONLY
%10 = load i32, i32* @O_CLOEXEC
%11 = or i32 %10, %9
%12 = load i32, i32* @O_NONBLOCK
%13 = or i32 %11, %12
%14 = tail call i32 @open(i8* %1, i32 %13)
store i32 %14, i32* %0
%15 = icmp slt i32 %14, 0
br i1 %15, label %27, label %16
16: ; preds = %8
%17 = call i64 @fstat(i32 %14, %struct.stat* %3)
%18 = icmp slt i64 %17, 0
br i1 %18, label %27, label %19
19: ; preds = %16
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = tail call i32 @S_ISFIFO(i32 %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %19
%25 = load i32, i32* @EINVAL
%26 = sub nsw i32 0, %25
store i32 %26, i32* @errno
br label %27
27: ; preds = %16, %8, %24
%28 = load i32, i32* @errno
%29 = load i32, i32* %0
%30 = tail call i32 @close(i32 %29)
store i32 -1, i32* %0
store i32 %28, i32* @errno
br label %31
31: ; preds = %19, %2, %27
%32 = phi i32 [ -1, %27 ], [ 0, %2 ], [ 0, %19 ]
ret i32 %32
}