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

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ ENAMETOOLONG;
int PATH_MAX;
scalar_t__ errno;
scalar_t__ mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
int mkdir_p(const char *path, mode_t mode) {
  const size_t len = strlen(path);
  char _path[PATH_MAX];
  char *p;
  errno = 0;
  if (len > sizeof(_path) - 1) {
    errno = ENAMETOOLONG;
    return -1;
  }
  strcpy(_path, path);
  for (p = _path + 1; *p; p++) {
    if (*p == '/') {
      *p = '\0';
      if (mkdir(_path, mode) != 0) {
        if (errno != EEXIST)
          return -1;
      }
      *p = '/';
    }
  }
  if (mkdir(_path, mode) != 0) {
    if (errno != EEXIST)
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<mkdir_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x48(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x30(%rbp)
mov    0x2e4f(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rax
sub    $0x1,%rcx
cmp    %rcx,%rax
jbe    11c0 <mkdir_p+0x90>
mov    0x2e28(%rip),%rax
mov    (%rax),%rcx
mov    0x2e26(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12b8 <mkdir_p+0x188>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmpb   $0x0,(%rax)
je     1262 <mkdir_p+0x132>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    124c <mkdir_p+0x11c>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     1245 <mkdir_p+0x115>
mov    0x2db6(%rip),%rax
mov    (%rax),%rax
mov    0x2db4(%rip),%rcx
cmp    (%rcx),%rax
je     1240 <mkdir_p+0x110>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12b8 <mkdir_p+0x188>
jmp    1245 <mkdir_p+0x115>
mov    -0x38(%rbp),%rax
movb   $0x2f,(%rax)
jmp    1251 <mkdir_p+0x121>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11d9 <mkdir_p+0xa9>
mov    -0x48(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     12aa <mkdir_p+0x17a>
mov    0x2d51(%rip),%rax
mov    (%rax),%rax
mov    0x2d4f(%rip),%rcx
cmp    (%rcx),%rax
je     12a5 <mkdir_p+0x175>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12b8 <mkdir_p+0x188>
jmp    12aa <mkdir_p+0x17a>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_p>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
call   1050 <strlen@plt>
mov    0x2ea0(%rip),%rcx
mov    (%rcx),%ecx
mov    %rsp,%r13
lea    0xf(%rcx),%rdx
and    $0xfffffffffffffff0,%rdx
sub    %rdx,%r13
mov    %r13,%rsp
mov    0x2e66(%rip),%r12
movq   $0x0,(%r12)
add    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jbe    1193 <mkdir_p+0x63>
mov    0x2e46(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r12)
mov    $0xffffffff,%eax
jmp    1213 <mkdir_p+0xe3>
mov    %r13,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
lea    0x1(%r13),%rbx
mov    0x2e2f(%rip),%r15
jmp    11b7 <mkdir_p+0x87>
nopl   0x0(%rax,%rax,1)
movb   $0x2f,(%rbx)
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x2f,%al
je     11d0 <mkdir_p+0xa0>
test   %al,%al
je     11f3 <mkdir_p+0xc3>
add    $0x1,%rbx
jmp    11b7 <mkdir_p+0x87>
nopl   0x0(%rax,%rax,1)
movb   $0x0,(%rbx)
mov    %r13,%rdi
mov    %r14d,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     11b0 <mkdir_p+0x80>
mov    (%r12),%rax
cmp    (%r15),%rax
je     11b0 <mkdir_p+0x80>
mov    $0xffffffff,%eax
jmp    1213 <mkdir_p+0xe3>
mov    %r13,%rdi
mov    %r14d,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     1211 <mkdir_p+0xe1>
mov    (%r12),%rcx
mov    $0xffffffff,%eax
cmp    (%r15),%rcx
jne    1213 <mkdir_p+0xe3>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@errno = global i64 0
@ENAMETOOLONG = global i64 0
@EEXIST = global i64 0
define i32 @mkdir_p(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i8*, i8** %4
%12 = call i64 @strlen(i8* %11)
store i64 %12, i64* %6
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %7
%16 = alloca i8, i64 %14
store i64 %14, i64* %8
store i64 0, i64* @errno
%17 = load i64, i64* %6
%18 = sub i64 %14, 1
%19 = icmp ugt i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = load i64, i64* @ENAMETOOLONG
store i64 %21, i64* @errno
store i32 -1, i32* %3
store i32 1, i32* %10
br label %63
22: ; preds = %2
%23 = load i8*, i8** %4
%24 = call i32 @strcpy(i8* %16, i8* %23)
%25 = getelementptr inbounds i8, i8* %16, i64 1
store i8* %25, i8** %9
br label %26
26: ; preds = %49, %22
%27 = load i8*, i8** %9
%28 = load i8, i8* %27
%29 = icmp ne i8 %28, 0
br i1 %29, label %30, label %52
30: ; preds = %26
%31 = load i8*, i8** %9
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 47
br i1 %34, label %35, label %48
35: ; preds = %30
%36 = load i8*, i8** %9
store i8 0, i8* %36
%37 = load i32, i32* %5
%38 = call i64 @mkdir(i8* %16, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %35
%41 = load i64, i64* @errno
%42 = load i64, i64* @EEXIST
%43 = icmp ne i64 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %40
store i32 -1, i32* %3
store i32 1, i32* %10
br label %63
45: ; preds = %40
br label %46
46: ; preds = %45, %35
%47 = load i8*, i8** %9
store i8 47, i8* %47
br label %48
48: ; preds = %46, %30
br label %49
49: ; preds = %48
%50 = load i8*, i8** %9
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %9
br label %26
52: ; preds = %26
%53 = load i32, i32* %5
%54 = call i64 @mkdir(i8* %16, i32 %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %62
56: ; preds = %52
%57 = load i64, i64* @errno
%58 = load i64, i64* @EEXIST
%59 = icmp ne i64 %57, %58
br i1 %59, label %60, label %61
60: ; preds = %56
store i32 -1, i32* %3
store i32 1, i32* %10
br label %63
61: ; preds = %56
br label %62
62: ; preds = %61, %52
store i32 0, i32* %3
store i32 1, i32* %10
br label %63
63: ; preds = %62, %60, %44, %20
%64 = load i8*, i8** %7
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@errno = global i64 0
@ENAMETOOLONG = global i64 0
@EEXIST = global i64 0
define i32 @mkdir_p(i8* %0, i32 %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
store i64 0, i64* @errno
%7 = add nsw i64 %5, -1
%8 = icmp ugt i64 %3, %7
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i64, i64* @ENAMETOOLONG
store i64 %10, i64* @errno
br label %34
11: ; preds = %2
%12 = call i32 @strcpy(i8* %6, i8* %0)
br label %13
13: ; preds = %17, %11
%14 = phi i8* [ %6, %11 ], [ %15, %17 ]
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = load i8, i8* %15
switch i8 %16, label %17 [
i8 0, label %26
i8 47, label %18
]
17: ; preds = %13, %25
br label %13
18: ; preds = %13
store i8 0, i8* %15
%19 = call i64 @mkdir(i8* %6, i32 %1)
%20 = icmp eq i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %18
%22 = load i64, i64* @errno
%23 = load i64, i64* @EEXIST
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %34
25: ; preds = %21, %18
store i8 47, i8* %15
br label %17
26: ; preds = %13
%27 = call i64 @mkdir(i8* %6, i32 %1)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %26
%30 = load i64, i64* @errno
%31 = load i64, i64* @EEXIST
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29, %26
br label %34
34: ; preds = %21, %29, %33, %9
%35 = phi i32 [ -1, %9 ], [ 0, %33 ], [ -1, %29 ], [ -1, %21 ]
ret i32 %35
}