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

/* Variables and functions */
int S_IFDIR;
char *lastchar(char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int isdir(const char *filename) {
  char path[260 + 1];
  char *p;
  struct stat st;
  (strncpy(path, filename, sizeof(path)), path[sizeof(path) - 1] = 0);
  p = lastchar(path);
  if (p != path && (*p == '/')) {
    *p = 0;
  }
  if (stat(path, &st) != 0)
    return (0);
  return (((st.st_mode) & S_IFDIR) ? 1 : 0);
}


x86 assembly (clang, -O0):
<isdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x105,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x1c(%rbp)
lea    -0x120(%rbp),%rdi
call   1040 <lastchar@plt>
mov    %rax,-0x128(%rbp)
mov    -0x128(%rbp),%rax
lea    -0x120(%rbp),%rcx
cmp    %rcx,%rax
je     119f <isdir+0x6f>
mov    -0x128(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    119f <isdir+0x6f>
mov    -0x128(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x120(%rbp),%rdi
lea    -0x130(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     11c8 <isdir+0x98>
movl   $0x0,-0x4(%rbp)
jmp    11e7 <isdir+0xb7>
mov    -0x130(%rbp),%edx
mov    0x2e03(%rip),%rax
and    (%rax),%edx
xor    %eax,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isdir>:
push   %rbx
sub    $0x120,%rsp
mov    %rdi,%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x105,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x114(%rsp)
mov    %rbx,%rdi
call   1040 <lastchar@plt>
cmp    %rbx,%rax
je     116a <isdir+0x3a>
cmpb   $0x2f,(%rax)
jne    116a <isdir+0x3a>
movb   $0x0,(%rax)
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1195 <isdir+0x65>
mov    0x2e4e(%rip),%rax
mov    (%rax),%ecx
xor    %eax,%eax
test   %ecx,0x8(%rsp)
setne  %al
add    $0x120,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IFDIR = global i32 0
define i32 @isdir(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [261 x i8]
%5 = alloca i8*
%6 = alloca %struct.stat
store i8* %0, i8** %3
%7 = getelementptr inbounds [261 x i8], [261 x i8]* %4, i64 0, i64 0
%8 = load i8*, i8** %3
%9 = call i32 @strncpy(i8* %7, i8* %8, i32 261)
%10 = getelementptr inbounds [261 x i8], [261 x i8]* %4, i64 0, i64 260
store i8 0, i8* %10
%11 = getelementptr inbounds [261 x i8], [261 x i8]* %4, i64 0, i64 0
%12 = call i8* @lastchar(i8* %11)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = getelementptr inbounds [261 x i8], [261 x i8]* %4, i64 0, i64 0
%15 = icmp ne i8* %13, %14
br i1 %15, label %16, label %23
16: ; preds = %1
%17 = load i8*, i8** %5
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 47
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = load i8*, i8** %5
store i8 0, i8* %22
br label %23
23: ; preds = %21, %16, %1
%24 = getelementptr inbounds [261 x i8], [261 x i8]* %4, i64 0, i64 0
%25 = call i64 @stat(i8* %24, %struct.stat* %6)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %23
store i32 0, i32* %2
br label %36
28: ; preds = %23
%29 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%30 = load i32, i32* %29
%31 = load i32, i32* @S_IFDIR
%32 = and i32 %30, %31
%33 = icmp ne i32 %32, 0
%34 = zext i1 %33 to i64
%35 = select i1 %33, i32 1, i32 0
store i32 %35, i32* %2
br label %36
36: ; preds = %28, %27
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IFDIR = global i32 0
define i32 @isdir(i8* %0) {
%2 = alloca [261 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [261 x i8], [261 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.stat* %3 to i8*
%6 = call i32 @strncpy(i8* %4, i8* %0, i32 261)
%7 = getelementptr inbounds [261 x i8], [261 x i8]* %2, i64 0, i64 260
store i8 0, i8* %7
%8 = call i8* @lastchar(i8* %4)
%9 = icmp eq i8* %8, %4
br i1 %9, label %14, label %10
10: ; preds = %1
%11 = load i8, i8* %8
%12 = icmp eq i8 %11, 47
br i1 %12, label %13, label %14
13: ; preds = %10
store i8 0, i8* %8
br label %14
14: ; preds = %13, %10, %1
%15 = call i64 @stat(i8* %4, %struct.stat* %3)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %14
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%19 = load i32, i32* %18
%20 = load i32, i32* @S_IFDIR
%21 = and i32 %20, %19
%22 = icmp ne i32 %21, 0
%23 = zext i1 %22 to i32
br label %24
24: ; preds = %14, %17
%25 = phi i32 [ %23, %17 ], [ 0, %14 ]
ret i32 %25
}