#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;
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ ENOTDIR;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ mkdir(char const *, int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
int do_mkdir(const char *path, mode_t mode) {
  struct stat st = {0};
  int status = 0;
  if (stat(path, &st) != 0) {
    if (mkdir(path, mode) != 0 && errno != EEXIST)
      status = -1;
  } else if (!S_ISDIR(st.st_mode)) {
    errno = ENOTDIR;
    status = -1;
  }
  return (status);
}


x86 assembly (clang, -O0):
<do_mkdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     11b9 <do_mkdir+0x79>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     11b4 <do_mkdir+0x74>
mov    0x2e36(%rip),%rax
mov    (%rax),%rax
mov    0x2e34(%rip),%rcx
cmp    (%rcx),%rax
je     11b4 <do_mkdir+0x74>
movl   $0xffffffff,-0x1c(%rbp)
jmp    11ea <do_mkdir+0xaa>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11e5 <do_mkdir+0xa5>
mov    0x2e27(%rip),%rax
mov    (%rax),%rcx
mov    0x2df5(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x1c(%rbp)
jmp    11ea <do_mkdir+0xaa>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_mkdir>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
movq   $0x0,(%rsp)
mov    %rsp,%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     1179 <do_mkdir+0x49>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     11a4 <do_mkdir+0x74>
mov    0x2e6d(%rip),%rax
mov    (%rax),%rcx
mov    0x2e6b(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
neg    %eax
jmp    11a6 <do_mkdir+0x76>
mov    (%rsp),%edi
call   1040 <S_ISDIR@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    11a6 <do_mkdir+0x76>
mov    0x2e68(%rip),%rax
mov    (%rax),%rax
mov    0x2e36(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
jmp    11a6 <do_mkdir+0x76>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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