#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*/ EEXIST;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int mkdir(char const *, int);
int stat(char const *, struct stat *);
int utils_mkdir_safe(const char *path, int mask) {
  int res = 0;
  struct stat st;
  res = stat(path, &st);
  if (res == -1)
    return mkdir(path, mask);
  if (!S_ISDIR(st.st_mode)) {
    errno = EEXIST;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<utils_mkdir_safe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1174 <utils_mkdir_safe+0x44>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mkdir@plt>
mov    %eax,-0x4(%rbp)
jmp    11aa <utils_mkdir_safe+0x7a>
mov    -0x20(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11a3 <utils_mkdir_safe+0x73>
mov    0x2e54(%rip),%rax
mov    (%rax),%ecx
mov    0x2e43(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <utils_mkdir_safe+0x7a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utils_mkdir_safe>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
je     1173 <utils_mkdir_safe+0x43>
mov    (%rsp),%edi
call   1040 <S_ISDIR@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    116c <utils_mkdir_safe+0x3c>
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
mov    0x2e73(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    %rbx,%rdi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <mkdir@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@EEXIST = global i32 0
@errno = global i32 0
define i32 @utils_mkdir_safe(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
%8 = load i8*, i8** %4
%9 = call i32 @stat(i8* %8, %struct.stat* %7)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i32, i32* %5
%15 = call i32 @mkdir(i8* %13, i32 %14)
store i32 %15, i32* %3
br label %24
16: ; preds = %2
%17 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @S_ISDIR(i32 %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = load i32, i32* @EEXIST
store i32 %22, i32* @errno
store i32 -1, i32* %3
br label %24
23: ; preds = %16
store i32 0, i32* %3
br label %24
24: ; preds = %23, %21, %12
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@EEXIST = global i32 0
@errno = global i32 0
define i32 @utils_mkdir_safe(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @mkdir(i8* %0, i32 %1)
br label %16
9: ; preds = %2
%10 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%11 = load i32, i32* %10
%12 = tail call i32 @S_ISDIR(i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = load i32, i32* @EEXIST
store i32 %15, i32* @errno
br label %16
16: ; preds = %9, %14, %7
%17 = phi i32 [ %8, %7 ], [ -1, %14 ], [ 0, %9 ]
ret i32 %17
}