#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 */
scalar_t__ EEXIST;
scalar_t__ ENOENT;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
char *dirname(char *);
int /*<<< orphan*/ err(int, char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ mkdir(char const *, int);
scalar_t__ stat(char const *, struct stat *);
char *strdup(char const *);
void createDirectory(const char *path, bool parents) {
  if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
    if (parents && errno == EEXIST) {
      struct stat st;
      if (stat(path, &st) < 0)
        err(1, "stat");
      if (!S_ISDIR(st.st_mode)) {
        errno = EEXIST;
        err(1, "'%s'", path);
      }
    } else if (parents && errno == ENOENT) {
      char *parentName = strdup(path);
      if (!parentName)
        err(1, "strdup");
      parentName = dirname(parentName);
      createDirectory(parentName, parents);
      free(parentName);
      createDirectory(path, parents);
    } else {
      err(1, "'%s'", path);
    }
  }
}


x86 assembly (clang, -O0):
<createDirectory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    0x2e44(%rip),%rax
or     (%rax),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
jge    12ef <createDirectory+0x16f>
cmpl   $0x0,-0xc(%rbp)
je     124c <createDirectory+0xcc>
mov    0x2df2(%rip),%rax
mov    (%rax),%rax
mov    0x2df0(%rip),%rcx
cmp    (%rcx),%rax
jne    124c <createDirectory+0xcc>
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    120b <createDirectory+0x8b>
mov    $0x1,%edi
lea    0xdfc(%rip),%rsi
mov    $0x0,%al
call   1090 <err@plt>
mov    -0x10(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1247 <createDirectory+0xc7>
mov    0x2da5(%rip),%rax
mov    (%rax),%rcx
mov    0x2d93(%rip),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xdc5(%rip),%rsi
mov    $0x0,%al
call   1090 <err@plt>
jmp    12ea <createDirectory+0x16a>
cmpl   $0x0,-0xc(%rbp)
je     12ce <createDirectory+0x14e>
mov    0x2d63(%rip),%rax
mov    (%rax),%rax
mov    0x2d49(%rip),%rcx
cmp    (%rcx),%rax
jne    12ce <createDirectory+0x14e>
mov    -0x8(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    129b <createDirectory+0x11b>
mov    $0x1,%edi
lea    0xd76(%rip),%rsi
mov    $0x0,%al
call   1090 <err@plt>
mov    -0x18(%rbp),%rdi
call   1070 <dirname@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <createDirectory@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <createDirectory@plt>
jmp    12e5 <createDirectory+0x165>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xd27(%rip),%rsi
mov    $0x0,%al
call   1090 <err@plt>
jmp    12ea <createDirectory+0x16a>
jmp    12ef <createDirectory+0x16f>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createDirectory>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e4e(%rip),%r14
mov    0x2e3f(%rip),%r12
mov    (%r12),%eax
or     (%r14),%eax
mov    %esi,%r15d
mov    %rdi,%rbx
mov    0x2e43(%rip),%r13
or     0x0(%r13),%eax
mov    %eax,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
js     11cc <createDirectory+0x4c>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
test   %r15d,%r15d
jne    1236 <createDirectory+0xb6>
lea    0xe2d(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1090 <err@plt>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
call   1070 <dirname@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %r15d,%esi
call   1050 <createDirectory@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    (%r12),%esi
or     (%r14),%esi
or     0x0(%r13),%esi
mov    %rbx,%rdi
call   1040 <mkdir@plt>
test   %rax,%rax
jns    11bd <createDirectory+0x3d>
mov    0x2d83(%rip),%rax
mov    (%rax),%rax
mov    0x2d81(%rip),%rcx
cmp    (%rcx),%rax
je     1284 <createDirectory+0x104>
mov    0x2d5d(%rip),%rcx
cmp    (%rcx),%rax
jne    11d1 <createDirectory+0x51>
mov    %rbx,%rdi
call   10a0 <strdup@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1200 <createDirectory+0x80>
mov    $0x1,%edi
lea    0xd92(%rip),%rsi
xor    %eax,%eax
call   1090 <err@plt>
jmp    1200 <createDirectory+0x80>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
test   %rax,%rax
jns    12a7 <createDirectory+0x127>
lea    0xd65(%rip),%rsi
mov    $0x1,%edi
xor    %eax,%eax
call   1090 <err@plt>
mov    (%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
jne    11bd <createDirectory+0x3d>
mov    0x2d0a(%rip),%rax
mov    (%rax),%rax
mov    0x2cf8(%rip),%rcx
mov    %rax,(%rcx)
lea    0xd33(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1090 <err@plt>
jmp    11bd <createDirectory+0x3d>

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str = [5 x i8] c"stat\00"
@.str.1 = [5 x i8] c"'%s'\00"
@ENOENT = global i64 0
@.str.2 = [7 x i8] c"strdup\00"
define void @createDirectory(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = load i32, i32* @S_IRWXU
%9 = load i32, i32* @S_IRWXG
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRWXO
%12 = or i32 %10, %11
%13 = call i64 @mkdir(i8* %7, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %66
15: ; preds = %2
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %38
18: ; preds = %15
%19 = load i64, i64* @errno
%20 = load i64, i64* @EEXIST
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %38
22: ; preds = %18
%23 = load i8*, i8** %3
%24 = call i64 @stat(i8* %23, %struct.stat* %5)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
%29 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @S_ISDIR(i32 %30)
%32 = icmp ne i32 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %28
%34 = load i64, i64* @EEXIST
store i64 %34, i64* @errno
%35 = load i8*, i8** %3
%36 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %33, %28
br label %65
38: ; preds = %18, %15
%39 = load i32, i32* %4
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %61
41: ; preds = %38
%42 = load i64, i64* @errno
%43 = load i64, i64* @ENOENT
%44 = icmp eq i64 %42, %43
br i1 %44, label %45, label %61
45: ; preds = %41
%46 = load i8*, i8** %3
%47 = call i8* @strdup(i8* %46)
store i8* %47, i8** %6
%48 = load i8*, i8** %6
%49 = icmp ne i8* %48, null
br i1 %49, label %52, label %50
50: ; preds = %45
%51 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %52
52: ; preds = %50, %45
%53 = load i8*, i8** %6
%54 = call i8* @dirname(i8* %53)
store i8* %54, i8** %6
%55 = load i8*, i8** %6
%56 = load i32, i32* %4
%57 = load i8*, i8** %6
%58 = call i32 @free(i8* %57)
%59 = load i8*, i8** %3
%60 = load i32, i32* %4
br label %64
61: ; preds = %41, %38
%62 = load i8*, i8** %3
%63 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %62)
br label %64
64: ; preds = %61, %52
br label %65
65: ; preds = %64, %37
br label %66
66: ; preds = %65, %2
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str = [5 x i8] c"stat\00"
@.str.1 = [5 x i8] c"'%s'\00"
@ENOENT = global i64 0
@.str.2 = [7 x i8] c"strdup\00"
define void @createDirectory(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @S_IRWXU
%5 = load i32, i32* @S_IRWXG
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IRWXO
%8 = or i32 %6, %7
%9 = tail call i64 @mkdir(i8* %0, i32 %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %52
11: ; preds = %2
%12 = icmp eq i32 %1, 0
br i1 %12, label %50, label %13
13: ; preds = %11, %40
%14 = load i64, i64* @errno
%15 = load i64, i64* @EEXIST
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %32
17: ; preds = %13
%18 = bitcast %struct.stat* %3 to i8*
%19 = call i64 @stat(i8* %0, %struct.stat* %3)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @S_ISDIR(i32 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = load i64, i64* @EEXIST
store i64 %29, i64* @errno
%30 = call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %31
31: ; preds = %28, %23
br label %52
32: ; preds = %13
%33 = load i64, i64* @ENOENT
%34 = icmp eq i64 %14, %33
br i1 %34, label %35, label %50
35: ; preds = %32
%36 = tail call i8* @strdup(i8* %0)
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %35
%41 = tail call i8* @dirname(i8* %36)
tail call void @createDirectory(i8* %41, i32 %1)
%42 = tail call i32 @free(i8* %41)
%43 = load i32, i32* @S_IRWXU
%44 = load i32, i32* @S_IRWXG
%45 = or i32 %44, %43
%46 = load i32, i32* @S_IRWXO
%47 = or i32 %45, %46
%48 = tail call i64 @mkdir(i8* %0, i32 %47)
%49 = icmp slt i64 %48, 0
br i1 %49, label %13, label %52
50: ; preds = %32, %11
%51 = tail call i32 (i32, i8*, ...) @err(i32 1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %52
52: ; preds = %40, %2, %31, %50
ret void
}