#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 */
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ log_debug(char *, char const *);
int /*<<< orphan*/ log_error(char *, char const *, int /*<<< orphan*/);
scalar_t__ mkdir(char const *, int);
int stat(char const *, struct stat *);
int /*<<< orphan*/ strerror(scalar_t__);
int mkdir_if_nonexistent(const char *dir) {
  struct stat buf;
  int rc = stat(dir, &buf);
  if (rc) {
    if (errno == ENOENT) {
      log_debug("creating directory %s", dir);
      if (mkdir(dir, 0777)) {
        log_error("error creating %s: %s", dir, strerror(errno));
        return -1;
      }
    } else {
      log_error("error stat'ing %s: %s", dir, strerror(errno));
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<mkdir_if_nonexistent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     122c <mkdir_if_nonexistent+0xdc>
mov    0x2e63(%rip),%rax
mov    (%rax),%rax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%rax
jne    11f2 <mkdir_if_nonexistent+0xa2>
mov    -0x10(%rbp),%rsi
lea    0xe65(%rip),%rdi
call   1050 <log_debug@plt>
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     11ed <mkdir_if_nonexistent+0x9d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x28(%rbp),%rsi
mov    %eax,%edx
lea    0xe3a(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <mkdir_if_nonexistent+0xe3>
jmp    1227 <mkdir_if_nonexistent+0xd7>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xe16(%rip),%rdi
call   1040 <log_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1233 <mkdir_if_nonexistent+0xe3>
jmp    122c <mkdir_if_nonexistent+0xdc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_if_nonexistent>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
xor    %ebp,%ebp
test   %eax,%eax
je     11cc <mkdir_if_nonexistent+0x7c>
mov    0x2e6e(%rip),%r14
mov    (%r14),%rdi
mov    0x2e54(%rip),%rax
cmp    (%rax),%rdi
jne    11ae <mkdir_if_nonexistent+0x5e>
lea    0xe78(%rip),%rdi
mov    %rbx,%rsi
call   1050 <log_debug@plt>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     11cc <mkdir_if_nonexistent+0x7c>
mov    (%r14),%rdi
lea    0xe6a(%rip),%rbp
jmp    11b5 <mkdir_if_nonexistent+0x65>
lea    0xe77(%rip),%rbp
call   1070 <strerror@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <log_error@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [22 x i8] c"creating directory %s\00"
@.str.1 = [22 x i8] c"error creating %s: %s\00"
@.str.2 = [22 x i8] c"error stat'ing %s: %s\00"
define i32 @mkdir_if_nonexistent(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @stat(i8* %6, %struct.stat* %4)
store i32 %7, i32* %5
%8 = load i32, i32* %5
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %32
10: ; preds = %1
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %26
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = call i32 @log_debug(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i8*, i8** %3
%18 = call i64 @mkdir(i8* %17, i32 511)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %14
%21 = load i8*, i8** %3
%22 = load i64, i64* @errno
%23 = call i32 @strerror(i64 %22)
%24 = call i32 @log_error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %21, i32 %23)
store i32 -1, i32* %2
br label %33
25: ; preds = %14
br label %31
26: ; preds = %10
%27 = load i8*, i8** %3
%28 = load i64, i64* @errno
%29 = call i32 @strerror(i64 %28)
%30 = call i32 @log_error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %27, i32 %29)
store i32 -1, i32* %2
br label %33
31: ; preds = %25
br label %32
32: ; preds = %31, %1
store i32 0, i32* %2
br label %33
33: ; preds = %32, %26, %20
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@.str = [22 x i8] c"creating directory %s\00"
@.str.1 = [22 x i8] c"error creating %s: %s\00"
@.str.2 = [22 x i8] c"error stat'ing %s: %s\00"
define i32 @mkdir_if_nonexistent(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, 0
br i1 %5, label %21, label %6
6: ; preds = %1
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %16
10: ; preds = %6
%11 = tail call i32 @log_debug(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = tail call i64 @mkdir(i8* %0, i32 511)
%13 = icmp eq i64 %12, 0
br i1 %13, label %21, label %14
14: ; preds = %10
%15 = load i64, i64* @errno
br label %16
16: ; preds = %6, %14
%17 = phi i64 [ %15, %14 ], [ %7, %6 ]
%18 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), %14 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), %6 ]
%19 = tail call i32 @strerror(i64 %17)
%20 = tail call i32 @log_error(i8* %18, i8* %0, i32 %19)
br label %21
21: ; preds = %16, %1, %10
%22 = phi i32 [ 0, %10 ], [ 0, %1 ], [ -1, %16 ]
ret i32 %22
}