#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 */
scalar_t__ EEXIST;
size_t PATH_MAX;
int chdir(char *);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
int mkdir(char *, int);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
size_t strlen(char const *);
int mkdir_p(const char *orig_path) {
  size_t path_len = strlen(orig_path);
  if (path_len > PATH_MAX) {
    fprintf(stderr, "Path too long.\n");
    return 1;
  }
  char path_buf[PATH_MAX + 1];
  char *cur = path_buf;
  char *end = path_buf + 1;
  memcpy(path_buf, orig_path, path_len + 1);
  for (;;) {
    while (*end != '\0' && *end != '/') {
      end++;
    }
    int last_component = (*end == '\0');
    *end = '\0';
    int ret = mkdir(cur, 0755);
    if (ret != 0 && errno != EEXIST) {
      fprintf(stderr, "mkdir(%s) failed: %s\n", cur, strerror(errno));
      return ret;
    }
    ret = chdir(cur);
    if (ret != 0) {
      fprintf(stderr, "chdir(%s) failed: %s\n", cur, strerror(errno));
      return ret;
    }
    if (last_component) {
      return 0;
    }
    cur = end + 1;
    end = end + 1;
  }
}


x86 assembly (clang, -O0):
<mkdir_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e6c(%rip),%rcx
cmp    (%rcx),%rax
jbe    11b0 <mkdir_p+0x50>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
lea    0xe63(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    138a <mkdir_p+0x22a>
mov    0x2e39(%rip),%rax
mov    (%rax),%rcx
mov    %rcx,%rax
add    $0x1,%rax
mov    %rsp,%rdx
mov    %rdx,-0x20(%rbp)
add    $0x10,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
call   1070 <memcpy@plt>
jmp    1202 <mkdir_p+0xa2>
mov    -0x38(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x45(%rbp)
je     1227 <mkdir_p+0xc7>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x45(%rbp)
mov    -0x45(%rbp),%al
test   $0x1,%al
jne    1237 <mkdir_p+0xd7>
jmp    1248 <mkdir_p+0xe8>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1202 <mkdir_p+0xa2>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
je     12e6 <mkdir_p+0x186>
mov    0x2d42(%rip),%rax
mov    (%rax),%rax
mov    0x2d48(%rip),%rcx
cmp    (%rcx),%rax
je     12e6 <mkdir_p+0x186>
mov    0x2d30(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d14(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xd43(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1383 <mkdir_p+0x223>
mov    -0x30(%rbp),%rdi
call   1050 <chdir@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
je     1349 <mkdir_p+0x1e9>
mov    0x2ccd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2cb1(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %rax,%rcx
lea    0xcf6(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x40(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1383 <mkdir_p+0x223>
cmpl   $0x0,-0x3c(%rbp)
je     1366 <mkdir_p+0x206>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x44(%rbp)
jmp    1383 <mkdir_p+0x223>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11fd <mkdir_p+0x9d>
mov    -0x20(%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    %rdi,%r15
call   1040 <strlen@plt>
mov    0x2e73(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
jbe    11a7 <mkdir_p+0x47>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
lea    0xe6b(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%r13d
jmp    1293 <mkdir_p+0x133>
mov    %rsp,-0x30(%rbp)
mov    %rsp,%rdx
add    $0x10,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rdx,%r14
sub    %rcx,%r14
mov    %r14,%rsp
neg    %rcx
lea    (%rdx,%rcx,1),%rbx
add    $0x1,%rbx
add    $0x1,%rax
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1070 <memcpy@plt>
mov    0x2df5(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0x1,%rbx
movzbl -0x1(%rbx),%r12d
test   %r12b,%r12b
je     1204 <mkdir_p+0xa4>
cmp    $0x2f,%r12b
jne    11f0 <mkdir_p+0x90>
movb   $0x0,-0x1(%rbx)
mov    %r14,%rdi
mov    $0x1ed,%esi
call   1030 <mkdir@plt>
test   %eax,%eax
je     122b <mkdir_p+0xcb>
mov    %eax,%r13d
mov    0x2da5(%rip),%rax
mov    (%rax),%rdi
cmp    (%r15),%rdi
jne    1269 <mkdir_p+0x109>
mov    %r14,%rdi
call   1050 <chdir@plt>
test   %eax,%eax
jne    1244 <mkdir_p+0xe4>
mov    %rbx,%r14
test   %r12b,%r12b
jne    11f0 <mkdir_p+0x90>
xor    %r13d,%r13d
jmp    128f <mkdir_p+0x12f>
mov    %eax,%r13d
mov    0x2d82(%rip),%rax
mov    (%rax),%r15d
mov    0x2d70(%rip),%rax
mov    (%rax),%rdi
call   1080 <strerror@plt>
lea    0xdbf(%rip),%rsi
jmp    127f <mkdir_p+0x11f>
mov    0x2d60(%rip),%rax
mov    (%rax),%r15d
call   1080 <strerror@plt>
lea    0xd91(%rip),%rsi
mov    %r15d,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%rsp
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i64 0
@stderr = global i32 0
@.str = [16 x i8] c"Path too long.\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [22 x i8] c"mkdir(%s) failed: %s\0A\00"
@.str.2 = [22 x i8] c"chdir(%s) failed: %s\0A\00"
define i32 @mkdir_p(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
%12 = load i8*, i8** %3
%13 = call i64 @strlen(i8* %12)
store i64 %13, i64* %4
%14 = load i64, i64* %4
%15 = load i64, i64* @PATH_MAX
%16 = icmp ugt i64 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %1
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %91
20: ; preds = %1
%21 = load i64, i64* @PATH_MAX
%22 = add i64 %21, 1
%23 = call i8* @llvm.stacksave()
store i8* %23, i8** %5
%24 = alloca i8, i64 %22
store i64 %22, i64* %6
store i8* %24, i8** %7
%25 = getelementptr inbounds i8, i8* %24, i64 1
store i8* %25, i8** %8
%26 = load i8*, i8** %3
%27 = load i64, i64* %4
%28 = add i64 %27, 1
%29 = call i32 @memcpy(i8* %24, i8* %26, i64 %28)
br label %30
30: ; preds = %84, %20
br label %31
31: ; preds = %43, %30
%32 = load i8*, i8** %8
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %31
%37 = load i8*, i8** %8
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 47
br label %41
41: ; preds = %36, %31
%42 = phi i1 [ false, %31 ], [ %40, %36 ]
br i1 %42, label %43, label %46
43: ; preds = %41
%44 = load i8*, i8** %8
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %8
br label %31
46: ; preds = %41
%47 = load i8*, i8** %8
%48 = load i8, i8* %47
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 0
%51 = zext i1 %50 to i32
store i32 %51, i32* %9
%52 = load i8*, i8** %8
store i8 0, i8* %52
%53 = load i8*, i8** %7
%54 = call i32 @mkdir(i8* %53, i32 493)
store i32 %54, i32* %10
%55 = load i32, i32* %10
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %68
57: ; preds = %46
%58 = load i64, i64* @errno
%59 = load i64, i64* @EEXIST
%60 = icmp ne i64 %58, %59
br i1 %60, label %61, label %68
61: ; preds = %57
%62 = load i32, i32* @stderr
%63 = load i8*, i8** %7
%64 = load i64, i64* @errno
%65 = call i8* @strerror(i64 %64)
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %63, i8* %65)
%67 = load i32, i32* %10
store i32 %67, i32* %2
store i32 1, i32* %11
br label %89
68: ; preds = %57, %46
%69 = load i8*, i8** %7
%70 = call i32 @chdir(i8* %69)
store i32 %70, i32* %10
%71 = load i32, i32* %10
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %80
73: ; preds = %68
%74 = load i32, i32* @stderr
%75 = load i8*, i8** %7
%76 = load i64, i64* @errno
%77 = call i8* @strerror(i64 %76)
%78 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %75, i8* %77)
%79 = load i32, i32* %10
store i32 %79, i32* %2
store i32 1, i32* %11
br label %89
80: ; preds = %68
%81 = load i32, i32* %9
%82 = icmp ne i32 %81, 0
br i1 %82, label %83, label %84
83: ; preds = %80
store i32 0, i32* %2
store i32 1, i32* %11
br label %89
84: ; preds = %80
%85 = load i8*, i8** %8
%86 = getelementptr inbounds i8, i8* %85, i64 1
store i8* %86, i8** %7
%87 = load i8*, i8** %8
%88 = getelementptr inbounds i8, i8* %87, i64 1
store i8* %88, i8** %8
br label %30
89: ; preds = %83, %73, %61
%90 = load i8*, i8** %5
br label %91
91: ; preds = %89, %17
%92 = load i32, i32* %2
ret i32 %92
}

LLVM IR (-O2):
@PATH_MAX = global i64 0
@stderr = global i32 0
@.str = [16 x i8] c"Path too long.\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [22 x i8] c"mkdir(%s) failed: %s\0A\00"
@.str.2 = [22 x i8] c"chdir(%s) failed: %s\0A\00"
define i32 @mkdir_p(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = load i64, i64* @PATH_MAX
%4 = icmp ugt i64 %2, %3
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %47
8: ; preds = %1
%9 = add i64 %3, 1
%10 = tail call i8* @llvm.stacksave()
%11 = alloca i8, i64 %9
%12 = getelementptr inbounds i8, i8* %11, i64 1
%13 = add i64 %2, 1
%14 = call i32 @memcpy(i8* %11, i8* %0, i64 %13)
br label %15
15: ; preds = %43, %8
%16 = phi i8* [ %11, %8 ], [ %44, %43 ]
%17 = phi i8* [ %12, %8 ], [ %44, %43 ]
br label %18
18: ; preds = %21, %15
%19 = phi i8* [ %17, %15 ], [ %22, %21 ]
%20 = load i8, i8* %19
switch i8 %20, label %21 [
i8 0, label %23
i8 47, label %23
]
21: ; preds = %18
%22 = getelementptr inbounds i8, i8* %19, i64 1
br label %18
23: ; preds = %18, %18
%24 = icmp eq i8 %20, 0
store i8 0, i8* %19
%25 = call i32 @mkdir(i8* %16, i32 493)
%26 = icmp eq i32 %25, 0
br i1 %26, label %35, label %27
27: ; preds = %23
%28 = load i64, i64* @errno
%29 = load i64, i64* @EEXIST
%30 = icmp eq i64 %28, %29
br i1 %30, label %35, label %31
31: ; preds = %27
%32 = load i32, i32* @stderr
%33 = call i8* @strerror(i64 %28)
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %16, i8* %33)
br label %45
35: ; preds = %27, %23
%36 = call i32 @chdir(i8* %16)
%37 = icmp eq i32 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = load i64, i64* @errno
%41 = call i8* @strerror(i64 %40)
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %16, i8* %41)
br label %45
43: ; preds = %35
%44 = getelementptr inbounds i8, i8* %19, i64 1
br i1 %24, label %45, label %15
45: ; preds = %43, %38, %31
%46 = phi i32 [ %25, %31 ], [ %36, %38 ], [ 0, %43 ]
br label %47
47: ; preds = %45, %5
%48 = phi i32 [ 1, %5 ], [ %46, %45 ]
ret i32 %48
}