#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 */
int /*<<< orphan*/ S_IRWXU;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ err(char *, char *);
int mkdir(char const *, int /*<<< orphan*/);
int snprintf(char *, int, char *, char const *);
int stat(char const *, struct stat *);
bool create_dirtree(const char *path) {
  struct stat st;
  char p[256];
  if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
    return true;
  for (int i = 1, len = snprintf(p, sizeof p, "%s", path); i < len; ++i)
    if (p[i] == '/') {
      p[i] = '\0';
      if (stat(p, &st) == -1 && mkdir(p, S_IRWXU) == -1)
        err("cannot create directory '%s'\n", p);
      else if (!S_ISDIR(st.st_mode))
        err("file blocking directory creation '%s'\n", p);
      p[i] = '/';
    }
  return mkdir(path, S_IRWXU) == 0;
}


x86 assembly (clang, -O0):
<create_dirtree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%eax
jne    1193 <create_dirtree+0x43>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1193 <create_dirtree+0x43>
movl   $0x1,-0x4(%rbp)
jmp    12b9 <create_dirtree+0x169>
movl   $0x1,-0x124(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x100,%esi
lea    0xe4c(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x128(%rbp)
mov    -0x124(%rbp),%eax
cmp    -0x128(%rbp),%eax
jge    1299 <create_dirtree+0x149>
movslq -0x124(%rbp),%rax
movsbl -0x120(%rbp,%rax,1),%eax
ff
cmp    $0x2f,%eax
jne    1280 <create_dirtree+0x130>
movslq -0x124(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
lea    -0x120(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    1247 <create_dirtree+0xf7>
lea    -0x120(%rbp),%rdi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    1247 <create_dirtree+0xf7>
lea    -0x120(%rbp),%rsi
lea    0xdc6(%rip),%rdi
call   1070 <err@plt>
jmp    1271 <create_dirtree+0x121>
mov    -0x18(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
jne    126c <create_dirtree+0x11c>
lea    -0x120(%rbp),%rsi
lea    0xdba(%rip),%rdi
call   1070 <err@plt>
jmp    1271 <create_dirtree+0x121>
movslq -0x124(%rbp),%rax
movb   $0x2f,-0x120(%rbp,%rax,1)
2f
jmp    1285 <create_dirtree+0x135>
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    11bf <create_dirtree+0x6f>
mov    -0x10(%rbp),%rdi
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_dirtree>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %eax,%eax
jne    118c <create_dirtree+0x3c>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    124c <create_dirtree+0xfc>
lea    0xe6d(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %r14,%rcx
call   1050 <snprintf@plt>
cmp    $0x2,%eax
jl     1232 <create_dirtree+0xe2>
mov    %eax,%r13d
mov    $0x1,%ebx
lea    0x10(%rsp),%r12
lea    0x8(%rsp),%r15
lea    0xe5a(%rip),%rbp
jmp    11e6 <create_dirtree+0x96>
nopl   0x0(%rax)
mov    %r12,%rsi
call   1070 <err@plt>
movb   $0x2f,0x10(%rsp,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
je     1232 <create_dirtree+0xe2>
cmpb   $0x2f,0x10(%rsp,%rbx,1)
jne    11dd <create_dirtree+0x8d>
movb   $0x0,0x10(%rsp,%rbx,1)
mov    %r12,%rdi
mov    %r15,%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
jne    121f <create_dirtree+0xcf>
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1030 <mkdir@plt>
lea    0xde9(%rip),%rdi
cmp    $0xffffffff,%eax
je     11d0 <create_dirtree+0x80>
mov    0x8(%rsp),%edi
call   1040 <S_ISDIR@plt>
mov    %rbp,%rdi
test   %rax,%rax
jne    11d8 <create_dirtree+0x88>
jmp    11d0 <create_dirtree+0x80>
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1030 <mkdir@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
sete   %al
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"%s\00"
@S_IRWXU = global i32 0
@.str.1 = [30 x i8] c"cannot create directory '%s'\0A\00"
@.str.2 = [39 x i8] c"file blocking directory creation '%s'\0A\00"
define i32 @create_dirtree(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca [256 x i8]
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @stat(i8* %8, %struct.stat* %4)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i64 @S_ISDIR(i32 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %11
store i32 1, i32* %2
br label %70
17: ; preds = %11, %1
store i32 1, i32* %6
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%19 = load i8*, i8** %3
%20 = call i32 @snprintf(i8* %18, i32 256, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 %20, i32* %7
br label %21
21: ; preds = %61, %17
%22 = load i32, i32* %6
%23 = load i32, i32* %7
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %64
25: ; preds = %21
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %27
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 47
br i1 %31, label %32, label %60
32: ; preds = %25
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %34
store i8 0, i8* %35
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%37 = call i32 @stat(i8* %36, %struct.stat* %4)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %47
39: ; preds = %32
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%41 = load i32, i32* @S_IRWXU
%42 = call i32 @mkdir(i8* %40, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%46 = call i32 @err(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %45)
br label %56
47: ; preds = %39, %32
%48 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i64 @S_ISDIR(i32 %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %55, label %52
52: ; preds = %47
%53 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%54 = call i32 @err(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %52, %47
br label %56
56: ; preds = %55, %44
%57 = load i32, i32* %6
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %58
store i8 47, i8* %59
br label %60
60: ; preds = %56, %25
br label %61
61: ; preds = %60
%62 = load i32, i32* %6
%63 = add nsw i32 %62, 1
store i32 %63, i32* %6
br label %21
64: ; preds = %21
%65 = load i8*, i8** %3
%66 = load i32, i32* @S_IRWXU
%67 = call i32 @mkdir(i8* %65, i32 %66)
%68 = icmp eq i32 %67, 0
%69 = zext i1 %68 to i32
store i32 %69, i32* %2
br label %70
70: ; preds = %64, %16
%71 = load i32, i32* %2
ret i32 %71
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [3 x i8] c"%s\00"
@S_IRWXU = global i32 0
@.str.1 = [30 x i8] c"cannot create directory '%s'\0A\00"
@.str.2 = [39 x i8] c"file blocking directory creation '%s'\0A\00"
define i32 @create_dirtree(i8* %0) {
%2 = alloca %struct.stat
%3 = alloca [256 x i8]
%4 = bitcast %struct.stat* %2 to i8*
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%6 = call i32 @stat(i8* %0, %struct.stat* %2)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i64 @S_ISDIR(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %47
13: ; preds = %8, %1
%14 = call i32 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%18 = zext i32 %14 to i64
br label %24
19: ; preds = %44, %13
%20 = load i32, i32* @S_IRWXU
%21 = call i32 @mkdir(i8* %0, i32 %20)
%22 = icmp eq i32 %21, 0
%23 = zext i1 %22 to i32
br label %47
24: ; preds = %16, %44
%25 = phi i64 [ 1, %16 ], [ %45, %44 ]
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 47
br i1 %28, label %29, label %44
29: ; preds = %24
store i8 0, i8* %26
%30 = call i32 @stat(i8* %5, %struct.stat* %2)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i32, i32* @S_IRWXU
%34 = call i32 @mkdir(i8* %5, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %40, label %36
36: ; preds = %32, %29
%37 = load i32, i32* %17
%38 = call i64 @S_ISDIR(i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36, %32
%41 = phi i8* [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %32 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), %36 ]
%42 = call i32 @err(i8* %41, i8* %5)
br label %43
43: ; preds = %40, %36
store i8 47, i8* %26
br label %44
44: ; preds = %24, %43
%45 = add nuw nsw i64 %25, 1
%46 = icmp eq i64 %45, %18
br i1 %46, label %19, label %24
47: ; preds = %8, %19
%48 = phi i32 [ %23, %19 ], [ 1, %8 ]
ret i32 %48
}