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

/* Variables and functions */
scalar_t__ EEXIST;
int PATH_MAX;
scalar_t__ errno;
scalar_t__ mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
size_t strlen(char *);
int mkpath(const char *path, mode_t mode) {
  char tmp[PATH_MAX];
  char *p = NULL;
  size_t len;
  snprintf(tmp, sizeof(tmp), "%s", path);
  len = strlen(tmp);
  if (tmp[len - 1] == '/')
    tmp[len - 1] = 0;
  for (p = tmp + 1; *p; p++)
    if (*p == '/') {
      *p = 0;
      if (mkdir(tmp, mode) < 0 && errno != EEXIST)
        return -1;
      *p = '/';
    }
  if (mkdir(tmp, mode) < 0 && errno != EEXIST)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<mkpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2eaa(%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,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xe80(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x48(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
mov    %rcx,-0x38(%rbp)
mov    -0x38(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11be <mkpath+0x8e>
mov    -0x48(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
je     124e <mkpath+0x11e>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    1238 <mkpath+0x108>
mov    -0x48(%rbp),%rdi
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x14(%rbp),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
jge    1231 <mkpath+0x101>
mov    0x2dc5(%rip),%rax
mov    (%rax),%rax
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%rax
je     1231 <mkpath+0x101>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    129f <mkpath+0x16f>
mov    -0x30(%rbp),%rax
movb   $0x2f,(%rax)
jmp    123d <mkpath+0x10d>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11ca <mkpath+0x9a>
mov    -0x48(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
jge    1291 <mkpath+0x161>
mov    0x2d65(%rip),%rax
mov    (%rax),%rax
mov    0x2d63(%rip),%rcx
cmp    (%rcx),%rax
je     1291 <mkpath+0x161>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    129f <mkpath+0x16f>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkpath>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rcx
mov    0x2ea5(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%r13
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    0xe98(%rip),%rdx
mov    %r13,%rdi
call   1050 <snprintf@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
cmpb   $0x2f,-0x1(%rax,%r13,1)
jne    1186 <mkpath+0x56>
movb   $0x0,-0x1(%r13,%rax,1)
lea    0x1(%r13),%rbx
mov    0x2e3f(%rip),%r12
mov    0x2e40(%rip),%r15
jmp    11ba <mkpath+0x8a>
nopw   0x0(%rax,%rax,1)
movb   $0x0,(%rbx)
mov    %r13,%rdi
mov    %r14d,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
js     11cb <mkpath+0x9b>
movb   $0x2f,(%rbx)
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x2f,%al
je     11a0 <mkpath+0x70>
test   %al,%al
je     11db <mkpath+0xab>
add    $0x1,%rbx
jmp    11ba <mkpath+0x8a>
mov    (%r12),%rax
cmp    (%r15),%rax
je     11b3 <mkpath+0x83>
mov    $0xffffffff,%eax
jmp    11fb <mkpath+0xcb>
mov    %r13,%rdi
mov    %r14d,%esi
call   1030 <mkdir@plt>
test   %rax,%rax
jns    11f9 <mkpath+0xc9>
mov    (%r12),%rcx
mov    $0xffffffff,%eax
cmp    (%r15),%rcx
jne    11fb <mkpath+0xcb>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [3 x i8] c"%s\00"
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkpath(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %6
%14 = alloca i8, i64 %12
store i64 %12, i64* %7
store i8* null, i8** %8
%15 = trunc i64 %12 to i32
%16 = load i8*, i8** %4
%17 = call i32 @snprintf(i8* %14, i32 %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i64 @strlen(i8* %14)
store i64 %18, i64* %9
%19 = load i64, i64* %9
%20 = sub i64 %19, 1
%21 = getelementptr inbounds i8, i8* %14, i64 %20
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 47
br i1 %24, label %25, label %29
25: ; preds = %2
%26 = load i64, i64* %9
%27 = sub i64 %26, 1
%28 = getelementptr inbounds i8, i8* %14, i64 %27
store i8 0, i8* %28
br label %29
29: ; preds = %25, %2
%30 = getelementptr inbounds i8, i8* %14, i64 1
store i8* %30, i8** %8
br label %31
31: ; preds = %53, %29
%32 = load i8*, i8** %8
%33 = load i8, i8* %32
%34 = icmp ne i8 %33, 0
br i1 %34, label %35, label %56
35: ; preds = %31
%36 = load i8*, i8** %8
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 47
br i1 %39, label %40, label %52
40: ; preds = %35
%41 = load i8*, i8** %8
store i8 0, i8* %41
%42 = load i32, i32* %5
%43 = call i64 @mkdir(i8* %14, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %40
%46 = load i64, i64* @errno
%47 = load i64, i64* @EEXIST
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %45
store i32 -1, i32* %3
store i32 1, i32* %10
br label %66
50: ; preds = %45, %40
%51 = load i8*, i8** %8
store i8 47, i8* %51
br label %52
52: ; preds = %50, %35
br label %53
53: ; preds = %52
%54 = load i8*, i8** %8
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %8
br label %31
56: ; preds = %31
%57 = load i32, i32* %5
%58 = call i64 @mkdir(i8* %14, i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %56
%61 = load i64, i64* @errno
%62 = load i64, i64* @EEXIST
%63 = icmp ne i64 %61, %62
br i1 %63, label %64, label %65
64: ; preds = %60
store i32 -1, i32* %3
store i32 1, i32* %10
br label %66
65: ; preds = %60, %56
store i32 0, i32* %3
store i32 1, i32* %10
br label %66
66: ; preds = %65, %64, %49
%67 = load i8*, i8** %6
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [3 x i8] c"%s\00"
@errno = global i64 0
@EEXIST = global i64 0
define i32 @mkpath(i8* %0, i32 %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%7 = call i64 @strlen(i8* %5)
%8 = add i64 %7, -1
%9 = getelementptr inbounds i8, i8* %5, i64 %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 47
br i1 %11, label %12, label %13
12: ; preds = %2
store i8 0, i8* %9
br label %13
13: ; preds = %12, %2
br label %14
14: ; preds = %18, %13
%15 = phi i8* [ %5, %13 ], [ %16, %18 ]
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = load i8, i8* %16
switch i8 %17, label %18 [
i8 0, label %27
i8 47, label %19
]
18: ; preds = %14, %26
br label %14
19: ; preds = %14
store i8 0, i8* %16
%20 = call i64 @mkdir(i8* %5, i32 %1)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i64, i64* @errno
%24 = load i64, i64* @EEXIST
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %35
26: ; preds = %22, %19
store i8 47, i8* %16
br label %18
27: ; preds = %14
%28 = call i64 @mkdir(i8* %5, i32 %1)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %27
%31 = load i64, i64* @errno
%32 = load i64, i64* @EEXIST
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %35
34: ; preds = %30, %27
br label %35
35: ; preds = %22, %30, %34
%36 = phi i32 [ 0, %34 ], [ -1, %30 ], [ -1, %22 ]
ret i32 %36
}