#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*/ st;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ memset(struct stat *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *, char *);
int stat(char *, struct stat *);
char *strerror(int /*<<< orphan*/);
int eslib_file_isdir(char *path) {
  struct stat st;
  int ret;
  if (path == NULL)
    return -1;
  memset(&st, 0, sizeof(st));
  ret = stat(path, &st);
  if (ret == -1) {
    printf("stat(%s): %s\n", path, strerror(errno));
    return -1;
  }
  if (ret != -1 && S_ISDIR(st.st_mode))
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<eslib_file_isdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1173 <eslib_file_isdir+0x23>
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <eslib_file_isdir+0xb1>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d2 <eslib_file_isdir+0x82>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdx
lea    0xe3f(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1201 <eslib_file_isdir+0xb1>
cmpl   $0xffffffff,-0x1c(%rbp)
je     11fa <eslib_file_isdir+0xaa>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11fa <eslib_file_isdir+0xaa>
movl   $0x1,-0x4(%rbp)
jmp    1201 <eslib_file_isdir+0xb1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eslib_file_isdir>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0xffffffff,%ebp
test   %rdi,%rdi
je     11bd <eslib_file_isdir+0x6d>
mov    %rdi,%rbx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     119d <eslib_file_isdir+0x4d>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
jmp    11bd <eslib_file_isdir+0x6d>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe4e(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <printf@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [14 x i8] c"stat(%s): %s\0A\00"
@errno = global i32 0
define i32 @eslib_file_isdir(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 eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %2
br label %30
9: ; preds = %1
%10 = call i32 @memset(%struct.stat* %4, i32 0, i32 4)
%11 = load i8*, i8** %3
%12 = call i32 @stat(i8* %11, %struct.stat* %4)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %20
15: ; preds = %9
%16 = load i8*, i8** %3
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %16, i8* %18)
store i32 -1, i32* %2
br label %30
20: ; preds = %9
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, -1
br i1 %22, label %23, label %29
23: ; preds = %20
%24 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i64 @S_ISDIR(i32 %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 1, i32* %2
br label %30
29: ; preds = %23, %20
store i32 0, i32* %2
br label %30
30: ; preds = %29, %28, %15, %8
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [14 x i8] c"stat(%s): %s\0A\00"
@errno = global i32 0
define i32 @eslib_file_isdir(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = icmp eq i8* %0, null
br i1 %4, label %19, label %5
5: ; preds = %1
%6 = call i32 @memset(%struct.stat* %2, i32 0, i32 4)
%7 = call i32 @stat(i8* %0, %struct.stat* %2)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %13
9: ; preds = %5
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %11)
br label %19
13: ; preds = %5
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i64 @S_ISDIR(i32 %15)
%17 = icmp ne i64 %16, 0
%18 = zext i1 %17 to i32
br label %19
19: ; preds = %13, %1, %9
%20 = phi i32 [ -1, %9 ], [ -1, %1 ], [ %18, %13 ]
ret i32 %20
}