#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 save_error = errno;
  int result = fstat(fildes, &st);
  __set_errno(save_error);
  if (result)
    return result;
  return (st.st_mode & S_IFMT) == (mode_t)fdtype;
}


x86 assembly (clang, -O0):
<isfdtype>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1040 <fstat@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <__set_errno@plt>
cmpl   $0x0,-0x18(%rbp)
je     1166 <isfdtype+0x46>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1180 <isfdtype+0x60>
mov    -0x10(%rbp),%eax
mov    0x2e70(%rip),%rcx
and    (%rcx),%eax
cmp    -0xc(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%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 i32
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = load i32, i32* @errno
store i32 %9, i32* %7
%10 = load i32, i32* %4
%11 = call i32 @fstat(i32 %10, %struct.stat* %6)
store i32 %11, i32* %8
%12 = load i32, i32* %7
%13 = call i32 @__set_errno(i32 %12)
%14 = load i32, i32* %8
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = load i32, i32* %8
store i32 %17, i32* %3
br label %26
18: ; preds = %2
%19 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i32, i32* @S_IFMT
%22 = and i32 %20, %21
%23 = load i32, i32* %5
%24 = icmp eq i32 %22, %23
%25 = zext i1 %24 to i32
store i32 %25, i32* %3
br label %26
26: ; preds = %18, %16
%27 = load i32, i32* %3
ret i32 %27
}

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
}