#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;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ runes_die(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ runes_sprintf_dup(char **, char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
char *strdup(char *);
int /*<<< orphan*/ strerror(scalar_t__);
char *strtok_r(char *, char *, char **);
void runes_mkdir_p(char *dir) {
  char *path_component, *save_ptr, *tok_str, *partial_path;
  struct stat st;
  dir = strdup(dir);
  partial_path = strdup("/");
  tok_str = dir;
  while ((path_component = strtok_r(tok_str, "/", &save_ptr))) {
    char *new_path;
    runes_sprintf_dup(&new_path, "%s%s/", partial_path, path_component);
    if (mkdir(new_path, 0755)) {
      if (errno != EEXIST) {
        runes_die("couldn't create directory %s: %s", new_path,
                  strerror(errno));
      }
    }
    free(partial_path);
    partial_path = new_path;
    tok_str = NULL;
  }
  if (stat(partial_path, &st)) {
    runes_die("couldn't stat %s: %s", partial_path, strerror(errno));
  }
  if (!S_ISDIR(st.st_mode)) {
    runes_die("couldn't create directory %s: %s", partial_path,
              strerror(EEXIST));
  }
  free(partial_path);
  free(dir);
}


x86 assembly (clang, -O0):
<runes_mkdir_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x8(%rbp)
lea    0xe54(%rip),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe38(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1070 <strtok_r@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1279 <runes_mkdir_p+0xe9>
mov    -0x28(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    -0x38(%rbp),%rdi
lea    0xe0a(%rip),%rsi
call   1090 <runes_sprintf_dup@plt>
mov    -0x38(%rbp),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     125b <runes_mkdir_p+0xcb>
mov    0x2dbe(%rip),%rax
mov    (%rax),%rax
mov    0x2dbc(%rip),%rcx
cmp    (%rcx),%rax
je     1256 <runes_mkdir_p+0xc6>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d9c(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    0xdb5(%rip),%rdi
call   1050 <runes_die@plt>
jmp    125b <runes_mkdir_p+0xcb>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
jmp    11c1 <runes_mkdir_p+0x31>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     12b9 <runes_mkdir_p+0x129>
mov    -0x28(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d39(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
lea    0xd73(%rip),%rdi
call   1050 <runes_die@plt>
mov    -0x30(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12f3 <runes_mkdir_p+0x163>
mov    -0x28(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d07(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
lea    0xd18(%rip),%rdi
call   1050 <runes_die@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<runes_mkdir_p>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
call   10a0 <strdup@plt>
mov    %rax,%rbp
lea    0xe57(%rip),%r14
mov    %r14,%rdi
call   10a0 <strdup@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rdx
mov    %rbp,0x10(%rsp)
mov    %rbp,%rdi
mov    %r14,%rsi
call   1070 <strtok_r@plt>
test   %rax,%rax
je     1288 <runes_mkdir_p+0xf8>
lea    0xe23(%rip),%r14
mov    %rsp,%r12
lea    0xe1d(%rip),%r13
lea    0x8(%rsp),%r15
mov    0x2de5(%rip),%rbp
jmp    121e <runes_mkdir_p+0x8e>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%rsp),%rbx
xor    %edi,%edi
mov    %r13,%rsi
mov    %r15,%rdx
call   1070 <strtok_r@plt>
test   %rax,%rax
je     1288 <runes_mkdir_p+0xf8>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1090 <runes_sprintf_dup@plt>
mov    (%rsp),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     1200 <runes_mkdir_p+0x70>
mov    0x0(%rbp),%rdi
mov    0x2d93(%rip),%rax
cmp    (%rax),%rdi
je     1200 <runes_mkdir_p+0x70>
mov    %r13,%r15
mov    %r12,%r13
mov    %r14,%r12
mov    (%rsp),%r14
call   10b0 <strerror@plt>
lea    0xd9b(%rip),%rdi
mov    %r14,%rsi
mov    %r12,%r14
mov    %r13,%r12
mov    %r15,%r13
lea    0x8(%rsp),%r15
mov    %eax,%edx
call   1050 <runes_die@plt>
jmp    1200 <runes_mkdir_p+0x70>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
test   %rax,%rax
je     12b8 <runes_mkdir_p+0x128>
mov    0x2d39(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
lea    0xd79(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <runes_die@plt>
mov    (%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
jne    12e4 <runes_mkdir_p+0x154>
mov    0x2d15(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
lea    0xd2c(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <runes_die@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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