#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;
};

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ ENOTDIR;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ paxwarn(int, char *, char *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcspn(char *, char *);
int /*<<< orphan*/ strspn(char *, char *);
int /*<<< orphan*/ syswarn(int, int /*<<< orphan*/, char *, char *);
int mkpath(char *path) {
  struct stat sb;
  char *slash;
  int done = 0;
  slash = path;
  while (!done) {
    slash += strspn(slash, "/");
    slash += strcspn(slash, "/");
    done = (*slash == '\0');
    *slash = '\0';
    if (stat(path, &sb)) {
      if (errno != ENOENT || mkdir(path, 0777)) {
        paxwarn(1, "%s", path);
        return (-1);
      }
    } else if (!S_ISDIR(sb.st_mode)) {
      syswarn(1, ENOTDIR, "%s", path);
      return (-1);
    }
    if (!done)
      *slash = '/';
  }
  return (0);
}


x86 assembly (clang, -O0):
<mkpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x24(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11a1 <mkpath+0x31>
jmp    12c2 <mkpath+0x152>
mov    -0x20(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   1060 <strspn@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe34(%rip),%rsi
call   1070 <strcspn@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
je     126c <mkpath+0xfc>
mov    0x2dbd(%rip),%rax
mov    (%rax),%rax
mov    0x2da3(%rip),%rcx
cmp    (%rcx),%rax
jne    1246 <mkpath+0xd6>
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
je     1267 <mkpath+0xf7>
mov    -0x10(%rbp),%rdx
mov    $0x1,%edi
lea    0xdac(%rip),%rsi
call   1090 <paxwarn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <mkpath+0x159>
jmp    12ac <mkpath+0x13c>
mov    -0x18(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12a7 <mkpath+0x137>
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edi
lea    0xd6c(%rip),%rdx
call   1040 <syswarn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c9 <mkpath+0x159>
jmp    12ac <mkpath+0x13c>
cmpl   $0x0,-0x24(%rbp)
jne    12bd <mkpath+0x14d>
mov    -0x20(%rbp),%rax
movb   $0x2f,(%rax)
jmp    118b <mkpath+0x1b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkpath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
lea    0xe7b(%rip),%r14
mov    %rsp,%r15
mov    %rdi,%r13
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %r14,%rsi
call   1060 <strspn@plt>
movslq %eax,%rbx
add    %r13,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strcspn@plt>
movslq %eax,%rbp
movzbl 0x0(%rbp,%rbx,1),%r13d
movb   $0x0,0x0(%rbp,%rbx,1)
mov    %r12,%rdi
mov    %r15,%rsi
call   1080 <stat@plt>
test   %rax,%rax
je     1200 <mkpath+0x90>
mov    0x2e07(%rip),%rax
mov    (%rax),%rax
mov    0x2ded(%rip),%rcx
cmp    (%rcx),%rax
jne    121f <mkpath+0xaf>
mov    %r12,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
je     120c <mkpath+0x9c>
jmp    121f <mkpath+0xaf>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     1239 <mkpath+0xc9>
test   %r13b,%r13b
je     1235 <mkpath+0xc5>
add    %rbp,%rbx
movb   $0x2f,(%rbx)
mov    %rbx,%r13
jmp    1190 <mkpath+0x20>
lea    0xddc(%rip),%rsi
mov    $0x1,%edi
mov    %r12,%rdx
call   1090 <paxwarn@plt>
jmp    1256 <mkpath+0xe6>
xor    %eax,%eax
jmp    125b <mkpath+0xeb>
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
lea    0xdb9(%rip),%rdx
mov    $0x1,%edi
mov    %r12,%rcx
call   1040 <syswarn@plt>
mov    $0xffffffff,%eax
add    $0x8,%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
@.str.1 = [3 x i8] c"%s\00"
@ENOTDIR = global i32 0
define i32 @mkpath(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %6
%7 = load i8*, i8** %3
store i8* %7, i8** %5
br label %8
8: ; preds = %59, %1
%9 = load i32, i32* %6
%10 = icmp ne i32 %9, 0
%11 = xor i1 %10, true
br i1 %11, label %12, label %60
12: ; preds = %8
%13 = load i8*, i8** %5
%14 = call i32 @strspn(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = load i8*, i8** %5
%16 = sext i32 %14 to i64
%17 = getelementptr inbounds i8, i8* %15, i64 %16
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = call i32 @strcspn(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = load i8*, i8** %5
%21 = sext i32 %19 to i64
%22 = getelementptr inbounds i8, i8* %20, i64 %21
store i8* %22, i8** %5
%23 = load i8*, i8** %5
%24 = load i8, i8* %23
%25 = sext i8 %24 to i32
%26 = icmp eq i32 %25, 0
%27 = zext i1 %26 to i32
store i32 %27, i32* %6
%28 = load i8*, i8** %5
store i8 0, i8* %28
%29 = load i8*, i8** %3
%30 = call i64 @stat(i8* %29, %struct.stat* %4)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %44
32: ; preds = %12
%33 = load i64, i64* @errno
%34 = load i64, i64* @ENOENT
%35 = icmp ne i64 %33, %34
br i1 %35, label %40, label %36
36: ; preds = %32
%37 = load i8*, i8** %3
%38 = call i64 @mkdir(i8* %37, i32 511)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36, %32
%41 = load i8*, i8** %3
%42 = call i32 @paxwarn(i32 1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %41)
store i32 -1, i32* %2
br label %61
43: ; preds = %36
br label %54
44: ; preds = %12
%45 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i32 @S_ISDIR(i32 %46)
%48 = icmp ne i32 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %44
%50 = load i32, i32* @ENOTDIR
%51 = load i8*, i8** %3
%52 = call i32 @syswarn(i32 1, i32 %50, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %51)
store i32 -1, i32* %2
br label %61
53: ; preds = %44
br label %54
54: ; preds = %53, %43
%55 = load i32, i32* %6
%56 = icmp ne i32 %55, 0
br i1 %56, label %59, label %57
57: ; preds = %54
%58 = load i8*, i8** %5
store i8 47, i8* %58
br label %59
59: ; preds = %57, %54
br label %8
60: ; preds = %8
store i32 0, i32* %2
br label %61
61: ; preds = %60, %49, %40
%62 = load i32, i32* %2
ret i32 %62
}

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