#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ parent;
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
scalar_t__ EEXIST;
int PATH_MAX;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ mkdir(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stat(char const *, struct stat *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
int mkdirp(const char *pathname, mode_t mode) {
  char parent[PATH_MAX], *p;
  strncpy(parent, pathname, sizeof(parent));
  parent[sizeof(parent) - 1] = '\0';
  for (p = parent + strlen(parent); *p != '/' && p != parent; p--)
    ;
  *p = '\0';
  if (p != parent && mkdirp(parent, mode) != 0)
    return -1;
  if (mkdir(pathname, mode) == 0)
    return 0;
  if (errno == EEXIST) {
    struct stat sb;
    stat(pathname, &sb);
    if (!S_ISDIR(sb.st_mode)) {
      return -1;
    }
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<mkdirp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,(%rdi,%rax,1)
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x2f,%ecx
mov    %al,-0x49(%rbp)
je     11f2 <mkdirp+0x92>
mov    -0x40(%rbp),%rax
cmp    %rax,-0x30(%rbp)
setne  %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    1202 <mkdirp+0xa2>
jmp    1218 <mkdirp+0xb8>
jmp    1207 <mkdirp+0xa7>
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
jmp    11cf <mkdirp+0x6f>
mov    -0x40(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rcx)
cmp    %rax,-0x30(%rbp)
je     1255 <mkdirp+0xf5>
mov    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <mkdirp@plt>
cmp    $0x0,%eax
je     1255 <mkdirp+0xf5>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    12ea <mkdirp+0x18a>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
jne    127e <mkdirp+0x11e>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    12ea <mkdirp+0x18a>
mov    0x2d4b(%rip),%rax
mov    (%rax),%rax
mov    0x2d49(%rip),%rcx
cmp    (%rcx),%rax
jne    12dc <mkdirp+0x17c>
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <stat@plt>
mov    -0x38(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12c9 <mkdirp+0x169>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    12ea <mkdirp+0x18a>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    12ea <mkdirp+0x18a>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdirp>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e74(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r12,%rbx,1)
mov    %rbx,%rdi
call   1050 <strlen@plt>
movslq %eax,%rcx
mov    %rcx,%rax
test   %rcx,%rcx
je     11c2 <mkdirp+0x62>
lea    -0x1(%rax),%rcx
cmpb   $0x2f,(%rbx,%rax,1)
jne    11b0 <mkdirp+0x50>
movb   $0x0,(%rbx,%rax,1)
test   %rax,%rax
je     11e1 <mkdirp+0x81>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1070 <mkdirp@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
jne    1229 <mkdirp+0xc9>
mov    %r15,%rdi
mov    %r14d,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     1227 <mkdirp+0xc7>
mov    0x2dd8(%rip),%rax
mov    (%rax),%rcx
mov    0x2dd6(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
jne    1229 <mkdirp+0xc9>
lea    -0x28(%rbp),%rsi
mov    %r15,%rdi
call   1080 <stat@plt>
mov    -0x28(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x1,%eax
sbb    %eax,%eax
jmp    1229 <mkdirp+0xc9>
xor    %eax,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkdirp(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32 @strncpy(i8* %6, i8* %0, i32 %4)
%8 = add nsw i64 %5, -1
%9 = getelementptr inbounds i8, i8* %6, i64 %8
store i8 0, i8* %9
%10 = call i32 @strlen(i8* %6)
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %6, i64 %11
br label %13
13: ; preds = %13, %2
%14 = phi i8* [ %12, %2 ], [ %19, %13 ]
%15 = load i8, i8* %14
%16 = icmp ne i8 %15, 47
%17 = icmp ne i8* %14, %6
%18 = and i1 %17, %16
%19 = getelementptr inbounds i8, i8* %14, i64 -1
br i1 %18, label %13, label %20
20: ; preds = %13
store i8 0, i8* %14
br i1 %17, label %21, label %24
21: ; preds = %20
%22 = call i32 @mkdirp(i8* %6, i32 %1)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %39
24: ; preds = %21, %20
%25 = call i64 @mkdir(i8* %0, i32 %1)
%26 = icmp eq i64 %25, 0
br i1 %26, label %39, label %27
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EEXIST
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %39
31: ; preds = %27
%32 = bitcast %struct.stat* %3 to i8*
%33 = call i32 @stat(i8* %0, %struct.stat* %3)
%34 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @S_ISDIR(i32 %35)
%37 = icmp eq i32 %36, 0
%38 = sext i1 %37 to i32
br label %39
39: ; preds = %27, %24, %21, %31
%40 = phi i32 [ %38, %31 ], [ -1, %21 ], [ 0, %24 ], [ -1, %27 ]
ret i32 %40
}