#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__ S_ISDIR(int /*<<< orphan*/);
char *dirname(char *);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ stat(char *, struct stat *);
char *strchr(char *, char);
char *strdup(char *);
void mkdir_p(char *path) {
  char *ptr;
  struct stat statbuf;
  path = strdup(path);
  path = dirname(path);
  stat(path, &statbuf);
  if (S_ISDIR(statbuf.st_mode)) {
    free(path);
    return;
  }
  for (ptr = path; ptr != NULL; ptr = strchr(ptr, '/')) {
    if (ptr == path) {
      ptr++;
      continue;
    }
    errno = 0;
    *ptr = '\0';
    mkdir(path, 0755);
    *ptr = '/';
    if (errno != 0 && errno != EEXIST)
      break;
    ptr++;
  }
  mkdir(path, 0755);
  free(path);
}


x86 assembly (clang, -O0):
<mkdir_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <dirname@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
mov    -0x18(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11c3 <mkdir_p+0x53>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
jmp    1289 <mkdir_p+0x119>
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1272 <mkdir_p+0x102>
mov    -0x10(%rbp),%rax
cmp    -0x8(%rbp),%rax
jne    11f5 <mkdir_p+0x85>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    125b <mkdir_p+0xeb>
mov    0x2ddc(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
mov    -0x10(%rbp),%rax
movb   $0x2f,(%rax)
mov    0x2db2(%rip),%rax
cmpq   $0x0,(%rax)
je     124f <mkdir_p+0xdf>
mov    0x2da1(%rip),%rax
mov    (%rax),%rax
mov    0x2d9f(%rip),%rcx
cmp    (%rcx),%rax
je     124f <mkdir_p+0xdf>
jmp    1272 <mkdir_p+0x102>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0x10(%rbp)
jmp    11cb <mkdir_p+0x5b>
mov    -0x8(%rbp),%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
mov    -0x8(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_p>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
call   1090 <strdup@plt>
mov    %rax,%rdi
call   1070 <dirname@plt>
mov    %rax,%r14
mov    %rsp,%rsi
mov    %rax,%rdi
call   1080 <stat@plt>
mov    (%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
jne    1213 <mkdir_p+0xa3>
test   %r14,%r14
je     1206 <mkdir_p+0x96>
mov    0x2e2c(%rip),%r15
mov    0x2e2d(%rip),%r12
mov    %r14,%rbx
jmp    11d9 <mkdir_p+0x69>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1206 <mkdir_p+0x96>
cmp    %r14,%rbx
je     11c0 <mkdir_p+0x50>
movq   $0x0,(%r15)
movb   $0x0,(%rbx)
mov    %r14,%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
movb   $0x2f,(%rbx)
mov    (%r15),%rax
test   %rax,%rax
je     11c0 <mkdir_p+0x50>
cmp    (%r12),%rax
je     11c0 <mkdir_p+0x50>
mov    %r14,%rdi
mov    $0x1ed,%esi
call   1040 <mkdir@plt>
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <free@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@EEXIST = global i64 0
define void @mkdir_p(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i8* @strdup(i8* %5)
store i8* %6, i8** %2
%7 = load i8*, i8** %2
%8 = call i8* @dirname(i8* %7)
store i8* %8, i8** %2
%9 = load i8*, i8** %2
%10 = call i32 @stat(i8* %9, %struct.stat* %4)
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i64 @S_ISDIR(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %1
%16 = load i8*, i8** %2
%17 = call i32 @free(i8* %16)
br label %53
18: ; preds = %1
%19 = load i8*, i8** %2
store i8* %19, i8** %3
br label %20
20: ; preds = %45, %18
%21 = load i8*, i8** %3
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %48
23: ; preds = %20
%24 = load i8*, i8** %3
%25 = load i8*, i8** %2
%26 = icmp eq i8* %24, %25
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i8*, i8** %3
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %3
br label %45
30: ; preds = %23
store i64 0, i64* @errno
%31 = load i8*, i8** %3
store i8 0, i8* %31
%32 = load i8*, i8** %2
%33 = call i32 @mkdir(i8* %32, i32 493)
%34 = load i8*, i8** %3
store i8 47, i8* %34
%35 = load i64, i64* @errno
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %30
%38 = load i64, i64* @errno
%39 = load i64, i64* @EEXIST
%40 = icmp ne i64 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %37
br label %48
42: ; preds = %37, %30
%43 = load i8*, i8** %3
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %3
br label %45
45: ; preds = %42, %27
%46 = load i8*, i8** %3
%47 = call i8* @strchr(i8* %46, i8 signext 47)
store i8* %47, i8** %3
br label %20
48: ; preds = %41, %20
%49 = load i8*, i8** %2
%50 = call i32 @mkdir(i8* %49, i32 493)
%51 = load i8*, i8** %2
%52 = call i32 @free(i8* %51)
br label %53
53: ; preds = %48, %15
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@EEXIST = global i64 0
define void @mkdir_p(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i8* @strdup(i8* %0)
%5 = tail call i8* @dirname(i8* %4)
%6 = call i32 @stat(i8* %5, %struct.stat* %2)
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i64 @S_ISDIR(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %29
11: ; preds = %1
%12 = icmp eq i8* %5, null
br i1 %12, label %27, label %13
13: ; preds = %11, %23
%14 = phi i8* [ %25, %23 ], [ %5, %11 ]
%15 = icmp eq i8* %14, %5
br i1 %15, label %23, label %16
16: ; preds = %13
store i64 0, i64* @errno
store i8 0, i8* %14
%17 = tail call i32 @mkdir(i8* %5, i32 493)
store i8 47, i8* %14
%18 = load i64, i64* @errno
%19 = icmp eq i64 %18, 0
%20 = load i64, i64* @EEXIST
%21 = icmp eq i64 %18, %20
%22 = select i1 %19, i1 true, i1 %21
br i1 %22, label %23, label %27
23: ; preds = %16, %13
%24 = getelementptr inbounds i8, i8* %14, i64 1
%25 = tail call i8* @strchr(i8* %24, i8 signext 47)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %13
27: ; preds = %23, %16, %11
%28 = tail call i32 @mkdir(i8* %5, i32 493)
br label %29
29: ; preds = %1, %27
%30 = tail call i32 @free(i8* %5)
ret void
}