#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 R_OK;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int W_OK;
int X_OK;
scalar_t__ access(char const *, int);
scalar_t__ stat(char const *, struct stat *);
char *check_dir(char const *actdir) {
  struct stat st;
  if (!actdir)
    return 0;
  if (stat(actdir, &st) >= 0 && S_ISDIR(st.st_mode) &&
      access(actdir, R_OK | W_OK | X_OK) >= 0)
    return (char *)actdir;
  return 0;
}


x86 assembly (clang, -O0):
<check_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <check_dir+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11c0 <check_dir+0x90>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jl     11b8 <check_dir+0x88>
mov    -0x18(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11b8 <check_dir+0x88>
mov    -0x10(%rbp),%rdi
mov    0x2e48(%rip),%rax
mov    (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
mov    0x2e46(%rip),%rax
or     (%rax),%esi
call   1050 <access@plt>
cmp    $0x0,%rax
jl     11b8 <check_dir+0x88>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c0 <check_dir+0x90>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_dir>:
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     1182 <check_dir+0x52>
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
test   %rax,%rax
js     1182 <check_dir+0x52>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
je     1182 <check_dir+0x52>
mov    0x2e6f(%rip),%rax
mov    0x2e80(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e6d(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
call   1050 <access@plt>
test   %rax,%rax
jns    1184 <check_dir+0x54>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@R_OK = global i32 0
@W_OK = global i32 0
@X_OK = global i32 0
define i8* @check_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %8, label %7
7: ; preds = %1
store i8* null, i8** %2
br label %29
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i64 @stat(i8* %9, %struct.stat* %4)
%11 = icmp sge i64 %10, 0
br i1 %11, label %12, label %28
12: ; preds = %8
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i64 @S_ISDIR(i32 %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = load i32, i32* @R_OK
%20 = load i32, i32* @W_OK
%21 = or i32 %19, %20
%22 = load i32, i32* @X_OK
%23 = or i32 %21, %22
%24 = call i64 @access(i8* %18, i32 %23)
%25 = icmp sge i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %17
%27 = load i8*, i8** %3
store i8* %27, i8** %2
br label %29
28: ; preds = %17, %12, %8
store i8* null, i8** %2
br label %29
29: ; preds = %28, %26, %7
%30 = load i8*, i8** %2
ret i8* %30
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@R_OK = global i32 0
@W_OK = global i32 0
@X_OK = global i32 0
define i8* @check_dir(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = icmp eq i8* %0, null
br i1 %4, label %22, label %5
5: ; preds = %1
%6 = call i64 @stat(i8* %0, %struct.stat* %2)
%7 = icmp sgt i64 %6, -1
br i1 %7, label %8, label %21
8: ; preds = %5
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = call i64 @S_ISDIR(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %8
%14 = load i32, i32* @R_OK
%15 = load i32, i32* @W_OK
%16 = or i32 %15, %14
%17 = load i32, i32* @X_OK
%18 = or i32 %16, %17
%19 = call i64 @access(i8* %0, i32 %18)
%20 = icmp sgt i64 %19, -1
br i1 %20, label %22, label %21
21: ; preds = %13, %8, %5
br label %22
22: ; preds = %13, %1, %21
%23 = phi i8* [ null, %21 ], [ null, %1 ], [ %0, %13 ]
ret i8* %23
}