#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__ ENOENT;
scalar_t__ ENOTDIR;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
scalar_t__ mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
int create_dir(const char *path, int mode) {
  char *temp;
  char *current;
  current = temp = malloc(strlen(path) + 2);
  strcpy(temp, path);
  strcat(temp, "/");
  while ((current = strchr(current, '/'))) {
    struct stat buf;
    char oldchar = *current;
    if (current == temp) {
      current += 1;
      continue;
    }
    *current = 0;
    if (stat(temp, &buf) == 0) {
      if (S_ISDIR(buf.st_mode)) {
      } else {
        free(temp);
        errno = ENOTDIR;
        return 0;
      }
    } else if (errno == ENOENT && mkdir(temp, mode) == 0) {
    } else {
      free(temp);
      return 0;
    }
    *current = oldchar;
    current += 1;
  }
  free(temp);
  return 1;
}


x86 assembly (clang, -O0):
<create_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x2,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rdi
lea    0xe2c(%rip),%rsi
call   10b0 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12e9 <create_dir+0x159>
mov    -0x28(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x31(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    121d <create_dir+0x8d>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11d9 <create_dir+0x49>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jne    1280 <create_dir+0xf0>
mov    -0x30(%rbp),%edi
call   1080 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1252 <create_dir+0xc2>
jmp    127b <create_dir+0xeb>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2d96(%rip),%rax
mov    (%rax),%rcx
mov    0x2d6c(%rip),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    12f9 <create_dir+0x169>
jmp    12cf <create_dir+0x13f>
mov    0x2d51(%rip),%rax
mov    (%rax),%rax
mov    0x2d37(%rip),%rcx
cmp    (%rcx),%rax
jne    12b5 <create_dir+0x125>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <mkdir@plt>
cmp    $0x0,%rax
jne    12b5 <create_dir+0x125>
jmp    12ca <create_dir+0x13a>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    12f9 <create_dir+0x169>
jmp    12cf <create_dir+0x13f>
mov    -0x31(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11d9 <create_dir+0x49>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_dir>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbp
call   1060 <strlen@plt>
lea    0x2(%rax),%rdi
call   10a0 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
lea    0xe3c(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <strcat@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     1278 <create_dir+0xe8>
mov    %rax,%rbp
mov    %rsp,%r15
mov    0x2dd9(%rip),%r13
jmp    122e <create_dir+0x9e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%edi
call   1080 <S_ISDIR@plt>
test   %rax,%rax
je     1294 <create_dir+0x104>
mov    %r12b,0x0(%rbp)
add    $0x1,%rbp
mov    %rbp,%rdi
mov    $0x2f,%esi
call   1070 <strchr@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1278 <create_dir+0xe8>
cmp    %rbx,%rbp
je     1215 <create_dir+0x85>
movzbl 0x0(%rbp),%r12d
movb   $0x0,0x0(%rbp)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1090 <stat@plt>
test   %rax,%rax
je     1200 <create_dir+0x70>
mov    0x2d85(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
jne    126c <create_dir+0xdc>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1050 <mkdir@plt>
test   %rax,%rax
je     1211 <create_dir+0x81>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1285 <create_dir+0xf5>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2d55(%rip),%rax
mov    (%rax),%rax
mov    0x2d2b(%rip),%rcx
mov    %rax,(%rcx)
xor    %eax,%eax
jmp    1285 <create_dir+0xf5>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"/\00"
@ENOTDIR = global i64 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @create_dir(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = add nsw i64 %4, 2
%6 = tail call i8* @malloc(i64 %5)
%7 = tail call i32 @strcpy(i8* %6, i8* %0)
%8 = tail call i32 @strcat(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = call i8* @strchr(i8* dereferenceable(1) %6, i8 signext 47)
%10 = icmp eq i8* %9, null
br i1 %10, label %43, label %11
11: ; preds = %2
%12 = bitcast %struct.stat* %3 to i8*
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %14
14: ; preds = %11, %39
%15 = phi i8* [ %9, %11 ], [ %41, %39 ]
%16 = load i8, i8* %15
%17 = icmp eq i8* %15, %6
br i1 %17, label %39, label %18
18: ; preds = %14
store i8 0, i8* %15
%19 = call i64 @stat(i8* %6, %struct.stat* %3)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %18
%22 = load i32, i32* %13
%23 = call i64 @S_ISDIR(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %37
25: ; preds = %21
%26 = call i32 @free(i8* %6)
%27 = load i64, i64* @ENOTDIR
store i64 %27, i64* @errno
br label %38
28: ; preds = %18
%29 = load i64, i64* @errno
%30 = load i64, i64* @ENOENT
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = call i64 @mkdir(i8* %6, i32 %1)
%34 = icmp eq i64 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %32, %28
%36 = call i32 @free(i8* %6)
br label %38
37: ; preds = %32, %21
store i8 %16, i8* %15
br label %39
38: ; preds = %25, %35
br label %45
39: ; preds = %14, %37
%40 = getelementptr inbounds i8, i8* %15, i64 1
%41 = call i8* @strchr(i8* dereferenceable(1) %40, i8 signext 47)
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %14
43: ; preds = %39, %2
%44 = call i32 @free(i8* %6)
br label %45
45: ; preds = %38, %43
%46 = phi i32 [ 1, %43 ], [ 0, %38 ]
ret i32 %46
}