#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 */
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
size_t strlen(char *);
void mkdir_p(const char *path) {
  char dir[4096];
  char *p = NULL;
  size_t len;
  sprintf(dir, "%s", path);
  len = strlen(dir);
  if (dir[len - 1] == '/')
    dir[len - 1] = '\0';
  for (p = dir + 1; *p; p++)
    if (*p == '/') {
      *p = '\0';
      mkdir(dir, S_IRWXU);
      *p = '/';
    }
  mkdir(dir, S_IRWXU);
}


x86 assembly (clang, -O0):
<mkdir_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x1018(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xea4(%rip),%rsi
call   1050 <sprintf@plt>
lea    -0x1010(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x1020(%rbp)
mov    -0x1020(%rbp),%rax
sub    $0x1,%rax
movsbl -0x1010(%rbp,%rax,1),%eax
ff
cmp    $0x2f,%eax
jne    11a3 <mkdir_p+0x73>
mov    -0x1020(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,-0x1010(%rbp,%rax,1)
lea    -0x1010(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1018(%rbp)
mov    -0x1018(%rbp),%rax
cmpb   $0x0,(%rax)
je     121d <mkdir_p+0xed>
mov    -0x1018(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1201 <mkdir_p+0xd1>
mov    -0x1018(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x1010(%rbp),%rdi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
mov    -0x1018(%rbp),%rax
movb   $0x2f,(%rax)
jmp    1206 <mkdir_p+0xd6>
mov    -0x1018(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1018(%rbp)
jmp    11b5 <mkdir_p+0x85>
lea    -0x1010(%rbp),%rdi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_p>:
push   %r15
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %rdi,%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmpb   $0x2f,-0x1(%rax,%rbx,1)
jne    115e <mkdir_p+0x2e>
movb   $0x0,-0x1(%rsp,%rax,1)
lea    0x1(%rsp),%rbx
mov    0x2e7e(%rip),%r14
mov    %rsp,%r15
jmp    1185 <mkdir_p+0x55>
nop
movb   $0x0,(%rbx)
mov    (%r14),%esi
mov    %r15,%rdi
call   1040 <mkdir@plt>
movb   $0x2f,(%rbx)
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x2f,%al
je     1170 <mkdir_p+0x40>
test   %al,%al
jne    1181 <mkdir_p+0x51>
mov    (%r14),%esi
mov    %rsp,%rdi
call   1040 <mkdir@plt>
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@S_IRWXU = global i32 0
define void @mkdir_p(i8* %0) {
%2 = alloca i8*
%3 = alloca [4096 x i8]
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %2
store i8* null, i8** %4
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%7 = load i8*, i8** %2
%8 = call i32 @sprintf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%10 = call i64 @strlen(i8* %9)
store i64 %10, i64* %5
%11 = load i64, i64* %5
%12 = sub i64 %11, 1
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 47
br i1 %16, label %17, label %21
17: ; preds = %1
%18 = load i64, i64* %5
%19 = sub i64 %18, 1
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %19
store i8 0, i8* %20
br label %21
21: ; preds = %17, %1
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%23 = getelementptr inbounds i8, i8* %22, i64 1
store i8* %23, i8** %4
br label %24
24: ; preds = %40, %21
%25 = load i8*, i8** %4
%26 = load i8, i8* %25
%27 = icmp ne i8 %26, 0
br i1 %27, label %28, label %43
28: ; preds = %24
%29 = load i8*, i8** %4
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp eq i32 %31, 47
br i1 %32, label %33, label %39
33: ; preds = %28
%34 = load i8*, i8** %4
store i8 0, i8* %34
%35 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%36 = load i32, i32* @S_IRWXU
%37 = call i32 @mkdir(i8* %35, i32 %36)
%38 = load i8*, i8** %4
store i8 47, i8* %38
br label %39
39: ; preds = %33, %28
br label %40
40: ; preds = %39
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %4
br label %24
43: ; preds = %24
%44 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%45 = load i32, i32* @S_IRWXU
%46 = call i32 @mkdir(i8* %44, i32 %45)
ret void
}

LLVM IR (-O2):
@S_IRWXU = global i32 0
define void @mkdir_p(i8* nocapture %0) {
%2 = alloca [4096 x i8]
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%4 = call i8* @strcpy(i8* %3, i8* dereferenceable(1) %0)
%5 = call i64 @strlen(i8* %3)
%6 = add i64 %5, -1
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 %6
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 47
br i1 %9, label %10, label %11
10: ; preds = %1
store i8 0, i8* %7
br label %11
11: ; preds = %10, %1
%12 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 1
br label %13
13: ; preds = %19, %11
%14 = phi i8* [ %12, %11 ], [ %20, %19 ]
%15 = load i8, i8* %14
switch i8 %15, label %19 [
i8 0, label %21
i8 47, label %16
]
16: ; preds = %13
store i8 0, i8* %14
%17 = load i32, i32* @S_IRWXU
%18 = call i32 @mkdir(i8* %3, i32 %17)
store i8 47, i8* %14
br label %19
19: ; preds = %13, %16
%20 = getelementptr inbounds i8, i8* %14, i64 1
br label %13
21: ; preds = %13
%22 = load i32, i32* @S_IRWXU
%23 = call i32 @mkdir(i8* %3, i32 %22)
ret void
}