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


x86 assembly (clang, -O0):
<checkpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <realpath@plt>
cmp    $0x0,%rax
je     116e <checkpath+0x3e>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     117a <checkpath+0x4a>
movl   $0x1,-0x4(%rbp)
jmp    11b0 <checkpath+0x80>
mov    -0x20(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11a9 <checkpath+0x79>
mov    0x2e66(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3d(%rip),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    11b0 <checkpath+0x80>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checkpath>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
call   1050 <realpath@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     117a <checkpath+0x4a>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <stat@plt>
test   %rax,%rax
jne    117a <checkpath+0x4a>
mov    (%rsp),%edi
call   1030 <S_ISDIR@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    117a <checkpath+0x4a>
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    0x2e65(%rip),%rcx
mov    %eax,(%rcx)
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@ENOTDIR = global i32 0
@errno = global i32 0
define i32 @checkpath(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.stat
store i8* %0, i8** %4
store i8* %1, i8** %5
%7 = load i8*, i8** %4
%8 = load i8*, i8** %5
%9 = call i32* @realpath(i8* %7, i8* %8)
%10 = icmp eq i32* %9, null
br i1 %10, label %15, label %11
11: ; preds = %2
%12 = load i8*, i8** %5
%13 = call i64 @stat(i8* %12, %struct.stat* %6)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %11, %2
store i32 1, i32* %3
br label %24
16: ; preds = %11
%17 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @S_ISDIR(i32 %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = load i32, i32* @ENOTDIR
store i32 %22, i32* @errno
store i32 1, i32* %3
br label %24
23: ; preds = %16
store i32 0, i32* %3
br label %24
24: ; preds = %23, %21, %15
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@ENOTDIR = global i32 0
@errno = global i32 0
define i32 @checkpath(i8* nocapture %0, i8* %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32* @realpath(i8* %0, i8* %1)
%6 = icmp eq i32* %5, null
br i1 %6, label %17, label %7
7: ; preds = %2
%8 = call i64 @stat(i8* %1, %struct.stat* %3)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %7
%11 = getelementptr inbounds %struct.stat, %struct.stat* %3, 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 = load i32, i32* @ENOTDIR
store i32 %16, i32* @errno
br label %17
17: ; preds = %10, %2, %7, %15
%18 = phi i32 [ 1, %15 ], [ 1, %7 ], [ 1, %2 ], [ 0, %10 ]
ret i32 %18
}