#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*/ free(char *);
int /*<<< orphan*/ mkdir(char *, int /*<<< orphan*/);
size_t strlen(char *);
char *strndup(char const *, int);
void file_mkdir_p(const char *path) {
  char *tmp = strndup(path, 256);
  if (!tmp)
    return;
  size_t len = strlen(tmp);
  if (tmp[len - 1] == '/')
    tmp[len - 1] = '\0';
  char *p = NULL;
  for (p = tmp; *p != '\0'; p++) {
    if (*p == '/') {
      *p = '\0';
      mkdir(tmp, S_IRWXU);
      *p = '/';
    }
  }
  mkdir(tmp, S_IRWXU);
  free(tmp);
}


x86 assembly (clang, -O0):
<file_mkdir_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x100,%esi
call   1060 <strndup@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116e <file_mkdir_p+0x2e>
jmp    1225 <file_mkdir_p+0xe5>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11a4 <file_mkdir_p+0x64>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     120a <file_mkdir_p+0xca>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11f4 <file_mkdir_p+0xb4>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
call   1040 <mkdir@plt>
mov    -0x20(%rbp),%rax
movb   $0x2f,(%rax)
jmp    11f9 <file_mkdir_p+0xb9>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11b4 <file_mkdir_p+0x74>
mov    -0x10(%rbp),%rdi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
call   1040 <mkdir@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_mkdir_p>:
push   %r15
push   %r14
push   %rbx
mov    $0x100,%esi
call   1060 <strndup@plt>
test   %rax,%rax
je     11b8 <file_mkdir_p+0x78>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <strlen@plt>
cmpb   $0x2f,-0x1(%rax,%r15,1)
jne    116d <file_mkdir_p+0x2d>
movb   $0x0,-0x1(%r15,%rax,1)
mov    0x2e74(%rip),%r14
mov    %r15,%rbx
jmp    1195 <file_mkdir_p+0x55>
nopl   0x0(%rax)
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     1180 <file_mkdir_p+0x40>
test   %al,%al
jne    1191 <file_mkdir_p+0x51>
mov    (%r14),%esi
mov    %r15,%rdi
call   1040 <mkdir@plt>
mov    %r15,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <free@plt>
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@S_IRWXU = global i32 0
define void @file_mkdir_p(i8* nocapture %0) {
%2 = tail call i8* @strndup(i8* %0, i32 256)
%3 = icmp eq i8* %2, null
br i1 %3, label %24, label %4
4: ; preds = %1
%5 = tail call i64 @strlen(i8* %2)
%6 = add i64 %5, -1
%7 = getelementptr inbounds i8, i8* %2, i64 %6
%8 = load i8, i8* %7
%9 = icmp eq i8 %8, 47
br i1 %9, label %10, label %11
10: ; preds = %4
store i8 0, i8* %7
br label %11
11: ; preds = %10, %4
br label %12
12: ; preds = %11, %18
%13 = phi i8* [ %19, %18 ], [ %2, %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 = tail call i32 @mkdir(i8* %2, 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 = tail call i32 @mkdir(i8* %2, i32 %21)
%23 = tail call i32 @free(i8* %2)
br label %24
24: ; preds = %1, %20
ret void
}