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

/* Variables and functions */
scalar_t__ chmod(char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int eslib_file_exists(char *);
int eslib_file_isdir(char *);
scalar_t__ eslib_file_path_check(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int mkdir(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int eslib_file_mkdirpath(char *path, mode_t mode) {
  int i;
  char curdir[2048];
  int ret;
  if (eslib_file_path_check(path))
    return -1;
  memset(curdir, 0, 2048);
  for (i = 1; i < 2048 - 1; ++i) {
    while (i < 2048 - 1) {
      if (path[i] == '/' || path[i] == '\0')
        break;
      ++i;
    }
    if (i >= 2048 - 1)
      break;
    strncpy(curdir, path, (size_t)i);
    curdir[i] = '\0';
    ret = eslib_file_exists(curdir);
    if (ret == 0) {
      if (mkdir(curdir, mode) == -1) {
        printf("error creating directory: %s\n", strerror(errno));
        return -1;
      }
      if (chmod(curdir, mode)) {
        printf("chmod(%s): %s\n", curdir, strerror(errno));
        return -1;
      }
    } else if (ret == 1) {
      if (eslib_file_isdir(curdir) != 1) {
        printf("path exists but not a directory:%s\n", curdir);
        return -1;
      }
    } else {
      printf("file error(%s): %s\n", curdir, strerror(errno));
      return -1;
    }
    if (path[i] == '\0')
      break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<eslib_file_mkdirpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <eslib_file_path_check@plt>
cmp    $0x0,%rax
je     11c1 <eslib_file_mkdirpath+0x31>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ea <eslib_file_mkdirpath+0x25a>
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1070 <memset@plt>
movl   $0x1,-0x18(%rbp)
cmpl   $0x7ff,-0x18(%rbp)
jge    13e3 <eslib_file_mkdirpath+0x253>
jmp    11ed <eslib_file_mkdirpath+0x5d>
cmpl   $0x7ff,-0x18(%rbp)
jge    1237 <eslib_file_mkdirpath+0xa7>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1224 <eslib_file_mkdirpath+0x94>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    1229 <eslib_file_mkdirpath+0x99>
jmp    1237 <eslib_file_mkdirpath+0xa7>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ed <eslib_file_mkdirpath+0x5d>
cmpl   $0x7ff,-0x18(%rbp)
jl     1249 <eslib_file_mkdirpath+0xb9>
jmp    13e3 <eslib_file_mkdirpath+0x253>
lea    -0x820(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x820(%rbp,%rax,1)
lea    -0x820(%rbp),%rdi
call   1080 <eslib_file_exists@plt>
mov    %eax,-0x824(%rbp)
cmpl   $0x0,-0x824(%rbp)
jne    1329 <eslib_file_mkdirpath+0x199>
lea    -0x820(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    12cb <eslib_file_mkdirpath+0x13b>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd48(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ea <eslib_file_mkdirpath+0x25a>
lea    -0x820(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10a0 <chmod@plt>
cmp    $0x0,%rax
je     1324 <eslib_file_mkdirpath+0x194>
lea    -0x820(%rbp),%rax
mov    %rax,-0x830(%rbp)
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x830(%rbp),%rsi
mov    %rax,%rdx
lea    0xd0d(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ea <eslib_file_mkdirpath+0x25a>
jmp    13b6 <eslib_file_mkdirpath+0x226>
cmpl   $0x1,-0x824(%rbp)
jne    1371 <eslib_file_mkdirpath+0x1e1>
lea    -0x820(%rbp),%rdi
call   1090 <eslib_file_isdir@plt>
cmp    $0x1,%eax
je     136c <eslib_file_mkdirpath+0x1dc>
lea    -0x820(%rbp),%rsi
lea    0xcd4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ea <eslib_file_mkdirpath+0x25a>
jmp    13b1 <eslib_file_mkdirpath+0x221>
lea    -0x820(%rbp),%rax
mov    %rax,-0x838(%rbp)
mov    0x2c5a(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x838(%rbp),%rsi
mov    %rax,%rdx
lea    0xcb3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13ea <eslib_file_mkdirpath+0x25a>
jmp    13b6 <eslib_file_mkdirpath+0x226>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
jne    13d0 <eslib_file_mkdirpath+0x240>
jmp    13e3 <eslib_file_mkdirpath+0x253>
jmp    13d5 <eslib_file_mkdirpath+0x245>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11db <eslib_file_mkdirpath+0x4b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<eslib_file_mkdirpath>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %esi,%ebx
mov    %rdi,%r12
call   1050 <eslib_file_path_check@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
jne    12bb <eslib_file_mkdirpath+0x12b>
mov    %ebx,0xc(%rsp)
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1070 <memset@plt>
mov    $0x1,%ebp
movabs $0x100000000,%r13
mov    %rbp,%rbx
shl    $0x20,%rbx
mov    %ebp,%r14d
movslq %ebp,%rbp
add    $0x1,%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl -0x1(%r12,%rbp,1),%eax
test   %al,%al
je     1230 <eslib_file_mkdirpath+0xa0>
cmp    $0x2f,%al
je     1230 <eslib_file_mkdirpath+0xa0>
add    $0x1,%r14d
add    %r13,%rbx
add    $0x1,%rbp
cmp    $0x7ff,%r14d
jne    1200 <eslib_file_mkdirpath+0x70>
jmp    12b9 <eslib_file_mkdirpath+0x129>
nopw   0x0(%rax,%rax,1)
sar    $0x20,%rbx
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1030 <strncpy@plt>
movb   $0x0,0x10(%rsp,%rbx,1)
mov    %r15,%rdi
call   1080 <eslib_file_exists@plt>
cmp    $0x1,%eax
je     1290 <eslib_file_mkdirpath+0x100>
test   %eax,%eax
jne    12cd <eslib_file_mkdirpath+0x13d>
mov    %r15,%rdi
mov    0xc(%rsp),%r15d
mov    %r15d,%esi
call   1040 <mkdir@plt>
cmp    $0xffffffff,%eax
je     12e4 <eslib_file_mkdirpath+0x154>
lea    0x10(%rsp),%rdi
mov    %r15d,%esi
call   10a0 <chmod@plt>
test   %rax,%rax
lea    0x10(%rsp),%r15
je     12a1 <eslib_file_mkdirpath+0x111>
jmp    12fe <eslib_file_mkdirpath+0x16e>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1090 <eslib_file_isdir@plt>
cmp    $0x1,%eax
jne    1329 <eslib_file_mkdirpath+0x199>
xor    %eax,%eax
cmpb   $0x0,(%r12,%rbx,1)
je     12bb <eslib_file_mkdirpath+0x12b>
cmp    $0x7fe,%r14d
jl     11e3 <eslib_file_mkdirpath+0x53>
jmp    12bb <eslib_file_mkdirpath+0x12b>
xor    %eax,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd6f(%rip),%rdi
jmp    1313 <eslib_file_mkdirpath+0x183>
mov    0x2cf5(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd07(%rip),%rdi
mov    %rax,%rsi
jmp    1335 <eslib_file_mkdirpath+0x1a5>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd0b(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%eax
jmp    12bb <eslib_file_mkdirpath+0x12b>
lea    0xcfd(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%eax
jmp    12bb <eslib_file_mkdirpath+0x12b>

LLVM IR (-O0):
@.str = [30 x i8] c"error creating directory: %s\0A\00"
@errno = global i32 0
@.str.1 = [15 x i8] c"chmod(%s): %s\0A\00"
@.str.2 = [36 x i8] c"path exists but not a directory:%s\0A\00"
@.str.3 = [20 x i8] c"file error(%s): %s\0A\00"
define i32 @eslib_file_mkdirpath(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca [2048 x i8]
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i8*, i8** %4
%10 = call i64 @eslib_file_path_check(i8* %9)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %111
13: ; preds = %2
%14 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%15 = call i32 @memset(i8* %14, i32 0, i32 2048)
store i32 1, i32* %6
br label %16
16: ; preds = %107, %13
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 2047
br i1 %18, label %19, label %110
19: ; preds = %16
br label %20
20: ; preds = %40, %19
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 2047
br i1 %22, label %23, label %43
23: ; preds = %20
%24 = load i8*, i8** %4
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 47
br i1 %30, label %39, label %31
31: ; preds = %23
%32 = load i8*, i8** %4
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %31, %23
br label %43
40: ; preds = %31
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 1
store i32 %42, i32* %6
br label %20
43: ; preds = %39, %20
%44 = load i32, i32* %6
%45 = icmp sge i32 %44, 2047
br i1 %45, label %46, label %47
46: ; preds = %43
br label %110
47: ; preds = %43
%48 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%49 = load i8*, i8** %4
%50 = load i32, i32* %6
%51 = sext i32 %50 to i64
%52 = call i32 @strncpy(i8* %48, i8* %49, i64 %51)
%53 = load i32, i32* %6
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 %54
store i8 0, i8* %55
%56 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%57 = call i32 @eslib_file_exists(i8* %56)
store i32 %57, i32* %8
%58 = load i32, i32* %8
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %80
60: ; preds = %47
%61 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%62 = load i32, i32* %5
%63 = call i32 @mkdir(i8* %61, i32 %62)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %69
65: ; preds = %60
%66 = load i32, i32* @errno
%67 = call i8* @strerror(i32 %66)
%68 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %67)
store i32 -1, i32* %3
br label %111
69: ; preds = %60
%70 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%71 = load i32, i32* %5
%72 = call i64 @chmod(i8* %70, i32 %71)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %79
74: ; preds = %69
%75 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%76 = load i32, i32* @errno
%77 = call i8* @strerror(i32 %76)
%78 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %75, i8* %77)
store i32 -1, i32* %3
br label %111
79: ; preds = %69
br label %97
80: ; preds = %47
%81 = load i32, i32* %8
%82 = icmp eq i32 %81, 1
br i1 %82, label %83, label %91
83: ; preds = %80
%84 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%85 = call i32 @eslib_file_isdir(i8* %84)
%86 = icmp ne i32 %85, 1
br i1 %86, label %87, label %90
87: ; preds = %83
%88 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%89 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %88)
store i32 -1, i32* %3
br label %111
90: ; preds = %83
br label %96
91: ; preds = %80
%92 = getelementptr inbounds [2048 x i8], [2048 x i8]* %7, i64 0, i64 0
%93 = load i32, i32* @errno
%94 = call i8* @strerror(i32 %93)
%95 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %92, i8* %94)
store i32 -1, i32* %3
br label %111
96: ; preds = %90
br label %97
97: ; preds = %96, %79
%98 = load i8*, i8** %4
%99 = load i32, i32* %6
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i8, i8* %98, i64 %100
%102 = load i8, i8* %101
%103 = sext i8 %102 to i32
%104 = icmp eq i32 %103, 0
br i1 %104, label %105, label %106
105: ; preds = %97
br label %110
106: ; preds = %97
br label %107
107: ; preds = %106
%108 = load i32, i32* %6
%109 = add nsw i32 %108, 1
store i32 %109, i32* %6
br label %16
110: ; preds = %105, %46, %16
store i32 0, i32* %3
br label %111
111: ; preds = %110, %91, %87, %74, %65, %12
%112 = load i32, i32* %3
ret i32 %112
}

LLVM IR (-O2):
@.str = [30 x i8] c"error creating directory: %s\0A\00"
@errno = global i32 0
@.str.1 = [15 x i8] c"chmod(%s): %s\0A\00"
@.str.2 = [36 x i8] c"path exists but not a directory:%s\0A\00"
@.str.3 = [20 x i8] c"file error(%s): %s\0A\00"
define i32 @eslib_file_mkdirpath(i8* %0, i32 %1) {
%3 = alloca [2048 x i8]
%4 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 0
%5 = tail call i64 @eslib_file_path_check(i8* %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %58
7: ; preds = %2
%8 = call i32 @memset(i8* %4, i32 0, i32 2048)
br label %9
9: ; preds = %51, %7
%10 = phi i64 [ 1, %7 ], [ %55, %51 ]
%11 = shl i64 %10, 32
%12 = ashr exact i64 %11, 32
br label %13
13: ; preds = %9, %17
%14 = phi i64 [ %12, %9 ], [ %18, %17 ]
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = load i8, i8* %15
switch i8 %16, label %17 [
i8 47, label %21
i8 0, label %21
]
17: ; preds = %13
%18 = add nsw i64 %14, 1
%19 = trunc i64 %18 to i32
%20 = icmp eq i32 %19, 2047
br i1 %20, label %58, label %13
21: ; preds = %13, %13
%22 = trunc i64 %14 to i32
%23 = shl i64 %14, 32
%24 = ashr exact i64 %23, 32
%25 = call i32 @strncpy(i8* %4, i8* %0, i64 %24)
%26 = getelementptr inbounds [2048 x i8], [2048 x i8]* %3, i64 0, i64 %24
store i8 0, i8* %26
%27 = call i32 @eslib_file_exists(i8* %4)
switch i32 %27, label %47 [
i32 0, label %28
i32 1, label %42
]
28: ; preds = %21
%29 = call i32 @mkdir(i8* %4, i32 %1)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %35
31: ; preds = %28
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %33)
br label %58
35: ; preds = %28
%36 = call i64 @chmod(i8* %4, i32 %1)
%37 = icmp eq i64 %36, 0
br i1 %37, label %51, label %38
38: ; preds = %35
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %4, i8* %40)
br label %58
42: ; preds = %21
%43 = call i32 @eslib_file_isdir(i8* %4)
%44 = icmp eq i32 %43, 1
br i1 %44, label %51, label %45
45: ; preds = %42
%46 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %58
47: ; preds = %21
%48 = load i32, i32* @errno
%49 = call i8* @strerror(i32 %48)
%50 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %4, i8* %49)
br label %58
51: ; preds = %42, %35
%52 = getelementptr inbounds i8, i8* %0, i64 %24
%53 = load i8, i8* %52
%54 = icmp ne i8 %53, 0
%55 = add i64 %14, 1
%56 = icmp slt i32 %22, 2046
%57 = select i1 %54, i1 %56, i1 false
br i1 %57, label %9, label %58
58: ; preds = %51, %17, %2, %47, %45, %38, %31
%59 = phi i32 [ -1, %31 ], [ -1, %38 ], [ -1, %45 ], [ -1, %47 ], [ -1, %2 ], [ 0, %17 ], [ 0, %51 ]
ret i32 %59
}