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

/* Variables and functions */
scalar_t__ EEXIST;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *malloc(int);
scalar_t__ mkdir(char *, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int mkdir_p(const char *path, mode_t mode) {
  int result = 0;
  char *path_copy = NULL;
  int path_len = strlen(path);
  char *p1 = NULL;
  char *p2 = NULL;
  if (path == NULL || path[0] != '/') {
    fprintf(stderr, "ERROR: Absolute path is required\n");
    return -1;
  }
  path_copy = malloc(path_len + 1);
  strcpy(path_copy, path);
  p1 = p2 = path_copy + 1;
  while (p2 != path_copy + path_len) {
    struct stat st;
    p2 = strchr(p1, '/');
    if (p2 == 0) {
      p2 = path_copy + path_len;
    }
    p2[0] = 0;
    if (stat(path_copy, &st) != 0) {
      if (mkdir(path_copy, mode) != 0 && errno != EEXIST) {
        result = -1;
        fprintf(stderr, "ERROR: Cannot create directory %s\n", path_copy);
        break;
      }
    } else if (!S_ISDIR(st.st_mode)) {
      result = -1;
      fprintf(stderr, "ERROR: File %s exists\n", path_copy);
      break;
    }
    p2[0] = '/';
    p1 = p2 + 1;
  }
  return result;
}


x86 assembly (clang, -O0):
<mkdir_p>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11d5 <mkdir_p+0x55>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11f8 <mkdir_p+0x78>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133d <mkdir_p+0x1bd>
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%rcx
movslq -0x24(%rbp),%rdx
add    %rdx,%rcx
cmp    %rcx,%rax
je     1337 <mkdir_p+0x1b7>
mov    -0x30(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1268 <mkdir_p+0xe8>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
je     12e2 <mkdir_p+0x162>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     12dd <mkdir_p+0x15d>
mov    0x2d2d(%rip),%rax
mov    (%rax),%rax
mov    0x2d33(%rip),%rcx
cmp    (%rcx),%rax
je     12dd <mkdir_p+0x15d>
movl   $0xffffffff,-0x18(%rbp)
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd51(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    1337 <mkdir_p+0x1b7>
jmp    131f <mkdir_p+0x19f>
mov    -0x40(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    131a <mkdir_p+0x19a>
movl   $0xffffffff,-0x18(%rbp)
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xd37(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    1337 <mkdir_p+0x1b7>
jmp    131f <mkdir_p+0x19f>
mov    -0x38(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1224 <mkdir_p+0xa4>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkdir_p>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r13d
mov    %rdi,%rbx
call   1050 <strlen@plt>
test   %rbx,%rbx
je     1255 <mkdir_p+0xd5>
cmpb   $0x2f,(%rbx)
jne    1255 <mkdir_p+0xd5>
mov    %eax,%r15d
lea    0x1(%r15),%edi
call   10a0 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
xor    %r14d,%r14d
cmp    $0x1,%r15d
je     1272 <mkdir_p+0xf2>
movslq %r15d,%r15
add    %rbp,%r15
mov    %rsp,%r12
mov    %rbp,%rbx
jmp    11f8 <mkdir_p+0x78>
nopl   (%rax)
mov    (%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %eax,%eax
je     1284 <mkdir_p+0x104>
movb   $0x2f,(%rbx)
cmp    %r15,%rbx
je     1272 <mkdir_p+0xf2>
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,%rbx
test   %rax,%rax
cmove  %r15,%rbx
movb   $0x0,(%rbx)
mov    %rbp,%rdi
mov    %r12,%rsi
call   1090 <stat@plt>
test   %rax,%rax
je     11e0 <mkdir_p+0x60>
mov    %rbp,%rdi
mov    %r13d,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     11f0 <mkdir_p+0x70>
mov    0x2d93(%rip),%rax
mov    (%rax),%rax
mov    0x2d99(%rip),%rcx
cmp    (%rcx),%rax
je     11f0 <mkdir_p+0x70>
lea    0xdcf(%rip),%rsi
jmp    128b <mkdir_p+0x10b>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    0xd9b(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdba(%rip),%rsi
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
mov    %rbp,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    126c <mkdir_p+0xec>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [34 x i8] c"ERROR: Absolute path is required\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [35 x i8] c"ERROR: Cannot create directory %s\0A\00"
@.str.2 = [23 x i8] c"ERROR: File %s exists\0A\00"
define i32 @mkdir_p(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.stat
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
store i8* null, i8** %7
%12 = load i8*, i8** %4
%13 = call i32 @strlen(i8* %12)
store i32 %13, i32* %8
store i8* null, i8** %9
store i8* null, i8** %10
%14 = load i8*, i8** %4
%15 = icmp eq i8* %14, null
br i1 %15, label %22, label %16
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 47
br i1 %21, label %22, label %25
22: ; preds = %16, %2
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %88
25: ; preds = %16
%26 = load i32, i32* %8
%27 = add nsw i32 %26, 1
%28 = call i8* @malloc(i32 %27)
store i8* %28, i8** %7
%29 = load i8*, i8** %7
%30 = load i8*, i8** %4
%31 = call i32 @strcpy(i8* %29, i8* %30)
%32 = load i8*, i8** %7
%33 = getelementptr inbounds i8, i8* %32, i64 1
store i8* %33, i8** %10
store i8* %33, i8** %9
br label %34
34: ; preds = %81, %25
%35 = load i8*, i8** %10
%36 = load i8*, i8** %7
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = icmp ne i8* %35, %39
br i1 %40, label %41, label %86
41: ; preds = %34
%42 = load i8*, i8** %9
%43 = call i8* @strchr(i8* %42, i8 signext 47)
store i8* %43, i8** %10
%44 = load i8*, i8** %10
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %51
46: ; preds = %41
%47 = load i8*, i8** %7
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8* %50, i8** %10
br label %51
51: ; preds = %46, %41
%52 = load i8*, i8** %10
%53 = getelementptr inbounds i8, i8* %52, i64 0
store i8 0, i8* %53
%54 = load i8*, i8** %7
%55 = call i64 @stat(i8* %54, %struct.stat* %11)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %71
57: ; preds = %51
%58 = load i8*, i8** %7
%59 = load i32, i32* %5
%60 = call i64 @mkdir(i8* %58, i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %70
62: ; preds = %57
%63 = load i64, i64* @errno
%64 = load i64, i64* @EEXIST
%65 = icmp ne i64 %63, %64
br i1 %65, label %66, label %70
66: ; preds = %62
store i32 -1, i32* %6
%67 = load i32, i32* @stderr
%68 = load i8*, i8** %7
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %68)
br label %86
70: ; preds = %62, %57
br label %81
71: ; preds = %51
%72 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%73 = load i32, i32* %72
%74 = call i32 @S_ISDIR(i32 %73)
%75 = icmp ne i32 %74, 0
br i1 %75, label %80, label %76
76: ; preds = %71
store i32 -1, i32* %6
%77 = load i32, i32* @stderr
%78 = load i8*, i8** %7
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %77, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %78)
br label %86
80: ; preds = %71
br label %81
81: ; preds = %80, %70
%82 = load i8*, i8** %10
%83 = getelementptr inbounds i8, i8* %82, i64 0
store i8 47, i8* %83
%84 = load i8*, i8** %10
%85 = getelementptr inbounds i8, i8* %84, i64 1
store i8* %85, i8** %9
br label %34
86: ; preds = %76, %66, %34
%87 = load i32, i32* %6
store i32 %87, i32* %3
br label %88
88: ; preds = %86, %22
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [34 x i8] c"ERROR: Absolute path is required\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [35 x i8] c"ERROR: Cannot create directory %s\0A\00"
@.str.2 = [23 x i8] c"ERROR: File %s exists\0A\00"
define i32 @mkdir_p(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = tail call i32 @strlen(i8* %0)
%5 = icmp eq i8* %0, null
br i1 %5, label %9, label %6
6: ; preds = %2
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 47
br i1 %8, label %12, label %9
9: ; preds = %6, %2
%10 = load i32, i32* @stderr
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
br label %47
12: ; preds = %6
%13 = add nsw i32 %4, 1
%14 = tail call i8* @malloc(i32 %13)
%15 = tail call i32 @strcpy(i8* %14, i8* %0)
%16 = sext i32 %4 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = icmp eq i32 %4, 1
br i1 %18, label %47, label %19
19: ; preds = %12
%20 = bitcast %struct.stat* %3 to i8*
%21 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %22
22: ; preds = %19, %45
%23 = phi i8* [ %14, %19 ], [ %27, %45 ]
%24 = getelementptr inbounds i8, i8* %23, i64 1
%25 = call i8* @strchr(i8* dereferenceable(1) %24, i8 signext 47)
%26 = icmp eq i8* %25, null
%27 = select i1 %26, i8* %17, i8* %25
store i8 0, i8* %27
%28 = call i64 @stat(i8* %14, %struct.stat* %3)
%29 = icmp eq i64 %28, 0
br i1 %29, label %37, label %30
30: ; preds = %22
%31 = call i64 @mkdir(i8* %14, i32 %1)
%32 = icmp eq i64 %31, 0
br i1 %32, label %45, label %33
33: ; preds = %30
%34 = load i64, i64* @errno
%35 = load i64, i64* @EEXIST
%36 = icmp eq i64 %34, %35
br i1 %36, label %45, label %41
37: ; preds = %22
%38 = load i32, i32* %21
%39 = call i32 @S_ISDIR(i32 %38)
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37, %33
%42 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), %33 ], [ getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), %37 ]
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* %42, i8* %14)
br label %47
45: ; preds = %33, %30, %37
store i8 47, i8* %27
%46 = icmp eq i8* %27, %17
br i1 %46, label %47, label %22
47: ; preds = %45, %12, %41, %9
%48 = phi i32 [ -1, %9 ], [ -1, %41 ], [ 0, %12 ], [ 0, %45 ]
ret i32 %48
}