#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__ EEXIST;
scalar_t__ ENOENT;
int /*<<< orphan*/ ENOTDIR;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
scalar_t__ mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcspn(char *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strspn(char *, char *);
int /*<<< orphan*/ warn(char *, char *);
int /*<<< orphan*/ warnx(char *, char *, int /*<<< orphan*/);
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) && errno != EEXIST)) {
        warn("%s", path);
        return (-1);
      }
    } else if (!S_ISDIR(sb.st_mode)) {
      warnx("%s: %s", path, strerror(ENOTDIR));
      return (-1);
    }
    *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    11b1 <mkpath+0x31>
jmp    12e7 <mkpath+0x167>
mov    -0x20(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1070 <strspn@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe24(%rip),%rsi
call   1080 <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   1090 <stat@plt>
cmp    $0x0,%rax
je     1291 <mkpath+0x111>
mov    0x2da5(%rip),%rax
mov    (%rax),%rax
mov    0x2d8b(%rip),%rcx
cmp    (%rcx),%rax
jne    1270 <mkpath+0xf0>
mov    -0x10(%rbp),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     128c <mkpath+0x10c>
mov    0x2d73(%rip),%rax
mov    (%rax),%rax
mov    0x2d71(%rip),%rcx
cmp    (%rcx),%rax
je     128c <mkpath+0x10c>
mov    -0x10(%rbp),%rsi
lea    0xd8b(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ee <mkpath+0x16e>
jmp    12db <mkpath+0x15b>
mov    -0x18(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12d6 <mkpath+0x156>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
lea    0xd3d(%rip),%rdi
call   1050 <warnx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ee <mkpath+0x16e>
jmp    12db <mkpath+0x15b>
mov    -0x20(%rbp),%rax
movb   $0x2f,(%rax)
jmp    119b <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    0xe6b(%rip),%r14
mov    0x2e34(%rip),%rbp
mov    %rdi,%rbx
jmp    11cb <mkpath+0x4b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
je     124e <mkpath+0xce>
add    %r15,%rbx
movb   $0x2f,(%rbx)
test   %r13b,%r13b
je     124a <mkpath+0xca>
mov    %rbx,%r13
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <strspn@plt>
movslq %eax,%rbx
add    %r13,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcspn@plt>
movslq %eax,%r15
movzbl (%r15,%rbx,1),%r13d
movb   $0x0,(%r15,%rbx,1)
mov    %r12,%rdi
mov    %rsp,%rsi
call   1090 <stat@plt>
test   %rax,%rax
je     11b0 <mkpath+0x30>
mov    0x0(%rbp),%rax
mov    0x2dae(%rip),%rcx
cmp    (%rcx),%rax
jne    1239 <mkpath+0xb9>
mov    %r12,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     11c0 <mkpath+0x40>
mov    0x0(%rbp),%rax
mov    0x2da4(%rip),%rcx
cmp    (%rcx),%rax
je     11c0 <mkpath+0x40>
lea    0xdc6(%rip),%rdi
mov    %r12,%rsi
call   1030 <warn@plt>
jmp    126d <mkpath+0xed>
xor    %eax,%eax
jmp    1272 <mkpath+0xf2>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd9f(%rip),%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1050 <warnx@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
@EEXIST = global i64 0
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [7 x i8] c"%s: %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 %61
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 %48
32: ; preds = %12
%33 = load i64, i64* @errno
%34 = load i64, i64* @ENOENT
%35 = icmp ne i64 %33, %34
br i1 %35, label %44, 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 %47
40: ; preds = %36
%41 = load i64, i64* @errno
%42 = load i64, i64* @EEXIST
%43 = icmp ne i64 %41, %42
br i1 %43, label %44, label %47
44: ; preds = %40, %32
%45 = load i8*, i8** %3
%46 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %45)
store i32 -1, i32* %2
br label %62
47: ; preds = %40, %36
br label %59
48: ; preds = %12
%49 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i32 @S_ISDIR(i32 %50)
%52 = icmp ne i32 %51, 0
br i1 %52, label %58, label %53
53: ; preds = %48
%54 = load i8*, i8** %3
%55 = load i32, i32* @ENOTDIR
%56 = call i32 @strerror(i32 %55)
%57 = call i32 @warnx(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %54, i32 %56)
store i32 -1, i32* %2
br label %62
58: ; preds = %48
br label %59
59: ; preds = %58, %47
%60 = load i8*, i8** %5
store i8 47, i8* %60
br label %8
61: ; preds = %8
store i32 0, i32* %2
br label %62
62: ; preds = %61, %53, %44
%63 = load i32, i32* %2
ret i32 %63
}

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 = [3 x i8] c"%s\00"
@.str.2 = [7 x i8] c"%s: %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 = %1, %38
%6 = phi i8* [ %0, %1 ], [ %12, %38 ]
%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 %30, 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 %28
21: ; preds = %17
%22 = call i64 @mkdir(i8* %0, i32 511)
%23 = icmp eq i64 %22, 0
br i1 %23, label %38, label %24
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @EEXIST
%27 = icmp eq i64 %25, %26
br i1 %27, label %38, label %28
28: ; preds = %24, %17
%29 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %39
30: ; preds = %5
%31 = load i32, i32* %4
%32 = call i32 @S_ISDIR(i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i32, i32* @ENOTDIR
%36 = call i32 @strerror(i32 %35)
%37 = call i32 @warnx(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %36)
br label %39
38: ; preds = %30, %21, %24
store i8 47, i8* %12
br i1 %14, label %39, label %5
39: ; preds = %38, %34, %28
%40 = phi i32 [ -1, %28 ], [ -1, %34 ], [ 0, %38 ]
ret i32 %40
}