#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*/ log_err(char *, char const *);
scalar_t__ mkdir(char const *, int);
scalar_t__ stat(char const *, struct stat *);
int sensorbox_mkdir(const char *path) {
  struct stat st;
  if (stat(path, &st) != 0) {
    if (mkdir(path, 0777) != 0) {
      log_err("Failed to create directory %s", path);
      return -1;
    }
  } else if (!S_ISDIR(st.st_mode)) {
    log_err("Path exists but is not a directory: %s", path);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sensorbox_mkdir>:
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     119c <sensorbox_mkdir+0x5c>
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     1197 <sensorbox_mkdir+0x57>
mov    -0x10(%rbp),%rsi
lea    0xe7a(%rip),%rdi
call   1060 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <sensorbox_mkdir+0x95>
jmp    11ce <sensorbox_mkdir+0x8e>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11c9 <sensorbox_mkdir+0x89>
mov    -0x10(%rbp),%rsi
lea    0xe66(%rip),%rdi
call   1060 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <sensorbox_mkdir+0x95>
jmp    11ce <sensorbox_mkdir+0x8e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sensorbox_mkdir>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     1172 <sensorbox_mkdir+0x32>
mov    %rbx,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     1199 <sensorbox_mkdir+0x59>
lea    0xe90(%rip),%rdi
jmp    118a <sensorbox_mkdir+0x4a>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    119b <sensorbox_mkdir+0x5b>
lea    0xe94(%rip),%rdi
mov    %rbx,%rsi
call   1060 <log_err@plt>
mov    $0xffffffff,%eax
jmp    119b <sensorbox_mkdir+0x5b>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [30 x i8] c"Failed to create directory %s\00"
@.str.1 = [39 x i8] c"Path exists but is not a directory: %s\00"
define i32 @sensorbox_mkdir(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 %16
8: ; preds = %1
%9 = load i8*, i8** %3
%10 = call i64 @mkdir(i8* %9, i32 511)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i8*, i8** %3
%14 = call i32 @log_err(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 -1, i32* %2
br label %26
15: ; preds = %8
br label %25
16: ; preds = %1
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, 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 %24, label %21
21: ; preds = %16
%22 = load i8*, i8** %3
%23 = call i32 @log_err(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %22)
store i32 -1, i32* %2
br label %26
24: ; preds = %16
br label %25
25: ; preds = %24, %15
store i32 0, i32* %2
br label %26
26: ; preds = %25, %21, %12
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [30 x i8] c"Failed to create directory %s\00"
@.str.1 = [39 x i8] c"Path exists but is not a directory: %s\00"
define i32 @sensorbox_mkdir(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 %9, label %6
6: ; preds = %1
%7 = call i64 @mkdir(i8* %0, i32 511)
%8 = icmp eq i64 %7, 0
br i1 %8, label %17, label %14
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%11 = load i32, i32* %10
%12 = call i32 @S_ISDIR(i32 %11)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9, %6
%15 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), %6 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), %9 ]
%16 = call i32 @log_err(i8* %15, i8* %0)
br label %17
17: ; preds = %14, %6, %9
%18 = phi i32 [ 0, %9 ], [ 0, %6 ], [ -1, %14 ]
ret i32 %18
}