#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 */

/* Variables and functions */
#define EEXIST 129
#define ENOENT 128
int /*<<< orphan*/ S_IRWXU;
char *dirname(char *);
int errno;
int /*<<< orphan*/ free(char *);
char *malloc(int);
scalar_t__ mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
bool create_directory_if_missing(char *dir) {
  if (!mkdir(dir, S_IRWXU))
    return true;
  switch (errno) {
  case EEXIST:
    return true;
  case ENOENT:
    break;
  default:
    return false;
  }
  char *dir_copy, *dir_parent;
  if ((NULL == ((dir_copy) = malloc((strlen(dir) + 1) * sizeof(*(dir_copy))))))
    return false;
  strcpy(dir_copy, dir);
  dir_parent = dirname(dir_copy);
  if (!strcmp(dir_parent, ".")) {
    free(dir_copy);
    return false;
  }
  bool success =
      create_directory_if_missing(dir_parent) && (0 == mkdir(dir, S_IRWXU));
  free(dir_copy);
  return success;
}


x86 assembly (clang, -O0):
<create_directory_if_missing>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1050 <mkdir@plt>
cmp    $0x0,%rax
jne    11b4 <create_directory_if_missing+0x34>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <create_directory_if_missing+0x152>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
sub    $0x80,%eax
je     11ef <create_directory_if_missing+0x6f>
jmp    11d0 <create_directory_if_missing+0x50>
mov    -0x28(%rbp),%eax
sub    $0x81,%eax
jne    11f4 <create_directory_if_missing+0x74>
jmp    11e3 <create_directory_if_missing+0x63>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <create_directory_if_missing+0x152>
jmp    1200 <create_directory_if_missing+0x80>
movl   $0x0,-0x4(%rbp)
jmp    12d2 <create_directory_if_missing+0x152>
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10a0 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    1237 <create_directory_if_missing+0xb7>
movl   $0x0,-0x4(%rbp)
jmp    12d2 <create_directory_if_missing+0x152>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   1080 <dirname@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xda4(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    127f <create_directory_if_missing+0xff>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d2 <create_directory_if_missing+0x152>
mov    -0x20(%rbp),%rdi
call   1060 <create_directory_if_missing@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29(%rbp)
je     12b8 <create_directory_if_missing+0x138>
mov    -0x10(%rbp),%rdi
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
call   1050 <mkdir@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_directory_if_missing>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
mov    0x2e55(%rip),%r13
mov    0x0(%r13),%esi
call   1050 <mkdir@plt>
mov    $0x1,%ebx
test   %rax,%rax
je     1234 <create_directory_if_missing+0xb4>
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
cmp    $0x80,%eax
je     11c5 <create_directory_if_missing+0x45>
cmp    $0x81,%eax
je     1234 <create_directory_if_missing+0xb4>
xor    %ebx,%ebx
jmp    1234 <create_directory_if_missing+0xb4>
mov    %r14,%rdi
call   1070 <strlen@plt>
lea    0x1(%rax),%edi
call   10a0 <malloc@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1234 <create_directory_if_missing+0xb4>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   1080 <dirname@plt>
mov    %rax,%r12
lea    0xe04(%rip),%rsi
mov    %rax,%rdi
call   1090 <strcmp@plt>
test   %eax,%eax
je     122c <create_directory_if_missing+0xac>
mov    %r12,%rdi
call   1060 <create_directory_if_missing@plt>
test   %eax,%eax
je     122a <create_directory_if_missing+0xaa>
mov    0x0(%r13),%esi
mov    %r14,%rdi
call   1050 <mkdir@plt>
xor    %ebx,%ebx
test   %rax,%rax
sete   %bl
jmp    122c <create_directory_if_missing+0xac>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@S_IRWXU = global i32 0
@errno = global i32 0
@.str = [2 x i8] c".\00"
define i32 @create_directory_if_missing(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @S_IRWXU
%9 = call i64 @mkdir(i8* %7, i32 %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %12, label %11
11: ; preds = %1
store i32 1, i32* %2
br label %54
12: ; preds = %1
%13 = load i32, i32* @errno
switch i32 %13, label %16 [
i32 129, label %14
i32 128, label %15
]
14: ; preds = %12
store i32 1, i32* %2
br label %54
15: ; preds = %12
br label %17
16: ; preds = %12
store i32 0, i32* %2
br label %54
17: ; preds = %15
%18 = load i8*, i8** %3
%19 = call i32 @strlen(i8* %18)
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = mul i64 %21, 1
%23 = trunc i64 %22 to i32
%24 = call i8* @malloc(i32 %23)
store i8* %24, i8** %4
%25 = icmp eq i8* null, %24
br i1 %25, label %26, label %27
26: ; preds = %17
store i32 0, i32* %2
br label %54
27: ; preds = %17
%28 = load i8*, i8** %4
%29 = load i8*, i8** %3
%30 = call i32 @strcpy(i8* %28, i8* %29)
%31 = load i8*, i8** %4
%32 = call i8* @dirname(i8* %31)
store i8* %32, i8** %5
%33 = load i8*, i8** %5
%34 = call i32 @strcmp(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%35 = icmp ne i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %27
%37 = load i8*, i8** %4
%38 = call i32 @free(i8* %37)
store i32 0, i32* %2
br label %54
39: ; preds = %27
%40 = load i8*, i8** %5
%41 = call i32 @create_directory_if_missing(i8* %40)
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %39
%44 = load i8*, i8** %3
%45 = load i32, i32* @S_IRWXU
%46 = call i64 @mkdir(i8* %44, i32 %45)
%47 = icmp eq i64 0, %46
br label %48
48: ; preds = %43, %39
%49 = phi i1 [ false, %39 ], [ %47, %43 ]
%50 = zext i1 %49 to i32
store i32 %50, i32* %6
%51 = load i8*, i8** %4
%52 = call i32 @free(i8* %51)
%53 = load i32, i32* %6
store i32 %53, i32* %2
br label %54
54: ; preds = %48, %36, %26, %16, %14, %11
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
@S_IRWXU = global i32 0
@errno = global i32 0
@.str = [2 x i8] c".\00"
define i32 @create_directory_if_missing(i8* %0) {
%2 = load i32, i32* @S_IRWXU
%3 = tail call i64 @mkdir(i8* %0, i32 %2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %31, label %5
5: ; preds = %1
%6 = load i32, i32* @errno
switch i32 %6, label %7 [
i32 129, label %31
i32 128, label %8
]
7: ; preds = %5
br label %31
8: ; preds = %5
%9 = tail call i32 @strlen(i8* %0)
%10 = add nsw i32 %9, 1
%11 = tail call i8* @malloc(i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %31, label %13
13: ; preds = %8
%14 = tail call i32 @strcpy(i8* %11, i8* %0)
%15 = tail call i8* @dirname(i8* %11)
%16 = tail call i32 @strcmp(i8* dereferenceable(1) %15, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = tail call i32 @free(i8* %11)
br label %31
20: ; preds = %13
%21 = tail call i32 @create_directory_if_missing(i8* %15)
%22 = icmp eq i32 %21, 0
br i1 %22, label %28, label %23
23: ; preds = %20
%24 = load i32, i32* @S_IRWXU
%25 = tail call i64 @mkdir(i8* %0, i32 %24)
%26 = icmp eq i64 %25, 0
%27 = zext i1 %26 to i32
br label %28
28: ; preds = %23, %20
%29 = phi i32 [ 0, %20 ], [ %27, %23 ]
%30 = tail call i32 @free(i8* %11)
br label %31
31: ; preds = %18, %28, %8, %5, %1, %7
%32 = phi i32 [ 0, %7 ], [ 1, %1 ], [ 1, %5 ], [ %29, %28 ], [ 0, %18 ], [ 0, %8 ]
ret i32 %32
}