#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 */
typedef int /*<<< orphan*/ tmp;

/* Variables and functions */
int /*<<< orphan*/ S_IRWXU;
int /*<<< orphan*/ mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
size_t strlen(char *);
void mkdir_p(const char *dir) {
  char tmp[256];
  char *p = NULL;
  size_t len;
  snprintf(tmp, sizeof(tmp), "%s", dir);
  len = strlen(tmp);
  if (tmp[len - 1] == '/')
    tmp[len - 1] = '\0';
  for (p = tmp; *p != '\0'; p++) {
    if (*p == '/') {
      *p = '\0';
      mkdir(tmp, S_IRWXU);
      *p = '/';
    }
  }
  mkdir(tmp, S_IRWXU);
}


x86 assembly (clang, -O0):
<mkdir_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x118(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x100,%esi
lea    0xe9f(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x110(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rax
sub    $0x1,%rax
movsbl -0x110(%rbp,%rax,1),%eax
ff
cmp    $0x2f,%eax
jne    11a8 <mkdir_p+0x78>
mov    -0x120(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rax
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     1221 <mkdir_p+0xf1>
mov    -0x118(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1205 <mkdir_p+0xd5>
mov    -0x118(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x110(%rbp),%rdi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
mov    -0x118(%rbp),%rax
movb   $0x2f,(%rax)
jmp    120a <mkdir_p+0xda>
mov    -0x118(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x118(%rbp)
jmp    11b6 <mkdir_p+0x86>
lea    -0x110(%rbp),%rdi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
call   1030 <mkdir@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_p>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%rcx
lea    0xeba(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x100,%esi
call   1050 <snprintf@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
cmpb   $0x2f,-0x1(%rax,%r14,1)
jne    116b <mkdir_p+0x3b>
movb   $0x0,-0x1(%rsp,%rax,1)
mov    0x2e76(%rip),%r15
mov    %r14,%rbx
jmp    1195 <mkdir_p+0x65>
nopw   0x0(%rax,%rax,1)
movb   $0x0,(%rbx)
mov    (%r15),%esi
mov    %r14,%rdi
call   1030 <mkdir@plt>
movb   $0x2f,(%rbx)
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x2f,%al
je     1180 <mkdir_p+0x50>
test   %al,%al
jne    1191 <mkdir_p+0x61>
mov    (%r15),%esi
mov    %rsp,%rdi
call   1030 <mkdir@plt>
add    $0x100,%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 [256 x i8]
%4 = alloca i8*
%5 = alloca i64
store i8* %0, i8** %2
store i8* null, i8** %4
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = load i8*, i8** %2
%8 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = getelementptr inbounds [256 x i8], [256 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 [256 x i8], [256 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 [256 x i8], [256 x i8]* %3, i64 0, i64 %19
store i8 0, i8* %20
br label %21
21: ; preds = %17, %1
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
store i8* %22, i8** %4
br label %23
23: ; preds = %40, %21
%24 = load i8*, i8** %4
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %43
28: ; preds = %23
%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 [256 x i8], [256 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 %23
43: ; preds = %23
%44 = getelementptr inbounds [256 x i8], [256 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):
@.str = [3 x i8] c"%s\00"
@S_IRWXU = global i32 0
define void @mkdir_p(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 256, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = call i64 @strlen(i8* %3)
%6 = add i64 %5, -1
%7 = getelementptr inbounds [256 x i8], [256 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
br label %12
12: ; preds = %11, %18
%13 = phi i8* [ %19, %18 ], [ %3, %11 ]
%14 = load i8, i8* %13
switch i8 %14, label %18 [
i8 0, label %20
i8 47, label %15
]
15: ; preds = %12
store i8 0, i8* %13
%16 = load i32, i32* @S_IRWXU
%17 = call i32 @mkdir(i8* %3, i32 %16)
store i8 47, i8* %13
br label %18
18: ; preds = %12, %15
%19 = getelementptr inbounds i8, i8* %13, i64 1
br label %12
20: ; preds = %12
%21 = load i32, i32* @S_IRWXU
%22 = call i32 @mkdir(i8* %3, i32 %21)
ret void
}