#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 */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int statit(char const *, struct stat *);
int isdir(const char *path, struct stat *statdat) {
  struct stat sb;
  if (statdat == NULL) {
    if (statit(path, &sb) != 1) {
      if (errno != 0) {
        perror("isdir()->stat()");
      }
      return 0;
    }
    if (S_ISDIR(sb.st_mode))
      return 1;
    return 0;
  }
  if (S_ISDIR(statdat->st_mode))
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<isdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b4 <isdir+0x84>
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <statit@plt>
cmp    $0x1,%eax
je     118a <isdir+0x5a>
mov    0x2e78(%rip),%rax
cmpq   $0x0,(%rax)
je     117e <isdir+0x4e>
lea    0xe87(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    11dc <isdir+0xac>
mov    -0x20(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11a8 <isdir+0x78>
movl   $0x1,-0x4(%rbp)
jmp    11dc <isdir+0xac>
movl   $0x0,-0x4(%rbp)
jmp    11dc <isdir+0xac>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11d5 <isdir+0xa5>
movl   $0x1,-0x4(%rbp)
jmp    11dc <isdir+0xac>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isdir>:
push   %rax
test   %rsi,%rsi
je     113a <isdir+0xa>
mov    (%rsi),%edi
jmp    114a <isdir+0x1a>
mov    %rsp,%rsi
call   1040 <statit@plt>
cmp    $0x1,%eax
jne    115a <isdir+0x2a>
mov    (%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
setne  %al
movzbl %al,%eax
pop    %rcx
ret
mov    0x2e7f(%rip),%rax
cmpq   $0x0,(%rax)
jne    116b <isdir+0x3b>
xor    %eax,%eax
jmp    1155 <isdir+0x25>
lea    0xe8e(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
jmp    1155 <isdir+0x25>

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

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