#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 dummy;
};

/* Variables and functions */
int /*<<< orphan*/ R_OK;
int /*<<< orphan*/ W_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ log_error(char *, char *);
scalar_t__ stat(char *, struct stat *);
int check_dir(char *dir) {
  struct stat fs;
  if (stat(dir, &fs) != 0) {
    log_error("Missing directory: '%s'\n", dir);
    return -1;
  } else {
    if (0 != access(dir, W_OK)) {
      log_error("Cannot write directory: '%s'\n", dir);
      return -1;
    } else if (0 != access(dir, R_OK)) {
      log_error("Cannot read directory: '%s'\n", dir);
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
je     116f <check_dir+0x3f>
mov    -0x10(%rbp),%rsi
lea    0xea2(%rip),%rdi
call   1030 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <check_dir+0xc8>
mov    -0x10(%rbp),%rdi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     11ab <check_dir+0x7b>
mov    -0x10(%rbp),%rsi
lea    0xe7f(%rip),%rdi
call   1030 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <check_dir+0xc8>
mov    -0x10(%rbp),%rdi
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
call   1050 <access@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     11e7 <check_dir+0xb7>
mov    -0x10(%rbp),%rsi
lea    0xe61(%rip),%rdi
call   1030 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f8 <check_dir+0xc8>
jmp    11ec <check_dir+0xbc>
jmp    11f1 <check_dir+0xc1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_dir>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
test   %rax,%rax
je     1150 <check_dir+0x20>
lea    0xeb2(%rip),%rdi
jmp    118c <check_dir+0x5c>
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <access@plt>
test   %rax,%rax
je     116f <check_dir+0x3f>
lea    0xeac(%rip),%rdi
jmp    118c <check_dir+0x5c>
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <access@plt>
test   %rax,%rax
je     119f <check_dir+0x6f>
lea    0xeab(%rip),%rdi
mov    %rbx,%rsi
call   1030 <log_error@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret
xor    %eax,%eax
jmp    1199 <check_dir+0x69>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [25 x i8] c"Missing directory: '%s'\0A\00"
@W_OK = global i32 0
@.str.1 = [30 x i8] c"Cannot write directory: '%s'\0A\00"
@R_OK = global i32 0
@.str.2 = [29 x i8] c"Cannot read directory: '%s'\0A\00"
define i32 @check_dir(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 %11
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i32 @log_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %9)
store i32 -1, i32* %2
br label %30
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = load i32, i32* @W_OK
%14 = call i64 @access(i8* %12, i32 %13)
%15 = icmp ne i64 0, %14
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i8*, i8** %3
%18 = call i32 @log_error(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i32 -1, i32* %2
br label %30
19: ; preds = %11
%20 = load i8*, i8** %3
%21 = load i32, i32* @R_OK
%22 = call i64 @access(i8* %20, i32 %21)
%23 = icmp ne i64 0, %22
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i8*, i8** %3
%26 = call i32 @log_error(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %25)
store i32 -1, i32* %2
br label %30
27: ; preds = %19
br label %28
28: ; preds = %27
br label %29
29: ; preds = %28
store i32 0, i32* %2
br label %30
30: ; preds = %29, %24, %16, %8
%31 = load i32, i32* %2
ret i32 %31
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [25 x i8] c"Missing directory: '%s'\0A\00"
@W_OK = global i32 0
@.str.1 = [30 x i8] c"Cannot write directory: '%s'\0A\00"
@R_OK = global i32 0
@.str.2 = [29 x i8] c"Cannot read directory: '%s'\0A\00"
define i32 @check_dir(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 %14
6: ; preds = %1
%7 = load i32, i32* @W_OK
%8 = call i64 @access(i8* %0, i32 %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = load i32, i32* @R_OK
%12 = call i64 @access(i8* %0, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %10, %6, %1
%15 = phi i8* [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %6 ], [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), %10 ]
%16 = call i32 @log_error(i8* %15, i8* %0)
br label %17
17: ; preds = %14, %10
%18 = phi i32 [ 0, %10 ], [ -1, %14 ]
ret i32 %18
}