#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*/ mode_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int mkdir(char const *, int /*<<< orphan*/);
char *strdup(char const *);
size_t strlen(char *);
int mkdirp(const char *dir, mode_t mode) {
  char *tmp = strdup(dir);
  char *p = tmp;
  size_t len;
  len = strlen(tmp);
  if (tmp[len - 1] == '/')
    tmp[len - 1] = '\0';
  for (; *p; p++)
    if (*p == '/') {
      *p = '\0';
      mkdir(tmp, mode);
      *p = '/';
    }
  free(tmp);
  return mkdir(dir, mode);
}


x86 assembly (clang, -O0):
<mkdirp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    119a <mkdirp+0x5a>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    119f <mkdirp+0x5f>
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     11ec <mkdirp+0xac>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11d6 <mkdirp+0x96>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <mkdir@plt>
mov    -0x20(%rbp),%rax
movb   $0x2f,(%rax)
jmp    11db <mkdirp+0x9b>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    119f <mkdirp+0x5f>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1040 <mkdir@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdirp>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
call   1060 <strdup@plt>
mov    %rax,%rbp
mov    %rax,%rdi
call   1050 <strlen@plt>
cmpb   $0x2f,-0x1(%rax,%rbp,1)
jne    1169 <mkdirp+0x29>
movb   $0x0,-0x1(%rbp,%rax,1)
mov    %rbp,%rbx
jmp    1185 <mkdirp+0x45>
xchg   %ax,%ax
movb   $0x0,(%rbx)
mov    %rbp,%rdi
mov    %r15d,%esi
call   1040 <mkdir@plt>
movb   $0x2f,(%rbx)
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x2f,%al
je     1170 <mkdirp+0x30>
test   %al,%al
jne    1181 <mkdirp+0x41>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
mov    %r15d,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <mkdir@plt>

LLVM IR (-O0):
define i32 @mkdirp(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i8*, i8** %3
%9 = call i8* @strdup(i8* %8)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
store i8* %10, i8** %6
%11 = load i8*, i8** %5
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %7
%13 = load i8*, i8** %5
%14 = load i64, i64* %7
%15 = sub i64 %14, 1
%16 = getelementptr inbounds i8, i8* %13, i64 %15
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 47
br i1 %19, label %20, label %25
20: ; preds = %2
%21 = load i8*, i8** %5
%22 = load i64, i64* %7
%23 = sub i64 %22, 1
%24 = getelementptr inbounds i8, i8* %21, i64 %23
store i8 0, i8* %24
br label %25
25: ; preds = %20, %2
br label %26
26: ; preds = %42, %25
%27 = load i8*, i8** %6
%28 = load i8, i8* %27
%29 = icmp ne i8 %28, 0
br i1 %29, label %30, label %45
30: ; preds = %26
%31 = load i8*, i8** %6
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 47
br i1 %34, label %35, label %41
35: ; preds = %30
%36 = load i8*, i8** %6
store i8 0, i8* %36
%37 = load i8*, i8** %5
%38 = load i32, i32* %4
%39 = call i32 @mkdir(i8* %37, i32 %38)
%40 = load i8*, i8** %6
store i8 47, i8* %40
br label %41
41: ; preds = %35, %30
br label %42
42: ; preds = %41
%43 = load i8*, i8** %6
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %6
br label %26
45: ; preds = %26
%46 = load i8*, i8** %5
%47 = call i32 @free(i8* %46)
%48 = load i8*, i8** %3
%49 = load i32, i32* %4
%50 = call i32 @mkdir(i8* %48, i32 %49)
ret i32 %50
}

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