#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 {
  scalar_t__ st_dev;
  scalar_t__ st_ino;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ st_path;
typedef int /*<<< orphan*/ st_fd;

/* Variables and functions */
int EINVAL;
int ENOENT;
int ENOTDIR;
int /*<<< orphan*/ S_ISFIFO(int /*<<< orphan*/);
int errno;
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ memset(struct stat *, int /*<<< orphan*/, int);
scalar_t__ stat(char const *, struct stat *);
__attribute__((visibility("hidden"))) int sd_is_fifo(int fd, const char *path) {
  struct stat st_fd;
  if (fd < 0)
    return -EINVAL;
  memset(&st_fd, 0, sizeof(st_fd));
  if (fstat(fd, &st_fd) < 0)
    return -errno;
  if (!S_ISFIFO(st_fd.st_mode))
    return 0;
  if (path) {
    struct stat st_path;
    memset(&st_path, 0, sizeof(st_path));
    if (stat(path, &st_path) < 0) {
      if (errno == ENOENT || errno == ENOTDIR)
        return 0;
      return -errno;
    }
    return st_path.st_dev == st_fd.st_dev && st_path.st_ino == st_fd.st_ino;
  }
  return 1;
}


x86 assembly (clang, -O0):
<sd_is_fifo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    116c <sd_is_fifo+0x2c>
xor    %eax,%eax
mov    0x2e6e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    127b <sd_is_fifo+0x13b>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0x0,%rax
jge    11a5 <sd_is_fifo+0x65>
xor    %eax,%eax
mov    0x2e3d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    127b <sd_is_fifo+0x13b>
mov    -0x18(%rbp),%edi
call   1030 <S_ISFIFO@plt>
cmp    $0x0,%eax
jne    11c2 <sd_is_fifo+0x82>
movl   $0x0,-0x4(%rbp)
jmp    127b <sd_is_fifo+0x13b>
cmpq   $0x0,-0x10(%rbp)
je     1274 <sd_is_fifo+0x134>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    1243 <sd_is_fifo+0x103>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%eax
je     1224 <sd_is_fifo+0xe4>
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    0x2ddc(%rip),%rcx
cmp    (%rcx),%eax
jne    1230 <sd_is_fifo+0xf0>
movl   $0x0,-0x4(%rbp)
jmp    127b <sd_is_fifo+0x13b>
xor    %eax,%eax
mov    0x2d9f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    127b <sd_is_fifo+0x13b>
mov    -0x40(%rbp),%rcx
xor    %eax,%eax
cmp    -0x28(%rbp),%rcx
mov    %al,-0x41(%rbp)
jne    1264 <sd_is_fifo+0x124>
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
sete   %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
jmp    127b <sd_is_fifo+0x13b>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sd_is_fifo>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
test   %edi,%edi
js     11d2 <sd_is_fifo+0x92>
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x8(%rsp),%r15
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   1060 <fstat@plt>
test   %rax,%rax
js     11db <sd_is_fifo+0x9b>
mov    0x18(%rsp),%edi
call   1030 <S_ISFIFO@plt>
test   %eax,%eax
je     11e6 <sd_is_fifo+0xa6>
test   %r14,%r14
je     11f3 <sd_is_fifo+0xb3>
lea    0x20(%rsp),%rbx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <stat@plt>
test   %rax,%rax
js     11fa <sd_is_fifo+0xba>
mov    0x20(%rsp),%rax
mov    0x28(%rsp),%rcx
xor    0x8(%rsp),%rax
xor    0x10(%rsp),%rcx
xor    %ebx,%ebx
or     %rax,%rcx
sete   %bl
jmp    11e6 <sd_is_fifo+0xa6>
mov    0x2df7(%rip),%rax
jmp    11e2 <sd_is_fifo+0xa2>
mov    0x2df6(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%ebx
jmp    11e6 <sd_is_fifo+0xa6>
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    0x2db6(%rip),%rcx
mov    0x2de7(%rip),%rdx
mov    %eax,%ebx
neg    %ebx
cmp    (%rdx),%eax
cmove  %ebp,%ebx
cmp    (%rcx),%eax
cmove  %ebp,%ebx
jmp    11e6 <sd_is_fifo+0xa6>

LLVM IR (-O0):
%struct.stat = type { i64, i64, i32 }
@EINVAL = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
@ENOTDIR = global i32 0
define hidden i32 @sd_is_fifo(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca %struct.stat
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = load i32, i32* @EINVAL
%12 = sub nsw i32 0, %11
store i32 %12, i32* %3
br label %63
13: ; preds = %2
%14 = call i32 @memset(%struct.stat* %6, i32 0, i32 24)
%15 = load i32, i32* %4
%16 = call i64 @fstat(i32 %15, %struct.stat* %6)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
store i32 %20, i32* %3
br label %63
21: ; preds = %13
%22 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 2
%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 0, i32* %3
br label %63
27: ; preds = %21
%28 = load i8*, i8** %5
%29 = icmp ne i8* %28, null
br i1 %29, label %30, label %62
30: ; preds = %27
%31 = call i32 @memset(%struct.stat* %7, i32 0, i32 24)
%32 = load i8*, i8** %5
%33 = call i64 @stat(i8* %32, %struct.stat* %7)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %47
35: ; preds = %30
%36 = load i32, i32* @errno
%37 = load i32, i32* @ENOENT
%38 = icmp eq i32 %36, %37
br i1 %38, label %43, label %39
39: ; preds = %35
%40 = load i32, i32* @errno
%41 = load i32, i32* @ENOTDIR
%42 = icmp eq i32 %40, %41
br i1 %42, label %43, label %44
43: ; preds = %39, %35
store i32 0, i32* %3
br label %63
44: ; preds = %39
%45 = load i32, i32* @errno
%46 = sub nsw i32 0, %45
store i32 %46, i32* %3
br label %63
47: ; preds = %30
%48 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%49 = load i64, i64* %48
%50 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%51 = load i64, i64* %50
%52 = icmp eq i64 %49, %51
br i1 %52, label %53, label %59
53: ; preds = %47
%54 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 1
%55 = load i64, i64* %54
%56 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%57 = load i64, i64* %56
%58 = icmp eq i64 %55, %57
br label %59
59: ; preds = %53, %47
%60 = phi i1 [ false, %47 ], [ %58, %53 ]
%61 = zext i1 %60 to i32
store i32 %61, i32* %3
br label %63
62: ; preds = %27
store i32 1, i32* %3
br label %63
63: ; preds = %62, %59, %44, %43, %26, %18, %10
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
%struct.stat = type { i64, i64, i32 }
@EINVAL = global i32 0
@errno = global i32 0
@ENOENT = global i32 0
@ENOTDIR = global i32 0
define hidden i32 @sd_is_fifo(i32 %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %3 to i8*
%6 = icmp slt i32 %0, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @EINVAL
%9 = sub nsw i32 0, %8
br label %53
10: ; preds = %2
%11 = call i32 @memset(%struct.stat* %3, i32 0, i32 24)
%12 = call i64 @fstat(i32 %0, %struct.stat* %3)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @errno
%16 = sub nsw i32 0, %15
br label %53
17: ; preds = %10
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 2
%19 = load i32, i32* %18
%20 = call i32 @S_ISFIFO(i32 %19)
%21 = icmp eq i32 %20, 0
br i1 %21, label %53, label %22
22: ; preds = %17
%23 = icmp eq i8* %1, null
br i1 %23, label %53, label %24
24: ; preds = %22
%25 = bitcast %struct.stat* %4 to i8*
%26 = call i32 @memset(%struct.stat* %4, i32 0, i32 24)
%27 = call i64 @stat(i8* %1, %struct.stat* %4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %38
29: ; preds = %24
%30 = load i32, i32* @errno
%31 = load i32, i32* @ENOENT
%32 = icmp eq i32 %30, %31
%33 = load i32, i32* @ENOTDIR
%34 = icmp eq i32 %30, %33
%35 = select i1 %32, i1 true, i1 %34
%36 = sub nsw i32 0, %30
%37 = select i1 %35, i32 0, i32 %36
br label %51
38: ; preds = %24
%39 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%40 = load i64, i64* %39
%41 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%42 = load i64, i64* %41
%43 = icmp eq i64 %40, %42
%44 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%45 = load i64, i64* %44
%46 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%47 = load i64, i64* %46
%48 = icmp eq i64 %45, %47
%49 = select i1 %43, i1 %48, i1 false
%50 = zext i1 %49 to i32
br label %51
51: ; preds = %29, %38
%52 = phi i32 [ %50, %38 ], [ %37, %29 ]
br label %53
53: ; preds = %22, %17, %51, %14, %7
%54 = phi i32 [ %9, %7 ], [ %16, %14 ], [ %52, %51 ], [ 0, %17 ], [ 1, %22 ]
ret i32 %54
}