#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*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ W_OK;
scalar_t__ access(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error_reason(char *);
scalar_t__ stat(char const *, struct stat *);
char *strerror(int /*<<< orphan*/);
int misc_check_writable_directory(const char *directory) {
  struct stat stats;
  if (stat(directory, &stats)) {
    error_reason(strerror(errno));
    return -1;
  }
  if (!S_ISDIR(stats.st_mode)) {
    error_reason("Not a directory");
    return -1;
  }
  if (access(directory, W_OK)) {
    error_reason(strerror(errno));
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<misc_check_writable_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     1195 <misc_check_writable_directory+0x45>
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <misc_check_writable_directory+0xb3>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11be <misc_check_writable_directory+0x6e>
lea    0xe53(%rip),%rdi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <misc_check_writable_directory+0xb3>
mov    -0x10(%rbp),%rdi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
cmp    $0x0,%rax
je     11fc <misc_check_writable_directory+0xac>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <misc_check_writable_directory+0xb3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<misc_check_writable_directory>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     1188 <misc_check_writable_directory+0x38>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rdi
call   1030 <error_reason@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
je     11af <misc_check_writable_directory+0x5f>
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <access@plt>
test   %rax,%rax
jne    1167 <misc_check_writable_directory+0x17>
xor    %eax,%eax
jmp    1182 <misc_check_writable_directory+0x32>
lea    0xe4a(%rip),%rdi
jmp    1178 <misc_check_writable_directory+0x28>

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

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