#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*/ S_IRWXU;
int /*<<< orphan*/ W_OK;
scalar_t__ access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ log_error(char *, char *);
scalar_t__ mkdir(char *, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
int create_dir(char *dir) {
  struct stat fs;
  if (stat(dir, &fs) != 0) {
    if (0 != mkdir(dir, S_IRWXU)) {
      log_error("Cannot create directory: '%s'\n", dir);
      return -1;
    }
  } else {
    if (0 != access(dir, W_OK)) {
      log_error("Cannot write directory: '%s'\n", dir);
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<create_dir>:
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     11a4 <create_dir+0x64>
mov    -0x10(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     119f <create_dir+0x5f>
mov    -0x10(%rbp),%rsi
lea    0xe72(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <create_dir+0xac>
jmp    11e5 <create_dir+0xa5>
mov    -0x10(%rbp),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1060 <access@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     11e0 <create_dir+0xa0>
mov    -0x10(%rbp),%rsi
lea    0xe50(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ec <create_dir+0xac>
jmp    11e5 <create_dir+0xa5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_dir>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     1176 <create_dir+0x36>
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1030 <mkdir@plt>
test   %rax,%rax
je     11a2 <create_dir+0x62>
lea    0xe8c(%rip),%rdi
jmp    1193 <create_dir+0x53>
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <access@plt>
test   %rax,%rax
je     11a2 <create_dir+0x62>
lea    0xe8c(%rip),%rdi
mov    %rbx,%rsi
call   1040 <log_error@plt>
mov    $0xffffffff,%eax
jmp    11a4 <create_dir+0x64>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

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

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