#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;
};

/* Variables and functions */
int S_IFDIR;
int stat(char const *, struct stat *);
int /*<<< orphan*/ strlen(char const *);
int is_directory(const char *dirname) {
  struct stat buf;
  int n;
  if (!dirname || !strlen(dirname)) {
    return -1;
  }
  n = stat(dirname, &buf);
  return (n == 0 && (buf.st_mode & S_IFDIR) != 0) ? 0 : -1;
}


x86 assembly (clang, -O0):
<is_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1149 <is_directory+0x29>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%eax
jne    1155 <is_directory+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    119c <is_directory+0x7c>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x1d(%rbp)
jne    1189 <is_directory+0x69>
mov    -0x18(%rbp),%eax
mov    0x2e5a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%dl
mov    $0xffffffff,%eax
xor    %ecx,%ecx
test   $0x1,%dl
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_directory>:
push   %rbp
push   %rbx
push   %rax
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     115f <is_directory+0x3f>
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %eax,%eax
je     115f <is_directory+0x3f>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %eax,%eax
setne  %al
mov    0x2e88(%rip),%rcx
mov    (%rcx),%ecx
test   %ecx,(%rsp)
sete   %cl
or     %al,%cl
movzbl %cl,%ebp
neg    %ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IFDIR = global i32 0
define i32 @is_directory(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = icmp eq i8* %0, null
br i1 %4, label %18, label %5
5: ; preds = %1
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %18, label %8
8: ; preds = %5
%9 = call i32 @stat(i8* %0, %struct.stat* %2)
%10 = icmp ne i32 %9, 0
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = load i32, i32* @S_IFDIR
%14 = and i32 %13, %12
%15 = icmp eq i32 %14, 0
%16 = select i1 %10, i1 true, i1 %15
%17 = sext i1 %16 to i32
br label %18
18: ; preds = %1, %5, %8
%19 = phi i32 [ %17, %8 ], [ -1, %5 ], [ -1, %1 ]
ret i32 %19
}