#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*/ mode_t;

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ ENOENT;
scalar_t__ ENOTDIR;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcspn(char *, char *);
char *strerror(scalar_t__);
int /*<<< orphan*/ strspn(char *, char *);
int mkpath(char *path, char *p, mode_t mode, mode_t dir_mode, char *error,
           size_t errlen) {
  struct stat sb;
  char *slash;
  int done = 0;
  slash = p;
  while (!done) {
    slash += strspn(slash, "/");
    slash += strcspn(slash, "/");
    done = (*slash == '\0');
    *slash = '\0';
    if (stat(path, &sb)) {
      if (errno != ENOENT ||
          (mkdir(path, done ? mode : dir_mode) && errno != EEXIST)) {
        snprintf(error, errlen, "mkdir() error for '%s': %s\n", path,
                 strerror(errno));
        return (-1);
      }
    } else if (!S_ISDIR(sb.st_mode)) {
      snprintf(error, errlen, "Path '%s': %s ", path, strerror(ENOTDIR));
      return (-1);
    }
    *slash = '/';
  }
  return (0);
}


x86 assembly (clang, -O0):
<mkpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0x44(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b6 <mkpath+0x46>
jmp    135f <mkpath+0x1ef>
mov    -0x40(%rbp),%rdi
lea    0xe3f(%rip),%rsi
call   1060 <strspn@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    0xe1f(%rip),%rsi
call   1070 <strcspn@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     12e9 <mkpath+0x179>
mov    0x2da0(%rip),%rax
mov    (%rax),%rax
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
jne    1296 <mkpath+0x126>
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     1260 <mkpath+0xf0>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x54(%rbp)
jmp    1266 <mkpath+0xf6>
mov    -0x20(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x54(%rbp),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     12e4 <mkpath+0x174>
mov    0x2d4d(%rip),%rax
mov    (%rax),%rax
mov    0x2d4b(%rip),%rcx
cmp    (%rcx),%rax
je     12e4 <mkpath+0x174>
mov    -0x28(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2d1b(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x70(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    -0x60(%rbp),%rcx
mov    %rax,%r8
lea    0xd2f(%rip),%rdx
call   1050 <snprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1366 <mkpath+0x1f6>
jmp    1353 <mkpath+0x1e3>
mov    -0x38(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    134e <mkpath+0x1de>
mov    -0x28(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    0x2cdc(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x88(%rbp),%rdi
mov    -0x80(%rbp),%rsi
mov    -0x78(%rbp),%rcx
mov    %rax,%r8
lea    0xce1(%rip),%rdx
call   1050 <snprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1366 <mkpath+0x1f6>
jmp    1353 <mkpath+0x1e3>
mov    -0x40(%rbp),%rax
movb   $0x2f,(%rax)
jmp    11a0 <mkpath+0x30>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkpath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,0x10(%rsp)
mov    %r8,0x8(%rsp)
mov    %ecx,0x20(%rsp)
mov    %edx,0x24(%rsp)
mov    %rsi,%r12
mov    %rdi,%r15
lea    0xe63(%rip),%r14
jmp    11c1 <mkpath+0x51>
nop
mov    0x18(%rsp),%edi
call   1040 <S_ISDIR@plt>
test   %eax,%eax
je     1261 <mkpath+0xf1>
add    %rbp,%r12
movb   $0x2f,(%r12)
test   %bl,%bl
je     125d <mkpath+0xed>
mov    %r12,%r13
mov    %r12,%rdi
mov    %r14,%rsi
call   1060 <strspn@plt>
movslq %eax,%r12
add    %r13,%r12
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <strcspn@plt>
movslq %eax,%rbp
movzbl 0x0(%rbp,%r12,1),%ebx
movb   $0x0,0x0(%rbp,%r12,1)
mov    %r15,%rdi
lea    0x18(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
je     11a0 <mkpath+0x30>
mov    0x2dc8(%rip),%rax
mov    (%rax),%rdi
mov    0x2dae(%rip),%rax
cmp    (%rax),%rdi
jne    124a <mkpath+0xda>
mov    0x24(%rsp),%esi
test   %bl,%bl
je     1223 <mkpath+0xb3>
mov    0x20(%rsp),%esi
mov    %r15,%rdi
call   1030 <mkdir@plt>
test   %rax,%rax
je     11b1 <mkpath+0x41>
mov    0x2d99(%rip),%rax
mov    (%rax),%rdi
mov    0x2d97(%rip),%rax
cmp    (%rax),%rdi
je     11b1 <mkpath+0x41>
mov    0x10(%rsp),%rbx
mov    0x8(%rsp),%rbp
lea    0xda7(%rip),%r14
jmp    127c <mkpath+0x10c>
xor    %eax,%eax
jmp    129a <mkpath+0x12a>
mov    0x2d90(%rip),%rax
mov    (%rax),%rdi
lea    0xdac(%rip),%r14
mov    0x10(%rsp),%rbx
mov    0x8(%rsp),%rbp
call   1090 <strerror@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %rax,%r8
call   1050 <snprintf@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [2 x i8] c"/\00"
@errno = global i64 0
@ENOENT = global i64 0
@EEXIST = global i64 0
@.str.1 = [28 x i8] c"mkdir() error for '%s': %s\0A\00"
@.str.2 = [15 x i8] c"Path '%s': %s \00"
@ENOTDIR = global i64 0
define i32 @mkpath(i8* %0, i8* %1, i32 %2, i32 %3, i8* %4, i64 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i64
%14 = alloca %struct.stat
%15 = alloca i8*
%16 = alloca i32
store i8* %0, i8** %8
store i8* %1, i8** %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i64 %5, i64* %13
store i32 0, i32* %16
%17 = load i8*, i8** %9
store i8* %17, i8** %15
br label %18
18: ; preds = %83, %6
%19 = load i32, i32* %16
%20 = icmp ne i32 %19, 0
%21 = xor i1 %20, true
br i1 %21, label %22, label %85
22: ; preds = %18
%23 = load i8*, i8** %15
%24 = call i32 @strspn(i8* %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%25 = load i8*, i8** %15
%26 = sext i32 %24 to i64
%27 = getelementptr inbounds i8, i8* %25, i64 %26
store i8* %27, i8** %15
%28 = load i8*, i8** %15
%29 = call i32 @strcspn(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%30 = load i8*, i8** %15
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8* %32, i8** %15
%33 = load i8*, i8** %15
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 0
%37 = zext i1 %36 to i32
store i32 %37, i32* %16
%38 = load i8*, i8** %15
store i8 0, i8* %38
%39 = load i8*, i8** %8
%40 = call i64 @stat(i8* %39, %struct.stat* %14)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %70
42: ; preds = %22
%43 = load i64, i64* @errno
%44 = load i64, i64* @ENOENT
%45 = icmp ne i64 %43, %44
br i1 %45, label %62, label %46
46: ; preds = %42
%47 = load i8*, i8** %8
%48 = load i32, i32* %16
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = load i32, i32* %10
br label %54
52: ; preds = %46
%53 = load i32, i32* %11
br label %54
54: ; preds = %52, %50
%55 = phi i32 [ %51, %50 ], [ %53, %52 ]
%56 = call i64 @mkdir(i8* %47, i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %69
58: ; preds = %54
%59 = load i64, i64* @errno
%60 = load i64, i64* @EEXIST
%61 = icmp ne i64 %59, %60
br i1 %61, label %62, label %69
62: ; preds = %58, %42
%63 = load i8*, i8** %12
%64 = load i64, i64* %13
%65 = load i8*, i8** %8
%66 = load i64, i64* @errno
%67 = call i8* @strerror(i64 %66)
%68 = call i32 @snprintf(i8* %63, i64 %64, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %65, i8* %67)
store i32 -1, i32* %7
br label %86
69: ; preds = %58, %54
br label %83
70: ; preds = %22
%71 = getelementptr inbounds %struct.stat, %struct.stat* %14, i32 0, i32 0
%72 = load i32, i32* %71
%73 = call i32 @S_ISDIR(i32 %72)
%74 = icmp ne i32 %73, 0
br i1 %74, label %82, label %75
75: ; preds = %70
%76 = load i8*, i8** %12
%77 = load i64, i64* %13
%78 = load i8*, i8** %8
%79 = load i64, i64* @ENOTDIR
%80 = call i8* @strerror(i64 %79)
%81 = call i32 @snprintf(i8* %76, i64 %77, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %78, i8* %80)
store i32 -1, i32* %7
br label %86
82: ; preds = %70
br label %83
83: ; preds = %82, %69
%84 = load i8*, i8** %15
store i8 47, i8* %84
br label %18
85: ; preds = %18
store i32 0, i32* %7
br label %86
86: ; preds = %85, %75, %62
%87 = load i32, i32* %7
ret i32 %87
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"/\00"
@errno = global i64 0
@ENOENT = global i64 0
@EEXIST = global i64 0
@.str.1 = [28 x i8] c"mkdir() error for '%s': %s\0A\00"
@.str.2 = [15 x i8] c"Path '%s': %s \00"
@ENOTDIR = global i64 0
define i32 @mkpath(i8* %0, i8* nocapture %1, i32 %2, i32 %3, i8* %4, i64 %5) {
%7 = alloca %struct.stat
%8 = bitcast %struct.stat* %7 to i8*
%9 = getelementptr inbounds %struct.stat, %struct.stat* %7, i64 0, i32 0
br label %10
10: ; preds = %6, %40
%11 = phi i8* [ %1, %6 ], [ %17, %40 ]
%12 = call i32 @strspn(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = call i32 @strcspn(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
store i8 0, i8* %17
%20 = call i64 @stat(i8* %0, %struct.stat* %7)
%21 = icmp eq i64 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %10
%23 = load i64, i64* @errno
%24 = load i64, i64* @ENOENT
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %41
26: ; preds = %22
%27 = select i1 %19, i32 %2, i32 %3
%28 = call i64 @mkdir(i8* %0, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %40, label %30
30: ; preds = %26
%31 = load i64, i64* @errno
%32 = load i64, i64* @EEXIST
%33 = icmp eq i64 %31, %32
br i1 %33, label %40, label %41
34: ; preds = %10
%35 = load i32, i32* %9
%36 = call i32 @S_ISDIR(i32 %35)
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = load i64, i64* @ENOTDIR
br label %41
40: ; preds = %34, %26, %30
store i8 47, i8* %17
br i1 %19, label %46, label %10
41: ; preds = %22, %30, %38
%42 = phi i64 [ %39, %38 ], [ %31, %30 ], [ %23, %22 ]
%43 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), %38 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), %30 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), %22 ]
%44 = call i8* @strerror(i64 %42)
%45 = call i32 @snprintf(i8* %4, i64 %5, i8* %43, i8* %0, i8* %44)
br label %46
46: ; preds = %40, %41
%47 = phi i32 [ -1, %41 ], [ 0, %40 ]
ret i32 %47
}