#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;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
scalar_t__ fstat(int /*<<< orphan*/, struct stat *);
int is_dir(FILE *fp) {
  struct stat buf;
  if (!fp)
    return 0;
  if (fstat(fileno(fp), &buf))
    return 0;
  if (S_ISDIR(buf.st_mode))
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<is_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1153 <is_dir+0x23>
movl   $0x0,-0x4(%rbp)
jmp    11a2 <is_dir+0x72>
mov    -0x10(%rbp),%rdi
call   1040 <fileno@plt>
mov    %eax,%edi
lea    -0x18(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0x0,%rax
je     117d <is_dir+0x4d>
movl   $0x0,-0x4(%rbp)
jmp    11a2 <is_dir+0x72>
mov    -0x18(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     119b <is_dir+0x6b>
movl   $0x1,-0x4(%rbp)
jmp    11a2 <is_dir+0x72>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_dir>:
push   %rbx
sub    $0x10,%rsp
xor    %ebx,%ebx
test   %rdi,%rdi
je     1163 <is_dir+0x33>
call   1040 <fileno@plt>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1050 <fstat@plt>
test   %rax,%rax
jne    1163 <is_dir+0x33>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @is_dir(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca %struct.stat
store i32* %0, i32** %3
%5 = load i32*, i32** %3
%6 = icmp ne i32* %5, null
br i1 %6, label %8, label %7
7: ; preds = %1
store i32 0, i32* %2
br label %21
8: ; preds = %1
%9 = load i32*, i32** %3
%10 = call i32 @fileno(i32* %9)
%11 = call i64 @fstat(i32 %10, %struct.stat* %4)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %8
store i32 0, i32* %2
br label %21
14: ; preds = %8
%15 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i64 @S_ISDIR(i32 %16)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %14
store i32 1, i32* %2
br label %21
20: ; preds = %14
store i32 0, i32* %2
br label %21
21: ; preds = %20, %19, %13, %7
%22 = load i32, i32* %2
ret i32 %22
}

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