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

/* Variables and functions */
scalar_t__ EEXIST;
int PATH_MAX;
int S_IRWXG;
int S_IRWXO;
int S_IRWXU;
scalar_t__ errno;
int /*<<< orphan*/ estrlcpy(char *, char const *, int);
scalar_t__ mkdir(char *, int);
int /*<<< orphan*/ weprintf(char *, char *);
int mkdirp(const char *path) {
  char tmp[PATH_MAX], *p;
  estrlcpy(tmp, path, sizeof(tmp));
  for (p = tmp + (tmp[0] == '/'); *p; p++) {
    if (*p != '/')
      continue;
    *p = '\0';
    if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST) {
      weprintf("mkdir %s:", tmp);
      return -1;
    }
    *p = '/';
  }
  if (mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO) < 0 && errno != EEXIST) {
    weprintf("mkdir %s:", tmp);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<mkdirp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
mov    %eax,%edx
call   1050 <estrlcpy@plt>
mov    -0x38(%rbp),%rax
movsbl (%rax),%ecx
cmp    $0x2f,%ecx
sete   %cl
and    $0x1,%cl
movzbl %cl,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     123c <mkdirp+0x10c>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11b2 <mkdirp+0x82>
jmp    122b <mkdirp+0xfb>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2e14(%rip),%rax
mov    (%rax),%esi
mov    0x2e03(%rip),%rax
or     (%rax),%esi
mov    0x2e12(%rip),%rax
or     (%rax),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
jge    1224 <mkdirp+0xf4>
mov    0x2dca(%rip),%rax
mov    (%rax),%rax
mov    0x2dc8(%rip),%rcx
cmp    (%rcx),%rax
je     1224 <mkdirp+0xf4>
mov    -0x38(%rbp),%rsi
lea    0xdf4(%rip),%rdi
call   1040 <weprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12b5 <mkdirp+0x185>
mov    -0x28(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1190 <mkdirp+0x60>
mov    -0x38(%rbp),%rdi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
mov    0x2d80(%rip),%rax
or     (%rax),%esi
mov    0x2d8f(%rip),%rax
or     (%rax),%esi
call   1030 <mkdir@plt>
cmp    $0x0,%rax
jge    12a7 <mkdirp+0x177>
mov    0x2d47(%rip),%rax
mov    (%rax),%rax
mov    0x2d45(%rip),%rcx
cmp    (%rcx),%rax
je     12a7 <mkdirp+0x177>
mov    -0x38(%rbp),%rsi
lea    0xd71(%rip),%rdi
call   1040 <weprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    12b5 <mkdirp+0x185>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdirp>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rsi
mov    0x2ea8(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%r14
mov    %rdx,%r15
add    $0xf,%r15
and    $0xfffffffffffffff0,%r15
mov    %r14,%r12
sub    %r15,%r12
mov    %r12,%rsp
neg    %r15
mov    %r12,%rdi
call   1050 <estrlcpy@plt>
xor    %ebx,%ebx
cmpb   $0x2f,(%r14,%r15,1)
sete   %bl
add    %r12,%rbx
mov    0x2e58(%rip),%r13
mov    0x2e49(%rip),%r15
mov    0x2e5a(%rip),%r14
jmp    1198 <mkdirp+0x68>
test   %al,%al
je     11d4 <mkdirp+0xa4>
add    $0x1,%rbx
movzbl (%rbx),%eax
cmp    $0x2f,%al
jne    1190 <mkdirp+0x60>
movb   $0x0,(%rbx)
mov    (%r15),%esi
or     0x0(%r13),%esi
or     (%r14),%esi
mov    %r12,%rdi
call   1030 <mkdir@plt>
test   %rax,%rax
jns    11cf <mkdirp+0x9f>
mov    0x2df8(%rip),%rax
mov    (%rax),%rax
mov    0x2df6(%rip),%rcx
cmp    (%rcx),%rax
jne    1206 <mkdirp+0xd6>
movb   $0x2f,(%rbx)
jmp    1194 <mkdirp+0x64>
mov    (%r15),%esi
or     0x0(%r13),%esi
or     (%r14),%esi
mov    %r12,%rdi
call   1030 <mkdir@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    121a <mkdirp+0xea>
mov    0x2dc1(%rip),%rcx
mov    (%rcx),%rcx
mov    0x2dbf(%rip),%rdx
cmp    (%rdx),%rcx
je     121a <mkdirp+0xea>
lea    0xdf3(%rip),%rdi
mov    %r12,%rsi
call   1040 <weprintf@plt>
mov    $0xffffffff,%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
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IRWXO = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str = [10 x i8] c"mkdir %s:\00"
define i32 @mkdirp(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %4
%11 = alloca i8, i64 %9
store i64 %9, i64* %5
%12 = load i8*, i8** %3
%13 = trunc i64 %9 to i32
%14 = call i32 @estrlcpy(i8* %11, i8* %12, i32 %13)
%15 = getelementptr inbounds i8, i8* %11, i64 0
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 47
%19 = zext i1 %18 to i32
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %11, i64 %20
store i8* %21, i8** %6
br label %22
22: ; preds = %49, %1
%23 = load i8*, i8** %6
%24 = load i8, i8* %23
%25 = icmp ne i8 %24, 0
br i1 %25, label %26, label %52
26: ; preds = %22
%27 = load i8*, i8** %6
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 47
br i1 %30, label %31, label %32
31: ; preds = %26
br label %49
32: ; preds = %26
%33 = load i8*, i8** %6
store i8 0, i8* %33
%34 = load i32, i32* @S_IRWXU
%35 = load i32, i32* @S_IRWXG
%36 = or i32 %34, %35
%37 = load i32, i32* @S_IRWXO
%38 = or i32 %36, %37
%39 = call i64 @mkdir(i8* %11, i32 %38)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %47
41: ; preds = %32
%42 = load i64, i64* @errno
%43 = load i64, i64* @EEXIST
%44 = icmp ne i64 %42, %43
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @weprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %11)
store i32 -1, i32* %2
store i32 1, i32* %7
br label %67
47: ; preds = %41, %32
%48 = load i8*, i8** %6
store i8 47, i8* %48
br label %49
49: ; preds = %47, %31
%50 = load i8*, i8** %6
%51 = getelementptr inbounds i8, i8* %50, i32 1
store i8* %51, i8** %6
br label %22
52: ; preds = %22
%53 = load i32, i32* @S_IRWXU
%54 = load i32, i32* @S_IRWXG
%55 = or i32 %53, %54
%56 = load i32, i32* @S_IRWXO
%57 = or i32 %55, %56
%58 = call i64 @mkdir(i8* %11, i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %66
60: ; preds = %52
%61 = load i64, i64* @errno
%62 = load i64, i64* @EEXIST
%63 = icmp ne i64 %61, %62
br i1 %63, label %64, label %66
64: ; preds = %60
%65 = call i32 @weprintf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %11)
store i32 -1, i32* %2
store i32 1, i32* %7
br label %67
66: ; preds = %60, %52
store i32 0, i32* %2
store i32 1, i32* %7
br label %67
67: ; preds = %66, %64, %45
%68 = load i8*, i8** %4
%69 = load i32, i32* %2
ret i32 %69
}

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