#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;
};
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;
  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)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     1199 <do_mkdir+0x69>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     1194 <do_mkdir+0x64>
mov    0x2e56(%rip),%rax
mov    (%rax),%rax
mov    0x2e54(%rip),%rcx
cmp    (%rcx),%rax
je     1194 <do_mkdir+0x64>
movl   $0xffffffff,-0x14(%rbp)
jmp    11ca <do_mkdir+0x9a>
mov    -0x10(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11c5 <do_mkdir+0x95>
mov    0x2e47(%rip),%rax
mov    (%rax),%rcx
mov    0x2e15(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x14(%rbp)
jmp    11ca <do_mkdir+0x9a>
mov    -0x14(%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
mov    %rsp,%rsi
call   1050 <stat@plt>
test   %rax,%rax
je     1171 <do_mkdir+0x41>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     119c <do_mkdir+0x6c>
mov    0x2e75(%rip),%rax
mov    (%rax),%rcx
mov    0x2e73(%rip),%rdx
xor    %eax,%eax
cmp    (%rdx),%rcx
setne  %al
neg    %eax
jmp    119e <do_mkdir+0x6e>
mov    (%rsp),%edi
call   1040 <S_ISDIR@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    119e <do_mkdir+0x6e>
mov    0x2e70(%rip),%rax
mov    (%rax),%rax
mov    0x2e3e(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
jmp    119e <do_mkdir+0x6e>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

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