#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 */
int /*<<< orphan*/ ENOTDIR;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ 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)
      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     117f <do_mkdir+0x4f>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     117a <do_mkdir+0x4a>
movl   $0xffffffff,-0x14(%rbp)
jmp    11ae <do_mkdir+0x7e>
mov    -0x10(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    11a9 <do_mkdir+0x79>
mov    0x2e61(%rip),%rax
mov    (%rax),%ecx
mov    0x2e38(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x14(%rbp)
jmp    11ae <do_mkdir+0x7e>
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     1156 <do_mkdir+0x26>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <mkdir@plt>
neg    %rax
sbb    %eax,%eax
jmp    117d <do_mkdir+0x4d>
mov    (%rsp),%edi
call   1040 <S_ISDIR@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    117d <do_mkdir+0x4d>
mov    0x2e8b(%rip),%rax
mov    (%rax),%eax
mov    0x2e62(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@ENOTDIR = global i32 0
@errno = global i32 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 %17
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 %16
15: ; preds = %10
store i32 -1, i32* %6
br label %16
16: ; preds = %15, %10
br label %25
17: ; preds = %2
%18 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @S_ISDIR(i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %17
%23 = load i32, i32* @ENOTDIR
store i32 %23, i32* @errno
store i32 -1, i32* %6
br label %24
24: ; preds = %22, %17
br label %25
25: ; preds = %24, %16
%26 = load i32, i32* %6
ret i32 %26
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@ENOTDIR = global i32 0
@errno = global i32 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 %11, label %7
7: ; preds = %2
%8 = call i64 @mkdir(i8* %0, i32 %1)
%9 = icmp ne i64 %8, 0
%10 = sext i1 %9 to i32
br label %18
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @S_ISDIR(i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = load i32, i32* @ENOTDIR
store i32 %17, i32* @errno
br label %18
18: ; preds = %7, %11, %16
%19 = phi i32 [ 0, %11 ], [ -1, %16 ], [ %10, %7 ]
ret i32 %19
}