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

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ memcpy(char *, char const *, int);
scalar_t__ mkdir(char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strchr(char const *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char *);
int razor_create_dir(const char *root, const char *path) {
  char buffer[PATH_MAX], *p;
  const char *slash, *next;
  struct stat buf;
  strcpy(buffer, root);
  p = buffer + strlen(buffer);
  slash = path;
  for (slash = path; *slash != '\0'; slash = next) {
    next = strchr(slash + 1, '/');
    if (next == NULL)
      break;
    memcpy(p, slash, next - slash);
    p += next - slash;
    *p = '\0';
    if (stat(buffer, &buf) == 0) {
      if (!S_ISDIR(buf.st_mode)) {
        fprintf(stderr, "%s exists but is not a directory\n", buffer);
        return -1;
      }
    } else if (mkdir(buffer, 0777) < 0) {
      fprintf(stderr, "failed to make directory %s: %m\n", buffer);
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<razor_create_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e59(%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,-0x58(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x58(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     130c <razor_create_dir+0x18c>
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1224 <razor_create_dir+0xa4>
jmp    130c <razor_create_dir+0x18c>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   10a0 <memcpy@plt>
mov    -0x58(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x48(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jne    12b1 <razor_create_dir+0x131>
mov    -0x48(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    12ac <razor_create_dir+0x12c>
mov    -0x58(%rbp),%rdx
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0xd6c(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    131a <razor_create_dir+0x19a>
jmp    12fa <razor_create_dir+0x17a>
mov    -0x58(%rbp),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
jge    12f5 <razor_create_dir+0x175>
mov    -0x58(%rbp),%rdx
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
lea    0xd45(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    131a <razor_create_dir+0x19a>
jmp    12fa <razor_create_dir+0x17a>
jmp    12ff <razor_create_dir+0x17f>
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    11ee <razor_create_dir+0x6e>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<razor_create_dir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rsi
mov    0x2e55(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
mov    %r12,%rdi
call   1030 <strcpy@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
xor    %ecx,%ecx
cmpb   $0x0,(%r14)
je     1274 <razor_create_dir+0xf4>
movslq %eax,%rbx
add    %r12,%rbx
jmp    11f8 <razor_create_dir+0x78>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    -0x30(%rbp),%edi
call   1070 <S_ISDIR@plt>
test   %eax,%eax
je     1257 <razor_create_dir+0xd7>
add    %r13,%rbx
cmpb   $0x0,(%r15)
mov    %r15,%r14
je     1253 <razor_create_dir+0xd3>
lea    0x1(%r14),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     1253 <razor_create_dir+0xd3>
mov    %rax,%r15
mov    %rax,%r13
sub    %r14,%r13
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13d,%edx
call   10a0 <memcpy@plt>
movb   $0x0,(%rbx,%r13,1)
mov    %r12,%rdi
lea    -0x30(%rbp),%rsi
call   1090 <stat@plt>
test   %rax,%rax
je     11e0 <razor_create_dir+0x60>
mov    %r12,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
jns    11ec <razor_create_dir+0x6c>
lea    0xdd1(%rip),%rsi
jmp    125e <razor_create_dir+0xde>
xor    %ecx,%ecx
jmp    1274 <razor_create_dir+0xf4>
lea    0xda2(%rip),%rsi
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
mov    %r12,%rdx
call   1080 <fprintf@plt>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"%s exists but is not a directory\0A\00"
@.str.1 = [33 x i8] c"failed to make directory %s: %m\0A\00"
define i32 @razor_create_dir(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.stat
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %6
%16 = alloca i8, i64 %14
store i64 %14, i64* %7
%17 = load i8*, i8** %4
%18 = call i32 @strcpy(i8* %16, i8* %17)
%19 = call i32 @strlen(i8* %16)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %16, i64 %20
store i8* %21, i8** %8
%22 = load i8*, i8** %5
store i8* %22, i8** %9
%23 = load i8*, i8** %5
store i8* %23, i8** %9
br label %24
24: ; preds = %73, %2
%25 = load i8*, i8** %9
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %75
29: ; preds = %24
%30 = load i8*, i8** %9
%31 = getelementptr inbounds i8, i8* %30, i64 1
%32 = call i8* @strchr(i8* %31, i8 signext 47)
store i8* %32, i8** %10
%33 = load i8*, i8** %10
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %36
35: ; preds = %29
br label %75
36: ; preds = %29
%37 = load i8*, i8** %8
%38 = load i8*, i8** %9
%39 = load i8*, i8** %10
%40 = load i8*, i8** %9
%41 = ptrtoint i8* %39 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = trunc i64 %43 to i32
%45 = call i32 @memcpy(i8* %37, i8* %38, i32 %44)
%46 = load i8*, i8** %10
%47 = load i8*, i8** %9
%48 = ptrtoint i8* %46 to i64
%49 = ptrtoint i8* %47 to i64
%50 = sub i64 %48, %49
%51 = load i8*, i8** %8
%52 = getelementptr inbounds i8, i8* %51, i64 %50
store i8* %52, i8** %8
%53 = load i8*, i8** %8
store i8 0, i8* %53
%54 = call i64 @stat(i8* %16, %struct.stat* %11)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %65
56: ; preds = %36
%57 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i32 @S_ISDIR(i32 %58)
%60 = icmp ne i32 %59, 0
br i1 %60, label %64, label %61
61: ; preds = %56
%62 = load i32, i32* @stderr
%63 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
store i32 1, i32* %12
br label %76
64: ; preds = %56
br label %72
65: ; preds = %36
%66 = call i64 @mkdir(i8* %16, i32 511)
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i32, i32* @stderr
%70 = call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
store i32 1, i32* %12
br label %76
71: ; preds = %65
br label %72
72: ; preds = %71, %64
br label %73
73: ; preds = %72
%74 = load i8*, i8** %10
store i8* %74, i8** %9
br label %24
75: ; preds = %35, %24
store i32 0, i32* %3
store i32 1, i32* %12
br label %76
76: ; preds = %75, %68, %61
%77 = load i8*, i8** %6
%78 = load i32, i32* %3
ret i32 %78
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [34 x i8] c"%s exists but is not a directory\0A\00"
@.str.1 = [33 x i8] c"failed to make directory %s: %m\0A\00"
define i32 @razor_create_dir(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.stat* %3 to i8*
%8 = call i32 @strcpy(i8* %6, i8* %0)
%9 = call i32 @strlen(i8* %6)
%10 = load i8, i8* %1
%11 = icmp eq i8 %10, 0
br i1 %11, label %45, label %12
12: ; preds = %2
%13 = sext i32 %9 to i64
%14 = getelementptr inbounds i8, i8* %6, i64 %13
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
br label %16
16: ; preds = %12, %38
%17 = phi i8* [ %1, %12 ], [ %20, %38 ]
%18 = phi i8* [ %14, %12 ], [ %28, %38 ]
%19 = getelementptr inbounds i8, i8* %17, i64 1
%20 = call i8* @strchr(i8* %19, i8 signext 47)
%21 = icmp eq i8* %20, null
br i1 %21, label %45, label %22
22: ; preds = %16
%23 = ptrtoint i8* %20 to i64
%24 = ptrtoint i8* %17 to i64
%25 = sub i64 %23, %24
%26 = trunc i64 %25 to i32
%27 = call i32 @memcpy(i8* %18, i8* %17, i32 %26)
%28 = getelementptr inbounds i8, i8* %18, i64 %25
store i8 0, i8* %28
%29 = call i64 @stat(i8* %6, %struct.stat* %3)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %22
%32 = load i32, i32* %15
%33 = call i32 @S_ISDIR(i32 %32)
%34 = icmp eq i32 %33, 0
br i1 %34, label %41, label %38
35: ; preds = %22
%36 = call i64 @mkdir(i8* %6, i32 511)
%37 = icmp slt i64 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %31, %35
%39 = load i8, i8* %20
%40 = icmp eq i8 %39, 0
br i1 %40, label %45, label %16
41: ; preds = %35, %31
%42 = phi i8* [ getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), %31 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), %35 ]
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* %42, i8* %6)
br label %45
45: ; preds = %16, %38, %41, %2
%46 = phi i32 [ 0, %2 ], [ -1, %41 ], [ 0, %38 ], [ 0, %16 ]
ret i32 %46
}