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

/* Variables and functions */
int ENOTDIR;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int chmod(char *, int);
int errno;
int mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcspn(char *, char *);
int /*<<< orphan*/ strspn(char *, char *);
int /*<<< orphan*/ warn(char *, char *);
int mkpath(char *path, mode_t mode, mode_t dir_mode) {
  struct stat sb;
  char *slash;
  int done, rv;
  done = 0;
  slash = path;
  for (;;) {
    slash += strspn(slash, "/");
    slash += strcspn(slash, "/");
    done = (*slash == '\0');
    *slash = '\0';
    rv = mkdir(path, done ? mode : dir_mode);
    if (rv < 0) {
      int sverrno;
      sverrno = errno;
      if (stat(path, &sb) < 0) {
        errno = sverrno;
        warn("%s", path);
        return -1;
      }
      if (!S_ISDIR(sb.st_mode)) {
        errno = ENOTDIR;
        warn("%s", path);
        return -1;
      }
    } else if (done) {
      if ((mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) != 0 &&
          chmod(path, mode) == -1) {
        warn("%s", path);
        return -1;
      }
    }
    if (done) {
      break;
    }
    *slash = '/';
  }
  return 0;
}


x86 assembly (clang, -O0):
<mkpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe64(%rip),%rsi
call   1060 <strspn@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1070 <strcspn@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     120a <mkpath+0x9a>
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
jmp    1210 <mkpath+0xa0>
mov    -0x18(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x44(%rbp),%esi
call   1040 <mkdir@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    12b8 <mkpath+0x148>
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    1274 <mkpath+0x104>
mov    -0x34(%rbp),%ecx
mov    0x2d6a(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rsi
lea    0xd9f(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <mkpath+0x1db>
mov    -0x20(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12b3 <mkpath+0x143>
mov    0x2d6c(%rip),%rax
mov    (%rax),%ecx
mov    0x2d2b(%rip),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rsi
lea    0xd60(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <mkpath+0x1db>
jmp    1329 <mkpath+0x1b9>
cmpl   $0x0,-0x2c(%rbp)
je     1324 <mkpath+0x1b4>
mov    -0x14(%rbp),%eax
mov    0x2d0c(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2cfb(%rip),%rdx
or     (%rdx),%ecx
mov    0x2d0a(%rip),%rdx
or     (%rdx),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     131f <mkpath+0x1af>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <chmod@plt>
cmp    $0xffffffff,%eax
jne    131f <mkpath+0x1af>
mov    -0x10(%rbp),%rsi
lea    0xcf4(%rip),%rdi
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134b <mkpath+0x1db>
jmp    1324 <mkpath+0x1b4>
jmp    1329 <mkpath+0x1b9>
cmpl   $0x0,-0x2c(%rbp)
je     1338 <mkpath+0x1c8>
jmp    1344 <mkpath+0x1d4>
mov    -0x28(%rbp),%rax
movb   $0x2f,(%rax)
jmp    1191 <mkpath+0x21>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkpath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,0xc(%rsp)
mov    %esi,%r15d
mov    %rdi,%r12
lea    0xe71(%rip),%r13
mov    %rdi,%rbx
mov    %rdi,0x10(%rsp)
jmp    11bf <mkpath+0x4f>
nopl   0x0(%rax)
mov    %r12,%rdi
call   1040 <mkdir@plt>
test   %eax,%eax
js     1200 <mkpath+0x90>
test   %r14b,%r14b
je     123b <mkpath+0xcb>
add    %rbx,%rbp
movb   $0x2f,0x0(%rbp)
mov    %rbp,%rbx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1060 <strspn@plt>
movslq %eax,%rbp
add    %rbx,%rbp
mov    %rbp,%rdi
mov    %r13,%rsi
call   1070 <strcspn@plt>
movslq %eax,%rbx
movzbl (%rbx,%rbp,1),%r14d
movb   $0x0,(%rbx,%rbp,1)
mov    %r15d,%esi
test   %r14b,%r14b
je     11a0 <mkpath+0x30>
mov    0xc(%rsp),%esi
jmp    11a0 <mkpath+0x30>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x2db9(%rip),%rax
mov    %r12,%rdi
mov    (%rax),%r12d
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
js     127b <mkpath+0x10b>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
test   %eax,%eax
je     1271 <mkpath+0x101>
test   %r14b,%r14b
mov    0x10(%rsp),%r12
jne    11b5 <mkpath+0x45>
xor    %ebp,%ebp
jmp    129e <mkpath+0x12e>
mov    0x2d96(%rip),%rax
mov    0x2d87(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d94(%rip),%rax
or     (%rax),%ecx
not    %ecx
xor    %ebp,%ebp
test   %r15d,%ecx
je     129e <mkpath+0x12e>
mov    %r12,%rdi
mov    %r15d,%esi
call   1090 <chmod@plt>
cmp    $0xffffffff,%eax
jne    129e <mkpath+0x12e>
jmp    128a <mkpath+0x11a>
mov    0x2d80(%rip),%rax
mov    (%rax),%r12d
mov    0x2d3e(%rip),%rax
mov    %r12d,(%rax)
mov    0x10(%rsp),%r12
lea    0xd71(%rip),%rdi
mov    %r12,%rsi
call   1030 <warn@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%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 i32 0
@.str.1 = [3 x i8] c"%s\00"
@ENOTDIR = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
define i32 @mkpath(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %10
%13 = load i8*, i8** %5
store i8* %13, i8** %9
br label %14
14: ; preds = %89, %3
%15 = load i8*, i8** %9
%16 = call i32 @strspn(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = load i8*, i8** %9
%18 = sext i32 %16 to i64
%19 = getelementptr inbounds i8, i8* %17, i64 %18
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = call i32 @strcspn(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = load i8*, i8** %9
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %22, i64 %23
store i8* %24, i8** %9
%25 = load i8*, i8** %9
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 0
%29 = zext i1 %28 to i32
store i32 %29, i32* %10
%30 = load i8*, i8** %9
store i8 0, i8* %30
%31 = load i8*, i8** %5
%32 = load i32, i32* %10
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %14
%35 = load i32, i32* %6
br label %38
36: ; preds = %14
%37 = load i32, i32* %7
br label %38
38: ; preds = %36, %34
%39 = phi i32 [ %35, %34 ], [ %37, %36 ]
%40 = call i32 @mkdir(i8* %31, i32 %39)
store i32 %40, i32* %11
%41 = load i32, i32* %11
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %62
43: ; preds = %38
%44 = load i32, i32* @errno
store i32 %44, i32* %12
%45 = load i8*, i8** %5
%46 = call i64 @stat(i8* %45, %struct.stat* %8)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %43
%49 = load i32, i32* %12
store i32 %49, i32* @errno
%50 = load i8*, i8** %5
%51 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %50)
store i32 -1, i32* %4
br label %92
52: ; preds = %43
%53 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 @S_ISDIR(i32 %54)
%56 = icmp ne i32 %55, 0
br i1 %56, label %61, label %57
57: ; preds = %52
%58 = load i32, i32* @ENOTDIR
store i32 %58, i32* @errno
%59 = load i8*, i8** %5
%60 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %59)
store i32 -1, i32* %4
br label %92
61: ; preds = %52
br label %85
62: ; preds = %38
%63 = load i32, i32* %10
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %84
65: ; preds = %62
%66 = load i32, i32* %6
%67 = load i32, i32* @S_IRWXU
%68 = load i32, i32* @S_IRWXG
%69 = or i32 %67, %68
%70 = load i32, i32* @S_IRWXO
%71 = or i32 %69, %70
%72 = xor i32 %71, -1
%73 = and i32 %66, %72
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %83
75: ; preds = %65
%76 = load i8*, i8** %5
%77 = load i32, i32* %6
%78 = call i32 @chmod(i8* %76, i32 %77)
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %83
80: ; preds = %75
%81 = load i8*, i8** %5
%82 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %81)
store i32 -1, i32* %4
br label %92
83: ; preds = %75, %65
br label %84
84: ; preds = %83, %62
br label %85
85: ; preds = %84, %61
%86 = load i32, i32* %10
%87 = icmp ne i32 %86, 0
br i1 %87, label %88, label %89
88: ; preds = %85
br label %91
89: ; preds = %85
%90 = load i8*, i8** %9
store i8 47, i8* %90
br label %14
91: ; preds = %88
store i32 0, i32* %4
br label %92
92: ; preds = %91, %80, %57, %48
%93 = load i32, i32* %4
ret i32 %93
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [2 x i8] c"/\00"
@errno = global i32 0
@.str.1 = [3 x i8] c"%s\00"
@ENOTDIR = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
define i32 @mkpath(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %7
7: ; preds = %44, %3
%8 = phi i8* [ %0, %3 ], [ %14, %44 ]
%9 = call i32 @strspn(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %8, i64 %10
%12 = call i32 @strcspn(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 = load i8, i8* %14
%16 = icmp eq i8 %15, 0
store i8 0, i8* %14
%17 = select i1 %16, i32 %1, i32 %2
%18 = call i32 @mkdir(i8* %0, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %30
20: ; preds = %7
%21 = load i32, i32* @errno
%22 = call i64 @stat(i8* %0, %struct.stat* %4)
%23 = icmp slt i64 %22, 0
br i1 %23, label %45, label %24
24: ; preds = %20
%25 = load i32, i32* %6
%26 = call i32 @S_ISDIR(i32 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %43
28: ; preds = %24
%29 = load i32, i32* @ENOTDIR
br label %45
30: ; preds = %7
br i1 %16, label %31, label %44
31: ; preds = %30
%32 = load i32, i32* @S_IRWXU
%33 = load i32, i32* @S_IRWXG
%34 = or i32 %33, %32
%35 = load i32, i32* @S_IRWXO
%36 = or i32 %34, %35
%37 = xor i32 %36, -1
%38 = and i32 %37, %1
%39 = icmp eq i32 %38, 0
br i1 %39, label %49, label %40
40: ; preds = %31
%41 = call i32 @chmod(i8* %0, i32 %1)
%42 = icmp eq i32 %41, -1
br i1 %42, label %47, label %49
43: ; preds = %24
br i1 %16, label %49, label %44
44: ; preds = %30, %43
store i8 47, i8* %14
br label %7
45: ; preds = %20, %28
%46 = phi i32 [ %29, %28 ], [ %21, %20 ]
store i32 %46, i32* @errno
br label %47
47: ; preds = %45, %40
%48 = call i32 @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %49
49: ; preds = %43, %47, %31, %40
%50 = phi i32 [ 0, %40 ], [ 0, %31 ], [ -1, %47 ], [ 0, %43 ]
ret i32 %50
}