#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 st_mode;
};
typedef int mode_t;

/* Variables and functions */
int S_IFMT;
int /*<<< orphan*/ __set_errno(int);
int errno;
int fstat(int, struct stat *);
int isfdtype(int fildes, int fdtype) {
  struct stat st;
  int result;
  {
    int save_error = errno;
    result = fstat(fildes, &st);
    __set_errno(save_error);
  }
  return result ?: (st.st_mode & S_IFMT) == (mode_t)fdtype;
}


x86 assembly (clang, -O0):
<isfdtype>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1040 <fstat@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <__set_errno@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     116b <isfdtype+0x4b>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1185 <isfdtype+0x65>
mov    -0x10(%rbp),%eax
mov    0x2e6b(%rip),%rcx
and    (%rcx),%eax
cmp    -0x8(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isfdtype>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    0x2ea6(%rip),%rax
mov    (%rax),%ebx
lea    0x8(%rsp),%rsi
call   1040 <fstat@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <__set_errno@plt>
test   %ebp,%ebp
jne    1161 <isfdtype+0x41>
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
xor    %ebp,%ebp
cmp    %r14d,%eax
sete   %bpl
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i32 0
@S_IFMT = global i32 0
define i32 @isfdtype(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = load i32, i32* @errno
store i32 %8, i32* %7
%9 = load i32, i32* %3
%10 = call i32 @fstat(i32 %9, %struct.stat* %5)
store i32 %10, i32* %6
%11 = load i32, i32* %7
%12 = call i32 @__set_errno(i32 %11)
%13 = load i32, i32* %6
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
br label %24
16: ; preds = %2
%17 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load i32, i32* @S_IFMT
%20 = and i32 %18, %19
%21 = load i32, i32* %4
%22 = icmp eq i32 %20, %21
%23 = zext i1 %22 to i32
br label %24
24: ; preds = %16, %15
%25 = phi i32 [ %13, %15 ], [ %23, %16 ]
ret i32 %25
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i32 0
@S_IFMT = global i32 0
define i32 @isfdtype(i32 %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @errno
%6 = call i32 @fstat(i32 %0, %struct.stat* %3)
%7 = tail call i32 @__set_errno(i32 %5)
%8 = icmp eq i32 %6, 0
br i1 %8, label %9, label %16
9: ; preds = %2
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @S_IFMT
%13 = and i32 %12, %11
%14 = icmp eq i32 %13, %1
%15 = zext i1 %14 to i32
br label %16
16: ; preds = %2, %9
%17 = phi i32 [ %15, %9 ], [ %6, %2 ]
ret i32 %17
}