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

/* Variables and functions */
scalar_t__ EEXIST;
int PATH_MAX;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char *, char);
int mkdirp(const char *path) {
  struct stat s;
  char tmp[PATH_MAX];
  char *p;
  strncpy(tmp, path, sizeof(tmp) - 1);
  p = strrchr(tmp, '/');
  if (!p)
    return 0;
  *p = '\0';
  if (!stat(tmp, &s))
    return !S_ISDIR(s.st_mode);
  *p = '/';
  p = tmp;
  if (!*p)
    return 0;
  p++;
  while (*p) {
    if (*p == '/') {
      *p = '\0';
      if (!stat(tmp, &s)) {
        if (!S_ISDIR(s.st_mode)) {
          fprintf(stderr, "Error, is not a directory: %s\n", tmp);
          return 1;
        }
      } else if (mkdir(tmp, 0777)) {
        if (errno != EEXIST) {
          perror(tmp);
          return 1;
        }
      }
      *p = '/';
    }
    p++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<mkdirp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
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
sub    $0x1,%rax
mov    %eax,%edx
call   1030 <strncpy@plt>
mov    -0x40(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11e7 <mkdirp+0x77>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    135d <mkdirp+0x1ed>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%eax
jne    1228 <mkdirp+0xb8>
mov    -0x18(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    135d <mkdirp+0x1ed>
mov    -0x40(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x2f,(%rcx)
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1257 <mkdirp+0xe7>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    135d <mkdirp+0x1ed>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
je     134f <mkdirp+0x1df>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    133e <mkdirp+0x1ce>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%eax
jne    12df <mkdirp+0x16f>
mov    -0x18(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12da <mkdirp+0x16a>
mov    -0x40(%rbp),%rdx
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
lea    0xd3e(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    135d <mkdirp+0x1ed>
jmp    1337 <mkdirp+0x1c7>
mov    -0x40(%rbp),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     1332 <mkdirp+0x1c2>
mov    0x2cca(%rip),%rax
mov    (%rax),%rax
mov    0x2cd0(%rip),%rcx
cmp    (%rcx),%rax
je     132d <mkdirp+0x1bd>
mov    -0x40(%rbp),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    135d <mkdirp+0x1ed>
jmp    1332 <mkdirp+0x1c2>
jmp    1337 <mkdirp+0x1c7>
mov    -0x30(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1263 <mkdirp+0xf3>
movl   $0x0,-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   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rsi
mov    %rsp,%rbx
mov    0x2e62(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r13
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r13
mov    %r13,%rsp
lea    -0x1(%rax),%edx
mov    %r13,%rdi
call   1030 <strncpy@plt>
mov    %r13,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     12a3 <mkdirp+0x133>
mov    %rax,%r15
movb   $0x0,(%rax)
lea    -0x30(%rbp),%rsi
mov    %r13,%rdi
call   1080 <stat@plt>
test   %eax,%eax
je     126e <mkdirp+0xfe>
movb   $0x2f,(%r15)
cmpb   $0x0,0x0(%r13)
je     12a3 <mkdirp+0x133>
mov    %rbx,-0x38(%rbp)
lea    0x1(%r13),%rbx
lea    -0x30(%rbp),%r15
mov    0x2dd4(%rip),%r12
jmp    1219 <mkdirp+0xa9>
mov    -0x30(%rbp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     1281 <mkdirp+0x111>
movb   $0x2f,(%rbx)
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x2f,%al
je     1230 <mkdirp+0xc0>
test   %al,%al
je     129f <mkdirp+0x12f>
add    $0x1,%rbx
jmp    1219 <mkdirp+0xa9>
nopw   0x0(%rax,%rax,1)
movb   $0x0,(%rbx)
mov    %r13,%rdi
mov    %r15,%rsi
call   1080 <stat@plt>
test   %eax,%eax
je     1206 <mkdirp+0x96>
mov    %r13,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     1212 <mkdirp+0xa2>
mov    0x2d6d(%rip),%rax
mov    (%rax),%rax
cmp    (%r12),%rax
je     1212 <mkdirp+0xa2>
mov    %r13,%rdi
call   1090 <perror@plt>
jmp    1299 <mkdirp+0x129>
mov    -0x30(%rbp),%edi
call   1050 <S_ISDIR@plt>
xor    %r14d,%r14d
test   %eax,%eax
sete   %r14b
jmp    12a3 <mkdirp+0x133>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
lea    0xd6f(%rip),%rsi
mov    %r13,%rdx
call   1070 <fprintf@plt>
mov    $0x1,%r14d
mov    -0x38(%rbp),%rbx
mov    %rbx,%rsp
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Error, is not a directory: %s\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkdirp(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
%13 = load i8*, i8** %3
%14 = sub i64 %10, 1
%15 = trunc i64 %14 to i32
%16 = call i32 @strncpy(i8* %12, i8* %13, i32 %15)
%17 = call i8* @strrchr(i8* %12, i8 signext 47)
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %1
store i32 0, i32* %2
store i32 1, i32* %8
br label %80
21: ; preds = %1
%22 = load i8*, i8** %7
store i8 0, i8* %22
%23 = call i32 @stat(i8* %12, %struct.stat* %4)
%24 = icmp ne i32 %23, 0
br i1 %24, label %32, label %25
25: ; preds = %21
%26 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @S_ISDIR(i32 %27)
%29 = icmp ne i32 %28, 0
%30 = xor i1 %29, true
%31 = zext i1 %30 to i32
store i32 %31, i32* %2
store i32 1, i32* %8
br label %80
32: ; preds = %21
%33 = load i8*, i8** %7
store i8 47, i8* %33
store i8* %12, i8** %7
%34 = load i8*, i8** %7
%35 = load i8, i8* %34
%36 = icmp ne i8 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %32
store i32 0, i32* %2
store i32 1, i32* %8
br label %80
38: ; preds = %32
%39 = load i8*, i8** %7
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %7
br label %41
41: ; preds = %76, %38
%42 = load i8*, i8** %7
%43 = load i8, i8* %42
%44 = icmp ne i8 %43, 0
br i1 %44, label %45, label %79
45: ; preds = %41
%46 = load i8*, i8** %7
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 47
br i1 %49, label %50, label %76
50: ; preds = %45
%51 = load i8*, i8** %7
store i8 0, i8* %51
%52 = call i32 @stat(i8* %12, %struct.stat* %4)
%53 = icmp ne i32 %52, 0
br i1 %53, label %63, label %54
54: ; preds = %50
%55 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @S_ISDIR(i32 %56)
%58 = icmp ne i32 %57, 0
br i1 %58, label %62, label %59
59: ; preds = %54
%60 = load i32, i32* @stderr
%61 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %12)
store i32 1, i32* %2
store i32 1, i32* %8
br label %80
62: ; preds = %54
br label %74
63: ; preds = %50
%64 = call i64 @mkdir(i8* %12, i32 511)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %73
66: ; preds = %63
%67 = load i64, i64* @errno
%68 = load i64, i64* @EEXIST
%69 = icmp ne i64 %67, %68
br i1 %69, label %70, label %72
70: ; preds = %66
%71 = call i32 @perror(i8* %12)
store i32 1, i32* %2
store i32 1, i32* %8
br label %80
72: ; preds = %66
br label %73
73: ; preds = %72, %63
br label %74
74: ; preds = %73, %62
%75 = load i8*, i8** %7
store i8 47, i8* %75
br label %76
76: ; preds = %74, %45
%77 = load i8*, i8** %7
%78 = getelementptr inbounds i8, i8* %77, i32 1
store i8* %78, i8** %7
br label %41
79: ; preds = %41
store i32 0, i32* %2
store i32 1, i32* %8
br label %80
80: ; preds = %79, %70, %59, %37, %25, %20
%81 = load i8*, i8** %5
%82 = load i32, i32* %2
ret i32 %82
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Error, is not a directory: %s\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkdirp(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = tail call i8* @llvm.stacksave()
%7 = alloca i8, i64 %5
%8 = add i32 %4, -1
%9 = call i32 @strncpy(i8* %7, i8* %0, i32 %8)
%10 = call i8* @strrchr(i8* %7, i8 signext 47)
%11 = icmp eq i8* %10, null
br i1 %11, label %51, label %12
12: ; preds = %1
store i8 0, i8* %10
%13 = call i32 @stat(i8* %7, %struct.stat* %2)
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @S_ISDIR(i32 %17)
%19 = icmp eq i32 %18, 0
%20 = zext i1 %19 to i32
br label %51
21: ; preds = %12
store i8 47, i8* %10
%22 = load i8, i8* %7
%23 = icmp eq i8 %22, 0
br i1 %23, label %51, label %24
24: ; preds = %21
%25 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
br label %26
26: ; preds = %30, %24
%27 = phi i8* [ %7, %24 ], [ %28, %30 ]
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = load i8, i8* %28
switch i8 %29, label %30 [
i8 0, label %51
i8 47, label %31
]
30: ; preds = %26, %50
br label %26
31: ; preds = %26
store i8 0, i8* %28
%32 = call i32 @stat(i8* %7, %struct.stat* %2)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %31
%35 = load i32, i32* %25
%36 = call i32 @S_ISDIR(i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %50
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %51
41: ; preds = %31
%42 = call i64 @mkdir(i8* %7, i32 511)
%43 = icmp eq i64 %42, 0
br i1 %43, label %50, label %44
44: ; preds = %41
%45 = load i64, i64* @errno
%46 = load i64, i64* @EEXIST
%47 = icmp eq i64 %45, %46
br i1 %47, label %50, label %48
48: ; preds = %44
%49 = call i32 @perror(i8* %7)
br label %51
50: ; preds = %41, %44, %34
store i8 47, i8* %28
br label %30
51: ; preds = %26, %21, %1, %48, %38, %15
%52 = phi i32 [ 1, %48 ], [ 1, %38 ], [ %20, %15 ], [ 0, %1 ], [ 0, %21 ], [ 0, %26 ]
ret i32 %52
}