#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 /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ debug(char *, char *, ...);
int /*<<< orphan*/ errno;
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int isdir(char *name) {
  struct stat statbuf;
  if (stat(name, &statbuf) < 0) {
    debug("failed to stat() '%s': %s", name, strerror(errno));
    return 0;
  }
  if (!S_ISDIR(statbuf.st_mode)) {
    debug("'%s' not a directory", name);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<isdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    1199 <isdir+0x59>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1030 <debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    11cf <isdir+0x8f>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11c8 <isdir+0x88>
mov    -0x10(%rbp),%rsi
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1030 <debug@plt>
movl   $0x0,-0x4(%rbp)
jmp    11cf <isdir+0x8f>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isdir>:
push   %rbp
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1050 <stat@plt>
test   %rax,%rax
js     1179 <isdir+0x39>
mov    (%rsp),%edi
call   1040 <S_ISDIR@plt>
mov    $0x1,%ebp
test   %eax,%eax
jne    119c <isdir+0x5c>
lea    0xeaf(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <debug@plt>
jmp    119c <isdir+0x5c>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe72(%rip),%rdi
xor    %ebp,%ebp
mov    %rbx,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <debug@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [26 x i8] c"failed to stat() '%s': %s\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"'%s' not a directory\00"
define i32 @isdir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i64 @stat(i8* %5, %struct.stat* %4)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = load i32, i32* @errno
%11 = call i32 @strerror(i32 %10)
%12 = call i32 (i8*, i8*, ...) @debug(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %9, i32 %11)
store i32 0, i32* %2
br label %22
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @S_ISDIR(i32 %15)
%17 = icmp ne i32 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = load i8*, i8** %3
%20 = call i32 (i8*, i8*, ...) @debug(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %19)
store i32 0, i32* %2
br label %22
21: ; preds = %13
store i32 1, i32* %2
br label %22
22: ; preds = %21, %18, %8
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [26 x i8] c"failed to stat() '%s': %s\00"
@errno = global i32 0
@.str.1 = [21 x i8] c"'%s' not a directory\00"
define i32 @isdir(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = load i32, i32* @errno
%8 = call i32 @strerror(i32 %7)
%9 = call i32 (i8*, i8*, ...) @debug(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %8)
br label %17
10: ; preds = %1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = call i32 @S_ISDIR(i32 %12)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 (i8*, i8*, ...) @debug(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %17
17: ; preds = %10, %15, %6
%18 = phi i32 [ 0, %6 ], [ 0, %15 ], [ 1, %10 ]
ret i32 %18
}